Toss code around
authorunc0rr
Tue, 24 Jan 2017 20:28:16 +0300
changeset 12148 7e874846afe3
parent 12147 4d7d41be1993
child 12149 589a2d7d3dc5
Toss code around
gameServer2/src/server/actions.rs
gameServer2/src/server/client.rs
gameServer2/src/server/handlers.rs
gameServer2/src/server/mod.rs
gameServer2/src/server/server.rs
--- a/gameServer2/src/server/actions.rs	Mon Jan 23 23:43:29 2017 +0300
+++ b/gameServer2/src/server/actions.rs	Tue Jan 24 20:28:16 2017 +0300
@@ -1,4 +1,4 @@
-use protocol::messages::HWProtocolMessage;
+use protocol::messages::{HWProtocolMessage, HWServerMessage};
 
 pub enum Action {
     SendMe(String),
--- a/gameServer2/src/server/client.rs	Mon Jan 23 23:43:29 2017 +0300
+++ b/gameServer2/src/server/client.rs	Tue Jan 24 20:28:16 2017 +0300
@@ -1,4 +1,3 @@
-use slab;
 use mio::tcp::*;
 use mio::*;
 use std::io::Write;
@@ -10,14 +9,13 @@
 use protocol::messages::*;
 use server::actions::Action::*;
 use server::actions::Action;
-use log;
 
 pub struct HWClient {
     sock: TcpStream,
     decoder: ProtocolDecoder,
     buf_out: netbuf::Buf,
     pub nick: String,
-    roomId: Token,
+    room_id: Token,
 }
 
 impl HWClient {
@@ -27,7 +25,7 @@
             decoder: ProtocolDecoder::new(),
             buf_out: netbuf::Buf::new(),
             nick: String::new(),
-            roomId: roomId.clone(),
+            room_id: roomId.clone(),
         }
     }
 
@@ -68,17 +66,6 @@
         {
             for msg in self.decoder.extract_messages() {
                 response.push(ReactProtocolMessage(msg));
-/*                match msg {
-                    Ping => response.push(SendMe(Pong.to_raw_protocol())),
-                    Quit(Some(msg)) => response.push(ByeClient("User quit: ".to_string() + &msg)),
-                    Quit(None) => response.push(ByeClient("User quit".to_string())),
-                    Nick(nick) => if self.nick.len() == 0 {
-                        response.push(SetNick(nick.to_string()));
-                    },
-                    Malformed => warn!("Malformed/unknown message"),
-                    Empty => warn!("Empty message"),
-                    _ => unimplemented!(),
-                }*/
             }
         }
         self.decoder.sweep();
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/gameServer2/src/server/handlers.rs	Tue Jan 24 20:28:16 2017 +0300
@@ -0,0 +1,42 @@
+use mio;
+use std::io::Write;
+use std::io;
+
+use super::server::HWServer;
+use super::actions::Action;
+use super::actions::Action::*;
+use protocol::messages::HWProtocolMessage;
+use protocol::messages::HWServerMessage::*;
+
+pub fn handle(server: &mut HWServer, token: mio::Token, poll: &mio::Poll, action: Action) {
+    match action {
+        SendMe(msg) => server.send(token, &msg),
+        ByeClient(msg) => {
+            server.react(token, poll, vec![
+                SendMe(Bye(&msg).to_raw_protocol()),
+                RemoveClient,
+                ]);
+        },
+        RemoveClient => {
+            server.clients[token].deregister(poll);
+            server.clients.remove(token);
+        },
+        ReactProtocolMessage(msg) => match msg {
+            HWProtocolMessage::Ping =>
+                server.react(token, poll, vec![SendMe(Pong.to_raw_protocol())]),
+            HWProtocolMessage::Quit(Some(msg)) =>
+                server.react(token, poll, vec![ByeClient("User quit: ".to_string() + &msg)]),
+            HWProtocolMessage::Quit(None) =>
+                server.react(token, poll, vec![ByeClient("User quit".to_string())]),
+            HWProtocolMessage::Nick(nick) =>
+                if server.clients[token].nick.len() == 0 {
+                server.react(token, poll, vec![SendMe(Nick(&nick).to_raw_protocol())]);
+                server.clients[token].nick = nick;
+            },
+            HWProtocolMessage::Malformed => warn!("Malformed/unknown message"),
+            HWProtocolMessage::Empty => warn!("Empty message"),
+            _ => unimplemented!(),
+        }
+        //_ => unimplemented!(),
+    }
+}
--- a/gameServer2/src/server/mod.rs	Mon Jan 23 23:43:29 2017 +0300
+++ b/gameServer2/src/server/mod.rs	Tue Jan 24 20:28:16 2017 +0300
@@ -2,3 +2,4 @@
 pub mod client;
 pub mod coretypes;
 mod actions;
+mod handlers;
--- a/gameServer2/src/server/server.rs	Mon Jan 23 23:43:29 2017 +0300
+++ b/gameServer2/src/server/server.rs	Tue Jan 24 20:28:16 2017 +0300
@@ -1,23 +1,20 @@
 use slab;
 use mio::tcp::*;
 use mio::*;
-use std::io::Write;
 use std::io;
 
 use utils;
 use server::client::HWClient;
 use server::actions::Action;
-use server::actions::Action::*;
-use protocol::messages::HWProtocolMessage::*;
-use protocol::messages::HWServerMessage;
+use super::handlers;
 
 type Slab<T> = slab::Slab<T, Token>;
 
 pub struct HWServer {
     listener: TcpListener,
-    clients: Slab<HWClient>,
-    rooms: Slab<HWRoom>,
-    lobbyId: Token,
+    pub clients: Slab<HWClient>,
+    pub rooms: Slab<HWRoom>,
+    pub lobby_id: Token,
 }
 
 impl HWServer {
@@ -28,7 +25,7 @@
             listener: listener,
             clients: Slab::with_capacity(clients_limit),
             rooms: rooms,
-            lobbyId: token,
+            lobby_id: token,
         }
     }
 
@@ -41,7 +38,7 @@
         let (sock, addr) = self.listener.accept()?;
         info!("Connected: {}", addr);
 
-        let client = HWClient::new(sock, &self.lobbyId);
+        let client = HWClient::new(sock, &self.lobby_id);
         let token = self.clients.insert(client)
             .ok().expect("could not add connection to slab");
 
@@ -81,45 +78,20 @@
         Ok(())
     }
 
-    fn send(&mut self, token: Token, msg: &String) {
+    pub fn send(&mut self, token: Token, msg: &String) {
         self.clients[token].send_string(msg);
     }
 
-    fn react(&mut self, token: Token, poll: &Poll, actions: Vec<Action>) {
+    pub fn react(&mut self, token: Token, poll: &Poll, actions: Vec<Action>) {
         for action in actions {
-            match action {
-                SendMe(msg) => self.send(token, &msg),
-                ByeClient(msg) => {
-                    self.react(token, poll, vec![
-                        SendMe(HWServerMessage::Bye(&msg).to_raw_protocol()),
-                        RemoveClient,
-                    ]);
-                },
-                RemoveClient => {
-                    self.clients[token].deregister(poll);
-                    self.clients.remove(token);
-                },
-                ReactProtocolMessage(msg) => match msg {
-                    Ping => self.react(token, poll, vec![SendMe(HWServerMessage::Pong.to_raw_protocol())]),
-                    Quit(Some(msg)) => self.react(token, poll, vec![ByeClient("User quit: ".to_string() + &msg)]),
-                    Quit(None) => self.react(token, poll, vec![ByeClient("User quit".to_string())]),
-                    Nick(nick) => if self.clients[token].nick.len() == 0 {
-                        self.send(token, &HWServerMessage::Nick(&nick).to_raw_protocol());
-                        self.clients[token].nick = nick;
-                    },
-                    Malformed => warn!("Malformed/unknown message"),
-                    Empty => warn!("Empty message"),
-                    _ => unimplemented!(),
-                }
-                //_ => unimplemented!(),
-            }
+            handlers::handle(self, token, poll, action);
         }
     }
 }
 
 
-struct HWRoom {
-    name: String
+pub struct HWRoom {
+    pub name: String,
 }
 
 impl HWRoom {