gameServer2/src/server/server.rs
changeset 12144 f3121d7dedec
parent 12143 e0bf51609062
child 12146 78925eff02c2
equal deleted inserted replaced
12143:e0bf51609062 12144:f3121d7dedec
     6 
     6 
     7 use utils;
     7 use utils;
     8 use server::client::HWClient;
     8 use server::client::HWClient;
     9 use server::actions::Action;
     9 use server::actions::Action;
    10 use server::actions::Action::*;
    10 use server::actions::Action::*;
       
    11 use protocol::messages::HWProtocolMessage::*;
    11 
    12 
    12 type Slab<T> = slab::Slab<T, Token>;
    13 type Slab<T> = slab::Slab<T, Token>;
    13 
    14 
    14 pub struct HWServer {
    15 pub struct HWServer {
    15     listener: TcpListener,
    16     listener: TcpListener,
    49         let actions;
    50         let actions;
    50         {
    51         {
    51             actions = self.clients[token].readable(poll);
    52             actions = self.clients[token].readable(poll);
    52         }
    53         }
    53 
    54 
    54         for action in actions {
    55         self.react(token, poll, actions);
    55             self.react(token, action);
    56 
    56         }
       
    57         Ok(())
    57         Ok(())
    58     }
    58     }
    59 
    59 
    60     pub fn client_writable(&mut self, poll: &Poll,
    60     pub fn client_writable(&mut self, poll: &Poll,
    61                            token: Token) -> io::Result<()> {
    61                            token: Token) -> io::Result<()> {
    62         self.clients[token].writable(poll)
    62         self.clients[token].writable(poll)?;
       
    63 
       
    64         Ok(())
    63     }
    65     }
    64 
    66 
    65     pub fn client_error(&mut self, poll: &Poll,
    67     pub fn client_error(&mut self, poll: &Poll,
    66                            token: Token) -> io::Result<()> {
    68                            token: Token) -> io::Result<()> {
    67         self.clients[token].error(poll)
    69         let actions;
       
    70         {
       
    71             actions = self.clients[token].error(poll);
       
    72         }
       
    73 
       
    74         self.react(token, poll, actions);
       
    75 
       
    76         Ok(())
    68     }
    77     }
    69 
    78 
    70     fn react(&mut self, token: Token, action: Action) {
    79     fn react(&mut self, token: Token, poll: &Poll, actions: Vec<Action>) {
    71         match action {
    80         for action in actions {
    72             SendMe(msg) => self.clients[token].send_string(&msg),
    81             match action {
    73             //_ => unimplemented!(),
    82                 SendMe(msg) => self.clients[token].send_string(&msg),
       
    83                 ByeClient(msg) => {
       
    84                     self.react(token, poll, vec![
       
    85                         SendMe(Bye(&msg).to_raw_protocol()),
       
    86                         RemoveClient,
       
    87                     ]);
       
    88                 },
       
    89                 RemoveClient => {
       
    90                     self.clients[token].deregister(poll);
       
    91                     self.clients.remove(token);
       
    92                 },
       
    93                 //_ => unimplemented!(),
       
    94             }
    74         }
    95         }
    75     }
    96     }
    76 }
    97 }
    77 
    98 
    78 
    99