rust/hedgewars-server/src/server/io.rs
changeset 14801 65861ba8b4e8
parent 14800 f43ab2bd76ae
child 14802 01f8ab45f806
--- a/rust/hedgewars-server/src/server/io.rs	Tue Apr 09 21:08:35 2019 +0300
+++ b/rust/hedgewars-server/src/server/io.rs	Tue Apr 09 23:03:12 2019 +0300
@@ -11,6 +11,7 @@
 };
 use mio::{Evented, Poll, PollOpt};
 use mio_extras::channel;
+use log::*;
 
 pub type RequestId = u32;
 
@@ -29,7 +30,7 @@
 
         thread::spawn(move || {
             while let Ok((request_id, task)) = io_rx.try_recv() {
-                match task {
+                let response = match task {
                     IoTask::GetAccount {
                         nick,
                         protocol,
@@ -37,17 +38,52 @@
                         client_salt,
                         server_salt,
                     } => {
-                        if let Ok(account) = db.get_account(
+                        match db.get_account(
                             &nick,
                             protocol,
                             &password_hash,
                             &client_salt,
                             &server_salt,
                         ) {
-                            io_tx.send((request_id, IoResult::Account(account)));
+                            Ok(account) => {
+                                IoResult::Account(account)
+                            }
+                            Err(..) => {
+                                warn!("Unable to get account data: {}", 0);
+                                IoResult::Account(None)
+                            }
                         }
+                    },
+
+                    IoTask::SaveRoom { room_id, filename, contents} => {
+                        let result = match save_file(&filename, &contents) {
+                            Ok(()) => true,
+                            Err(e) => {
+                                warn!(
+                                    "Error while writing the room config file \"{}\": {}",
+                                    filename, e
+                                );
+                                false
+                           }
+                        };
+                        IoResult::SaveRoom(room_id, result)
+                    },
+
+                    IoTask::LoadRoom {room_id, filename} => {
+                        let result = match load_file(&filename) {
+                            Ok(contents) => Some(contents),
+                            Err(e) => {
+                                warn!(
+                                    "Error while writing the room config file \"{}\": {}",
+                                    filename, e
+                                );
+                                None
+                            }
+                        };
+                        IoResult::LoadRoom(room_id, result)
                     }
-                }
+                };
+                io_tx.send((request_id, response));
             }
         });
 
@@ -71,3 +107,15 @@
             .register(poll, token, mio::Ready::readable(), PollOpt::edge())
     }
 }
+
+fn save_file(filename: &str, contents: &str) -> Result<()> {
+    let mut writer = OpenOptions::new().create(true).write(true).open(filename)?;
+    writer.write_all(contents.as_bytes())
+}
+
+fn load_file(filename: &str) -> Result<String> {
+    let mut reader = File::open(filename)?;
+    let mut result = String::new();
+    reader.read_to_string(&mut result)?;
+    Ok(result)
+}
\ No newline at end of file