Introduce actions, just like in the old server
authorunc0rr
Sun, 15 Jan 2017 00:34:36 +0300
changeset 12143 e0bf51609062
parent 12142 193dfdcb0620
child 12144 f3121d7dedec
Introduce actions, just like in the old server
gameServer2/src/server/actions.rs
gameServer2/src/server/client.rs
gameServer2/src/server/mod.rs
gameServer2/src/server/server.rs
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/gameServer2/src/server/actions.rs	Sun Jan 15 00:34:36 2017 +0300
@@ -0,0 +1,5 @@
+use protocol::messages::HWProtocolMessage;
+
+pub enum Action {
+    SendMe(String),
+}
--- a/gameServer2/src/server/client.rs	Sat Jan 14 22:30:09 2017 +0300
+++ b/gameServer2/src/server/client.rs	Sun Jan 15 00:34:36 2017 +0300
@@ -9,6 +9,8 @@
 use protocol::ProtocolDecoder;
 use protocol::messages;
 use protocol::messages::HWProtocolMessage::*;
+use server::actions::Action::*;
+use server::actions::Action;
 use log;
 
 pub struct HWClient {
@@ -34,13 +36,17 @@
         self.send_msg(Connected(utils::PROTOCOL_VERSION));
     }
 
-    fn send_raw_msg(&mut self, msg: &[u8]) {
+    pub fn send_raw_msg(&mut self, msg: &[u8]) {
         self.buf_out.write(msg).unwrap();
         self.flush();
     }
 
-    fn send_msg(&mut self, msg: messages::HWProtocolMessage) {
-        self.send_raw_msg(&msg.to_raw_protocol().into_bytes());
+    pub fn send_string(&mut self, msg: &String) {
+        self.send_raw_msg(&msg.as_bytes());
+    }
+
+    pub fn send_msg(&mut self, msg: messages::HWProtocolMessage) {
+        self.send_string(&msg.to_raw_protocol());
     }
 
     fn flush(&mut self) {
@@ -48,26 +54,23 @@
         self.sock.flush();
     }
 
-    pub fn readable(&mut self, poll: &Poll) -> io::Result<()> {
-        let v = self.decoder.read_from(&mut self.sock)?;
+    pub fn readable(&mut self, poll: &Poll) -> Vec<Action> {
+        let v = self.decoder.read_from(&mut self.sock).unwrap();
         debug!("Read {} bytes", v);
         let mut response = Vec::new();
         {
             let msgs = self.decoder.extract_messages();
             for msg in msgs {
                 match msg {
-                    Ping => response.push(Pong),
+                    Ping => response.push(SendMe(Pong.to_raw_protocol())),
                     Malformed => warn!("Malformed/unknown message"),
                     Empty => warn!("Empty message"),
                     _ => unimplemented!(),
                 }
             }
         }
-        for r in response {
-            self.send_msg(r);
-        }
         self.decoder.sweep();
-        Ok(())
+        response
     }
 
     pub fn writable(&mut self, poll: &Poll) -> io::Result<()> {
--- a/gameServer2/src/server/mod.rs	Sat Jan 14 22:30:09 2017 +0300
+++ b/gameServer2/src/server/mod.rs	Sun Jan 15 00:34:36 2017 +0300
@@ -1,3 +1,4 @@
 pub mod server;
 pub mod client;
 pub mod coretypes;
+mod actions;
--- a/gameServer2/src/server/server.rs	Sat Jan 14 22:30:09 2017 +0300
+++ b/gameServer2/src/server/server.rs	Sun Jan 15 00:34:36 2017 +0300
@@ -6,6 +6,8 @@
 
 use utils;
 use server::client::HWClient;
+use server::actions::Action;
+use server::actions::Action::*;
 
 type Slab<T> = slab::Slab<T, Token>;
 
@@ -44,7 +46,15 @@
 
     pub fn client_readable(&mut self, poll: &Poll,
                            token: Token) -> io::Result<()> {
-        self.clients[token].readable(poll)
+        let actions;
+        {
+            actions = self.clients[token].readable(poll);
+        }
+
+        for action in actions {
+            self.react(token, action);
+        }
+        Ok(())
     }
 
     pub fn client_writable(&mut self, poll: &Poll,
@@ -56,6 +66,13 @@
                            token: Token) -> io::Result<()> {
         self.clients[token].error(poll)
     }
+
+    fn react(&mut self, token: Token, action: Action) {
+        match action {
+            SendMe(msg) => self.clients[token].send_string(&msg),
+            //_ => unimplemented!(),
+        }
+    }
 }