rust/hedgewars-server/src/server/io.rs
author alfadur
Mon, 03 Jun 2019 01:15:57 +0300
changeset 15103 823052e66611
parent 15098 9397e07b3086
child 15120 febccab419b1
permissions -rw-r--r--
check for account existence before asking passwords
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
14392
e335b3120f59 pull file io out of server handler
alfadur
parents:
diff changeset
     1
use std::{
e335b3120f59 pull file io out of server handler
alfadur
parents:
diff changeset
     2
    fs::{File, OpenOptions},
14457
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14415
diff changeset
     3
    io::{Error, ErrorKind, Read, Result, Write},
14779
f43ab2bd76ae add a thread for internal server IO and implement account checking with it
alfadur
parents: 14457
diff changeset
     4
    sync::mpsc,
f43ab2bd76ae add a thread for internal server IO and implement account checking with it
alfadur
parents: 14457
diff changeset
     5
    thread,
14392
e335b3120f59 pull file io out of server handler
alfadur
parents:
diff changeset
     6
};
e335b3120f59 pull file io out of server handler
alfadur
parents:
diff changeset
     7
15074
c5a6e8566425 shuffle server files
alfadur
parents: 14785
diff changeset
     8
use crate::{
15075
e935b1ad23f3 normalize type names
alfadur
parents: 15074
diff changeset
     9
    handlers::{IoResult, IoTask},
15074
c5a6e8566425 shuffle server files
alfadur
parents: 14785
diff changeset
    10
    server::database::Database,
14779
f43ab2bd76ae add a thread for internal server IO and implement account checking with it
alfadur
parents: 14457
diff changeset
    11
};
14781
01f8ab45f806 fix lobby joining
alfadur
parents: 14780
diff changeset
    12
use log::*;
14779
f43ab2bd76ae add a thread for internal server IO and implement account checking with it
alfadur
parents: 14457
diff changeset
    13
use mio::{Evented, Poll, PollOpt};
f43ab2bd76ae add a thread for internal server IO and implement account checking with it
alfadur
parents: 14457
diff changeset
    14
use mio_extras::channel;
f43ab2bd76ae add a thread for internal server IO and implement account checking with it
alfadur
parents: 14457
diff changeset
    15
f43ab2bd76ae add a thread for internal server IO and implement account checking with it
alfadur
parents: 14457
diff changeset
    16
pub type RequestId = u32;
f43ab2bd76ae add a thread for internal server IO and implement account checking with it
alfadur
parents: 14457
diff changeset
    17
15098
9397e07b3086 use blocking recv in IO thread
alfadur
parents: 15075
diff changeset
    18
pub struct IoThread {
14779
f43ab2bd76ae add a thread for internal server IO and implement account checking with it
alfadur
parents: 14457
diff changeset
    19
    core_tx: mpsc::Sender<(RequestId, IoTask)>,
f43ab2bd76ae add a thread for internal server IO and implement account checking with it
alfadur
parents: 14457
diff changeset
    20
    core_rx: channel::Receiver<(RequestId, IoResult)>,
14392
e335b3120f59 pull file io out of server handler
alfadur
parents:
diff changeset
    21
}
e335b3120f59 pull file io out of server handler
alfadur
parents:
diff changeset
    22
15098
9397e07b3086 use blocking recv in IO thread
alfadur
parents: 15075
diff changeset
    23
impl IoThread {
14392
e335b3120f59 pull file io out of server handler
alfadur
parents:
diff changeset
    24
    pub fn new() -> Self {
14779
f43ab2bd76ae add a thread for internal server IO and implement account checking with it
alfadur
parents: 14457
diff changeset
    25
        let (core_tx, io_rx) = mpsc::channel();
f43ab2bd76ae add a thread for internal server IO and implement account checking with it
alfadur
parents: 14457
diff changeset
    26
        let (io_tx, core_rx) = channel::channel();
f43ab2bd76ae add a thread for internal server IO and implement account checking with it
alfadur
parents: 14457
diff changeset
    27
f43ab2bd76ae add a thread for internal server IO and implement account checking with it
alfadur
parents: 14457
diff changeset
    28
        let mut db = Database::new();
f43ab2bd76ae add a thread for internal server IO and implement account checking with it
alfadur
parents: 14457
diff changeset
    29
        db.connect("localhost");
14392
e335b3120f59 pull file io out of server handler
alfadur
parents:
diff changeset
    30
14779
f43ab2bd76ae add a thread for internal server IO and implement account checking with it
alfadur
parents: 14457
diff changeset
    31
        thread::spawn(move || {
15098
9397e07b3086 use blocking recv in IO thread
alfadur
parents: 15075
diff changeset
    32
            while let Ok((request_id, task)) = io_rx.recv() {
14780
65861ba8b4e8 move room saves to IO thread
alfadur
parents: 14779
diff changeset
    33
                let response = match task {
15103
823052e66611 check for account existence before asking passwords
alfadur
parents: 15098
diff changeset
    34
                    IoTask::CheckRegistered { nick } => match db.is_registered(&nick) {
823052e66611 check for account existence before asking passwords
alfadur
parents: 15098
diff changeset
    35
                        Ok(is_registered) => IoResult::AccountRegistered(is_registered),
823052e66611 check for account existence before asking passwords
alfadur
parents: 15098
diff changeset
    36
                        Err(e) => {
823052e66611 check for account existence before asking passwords
alfadur
parents: 15098
diff changeset
    37
                            warn!("Unable to check account's existence: {}", e);
823052e66611 check for account existence before asking passwords
alfadur
parents: 15098
diff changeset
    38
                            IoResult::AccountRegistered(false)
823052e66611 check for account existence before asking passwords
alfadur
parents: 15098
diff changeset
    39
                        }
823052e66611 check for account existence before asking passwords
alfadur
parents: 15098
diff changeset
    40
                    },
823052e66611 check for account existence before asking passwords
alfadur
parents: 15098
diff changeset
    41
14779
f43ab2bd76ae add a thread for internal server IO and implement account checking with it
alfadur
parents: 14457
diff changeset
    42
                    IoTask::GetAccount {
f43ab2bd76ae add a thread for internal server IO and implement account checking with it
alfadur
parents: 14457
diff changeset
    43
                        nick,
f43ab2bd76ae add a thread for internal server IO and implement account checking with it
alfadur
parents: 14457
diff changeset
    44
                        protocol,
f43ab2bd76ae add a thread for internal server IO and implement account checking with it
alfadur
parents: 14457
diff changeset
    45
                        password_hash,
f43ab2bd76ae add a thread for internal server IO and implement account checking with it
alfadur
parents: 14457
diff changeset
    46
                        client_salt,
f43ab2bd76ae add a thread for internal server IO and implement account checking with it
alfadur
parents: 14457
diff changeset
    47
                        server_salt,
f43ab2bd76ae add a thread for internal server IO and implement account checking with it
alfadur
parents: 14457
diff changeset
    48
                    } => {
14780
65861ba8b4e8 move room saves to IO thread
alfadur
parents: 14779
diff changeset
    49
                        match db.get_account(
14779
f43ab2bd76ae add a thread for internal server IO and implement account checking with it
alfadur
parents: 14457
diff changeset
    50
                            &nick,
f43ab2bd76ae add a thread for internal server IO and implement account checking with it
alfadur
parents: 14457
diff changeset
    51
                            protocol,
f43ab2bd76ae add a thread for internal server IO and implement account checking with it
alfadur
parents: 14457
diff changeset
    52
                            &password_hash,
f43ab2bd76ae add a thread for internal server IO and implement account checking with it
alfadur
parents: 14457
diff changeset
    53
                            &client_salt,
f43ab2bd76ae add a thread for internal server IO and implement account checking with it
alfadur
parents: 14457
diff changeset
    54
                            &server_salt,
f43ab2bd76ae add a thread for internal server IO and implement account checking with it
alfadur
parents: 14457
diff changeset
    55
                        ) {
14781
01f8ab45f806 fix lobby joining
alfadur
parents: 14780
diff changeset
    56
                            Ok(account) => IoResult::Account(account),
14785
a1077e8d26f4 implement watch message apart from replay deserializing
alfadur
parents: 14781
diff changeset
    57
                            Err(e) => {
a1077e8d26f4 implement watch message apart from replay deserializing
alfadur
parents: 14781
diff changeset
    58
                                warn!("Unable to get account data: {}", e);
14780
65861ba8b4e8 move room saves to IO thread
alfadur
parents: 14779
diff changeset
    59
                                IoResult::Account(None)
65861ba8b4e8 move room saves to IO thread
alfadur
parents: 14779
diff changeset
    60
                            }
14779
f43ab2bd76ae add a thread for internal server IO and implement account checking with it
alfadur
parents: 14457
diff changeset
    61
                        }
14781
01f8ab45f806 fix lobby joining
alfadur
parents: 14780
diff changeset
    62
                    }
14780
65861ba8b4e8 move room saves to IO thread
alfadur
parents: 14779
diff changeset
    63
14785
a1077e8d26f4 implement watch message apart from replay deserializing
alfadur
parents: 14781
diff changeset
    64
                    IoTask::GetReplay { id } => {
a1077e8d26f4 implement watch message apart from replay deserializing
alfadur
parents: 14781
diff changeset
    65
                        let result = match db.get_replay_name(id) {
a1077e8d26f4 implement watch message apart from replay deserializing
alfadur
parents: 14781
diff changeset
    66
                            Ok(Some(filename)) => {
a1077e8d26f4 implement watch message apart from replay deserializing
alfadur
parents: 14781
diff changeset
    67
                                let filename = format!(
a1077e8d26f4 implement watch message apart from replay deserializing
alfadur
parents: 14781
diff changeset
    68
                                    "checked/{}",
a1077e8d26f4 implement watch message apart from replay deserializing
alfadur
parents: 14781
diff changeset
    69
                                    if filename.starts_with("replays/") {
a1077e8d26f4 implement watch message apart from replay deserializing
alfadur
parents: 14781
diff changeset
    70
                                        &filename[8..]
a1077e8d26f4 implement watch message apart from replay deserializing
alfadur
parents: 14781
diff changeset
    71
                                    } else {
a1077e8d26f4 implement watch message apart from replay deserializing
alfadur
parents: 14781
diff changeset
    72
                                        &filename
a1077e8d26f4 implement watch message apart from replay deserializing
alfadur
parents: 14781
diff changeset
    73
                                    }
a1077e8d26f4 implement watch message apart from replay deserializing
alfadur
parents: 14781
diff changeset
    74
                                );
a1077e8d26f4 implement watch message apart from replay deserializing
alfadur
parents: 14781
diff changeset
    75
                                match load_file(&filename) {
a1077e8d26f4 implement watch message apart from replay deserializing
alfadur
parents: 14781
diff changeset
    76
                                    Ok(contents) => Some(unimplemented!()),
a1077e8d26f4 implement watch message apart from replay deserializing
alfadur
parents: 14781
diff changeset
    77
                                    Err(e) => {
a1077e8d26f4 implement watch message apart from replay deserializing
alfadur
parents: 14781
diff changeset
    78
                                        warn!(
a1077e8d26f4 implement watch message apart from replay deserializing
alfadur
parents: 14781
diff changeset
    79
                                            "Error while writing the room config file \"{}\": {}",
a1077e8d26f4 implement watch message apart from replay deserializing
alfadur
parents: 14781
diff changeset
    80
                                            filename, e
a1077e8d26f4 implement watch message apart from replay deserializing
alfadur
parents: 14781
diff changeset
    81
                                        );
a1077e8d26f4 implement watch message apart from replay deserializing
alfadur
parents: 14781
diff changeset
    82
                                        None
a1077e8d26f4 implement watch message apart from replay deserializing
alfadur
parents: 14781
diff changeset
    83
                                    }
a1077e8d26f4 implement watch message apart from replay deserializing
alfadur
parents: 14781
diff changeset
    84
                                }
a1077e8d26f4 implement watch message apart from replay deserializing
alfadur
parents: 14781
diff changeset
    85
                            }
a1077e8d26f4 implement watch message apart from replay deserializing
alfadur
parents: 14781
diff changeset
    86
                            Ok(None) => None,
a1077e8d26f4 implement watch message apart from replay deserializing
alfadur
parents: 14781
diff changeset
    87
                            Err(e) => {
a1077e8d26f4 implement watch message apart from replay deserializing
alfadur
parents: 14781
diff changeset
    88
                                warn!("Unable to get replay name: {}", e);
a1077e8d26f4 implement watch message apart from replay deserializing
alfadur
parents: 14781
diff changeset
    89
                                None
a1077e8d26f4 implement watch message apart from replay deserializing
alfadur
parents: 14781
diff changeset
    90
                            }
a1077e8d26f4 implement watch message apart from replay deserializing
alfadur
parents: 14781
diff changeset
    91
                        };
a1077e8d26f4 implement watch message apart from replay deserializing
alfadur
parents: 14781
diff changeset
    92
                        IoResult::Replay(result)
a1077e8d26f4 implement watch message apart from replay deserializing
alfadur
parents: 14781
diff changeset
    93
                    }
a1077e8d26f4 implement watch message apart from replay deserializing
alfadur
parents: 14781
diff changeset
    94
14781
01f8ab45f806 fix lobby joining
alfadur
parents: 14780
diff changeset
    95
                    IoTask::SaveRoom {
01f8ab45f806 fix lobby joining
alfadur
parents: 14780
diff changeset
    96
                        room_id,
01f8ab45f806 fix lobby joining
alfadur
parents: 14780
diff changeset
    97
                        filename,
01f8ab45f806 fix lobby joining
alfadur
parents: 14780
diff changeset
    98
                        contents,
01f8ab45f806 fix lobby joining
alfadur
parents: 14780
diff changeset
    99
                    } => {
14780
65861ba8b4e8 move room saves to IO thread
alfadur
parents: 14779
diff changeset
   100
                        let result = match save_file(&filename, &contents) {
65861ba8b4e8 move room saves to IO thread
alfadur
parents: 14779
diff changeset
   101
                            Ok(()) => true,
65861ba8b4e8 move room saves to IO thread
alfadur
parents: 14779
diff changeset
   102
                            Err(e) => {
65861ba8b4e8 move room saves to IO thread
alfadur
parents: 14779
diff changeset
   103
                                warn!(
65861ba8b4e8 move room saves to IO thread
alfadur
parents: 14779
diff changeset
   104
                                    "Error while writing the room config file \"{}\": {}",
65861ba8b4e8 move room saves to IO thread
alfadur
parents: 14779
diff changeset
   105
                                    filename, e
65861ba8b4e8 move room saves to IO thread
alfadur
parents: 14779
diff changeset
   106
                                );
65861ba8b4e8 move room saves to IO thread
alfadur
parents: 14779
diff changeset
   107
                                false
14781
01f8ab45f806 fix lobby joining
alfadur
parents: 14780
diff changeset
   108
                            }
14780
65861ba8b4e8 move room saves to IO thread
alfadur
parents: 14779
diff changeset
   109
                        };
65861ba8b4e8 move room saves to IO thread
alfadur
parents: 14779
diff changeset
   110
                        IoResult::SaveRoom(room_id, result)
14781
01f8ab45f806 fix lobby joining
alfadur
parents: 14780
diff changeset
   111
                    }
14780
65861ba8b4e8 move room saves to IO thread
alfadur
parents: 14779
diff changeset
   112
14781
01f8ab45f806 fix lobby joining
alfadur
parents: 14780
diff changeset
   113
                    IoTask::LoadRoom { room_id, filename } => {
14780
65861ba8b4e8 move room saves to IO thread
alfadur
parents: 14779
diff changeset
   114
                        let result = match load_file(&filename) {
65861ba8b4e8 move room saves to IO thread
alfadur
parents: 14779
diff changeset
   115
                            Ok(contents) => Some(contents),
65861ba8b4e8 move room saves to IO thread
alfadur
parents: 14779
diff changeset
   116
                            Err(e) => {
65861ba8b4e8 move room saves to IO thread
alfadur
parents: 14779
diff changeset
   117
                                warn!(
65861ba8b4e8 move room saves to IO thread
alfadur
parents: 14779
diff changeset
   118
                                    "Error while writing the room config file \"{}\": {}",
65861ba8b4e8 move room saves to IO thread
alfadur
parents: 14779
diff changeset
   119
                                    filename, e
65861ba8b4e8 move room saves to IO thread
alfadur
parents: 14779
diff changeset
   120
                                );
65861ba8b4e8 move room saves to IO thread
alfadur
parents: 14779
diff changeset
   121
                                None
65861ba8b4e8 move room saves to IO thread
alfadur
parents: 14779
diff changeset
   122
                            }
65861ba8b4e8 move room saves to IO thread
alfadur
parents: 14779
diff changeset
   123
                        };
65861ba8b4e8 move room saves to IO thread
alfadur
parents: 14779
diff changeset
   124
                        IoResult::LoadRoom(room_id, result)
14779
f43ab2bd76ae add a thread for internal server IO and implement account checking with it
alfadur
parents: 14457
diff changeset
   125
                    }
14780
65861ba8b4e8 move room saves to IO thread
alfadur
parents: 14779
diff changeset
   126
                };
65861ba8b4e8 move room saves to IO thread
alfadur
parents: 14779
diff changeset
   127
                io_tx.send((request_id, response));
14779
f43ab2bd76ae add a thread for internal server IO and implement account checking with it
alfadur
parents: 14457
diff changeset
   128
            }
f43ab2bd76ae add a thread for internal server IO and implement account checking with it
alfadur
parents: 14457
diff changeset
   129
        });
f43ab2bd76ae add a thread for internal server IO and implement account checking with it
alfadur
parents: 14457
diff changeset
   130
f43ab2bd76ae add a thread for internal server IO and implement account checking with it
alfadur
parents: 14457
diff changeset
   131
        Self { core_rx, core_tx }
14392
e335b3120f59 pull file io out of server handler
alfadur
parents:
diff changeset
   132
    }
e335b3120f59 pull file io out of server handler
alfadur
parents:
diff changeset
   133
14779
f43ab2bd76ae add a thread for internal server IO and implement account checking with it
alfadur
parents: 14457
diff changeset
   134
    pub fn send(&self, request_id: RequestId, task: IoTask) {
f43ab2bd76ae add a thread for internal server IO and implement account checking with it
alfadur
parents: 14457
diff changeset
   135
        self.core_tx.send((request_id, task)).unwrap();
14392
e335b3120f59 pull file io out of server handler
alfadur
parents:
diff changeset
   136
    }
e335b3120f59 pull file io out of server handler
alfadur
parents:
diff changeset
   137
14779
f43ab2bd76ae add a thread for internal server IO and implement account checking with it
alfadur
parents: 14457
diff changeset
   138
    pub fn try_recv(&self) -> Option<(RequestId, IoResult)> {
f43ab2bd76ae add a thread for internal server IO and implement account checking with it
alfadur
parents: 14457
diff changeset
   139
        match self.core_rx.try_recv() {
f43ab2bd76ae add a thread for internal server IO and implement account checking with it
alfadur
parents: 14457
diff changeset
   140
            Ok(result) => Some(result),
f43ab2bd76ae add a thread for internal server IO and implement account checking with it
alfadur
parents: 14457
diff changeset
   141
            Err(mpsc::TryRecvError::Empty) => None,
f43ab2bd76ae add a thread for internal server IO and implement account checking with it
alfadur
parents: 14457
diff changeset
   142
            Err(mpsc::TryRecvError::Disconnected) => unreachable!(),
f43ab2bd76ae add a thread for internal server IO and implement account checking with it
alfadur
parents: 14457
diff changeset
   143
        }
f43ab2bd76ae add a thread for internal server IO and implement account checking with it
alfadur
parents: 14457
diff changeset
   144
    }
14392
e335b3120f59 pull file io out of server handler
alfadur
parents:
diff changeset
   145
14779
f43ab2bd76ae add a thread for internal server IO and implement account checking with it
alfadur
parents: 14457
diff changeset
   146
    pub fn register_rx(&self, poll: &mio::Poll, token: mio::Token) -> Result<()> {
f43ab2bd76ae add a thread for internal server IO and implement account checking with it
alfadur
parents: 14457
diff changeset
   147
        self.core_rx
f43ab2bd76ae add a thread for internal server IO and implement account checking with it
alfadur
parents: 14457
diff changeset
   148
            .register(poll, token, mio::Ready::readable(), PollOpt::edge())
14392
e335b3120f59 pull file io out of server handler
alfadur
parents:
diff changeset
   149
    }
e335b3120f59 pull file io out of server handler
alfadur
parents:
diff changeset
   150
}
14780
65861ba8b4e8 move room saves to IO thread
alfadur
parents: 14779
diff changeset
   151
65861ba8b4e8 move room saves to IO thread
alfadur
parents: 14779
diff changeset
   152
fn save_file(filename: &str, contents: &str) -> Result<()> {
65861ba8b4e8 move room saves to IO thread
alfadur
parents: 14779
diff changeset
   153
    let mut writer = OpenOptions::new().create(true).write(true).open(filename)?;
65861ba8b4e8 move room saves to IO thread
alfadur
parents: 14779
diff changeset
   154
    writer.write_all(contents.as_bytes())
65861ba8b4e8 move room saves to IO thread
alfadur
parents: 14779
diff changeset
   155
}
65861ba8b4e8 move room saves to IO thread
alfadur
parents: 14779
diff changeset
   156
65861ba8b4e8 move room saves to IO thread
alfadur
parents: 14779
diff changeset
   157
fn load_file(filename: &str) -> Result<String> {
65861ba8b4e8 move room saves to IO thread
alfadur
parents: 14779
diff changeset
   158
    let mut reader = File::open(filename)?;
65861ba8b4e8 move room saves to IO thread
alfadur
parents: 14779
diff changeset
   159
    let mut result = String::new();
65861ba8b4e8 move room saves to IO thread
alfadur
parents: 14779
diff changeset
   160
    reader.read_to_string(&mut result)?;
65861ba8b4e8 move room saves to IO thread
alfadur
parents: 14779
diff changeset
   161
    Ok(result)
14781
01f8ab45f806 fix lobby joining
alfadur
parents: 14780
diff changeset
   162
}