gameServer2/src/server/server.rs
changeset 13119 1e39b8749072
parent 12852 bd35cb2302b3
child 13416 cdf69667593b
--- a/gameServer2/src/server/server.rs	Thu Mar 08 16:49:49 2018 +0100
+++ b/gameServer2/src/server/server.rs	Thu Mar 08 15:01:18 2018 -0500
@@ -4,107 +4,85 @@
 use std::io;
 
 use utils;
-use super::client::HWClient;
+use super::client::*;
+use super::room::*;
 use super::actions;
+use protocol::messages::*;
+use super::handlers;
 
 type Slab<T> = slab::Slab<T>;
 
+pub enum Destination {
+    ToSelf(ClientId),
+    ToOthers(ClientId)
+}
+
+pub struct PendingMessage(pub Destination, pub HWServerMessage);
+
 pub struct HWServer {
-    listener: TcpListener,
     pub clients: Slab<HWClient>,
     pub rooms: Slab<HWRoom>,
-    pub lobby_id: usize,
+    pub lobby_id: RoomId,
+    pub output: Vec<PendingMessage>,
+    pub removed_clients: Vec<ClientId>,
 }
 
 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());
-        HWServer {
-            listener: listener,
-            clients: Slab::with_capacity(clients_limit),
-            rooms: rooms,
-            lobby_id: token,
-        }
+    pub fn new(clients_limit: usize, rooms_limit: usize) -> HWServer {
+        let rooms = Slab::with_capacity(rooms_limit);
+        let clients = Slab::with_capacity(clients_limit);
+        let mut server = HWServer {
+            clients, rooms,
+            lobby_id: 0,
+            output: vec![],
+            removed_clients: vec![]
+        };
+        server.lobby_id = server.add_room();
+        server
     }
 
-    pub fn register(&self, poll: &Poll) -> io::Result<()> {
-        poll.register(&self.listener, utils::SERVER, Ready::readable(),
-                      PollOpt::edge())
-    }
-
-    pub fn accept(&mut self, poll: &Poll) -> io::Result<()> {
-        let (sock, addr) = self.listener.accept()?;
-        info!("Connected: {}", addr);
-
-        let client = HWClient::new(sock);
-        let token = self.clients.insert(client);
-
-        self.clients[token].id = token;
-        self.clients[token].register(poll, Token(token));
-
-        Ok(())
+    pub fn add_client(&mut self) -> ClientId {
+        let key: ClientId;
+        {
+            let entry = self.clients.vacant_entry();
+            key = entry.key();
+            let client = HWClient::new(entry.key());
+            entry.insert(client);
+        }
+        self.send_self(key, HWServerMessage::Connected(utils::PROTOCOL_VERSION));
+        key
     }
 
-    pub fn client_readable(&mut self, poll: &Poll,
-                           token: usize) -> io::Result<()> {
-        let actions;
-        {
-            actions = self.clients[token].readable(poll);
-        }
-
-        self.react(token, poll, actions);
-
-        Ok(())
+    pub fn client_lost(&mut self, client_id: ClientId) {
+        actions::run_action(self, client_id,
+                            actions::Action::ByeClient("Connection reset".to_string()));
     }
 
-    pub fn client_writable(&mut self, poll: &Poll,
-                           token: usize) -> io::Result<()> {
-        self.clients[token].writable(poll)?;
-
-        Ok(())
+    pub fn add_room(&mut self) -> RoomId {
+        let entry = self.rooms.vacant_entry();
+        let key = entry.key();
+        let room = HWRoom::new(entry.key());
+        entry.insert(room);
+        key
     }
 
-    pub fn client_error(&mut self, poll: &Poll,
-                           token: usize) -> io::Result<()> {
-        let actions;
-        {
-            actions = self.clients[token].error(poll);
-        }
+    pub fn handle_msg(&mut self, client_id: ClientId, msg: HWProtocolMessage) {
+        handlers::handle(self, client_id, msg);
+    }
 
-        self.react(token, poll, actions);
-
-        Ok(())
+    pub fn send_self(&mut self, client_id: ClientId, msg: HWServerMessage) {
+        self.output.push(PendingMessage(
+            Destination::ToSelf(client_id), msg));
     }
 
-    pub fn send(&mut self, token: usize, msg: &String) {
-        self.clients[token].send_string(msg);
+    pub fn send_others(&mut self, client_id: ClientId, msg: HWServerMessage) {
+        self.output.push(PendingMessage(
+            Destination::ToOthers(client_id), msg));
     }
 
-    pub fn react(&mut self, token: usize, poll: &Poll, actions: Vec<actions::Action>) {
+    pub fn react(&mut self, client_id: ClientId, actions: Vec<actions::Action>) {
         for action in actions {
-            actions::run_action(self, token, poll, action);
+            actions::run_action(self, client_id, action);
         }
     }
 }
-
-
-pub struct HWRoom {
-    pub id: usize,
-    pub name: String,
-    pub password: Option<String>,
-    pub protocol_number: u32,
-    pub ready_players_number: u8,
-}
-
-impl HWRoom {
-    pub fn new() -> HWRoom {
-        HWRoom {
-            id: 0,
-            name: String::new(),
-            password: None,
-            protocol_number: 0,
-            ready_players_number: 0,
-        }
-    }
-}