Server action refactoring part 4 of N
authoralfadur <mail@none>
Mon, 04 Feb 2019 20:25:35 +0300
changeset 14694 08a8605bafaf
parent 14693 6e6632068a33
child 14695 b87c71ccd17d
Server action refactoring part 4 of N
rust/hedgewars-server/src/server/actions.rs
rust/hedgewars-server/src/server/core.rs
rust/hedgewars-server/src/server/handlers.rs
rust/hedgewars-server/src/server/handlers/common.rs
rust/hedgewars-server/src/server/handlers/loggingin.rs
rust/hedgewars-server/src/server/network.rs
--- a/rust/hedgewars-server/src/server/actions.rs	Mon Feb 04 19:22:21 2019 +0300
+++ b/rust/hedgewars-server/src/server/actions.rs	Mon Feb 04 20:25:35 2019 +0300
@@ -113,8 +113,6 @@
 
 pub enum Action {
     Send(PendingMessage),
-    RemoveClient,
-    ByeClient(String),
     CheckRegistered,
     JoinLobby,
     RemoveRoom(RoomId),
@@ -147,34 +145,6 @@
 pub fn run_action(server: &mut HWServer, client_id: usize, action: Action) {
     match action {
         Send(msg) => server.send(client_id, &msg.destination, msg.message),
-        ByeClient(msg) => {
-            let c = &server.clients[client_id];
-            let nick = c.nick.clone();
-
-            if let Some(id) = c.room_id {
-                if id != server.lobby_id {
-                    server.react(
-                        client_id,
-                        vec![MoveToLobby(format!("quit: {}", msg.clone()))],
-                    );
-                }
-            }
-
-            server.react(
-                client_id,
-                vec![
-                    LobbyLeft(nick, msg.clone()).send_all().action(),
-                    Bye(msg).send_self().action(),
-                    RemoveClient,
-                ],
-            );
-        }
-        RemoveClient => {
-            server.removed_clients.push(client_id);
-            if server.clients.contains(client_id) {
-                server.clients.remove(client_id);
-            }
-        }
         CheckRegistered => {
             let client = &server.clients[client_id];
             if client.protocol_number > 0 && client.nick != "" {
@@ -185,7 +155,8 @@
 
                 let actions = if !client.is_checker() && has_nick_clash {
                     if client.protocol_number < 38 {
-                        vec![ByeClient("Nickname is already in use".to_string())]
+                        //ByeClient("Nickname is already in use".to_string())
+                        vec![]
                     } else {
                         server.clients[client_id].nick.clear();
                         vec![Notice("NickAlreadyInUse".to_string()).send_self().action()]
--- a/rust/hedgewars-server/src/server/core.rs	Mon Feb 04 19:22:21 2019 +0300
+++ b/rust/hedgewars-server/src/server/core.rs	Mon Feb 04 20:25:35 2019 +0300
@@ -61,12 +61,20 @@
         key
     }
 
-    pub fn client_lost(&mut self, client_id: ClientId) {
-        actions::run_action(
-            self,
-            client_id,
-            actions::Action::ByeClient("Connection reset".to_string()),
-        );
+    pub fn remove_client(&mut self, client_id: ClientId) {
+        let client = &self.clients[client_id];
+        let nick = client.nick.clone();
+
+        if let Some(id) = client.room_id {
+            if id != self.lobby_id {
+                //MoveToLobby(format!("quit: {}", msg.clone()))
+            }
+        }
+
+        self.removed_clients.push(client_id);
+        if self.clients.contains(client_id) {
+            self.clients.remove(client_id);
+        }
     }
 
     pub fn add_room(&mut self) -> &mut HWRoom {
--- a/rust/hedgewars-server/src/server/handlers.rs	Mon Feb 04 19:22:21 2019 +0300
+++ b/rust/hedgewars-server/src/server/handlers.rs	Mon Feb 04 20:25:35 2019 +0300
@@ -102,10 +102,10 @@
             response.add(Pong.send_self());
         }
         HWProtocolMessage::Quit(Some(msg)) => {
-            //ByeClient("User quit: ".to_string() + &msg)
+            common::remove_client(server, response, "User quit: ".to_string() + &msg);
         }
         HWProtocolMessage::Quit(None) => {
-            //ByeClient("User quit".to_string())
+            common::remove_client(server, response, "User quit".to_string());
         }
         HWProtocolMessage::Malformed => warn!("Malformed/unknown message"),
         HWProtocolMessage::Empty => warn!("Empty message"),
@@ -118,3 +118,7 @@
         },
     }
 }
+
+pub fn handle_client_loss(server: &mut HWServer, client_id: ClientId, response: &mut Response) {
+    common::remove_client(server, response, "Connection reset".to_string());
+}
--- a/rust/hedgewars-server/src/server/handlers/common.rs	Mon Feb 04 19:22:21 2019 +0300
+++ b/rust/hedgewars-server/src/server/handlers/common.rs	Mon Feb 04 20:25:35 2019 +0300
@@ -21,6 +21,15 @@
     }
 }
 
+pub fn remove_client(server: &mut HWServer, response: &mut super::Response, msg: String) {
+    use HWServerMessage::*;
+    let nick = server.clients[response.client_id()].nick.clone();
+    response.add(LobbyLeft(nick, msg.to_string()).send_all());
+    response.add(Bye("User quit: ".to_string() + &msg).send_self());
+
+    server.remove_client(response.client_id());
+}
+
 #[cfg(test)]
 mod tests {
     use super::*;
--- a/rust/hedgewars-server/src/server/handlers/loggingin.rs	Mon Feb 04 19:22:21 2019 +0300
+++ b/rust/hedgewars-server/src/server/handlers/loggingin.rs	Mon Feb 04 20:25:35 2019 +0300
@@ -51,11 +51,29 @@
             } else if !client.nick.is_empty() {
                 response.add(Error("Nickname already provided.".to_string()).send_self());
             } else if is_name_illegal(&nick) {
-                // ByeClient("Illegal nickname! Nicknames must be between 1-40 characters long, must not have a trailing or leading space and must not have any of these characters: $()*+?[]^{|}".to_string())
+                super::common::remove_client(server, response, "Illegal nickname! Nicknames must be between 1-40 characters long, must not have a trailing or leading space and must not have any of these characters: $()*+?[]^{|}".to_string())
             } else {
                 client.nick = nick.clone();
                 response.add(Nick(nick).send_self());
-                //CheckRegistered
+
+                if client.protocol_number > 0 {
+                    //CheckRegistered
+                }
+            }
+        }
+        HWProtocolMessage::Proto(proto) => {
+            let client = &mut server.clients[client_id];
+            if client.protocol_number != 0 {
+                response.add(Error("Protocol already known.".to_string()).send_self());
+            } else if proto == 0 {
+                response.add(Error("Bad number.".to_string()).send_self());
+            } else {
+                client.protocol_number = proto;
+                response.add(Proto(proto).send_self());
+
+                if client.nick != "" {
+                    // CheckRegistered
+                }
             }
         }
         #[cfg(feature = "official-server")]
@@ -68,21 +86,9 @@
                 response.add(ServerAuth(format!("{:x}", server_hash)).send_self());
             //JoinLobby
             } else {
-                //ByeClient("Authentication failed".to_string())
+                super::common::remove_client(server, response, "Authentication failed".to_string())
             };
         }
-        HWProtocolMessage::Proto(proto) => {
-            let client = &mut server.clients[client_id];
-            if client.protocol_number != 0 {
-                response.add(Error("Protocol already known.".to_string()).send_self());
-            } else if proto == 0 {
-                response.add(Error("Bad number.".to_string()).send_self());
-            } else {
-                client.protocol_number = proto;
-                response.add(Proto(proto).send_self());
-                // CheckRegistered
-            }
-        }
         #[cfg(feature = "official-server")]
         HWProtocolMessage::Checker(protocol, nick, password) => {
             let c = &mut server.clients[client_id];
--- a/rust/hedgewars-server/src/server/network.rs	Mon Feb 04 19:22:21 2019 +0300
+++ b/rust/hedgewars-server/src/server/network.rs	Mon Feb 04 20:25:35 2019 +0300
@@ -470,7 +470,9 @@
 
     pub fn client_error(&mut self, poll: &Poll, client_id: ClientId) -> io::Result<()> {
         self.deregister_client(poll, client_id);
-        self.server.client_lost(client_id);
+        let mut response = handlers::Response::new(client_id);
+        handlers::handle_client_loss(&mut self.server, client_id, &mut response);
+        self.flush_server_messages(response);
 
         Ok(())
     }