More refactoring
authorunc0rr
Wed, 25 Jan 2017 23:48:23 +0300
changeset 12149 589a2d7d3dc5
parent 12148 7e874846afe3
child 12150 a482c7a5f6e3
More refactoring
gameServer2/src/server/actions.rs
gameServer2/src/server/client.rs
gameServer2/src/server/handlers.rs
gameServer2/src/server/server.rs
--- a/gameServer2/src/server/actions.rs	Tue Jan 24 20:28:16 2017 +0300
+++ b/gameServer2/src/server/actions.rs	Wed Jan 25 23:48:23 2017 +0300
@@ -1,4 +1,11 @@
-use protocol::messages::{HWProtocolMessage, HWServerMessage};
+use mio;
+use std::io::Write;
+use std::io;
+
+use super::server::HWServer;
+use protocol::messages::HWProtocolMessage;
+use protocol::messages::HWServerMessage::*;
+use super::handlers;
 
 pub enum Action {
     SendMe(String),
@@ -6,3 +13,25 @@
     ByeClient(String),
     ReactProtocolMessage(HWProtocolMessage),
 }
+
+use self::Action::*;
+
+pub fn run_action(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) =>
+            handlers::handle(server, token, poll, msg),
+        //_ => unimplemented!(),
+    }
+}
--- a/gameServer2/src/server/client.rs	Tue Jan 24 20:28:16 2017 +0300
+++ b/gameServer2/src/server/client.rs	Wed Jan 25 23:48:23 2017 +0300
@@ -7,15 +7,17 @@
 use utils;
 use protocol::ProtocolDecoder;
 use protocol::messages::*;
-use server::actions::Action::*;
-use server::actions::Action;
+use super::actions::Action::*;
+use super::actions::Action;
 
 pub struct HWClient {
     sock: TcpStream,
     decoder: ProtocolDecoder,
     buf_out: netbuf::Buf,
+    room_id: Token,
+
     pub nick: String,
-    room_id: Token,
+    pub protocolNumber: u32,
 }
 
 impl HWClient {
@@ -24,8 +26,10 @@
             sock: sock,
             decoder: ProtocolDecoder::new(),
             buf_out: netbuf::Buf::new(),
+            room_id: roomId.clone(),
+
             nick: String::new(),
-            room_id: roomId.clone(),
+            protocolNumber: 0,
         }
     }
 
--- a/gameServer2/src/server/handlers.rs	Tue Jan 24 20:28:16 2017 +0300
+++ b/gameServer2/src/server/handlers.rs	Wed Jan 25 23:48:23 2017 +0300
@@ -8,35 +8,23 @@
 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 {
+pub fn handle(server: &mut HWServer, token: mio::Token, poll: &mio::Poll, message: HWProtocolMessage) {
+    match message {
+        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!(),
+        HWProtocolMessage::Proto(proto) =>
+                server.clients[token].protocolNumber = proto,
+        HWProtocolMessage::Malformed => warn!("Malformed/unknown message"),
+        HWProtocolMessage::Empty => warn!("Empty message"),
+        _ => unimplemented!(),
     }
 }
--- a/gameServer2/src/server/server.rs	Tue Jan 24 20:28:16 2017 +0300
+++ b/gameServer2/src/server/server.rs	Wed Jan 25 23:48:23 2017 +0300
@@ -4,9 +4,8 @@
 use std::io;
 
 use utils;
-use server::client::HWClient;
-use server::actions::Action;
-use super::handlers;
+use super::client::HWClient;
+use super::actions;
 
 type Slab<T> = slab::Slab<T, Token>;
 
@@ -82,9 +81,9 @@
         self.clients[token].send_string(msg);
     }
 
-    pub fn react(&mut self, token: Token, poll: &Poll, actions: Vec<Action>) {
+    pub fn react(&mut self, token: Token, poll: &Poll, actions: Vec<actions::Action>) {
         for action in actions {
-            handlers::handle(self, token, poll, action);
+            actions::run_action(self, token, poll, action);
         }
     }
 }