- Handle errors
authorunc0rr
Wed, 18 Jan 2017 22:15:55 +0300
changeset 12139 f3121d7dedec
parent 12138 e0bf51609062
child 12140 7e259e47eb0d
- Handle errors - Client removal routine - Handle QUIT message
gameServer2/src/server/actions.rs
gameServer2/src/server/client.rs
gameServer2/src/server/server.rs
--- a/gameServer2/src/server/actions.rs	Sun Jan 15 00:34:36 2017 +0300
+++ b/gameServer2/src/server/actions.rs	Wed Jan 18 22:15:55 2017 +0300
@@ -2,4 +2,6 @@
 
 pub enum Action {
     SendMe(String),
+    RemoveClient,
+    ByeClient(String),
 }
--- a/gameServer2/src/server/client.rs	Sun Jan 15 00:34:36 2017 +0300
+++ b/gameServer2/src/server/client.rs	Wed Jan 18 22:15:55 2017 +0300
@@ -36,6 +36,10 @@
         self.send_msg(Connected(utils::PROTOCOL_VERSION));
     }
 
+    pub fn deregister(&mut self, poll: &Poll) {
+        poll.deregister(&self.sock);
+    }
+
     pub fn send_raw_msg(&mut self, msg: &[u8]) {
         self.buf_out.write(msg).unwrap();
         self.flush();
@@ -63,6 +67,8 @@
             for msg in msgs {
                 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())),
                     Malformed => warn!("Malformed/unknown message"),
                     Empty => warn!("Empty message"),
                     _ => unimplemented!(),
@@ -75,11 +81,11 @@
 
     pub fn writable(&mut self, poll: &Poll) -> io::Result<()> {
         self.buf_out.write_to(&mut self.sock)?;
+
         Ok(())
     }
 
-    pub fn error(&mut self, poll: &Poll) -> io::Result<()> {
-        debug!("Client error");
-        Ok(())
+    pub fn error(&mut self, poll: &Poll) -> Vec<Action> {
+        return vec![ByeClient("Connection reset".to_string())]
     }
 }
--- a/gameServer2/src/server/server.rs	Sun Jan 15 00:34:36 2017 +0300
+++ b/gameServer2/src/server/server.rs	Wed Jan 18 22:15:55 2017 +0300
@@ -8,6 +8,7 @@
 use server::client::HWClient;
 use server::actions::Action;
 use server::actions::Action::*;
+use protocol::messages::HWProtocolMessage::*;
 
 type Slab<T> = slab::Slab<T, Token>;
 
@@ -51,26 +52,46 @@
             actions = self.clients[token].readable(poll);
         }
 
-        for action in actions {
-            self.react(token, action);
-        }
+        self.react(token, poll, actions);
+
         Ok(())
     }
 
     pub fn client_writable(&mut self, poll: &Poll,
                            token: Token) -> io::Result<()> {
-        self.clients[token].writable(poll)
+        self.clients[token].writable(poll)?;
+
+        Ok(())
     }
 
     pub fn client_error(&mut self, poll: &Poll,
                            token: Token) -> io::Result<()> {
-        self.clients[token].error(poll)
+        let actions;
+        {
+            actions = self.clients[token].error(poll);
+        }
+
+        self.react(token, poll, actions);
+
+        Ok(())
     }
 
-    fn react(&mut self, token: Token, action: Action) {
-        match action {
-            SendMe(msg) => self.clients[token].send_string(&msg),
-            //_ => unimplemented!(),
+    fn react(&mut self, token: Token, poll: &Poll, actions: Vec<Action>) {
+        for action in actions {
+            match action {
+                SendMe(msg) => self.clients[token].send_string(&msg),
+                ByeClient(msg) => {
+                    self.react(token, poll, vec![
+                        SendMe(Bye(&msg).to_raw_protocol()),
+                        RemoveClient,
+                    ]);
+                },
+                RemoveClient => {
+                    self.clients[token].deregister(poll);
+                    self.clients.remove(token);
+                },
+                //_ => unimplemented!(),
+            }
         }
     }
 }