Basic support for NICK message
authorunc0rr
Wed, 18 Jan 2017 22:54:02 +0300
changeset 12141 78925eff02c2
parent 12140 7e259e47eb0d
child 12142 4d7d41be1993
Basic support for NICK message
gameServer2/src/protocol/messages.rs
gameServer2/src/server/actions.rs
gameServer2/src/server/client.rs
gameServer2/src/server/server.rs
--- a/gameServer2/src/protocol/messages.rs	Wed Jan 18 22:23:41 2017 +0300
+++ b/gameServer2/src/protocol/messages.rs	Wed Jan 18 22:54:02 2017 +0300
@@ -112,9 +112,11 @@
                 ]),
             &HWProtocolMessage::Bye(msg)
                 => construct_message(&["BYE", msg]),
+            &HWProtocolMessage::Nick(nick)
+            => construct_message(&["NICK", nick]),
             &HWProtocolMessage::LobbyLeft(msg)
                 => construct_message(&["LOBBY_LEFT", msg]),
-            _ => String::new()
+            _ => construct_message(&["ERROR", "UNIMPLEMENTED"]),
         }
     }
 }
--- a/gameServer2/src/server/actions.rs	Wed Jan 18 22:23:41 2017 +0300
+++ b/gameServer2/src/server/actions.rs	Wed Jan 18 22:54:02 2017 +0300
@@ -4,4 +4,5 @@
     SendMe(String),
     RemoveClient,
     ByeClient(String),
+    SetNick(String),
 }
--- a/gameServer2/src/server/client.rs	Wed Jan 18 22:23:41 2017 +0300
+++ b/gameServer2/src/server/client.rs	Wed Jan 18 22:54:02 2017 +0300
@@ -16,15 +16,19 @@
 pub struct HWClient {
     sock: TcpStream,
     decoder: ProtocolDecoder,
-    buf_out: netbuf::Buf
+    buf_out: netbuf::Buf,
+    pub nick: String,
+    roomId: Token,
 }
 
 impl HWClient {
-    pub fn new(sock: TcpStream) -> HWClient {
+    pub fn new(sock: TcpStream, roomId: &Token) -> HWClient {
         HWClient {
             sock: sock,
             decoder: ProtocolDecoder::new(),
             buf_out: netbuf::Buf::new(),
+            nick: String::new(),
+            roomId: roomId.clone(),
         }
     }
 
@@ -69,6 +73,9 @@
                     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())),
+                    Nick(nick) => if self.nick.len() == 0 {
+                        response.push(SetNick(nick.to_string()));
+                    },
                     Malformed => warn!("Malformed/unknown message"),
                     Empty => warn!("Empty message"),
                     _ => unimplemented!(),
--- a/gameServer2/src/server/server.rs	Wed Jan 18 22:23:41 2017 +0300
+++ b/gameServer2/src/server/server.rs	Wed Jan 18 22:54:02 2017 +0300
@@ -15,15 +15,19 @@
 pub struct HWServer {
     listener: TcpListener,
     clients: Slab<HWClient>,
-    rooms: Slab<HWRoom>
+    rooms: Slab<HWRoom>,
+    lobbyId: Token,
 }
 
 impl HWServer {
     pub fn new(listener: TcpListener, clients_limit: usize, rooms_limit: usize) -> HWServer {
+        let mut rooms = Slab::with_capacity(rooms_limit);
+        let token = rooms.insert(HWRoom::new()).ok().expect("Cannot create lobby");
         HWServer {
             listener: listener,
             clients: Slab::with_capacity(clients_limit),
-            rooms: Slab::with_capacity(rooms_limit),
+            rooms: rooms,
+            lobbyId: token,
         }
     }
 
@@ -36,7 +40,7 @@
         let (sock, addr) = self.listener.accept()?;
         info!("Connected: {}", addr);
 
-        let client = HWClient::new(sock);
+        let client = HWClient::new(sock, &self.lobbyId);
         let token = self.clients.insert(client)
             .ok().expect("could not add connection to slab");
 
@@ -76,16 +80,24 @@
         Ok(())
     }
 
+    fn send(&mut self, token: Token, msg: &String) {
+        self.clients[token].send_string(msg);
+    }
+
     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),
+                SendMe(msg) => self.send(token, &msg),
                 ByeClient(msg) => {
                     self.react(token, poll, vec![
                         SendMe(Bye(&msg).to_raw_protocol()),
                         RemoveClient,
                     ]);
                 },
+                SetNick(nick) => {
+                    self.send(token, &Nick(&nick).to_raw_protocol());
+                    self.clients[token].nick = nick;
+                }
                 RemoveClient => {
                     self.clients[token].deregister(poll);
                     self.clients.remove(token);
@@ -100,3 +112,11 @@
 struct HWRoom {
     name: String
 }
+
+impl HWRoom {
+    pub fn new() -> HWRoom {
+        HWRoom {
+            name: String::new(),
+        }
+    }
+}