Refactor modules layout
authorunc0rr
Mon, 02 Jan 2017 00:16:22 +0300
changeset 12128 f50876f3eff8
parent 12127 36ac9c075d0d
child 12129 07972a8c2433
Refactor modules layout
gameServer2/src/main.rs
gameServer2/src/server.rs
gameServer2/src/server/client.rs
gameServer2/src/server/mod.rs
gameServer2/src/server/server.rs
--- a/gameServer2/src/main.rs	Mon Jan 02 00:05:12 2017 +0300
+++ b/gameServer2/src/main.rs	Mon Jan 02 00:16:22 2017 +0300
@@ -17,7 +17,7 @@
 
     let address = "0.0.0.0:46631".parse().unwrap();
     let listener = TcpListener::bind(&address).unwrap();
-    let mut server = server::HWServer::new(listener, 1024, 512);
+    let mut server = server::server::HWServer::new(listener, 1024, 512);
 
     let poll = Poll::new().unwrap();
     server.register(&poll).unwrap();
--- a/gameServer2/src/server.rs	Mon Jan 02 00:05:12 2017 +0300
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,105 +0,0 @@
-use slab;
-use mio::tcp::*;
-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>,
-    rooms: Slab<HWRoom>
-}
-
-impl HWServer {
-    pub fn new(listener: TcpListener, clients_limit: usize, rooms_limit: usize) -> HWServer {
-        HWServer {
-            listener: listener,
-            clients: Slab::with_capacity(clients_limit),
-            rooms: Slab::with_capacity(rooms_limit),
-        }
-    }
-
-    pub fn register(&self, poll: &Poll) -> io::Result<()> {
-        poll.register(&self.listener, utils::SERVER, Ready::readable(),
-                      PollOpt::edge())
-    }
-
-    pub fn accept(&mut self, poll: &Poll) -> io::Result<()> {
-        let (sock, addr) = self.listener.accept().unwrap();
-        println!("Connected: {}", addr);
-
-        let client = HWClient::new(sock);
-        let token = self.clients.insert(client)
-            .ok().expect("could not add connection to slab");
-
-        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].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,
-    buf_in: netbuf::Buf,
-    buf_out: netbuf::Buf
-}
-
-impl HWClient {
-    fn new(sock: TcpStream) -> HWClient {
-        HWClient {
-            sock: sock,
-            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.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(())
-    }
-}
-
-struct HWRoom {
-    name: String
-}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/gameServer2/src/server/client.rs	Mon Jan 02 00:16:22 2017 +0300
@@ -0,0 +1,55 @@
+use slab;
+use mio::tcp::*;
+use mio::*;
+use std::io::Write;
+use std::io;
+use netbuf;
+
+use utils;
+
+pub struct HWClient {
+    sock: TcpStream,
+    buf_in: netbuf::Buf,
+    buf_out: netbuf::Buf
+}
+
+impl HWClient {
+    pub fn new(sock: TcpStream) -> HWClient {
+        HWClient {
+            sock: sock,
+            buf_in: netbuf::Buf::new(),
+            buf_out: netbuf::Buf::new(),
+        }
+    }
+
+    pub fn register(&mut self, poll: &Poll, token: Token) {
+        poll.register(&self.sock, token, Ready::readable(),
+                      PollOpt::edge())
+            .ok().expect("could not register socket with event loop");
+
+        self.send_raw_msg(
+            format!("CONNECTED\nHedgewars server http://www.hedgewars.org/\n{}\n\n"
+                    , utils::PROTOCOL_VERSION).as_bytes());
+    }
+
+    fn send_raw_msg(&mut self, msg: &[u8]) {
+        self.buf_out.write(msg).unwrap();
+        self.flush();
+    }
+
+    fn flush(&mut self) {
+        self.buf_out.write_to(&mut self.sock).unwrap();
+        self.sock.flush();
+    }
+
+    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());
+        Ok(())
+    }
+
+    pub fn writable(&mut self, poll: &Poll) -> io::Result<()> {
+        self.buf_out.write_to(&mut self.sock)?;
+        Ok(())
+    }
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/gameServer2/src/server/mod.rs	Mon Jan 02 00:16:22 2017 +0300
@@ -0,0 +1,2 @@
+pub mod server;
+pub mod client;
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/gameServer2/src/server/server.rs	Mon Jan 02 00:16:22 2017 +0300
@@ -0,0 +1,60 @@
+use slab;
+use mio::tcp::*;
+use mio::*;
+use std::io::Write;
+use std::io;
+use netbuf;
+
+use utils;
+use server::client::HWClient;
+
+type Slab<T> = slab::Slab<T, Token>;
+
+pub struct HWServer {
+    listener: TcpListener,
+    clients: Slab<HWClient>,
+    rooms: Slab<HWRoom>
+}
+
+impl HWServer {
+    pub fn new(listener: TcpListener, clients_limit: usize, rooms_limit: usize) -> HWServer {
+        HWServer {
+            listener: listener,
+            clients: Slab::with_capacity(clients_limit),
+            rooms: Slab::with_capacity(rooms_limit),
+        }
+    }
+
+    pub fn register(&self, poll: &Poll) -> io::Result<()> {
+        poll.register(&self.listener, utils::SERVER, Ready::readable(),
+                      PollOpt::edge())
+    }
+
+    pub fn accept(&mut self, poll: &Poll) -> io::Result<()> {
+        let (sock, addr) = self.listener.accept().unwrap();
+        println!("Connected: {}", addr);
+
+        let client = HWClient::new(sock);
+        let token = self.clients.insert(client)
+            .ok().expect("could not add connection to slab");
+
+        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 HWRoom {
+    name: String
+}