gameServer2/src/server/client.rs
changeset 12136 e25a82ce2374
parent 12129 07972a8c2433
child 12137 193dfdcb0620
--- a/gameServer2/src/server/client.rs	Wed Jan 11 22:42:59 2017 +0300
+++ b/gameServer2/src/server/client.rs	Sat Jan 14 00:46:52 2017 +0300
@@ -6,11 +6,13 @@
 use netbuf;
 
 use utils;
-use protocol::FrameDecoder;
+use protocol::ProtocolDecoder;
+use protocol::messages;
+use protocol::messages::HWProtocolMessage::*;
 
 pub struct HWClient {
     sock: TcpStream,
-    decoder: FrameDecoder,
+    decoder: ProtocolDecoder,
     buf_out: netbuf::Buf
 }
 
@@ -18,7 +20,7 @@
     pub fn new(sock: TcpStream) -> HWClient {
         HWClient {
             sock: sock,
-            decoder: FrameDecoder::new(),
+            decoder: ProtocolDecoder::new(),
             buf_out: netbuf::Buf::new(),
         }
     }
@@ -38,6 +40,10 @@
         self.flush();
     }
 
+    fn send_msg(&mut self, msg: messages::HWProtocolMessage) {
+        self.send_raw_msg(&msg.to_raw_protocol().into_bytes());
+    }
+
     fn flush(&mut self) {
         self.buf_out.write_to(&mut self.sock).unwrap();
         self.sock.flush();
@@ -45,8 +51,21 @@
 
     pub fn readable(&mut self, poll: &Poll) -> io::Result<()> {
         let v = self.decoder.read_from(&mut self.sock)?;
-        self.decoder.extract_messages();
         println!("Read {} bytes", v);
+        let mut response = Vec::new();
+        {
+            let msgs = self.decoder.extract_messages();
+            for msg in msgs {
+                match msg {
+                    Ping => response.push(Pong),
+                    _ => println!("Unknown message")
+                }
+            }
+        }
+        for r in response {
+            self.send_msg(r);
+        }
+        self.decoder.sweep();
         Ok(())
     }