gameServer2/src/server/client.rs
changeset 12134 07972a8c2433
parent 12133 f50876f3eff8
child 12141 e25a82ce2374
equal deleted inserted replaced
12133:f50876f3eff8 12134:07972a8c2433
     4 use std::io::Write;
     4 use std::io::Write;
     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::FrameDecoder;
     9 
    10 
    10 pub struct HWClient {
    11 pub struct HWClient {
    11     sock: TcpStream,
    12     sock: TcpStream,
    12     buf_in: netbuf::Buf,
    13     decoder: FrameDecoder,
    13     buf_out: netbuf::Buf
    14     buf_out: netbuf::Buf
    14 }
    15 }
    15 
    16 
    16 impl HWClient {
    17 impl HWClient {
    17     pub fn new(sock: TcpStream) -> HWClient {
    18     pub fn new(sock: TcpStream) -> HWClient {
    18         HWClient {
    19         HWClient {
    19             sock: sock,
    20             sock: sock,
    20             buf_in: netbuf::Buf::new(),
    21             decoder: FrameDecoder::new(),
    21             buf_out: netbuf::Buf::new(),
    22             buf_out: netbuf::Buf::new(),
    22         }
    23         }
    23     }
    24     }
    24 
    25 
    25     pub fn register(&mut self, poll: &Poll, token: Token) {
    26     pub fn register(&mut self, poll: &Poll, token: Token) {
    41         self.buf_out.write_to(&mut self.sock).unwrap();
    42         self.buf_out.write_to(&mut self.sock).unwrap();
    42         self.sock.flush();
    43         self.sock.flush();
    43     }
    44     }
    44 
    45 
    45     pub fn readable(&mut self, poll: &Poll) -> io::Result<()> {
    46     pub fn readable(&mut self, poll: &Poll) -> io::Result<()> {
    46         self.buf_in.read_from(&mut self.sock)?;
    47         let v = self.decoder.read_from(&mut self.sock)?;
    47         println!("Incoming buffer size: {}", self.buf_in.len());
    48         self.decoder.extract_messages();
       
    49         println!("Read {} bytes", v);
    48         Ok(())
    50         Ok(())
    49     }
    51     }
    50 
    52 
    51     pub fn writable(&mut self, poll: &Poll) -> io::Result<()> {
    53     pub fn writable(&mut self, poll: &Poll) -> io::Result<()> {
    52         self.buf_out.write_to(&mut self.sock)?;
    54         self.buf_out.write_to(&mut self.sock)?;