rust/hedgewars-server/src/server/handlers.rs
changeset 14671 455865ccd36c
parent 14457 98ef2913ec73
child 14672 6e6632068a33
--- a/rust/hedgewars-server/src/server/handlers.rs	Fri Jan 25 06:46:13 2019 +0100
+++ b/rust/hedgewars-server/src/server/handlers.rs	Sat Feb 02 15:06:39 2019 +0300
@@ -6,7 +6,10 @@
     core::HWServer,
     coretypes::ClientId,
 };
-use crate::protocol::messages::{HWProtocolMessage, HWServerMessage::*};
+use crate::{
+    protocol::messages::{HWProtocolMessage, HWServerMessage::*},
+    server::actions::PendingMessage,
+};
 use log::*;
 
 mod checker;
@@ -15,9 +18,39 @@
 mod lobby;
 mod loggingin;
 
-pub fn handle(server: &mut HWServer, client_id: ClientId, message: HWProtocolMessage) {
+pub struct Response {
+    client_id: ClientId,
+    messages: Vec<PendingMessage>,
+}
+
+impl Response {
+    pub fn new(client_id: ClientId) -> Self {
+        Self {
+            client_id,
+            messages: vec![],
+        }
+    }
+
+    pub fn client_id(&self) -> ClientId {
+        self.client_id
+    }
+
+    pub fn add(&mut self, message: PendingMessage) {
+        self.messages.push(message)
+    }
+}
+
+pub fn handle(
+    server: &mut HWServer,
+    client_id: ClientId,
+    response: &mut Response,
+    message: HWProtocolMessage,
+) {
     match message {
-        HWProtocolMessage::Ping => server.react(client_id, vec![Pong.send_self().action()]),
+        HWProtocolMessage::Ping => {
+            response.add(Pong.send_self());
+            server.react(client_id, vec![Pong.send_self().action()])
+        }
         HWProtocolMessage::Quit(Some(msg)) => {
             server.react(client_id, vec![ByeClient("User quit: ".to_string() + &msg)])
         }
@@ -27,9 +60,11 @@
         HWProtocolMessage::Malformed => warn!("Malformed/unknown message"),
         HWProtocolMessage::Empty => warn!("Empty message"),
         _ => match server.clients[client_id].room_id {
-            None => loggingin::handle(server, client_id, message),
-            Some(id) if id == server.lobby_id => lobby::handle(server, client_id, message),
-            Some(id) => inroom::handle(server, client_id, id, message),
+            None => loggingin::handle(server, client_id, response, message),
+            Some(id) if id == server.lobby_id => {
+                lobby::handle(server, client_id, response, message)
+            }
+            Some(id) => inroom::handle(server, client_id, response, id, message),
         },
     }
 }