gameServer2/src/server/client.rs
changeset 12148 7e874846afe3
parent 12147 4d7d41be1993
child 12149 589a2d7d3dc5
equal deleted inserted replaced
12147:4d7d41be1993 12148:7e874846afe3
     1 use slab;
       
     2 use mio::tcp::*;
     1 use mio::tcp::*;
     3 use mio::*;
     2 use mio::*;
     4 use std::io::Write;
     3 use std::io::Write;
     5 use std::io;
     4 use std::io;
     6 use netbuf;
     5 use netbuf;
     8 use utils;
     7 use utils;
     9 use protocol::ProtocolDecoder;
     8 use protocol::ProtocolDecoder;
    10 use protocol::messages::*;
     9 use protocol::messages::*;
    11 use server::actions::Action::*;
    10 use server::actions::Action::*;
    12 use server::actions::Action;
    11 use server::actions::Action;
    13 use log;
       
    14 
    12 
    15 pub struct HWClient {
    13 pub struct HWClient {
    16     sock: TcpStream,
    14     sock: TcpStream,
    17     decoder: ProtocolDecoder,
    15     decoder: ProtocolDecoder,
    18     buf_out: netbuf::Buf,
    16     buf_out: netbuf::Buf,
    19     pub nick: String,
    17     pub nick: String,
    20     roomId: Token,
    18     room_id: Token,
    21 }
    19 }
    22 
    20 
    23 impl HWClient {
    21 impl HWClient {
    24     pub fn new(sock: TcpStream, roomId: &Token) -> HWClient {
    22     pub fn new(sock: TcpStream, roomId: &Token) -> HWClient {
    25         HWClient {
    23         HWClient {
    26             sock: sock,
    24             sock: sock,
    27             decoder: ProtocolDecoder::new(),
    25             decoder: ProtocolDecoder::new(),
    28             buf_out: netbuf::Buf::new(),
    26             buf_out: netbuf::Buf::new(),
    29             nick: String::new(),
    27             nick: String::new(),
    30             roomId: roomId.clone(),
    28             room_id: roomId.clone(),
    31         }
    29         }
    32     }
    30     }
    33 
    31 
    34     pub fn register(&mut self, poll: &Poll, token: Token) {
    32     pub fn register(&mut self, poll: &Poll, token: Token) {
    35         poll.register(&self.sock, token, Ready::all(),
    33         poll.register(&self.sock, token, Ready::all(),
    66         debug!("Read {} bytes", v);
    64         debug!("Read {} bytes", v);
    67         let mut response = Vec::new();
    65         let mut response = Vec::new();
    68         {
    66         {
    69             for msg in self.decoder.extract_messages() {
    67             for msg in self.decoder.extract_messages() {
    70                 response.push(ReactProtocolMessage(msg));
    68                 response.push(ReactProtocolMessage(msg));
    71 /*                match msg {
       
    72                     Ping => response.push(SendMe(Pong.to_raw_protocol())),
       
    73                     Quit(Some(msg)) => response.push(ByeClient("User quit: ".to_string() + &msg)),
       
    74                     Quit(None) => response.push(ByeClient("User quit".to_string())),
       
    75                     Nick(nick) => if self.nick.len() == 0 {
       
    76                         response.push(SetNick(nick.to_string()));
       
    77                     },
       
    78                     Malformed => warn!("Malformed/unknown message"),
       
    79                     Empty => warn!("Empty message"),
       
    80                     _ => unimplemented!(),
       
    81                 }*/
       
    82             }
    69             }
    83         }
    70         }
    84         self.decoder.sweep();
    71         self.decoder.sweep();
    85         response
    72         response
    86     }
    73     }