gameServer2/src/server/client.rs
changeset 12138 e0bf51609062
parent 12137 193dfdcb0620
child 12139 f3121d7dedec
equal deleted inserted replaced
12137:193dfdcb0620 12138:e0bf51609062
     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::*;
    11 use protocol::messages::HWProtocolMessage::*;
       
    12 use server::actions::Action::*;
       
    13 use server::actions::Action;
    12 use log;
    14 use log;
    13 
    15 
    14 pub struct HWClient {
    16 pub struct HWClient {
    15     sock: TcpStream,
    17     sock: TcpStream,
    16     decoder: ProtocolDecoder,
    18     decoder: ProtocolDecoder,
    32             .ok().expect("could not register socket with event loop");
    34             .ok().expect("could not register socket with event loop");
    33 
    35 
    34         self.send_msg(Connected(utils::PROTOCOL_VERSION));
    36         self.send_msg(Connected(utils::PROTOCOL_VERSION));
    35     }
    37     }
    36 
    38 
    37     fn send_raw_msg(&mut self, msg: &[u8]) {
    39     pub fn send_raw_msg(&mut self, msg: &[u8]) {
    38         self.buf_out.write(msg).unwrap();
    40         self.buf_out.write(msg).unwrap();
    39         self.flush();
    41         self.flush();
    40     }
    42     }
    41 
    43 
    42     fn send_msg(&mut self, msg: messages::HWProtocolMessage) {
    44     pub fn send_string(&mut self, msg: &String) {
    43         self.send_raw_msg(&msg.to_raw_protocol().into_bytes());
    45         self.send_raw_msg(&msg.as_bytes());
       
    46     }
       
    47 
       
    48     pub fn send_msg(&mut self, msg: messages::HWProtocolMessage) {
       
    49         self.send_string(&msg.to_raw_protocol());
    44     }
    50     }
    45 
    51 
    46     fn flush(&mut self) {
    52     fn flush(&mut self) {
    47         self.buf_out.write_to(&mut self.sock).unwrap();
    53         self.buf_out.write_to(&mut self.sock).unwrap();
    48         self.sock.flush();
    54         self.sock.flush();
    49     }
    55     }
    50 
    56 
    51     pub fn readable(&mut self, poll: &Poll) -> io::Result<()> {
    57     pub fn readable(&mut self, poll: &Poll) -> Vec<Action> {
    52         let v = self.decoder.read_from(&mut self.sock)?;
    58         let v = self.decoder.read_from(&mut self.sock).unwrap();
    53         debug!("Read {} bytes", v);
    59         debug!("Read {} bytes", v);
    54         let mut response = Vec::new();
    60         let mut response = Vec::new();
    55         {
    61         {
    56             let msgs = self.decoder.extract_messages();
    62             let msgs = self.decoder.extract_messages();
    57             for msg in msgs {
    63             for msg in msgs {
    58                 match msg {
    64                 match msg {
    59                     Ping => response.push(Pong),
    65                     Ping => response.push(SendMe(Pong.to_raw_protocol())),
    60                     Malformed => warn!("Malformed/unknown message"),
    66                     Malformed => warn!("Malformed/unknown message"),
    61                     Empty => warn!("Empty message"),
    67                     Empty => warn!("Empty message"),
    62                     _ => unimplemented!(),
    68                     _ => unimplemented!(),
    63                 }
    69                 }
    64             }
    70             }
    65         }
    71         }
    66         for r in response {
       
    67             self.send_msg(r);
       
    68         }
       
    69         self.decoder.sweep();
    72         self.decoder.sweep();
    70         Ok(())
    73         response
    71     }
    74     }
    72 
    75 
    73     pub fn writable(&mut self, poll: &Poll) -> io::Result<()> {
    76     pub fn writable(&mut self, poll: &Poll) -> io::Result<()> {
    74         self.buf_out.write_to(&mut self.sock)?;
    77         self.buf_out.write_to(&mut self.sock)?;
    75         Ok(())
    78         Ok(())