- Use netbuf buffers for client connection stream
authorunc0rr
Mon, 02 Jan 2017 00:05:12 +0300
changeset 12132 36ac9c075d0d
parent 12131 4348997e502b
child 12133 f50876f3eff8
- Use netbuf buffers for client connection stream - Read data from client
gameServer2/Cargo.toml
gameServer2/src/main.rs
gameServer2/src/server.rs
--- a/gameServer2/Cargo.toml	Sun Jan 01 22:13:35 2017 +0300
+++ b/gameServer2/Cargo.toml	Mon Jan 02 00:05:12 2017 +0300
@@ -8,3 +8,4 @@
 rand = "0.3"
 mio = "0.6"
 slab = "0.3"
+netbuf = "0.3.8"
--- a/gameServer2/src/main.rs	Sun Jan 01 22:13:35 2017 +0300
+++ b/gameServer2/src/main.rs	Mon Jan 02 00:05:12 2017 +0300
@@ -1,6 +1,7 @@
 extern crate rand;
 extern crate mio;
 extern crate slab;
+extern crate netbuf;
 
 //use std::io::*;
 //use rand::Rng;
@@ -27,9 +28,17 @@
         poll.poll(&mut events, None).unwrap();
 
         for event in events.iter() {
-            match event.token() {
-                utils::SERVER => server.accept(&poll).unwrap(),
-                _ => unreachable!(),
+            if event.kind().is_readable() {
+                match event.token() {
+                    utils::SERVER => server.accept(&poll).unwrap(),
+                    tok => server.client_readable(&poll, tok).unwrap(),
+                }
+            }
+            if event.kind().is_writable() {
+                match event.token() {
+                    utils::SERVER => unreachable!(),
+                    tok => server.client_writable(&poll, tok).unwrap(),
+                }
             }
         }
     }
--- a/gameServer2/src/server.rs	Sun Jan 01 22:13:35 2017 +0300
+++ b/gameServer2/src/server.rs	Mon Jan 02 00:05:12 2017 +0300
@@ -1,12 +1,14 @@
-use slab::*;
+use slab;
 use mio::tcp::*;
 use mio::*;
-use mio;
 use std::io::Write;
 use std::io;
+use netbuf;
 
 use utils;
 
+type Slab<T> = slab::Slab<T, Token>;
+
 pub struct HWServer {
     listener: TcpListener,
     clients: Slab<HWClient>,
@@ -38,31 +40,63 @@
         self.clients[token].send_raw_msg(
             format!("CONNECTED\nHedgewars server http://www.hedgewars.org/\n{}\n\n"
             , utils::PROTOCOL_VERSION).as_bytes());
-
-        self.clients[token].uid = Some(token);
-        poll.register(&self.clients[token].sock, mio::Token(token), Ready::readable(),
-                      PollOpt::edge() | PollOpt::oneshot())
-            .ok().expect("could not register socket with event loop");
+        self.clients[token].register(poll, token);
 
         Ok(())
     }
+
+    pub fn client_readable(&mut self, poll: &Poll,
+                           token: Token) -> io::Result<()> {
+        self.clients[token].readable(poll)
+    }
+
+    pub fn client_writable(&mut self, poll: &Poll,
+                           token: Token) -> io::Result<()> {
+        self.clients[token].writable(poll)
+    }
 }
 
+
 struct HWClient {
     sock: TcpStream,
-    uid: Option<usize>
+    buf_in: netbuf::Buf,
+    buf_out: netbuf::Buf
 }
 
 impl HWClient {
     fn new(sock: TcpStream) -> HWClient {
         HWClient {
             sock: sock,
-            uid: None
+            buf_in: netbuf::Buf::new(),
+            buf_out: netbuf::Buf::new(),
         }
     }
 
+    fn register(&self, poll: &Poll, token: Token) {
+        poll.register(&self.sock, token, Ready::readable(),
+                      PollOpt::edge())
+            .ok().expect("could not register socket with event loop");
+    }
+
     fn send_raw_msg(&mut self, msg: &[u8]) {
-        self.sock.write_all(msg).unwrap();
+        self.buf_out.write(msg).unwrap();
+        self.flush();
+    }
+
+    fn flush(&mut self) {
+        self.buf_out.write_to(&mut self.sock).unwrap();
+        self.sock.flush();
+    }
+
+    fn readable(&mut self, poll: &Poll) -> io::Result<()> {
+        self.buf_in.read_from(&mut self.sock)?;
+        println!("Incoming buffer size: {}", self.buf_in.len());
+        Ok(())
+    }
+
+    fn writable(&mut self, poll: &Poll) -> io::Result<()> {
+        self.buf_out.write_to(&mut self.sock)?;
+        Ok(())
     }
 }