rust/hedgewars-server/src/server/handlers.rs
changeset 14701 8a45c90f4580
parent 14699 25c564f77b7d
child 14784 f43ab2bd76ae
--- a/rust/hedgewars-server/src/server/handlers.rs	Thu Feb 07 18:04:53 2019 +0300
+++ b/rust/hedgewars-server/src/server/handlers.rs	Thu Feb 07 22:26:56 2019 +0300
@@ -26,6 +26,7 @@
 pub struct Response {
     client_id: ClientId,
     messages: Vec<PendingMessage>,
+    removed_clients: Vec<ClientId>,
 }
 
 impl Response {
@@ -33,12 +34,13 @@
         Self {
             client_id,
             messages: vec![],
+            removed_clients: vec![],
         }
     }
 
     #[inline]
     pub fn is_empty(&self) -> bool {
-        self.messages.is_empty()
+        self.messages.is_empty() && self.removed_clients.is_empty()
     }
 
     #[inline]
@@ -66,6 +68,14 @@
             (ids, m.message)
         })
     }
+
+    pub fn remove_client(&mut self, client_id: ClientId) {
+        self.removed_clients.push(client_id);
+    }
+
+    pub fn extract_removed_clients(&mut self) -> impl Iterator<Item = ClientId> + '_ {
+        self.removed_clients.drain(..)
+    }
 }
 
 impl Extend<PendingMessage> for Response {
@@ -130,6 +140,7 @@
                     }
                     LoginResult::Exit => {
                         server.anteroom.remove_client(client_id);
+                        response.remove_client(client_id);
                     }
                 }
             } else {
@@ -162,5 +173,7 @@
 }
 
 pub fn handle_client_loss(server: &mut HWServer, client_id: ClientId, response: &mut Response) {
-    common::remove_client(server, response, "Connection reset".to_string());
+    if server.anteroom.remove_client(client_id).is_none() {
+        common::remove_client(server, response, "Connection reset".to_string());
+    }
 }