gameServer2/src/server/client.rs
changeset 12147 4d7d41be1993
parent 12146 78925eff02c2
child 12148 7e874846afe3
equal deleted inserted replaced
12146:78925eff02c2 12147:4d7d41be1993
     5 use std::io;
     5 use std::io;
     6 use netbuf;
     6 use netbuf;
     7 
     7 
     8 use utils;
     8 use utils;
     9 use protocol::ProtocolDecoder;
     9 use protocol::ProtocolDecoder;
    10 use protocol::messages;
    10 use protocol::messages::*;
    11 use protocol::messages::HWProtocolMessage::*;
       
    12 use server::actions::Action::*;
    11 use server::actions::Action::*;
    13 use server::actions::Action;
    12 use server::actions::Action;
    14 use log;
    13 use log;
    15 
    14 
    16 pub struct HWClient {
    15 pub struct HWClient {
    35     pub fn register(&mut self, poll: &Poll, token: Token) {
    34     pub fn register(&mut self, poll: &Poll, token: Token) {
    36         poll.register(&self.sock, token, Ready::all(),
    35         poll.register(&self.sock, token, Ready::all(),
    37                       PollOpt::edge())
    36                       PollOpt::edge())
    38             .ok().expect("could not register socket with event loop");
    37             .ok().expect("could not register socket with event loop");
    39 
    38 
    40         self.send_msg(Connected(utils::PROTOCOL_VERSION));
    39         self.send_msg(HWServerMessage::Connected(utils::PROTOCOL_VERSION));
    41     }
    40     }
    42 
    41 
    43     pub fn deregister(&mut self, poll: &Poll) {
    42     pub fn deregister(&mut self, poll: &Poll) {
    44         poll.deregister(&self.sock);
    43         poll.deregister(&self.sock);
    45     }
    44     }
    51 
    50 
    52     pub fn send_string(&mut self, msg: &String) {
    51     pub fn send_string(&mut self, msg: &String) {
    53         self.send_raw_msg(&msg.as_bytes());
    52         self.send_raw_msg(&msg.as_bytes());
    54     }
    53     }
    55 
    54 
    56     pub fn send_msg(&mut self, msg: messages::HWProtocolMessage) {
    55     pub fn send_msg(&mut self, msg: HWServerMessage) {
    57         self.send_string(&msg.to_raw_protocol());
    56         self.send_string(&msg.to_raw_protocol());
    58     }
    57     }
    59 
    58 
    60     fn flush(&mut self) {
    59     fn flush(&mut self) {
    61         self.buf_out.write_to(&mut self.sock).unwrap();
    60         self.buf_out.write_to(&mut self.sock).unwrap();
    65     pub fn readable(&mut self, poll: &Poll) -> Vec<Action> {
    64     pub fn readable(&mut self, poll: &Poll) -> Vec<Action> {
    66         let v = self.decoder.read_from(&mut self.sock).unwrap();
    65         let v = self.decoder.read_from(&mut self.sock).unwrap();
    67         debug!("Read {} bytes", v);
    66         debug!("Read {} bytes", v);
    68         let mut response = Vec::new();
    67         let mut response = Vec::new();
    69         {
    68         {
    70             let msgs = self.decoder.extract_messages();
    69             for msg in self.decoder.extract_messages() {
    71             for msg in msgs {
    70                 response.push(ReactProtocolMessage(msg));
    72                 match msg {
    71 /*                match msg {
    73                     Ping => response.push(SendMe(Pong.to_raw_protocol())),
    72                     Ping => response.push(SendMe(Pong.to_raw_protocol())),
    74                     Quit(Some(msg)) => response.push(ByeClient("User quit: ".to_string() + msg)),
    73                     Quit(Some(msg)) => response.push(ByeClient("User quit: ".to_string() + &msg)),
    75                     Quit(None) => response.push(ByeClient("User quit".to_string())),
    74                     Quit(None) => response.push(ByeClient("User quit".to_string())),
    76                     Nick(nick) => if self.nick.len() == 0 {
    75                     Nick(nick) => if self.nick.len() == 0 {
    77                         response.push(SetNick(nick.to_string()));
    76                         response.push(SetNick(nick.to_string()));
    78                     },
    77                     },
    79                     Malformed => warn!("Malformed/unknown message"),
    78                     Malformed => warn!("Malformed/unknown message"),
    80                     Empty => warn!("Empty message"),
    79                     Empty => warn!("Empty message"),
    81                     _ => unimplemented!(),
    80                     _ => unimplemented!(),
    82                 }
    81                 }*/
    83             }
    82             }
    84         }
    83         }
    85         self.decoder.sweep();
    84         self.decoder.sweep();
    86         response
    85         response
    87     }
    86     }