rust/hedgewars-server/src/server/io.rs
changeset 14780 65861ba8b4e8
parent 14779 f43ab2bd76ae
child 14781 01f8ab45f806
equal deleted inserted replaced
14779:f43ab2bd76ae 14780:65861ba8b4e8
     9     database::Database,
     9     database::Database,
    10     handlers::{IoResult, IoTask},
    10     handlers::{IoResult, IoTask},
    11 };
    11 };
    12 use mio::{Evented, Poll, PollOpt};
    12 use mio::{Evented, Poll, PollOpt};
    13 use mio_extras::channel;
    13 use mio_extras::channel;
       
    14 use log::*;
    14 
    15 
    15 pub type RequestId = u32;
    16 pub type RequestId = u32;
    16 
    17 
    17 pub struct IOThread {
    18 pub struct IOThread {
    18     core_tx: mpsc::Sender<(RequestId, IoTask)>,
    19     core_tx: mpsc::Sender<(RequestId, IoTask)>,
    27         let mut db = Database::new();
    28         let mut db = Database::new();
    28         db.connect("localhost");
    29         db.connect("localhost");
    29 
    30 
    30         thread::spawn(move || {
    31         thread::spawn(move || {
    31             while let Ok((request_id, task)) = io_rx.try_recv() {
    32             while let Ok((request_id, task)) = io_rx.try_recv() {
    32                 match task {
    33                 let response = match task {
    33                     IoTask::GetAccount {
    34                     IoTask::GetAccount {
    34                         nick,
    35                         nick,
    35                         protocol,
    36                         protocol,
    36                         password_hash,
    37                         password_hash,
    37                         client_salt,
    38                         client_salt,
    38                         server_salt,
    39                         server_salt,
    39                     } => {
    40                     } => {
    40                         if let Ok(account) = db.get_account(
    41                         match db.get_account(
    41                             &nick,
    42                             &nick,
    42                             protocol,
    43                             protocol,
    43                             &password_hash,
    44                             &password_hash,
    44                             &client_salt,
    45                             &client_salt,
    45                             &server_salt,
    46                             &server_salt,
    46                         ) {
    47                         ) {
    47                             io_tx.send((request_id, IoResult::Account(account)));
    48                             Ok(account) => {
       
    49                                 IoResult::Account(account)
       
    50                             }
       
    51                             Err(..) => {
       
    52                                 warn!("Unable to get account data: {}", 0);
       
    53                                 IoResult::Account(None)
       
    54                             }
    48                         }
    55                         }
       
    56                     },
       
    57 
       
    58                     IoTask::SaveRoom { room_id, filename, contents} => {
       
    59                         let result = match save_file(&filename, &contents) {
       
    60                             Ok(()) => true,
       
    61                             Err(e) => {
       
    62                                 warn!(
       
    63                                     "Error while writing the room config file \"{}\": {}",
       
    64                                     filename, e
       
    65                                 );
       
    66                                 false
       
    67                            }
       
    68                         };
       
    69                         IoResult::SaveRoom(room_id, result)
       
    70                     },
       
    71 
       
    72                     IoTask::LoadRoom {room_id, filename} => {
       
    73                         let result = match load_file(&filename) {
       
    74                             Ok(contents) => Some(contents),
       
    75                             Err(e) => {
       
    76                                 warn!(
       
    77                                     "Error while writing the room config file \"{}\": {}",
       
    78                                     filename, e
       
    79                                 );
       
    80                                 None
       
    81                             }
       
    82                         };
       
    83                         IoResult::LoadRoom(room_id, result)
    49                     }
    84                     }
    50                 }
    85                 };
       
    86                 io_tx.send((request_id, response));
    51             }
    87             }
    52         });
    88         });
    53 
    89 
    54         Self { core_rx, core_tx }
    90         Self { core_rx, core_tx }
    55     }
    91     }
    69     pub fn register_rx(&self, poll: &mio::Poll, token: mio::Token) -> Result<()> {
   105     pub fn register_rx(&self, poll: &mio::Poll, token: mio::Token) -> Result<()> {
    70         self.core_rx
   106         self.core_rx
    71             .register(poll, token, mio::Ready::readable(), PollOpt::edge())
   107             .register(poll, token, mio::Ready::readable(), PollOpt::edge())
    72     }
   108     }
    73 }
   109 }
       
   110 
       
   111 fn save_file(filename: &str, contents: &str) -> Result<()> {
       
   112     let mut writer = OpenOptions::new().create(true).write(true).open(filename)?;
       
   113     writer.write_all(contents.as_bytes())
       
   114 }
       
   115 
       
   116 fn load_file(filename: &str) -> Result<String> {
       
   117     let mut reader = File::open(filename)?;
       
   118     let mut result = String::new();
       
   119     reader.read_to_string(&mut result)?;
       
   120     Ok(result)
       
   121 }