- Start protocol parser implementation
authorunc0rr
Tue, 03 Jan 2017 00:19:47 +0300
changeset 12129 07972a8c2433
parent 12128 f50876f3eff8
child 12130 6273f89ab13d
- Start protocol parser implementation - Small fixes
gameServer2/src/main.rs
gameServer2/src/protocol.rs
gameServer2/src/server/client.rs
gameServer2/src/server/server.rs
--- a/gameServer2/src/main.rs	Mon Jan 02 00:16:22 2017 +0300
+++ b/gameServer2/src/main.rs	Tue Jan 03 00:19:47 2017 +0300
@@ -11,6 +11,7 @@
 
 mod utils;
 mod server;
+mod protocol;
 
 fn main() {
     println!("Hedgewars game server, protocol {}", utils::PROTOCOL_VERSION);
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/gameServer2/src/protocol.rs	Tue Jan 03 00:19:47 2017 +0300
@@ -0,0 +1,24 @@
+use netbuf;
+use std::io::Read;
+use std::io::Result;
+
+
+pub struct FrameDecoder {
+    buf: netbuf::Buf,
+}
+
+impl FrameDecoder {
+    pub fn new() -> FrameDecoder {
+        FrameDecoder {
+            buf: netbuf::Buf::new()
+        }
+    }
+
+    pub fn read_from<R: Read>(&mut self, stream: &mut R) -> Result<usize> {
+        self.buf.read_from(stream)
+    }
+
+    pub fn extract_messages(&mut self) -> &[u8] {
+        &self.buf[..]
+    }
+}
--- a/gameServer2/src/server/client.rs	Mon Jan 02 00:16:22 2017 +0300
+++ b/gameServer2/src/server/client.rs	Tue Jan 03 00:19:47 2017 +0300
@@ -6,10 +6,11 @@
 use netbuf;
 
 use utils;
+use protocol::FrameDecoder;
 
 pub struct HWClient {
     sock: TcpStream,
-    buf_in: netbuf::Buf,
+    decoder: FrameDecoder,
     buf_out: netbuf::Buf
 }
 
@@ -17,7 +18,7 @@
     pub fn new(sock: TcpStream) -> HWClient {
         HWClient {
             sock: sock,
-            buf_in: netbuf::Buf::new(),
+            decoder: FrameDecoder::new(),
             buf_out: netbuf::Buf::new(),
         }
     }
@@ -43,8 +44,9 @@
     }
 
     pub fn readable(&mut self, poll: &Poll) -> io::Result<()> {
-        self.buf_in.read_from(&mut self.sock)?;
-        println!("Incoming buffer size: {}", self.buf_in.len());
+        let v = self.decoder.read_from(&mut self.sock)?;
+        self.decoder.extract_messages();
+        println!("Read {} bytes", v);
         Ok(())
     }
 
--- a/gameServer2/src/server/server.rs	Mon Jan 02 00:16:22 2017 +0300
+++ b/gameServer2/src/server/server.rs	Tue Jan 03 00:19:47 2017 +0300
@@ -31,7 +31,7 @@
     }
 
     pub fn accept(&mut self, poll: &Poll) -> io::Result<()> {
-        let (sock, addr) = self.listener.accept().unwrap();
+        let (sock, addr) = self.listener.accept()?;
         println!("Connected: {}", addr);
 
         let client = HWClient::new(sock);