Refactor code to add more structure to it
authorunc0rr
Sun, 01 Jan 2017 22:13:35 +0300
changeset 12126 4348997e502b
parent 12125 858bf4d04c54
child 12127 36ac9c075d0d
Refactor code to add more structure to it
.hgignore
gameServer2/Cargo.toml
gameServer2/src/main.rs
gameServer2/src/server.rs
gameServer2/src/utils.rs
--- a/.hgignore	Sat Dec 31 23:56:29 2016 +0300
+++ b/.hgignore	Sun Jan 01 22:13:35 2017 +0300
@@ -78,3 +78,8 @@
 glob:xcuserdata
 glob:*.mode1v3
 glob:*.mode2v3
+glob:gameServer2/target
+glob:gameServer2/Cargo.lock
+glob:gameServer2/gameServer2.iml
+glob:.idea
+
--- a/gameServer2/Cargo.toml	Sat Dec 31 23:56:29 2016 +0300
+++ b/gameServer2/Cargo.toml	Sun Jan 01 22:13:35 2017 +0300
@@ -7,4 +7,4 @@
 [dependencies]
 rand = "0.3"
 mio = "0.6"
-
+slab = "0.3"
--- a/gameServer2/src/main.rs	Sat Dec 31 23:56:29 2016 +0300
+++ b/gameServer2/src/main.rs	Sun Jan 01 22:13:35 2017 +0300
@@ -1,27 +1,25 @@
 extern crate rand;
 extern crate mio;
+extern crate slab;
 
-use std::io::*;
+//use std::io::*;
 //use rand::Rng;
 //use std::cmp::Ordering;
 use mio::tcp::*;
 use mio::*;
 
 mod utils;
-
-const SERVER: mio::Token = mio::Token(0);
-
-
+mod server;
 
 fn main() {
     println!("Hedgewars game server, protocol {}", utils::PROTOCOL_VERSION);
 
     let address = "0.0.0.0:46631".parse().unwrap();
-    let server = TcpListener::bind(&address).unwrap();
+    let listener = TcpListener::bind(&address).unwrap();
+    let mut server = server::HWServer::new(listener, 1024, 512);
 
     let poll = Poll::new().unwrap();
-    poll.register(&server, SERVER, Ready::readable(),
-               PollOpt::edge()).unwrap();
+    server.register(&poll).unwrap();
 
     let mut events = Events::with_capacity(1024);
 
@@ -30,17 +28,7 @@
 
         for event in events.iter() {
             match event.token() {
-                SERVER => match server.accept() {
-                    Ok((mut client_stream, addr)) => {
-                        println!("Connected: {}", addr);
-                        client_stream.write_all(
-                            format!("CONNECTED\nHedgewars server http://www.hedgewars.org/\n{}\n\n"
-                                    , utils::PROTOCOL_VERSION).as_bytes()
-                        );
-                    },
-                    _ => unreachable!()
-                },
-
+                utils::SERVER => server.accept(&poll).unwrap(),
                 _ => unreachable!(),
             }
         }
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/gameServer2/src/server.rs	Sun Jan 01 22:13:35 2017 +0300
@@ -0,0 +1,71 @@
+use slab::*;
+use mio::tcp::*;
+use mio::*;
+use mio;
+use std::io::Write;
+use std::io;
+
+use utils;
+
+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].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");
+
+        Ok(())
+    }
+}
+
+struct HWClient {
+    sock: TcpStream,
+    uid: Option<usize>
+}
+
+impl HWClient {
+    fn new(sock: TcpStream) -> HWClient {
+        HWClient {
+            sock: sock,
+            uid: None
+        }
+    }
+
+    fn send_raw_msg(&mut self, msg: &[u8]) {
+        self.sock.write_all(msg).unwrap();
+    }
+}
+
+struct HWRoom {
+    name: String
+}
--- a/gameServer2/src/utils.rs	Sat Dec 31 23:56:29 2016 +0300
+++ b/gameServer2/src/utils.rs	Sun Jan 01 22:13:35 2017 +0300
@@ -1,1 +1,4 @@
+use mio;
+
 pub const PROTOCOL_VERSION : i32 = 3;
+pub const SERVER: mio::Token = mio::Token(1000000000 + 0);