gameServer2/src/server/server.rs
branchui-scaling
changeset 15283 c4fd2813b127
parent 13390 0135e64c6c66
parent 15279 7ab5cf405686
child 15663 d92eeb468dad
equal deleted inserted replaced
13390:0135e64c6c66 15283:c4fd2813b127
     1 use slab;
       
     2 use mio::net::*;
       
     3 use mio::*;
       
     4 use std::io;
       
     5 
       
     6 use utils;
       
     7 use super::client::*;
       
     8 use super::room::*;
       
     9 use super::actions;
       
    10 use protocol::messages::*;
       
    11 use super::handlers;
       
    12 
       
    13 type Slab<T> = slab::Slab<T>;
       
    14 
       
    15 pub enum Destination {
       
    16     ToSelf(ClientId),
       
    17     ToOthers(ClientId)
       
    18 }
       
    19 
       
    20 pub struct PendingMessage(pub Destination, pub HWServerMessage);
       
    21 
       
    22 pub struct HWServer {
       
    23     pub clients: Slab<HWClient>,
       
    24     pub rooms: Slab<HWRoom>,
       
    25     pub lobby_id: RoomId,
       
    26     pub output: Vec<PendingMessage>,
       
    27     pub removed_clients: Vec<ClientId>,
       
    28 }
       
    29 
       
    30 impl HWServer {
       
    31     pub fn new(clients_limit: usize, rooms_limit: usize) -> HWServer {
       
    32         let rooms = Slab::with_capacity(rooms_limit);
       
    33         let clients = Slab::with_capacity(clients_limit);
       
    34         let mut server = HWServer {
       
    35             clients, rooms,
       
    36             lobby_id: 0,
       
    37             output: vec![],
       
    38             removed_clients: vec![]
       
    39         };
       
    40         server.lobby_id = server.add_room();
       
    41         server
       
    42     }
       
    43 
       
    44     pub fn add_client(&mut self) -> ClientId {
       
    45         let key: ClientId;
       
    46         {
       
    47             let entry = self.clients.vacant_entry();
       
    48             key = entry.key();
       
    49             let client = HWClient::new(entry.key());
       
    50             entry.insert(client);
       
    51         }
       
    52         self.send_self(key, HWServerMessage::Connected(utils::PROTOCOL_VERSION));
       
    53         key
       
    54     }
       
    55 
       
    56     pub fn client_lost(&mut self, client_id: ClientId) {
       
    57         actions::run_action(self, client_id,
       
    58                             actions::Action::ByeClient("Connection reset".to_string()));
       
    59     }
       
    60 
       
    61     pub fn add_room(&mut self) -> RoomId {
       
    62         let entry = self.rooms.vacant_entry();
       
    63         let key = entry.key();
       
    64         let room = HWRoom::new(entry.key());
       
    65         entry.insert(room);
       
    66         key
       
    67     }
       
    68 
       
    69     pub fn handle_msg(&mut self, client_id: ClientId, msg: HWProtocolMessage) {
       
    70         handlers::handle(self, client_id, msg);
       
    71     }
       
    72 
       
    73     pub fn send_self(&mut self, client_id: ClientId, msg: HWServerMessage) {
       
    74         self.output.push(PendingMessage(
       
    75             Destination::ToSelf(client_id), msg));
       
    76     }
       
    77 
       
    78     pub fn send_others(&mut self, client_id: ClientId, msg: HWServerMessage) {
       
    79         self.output.push(PendingMessage(
       
    80             Destination::ToOthers(client_id), msg));
       
    81     }
       
    82 
       
    83     pub fn react(&mut self, client_id: ClientId, actions: Vec<actions::Action>) {
       
    84         for action in actions {
       
    85             actions::run_action(self, client_id, action);
       
    86         }
       
    87     }
       
    88 }