gameServer2/src/server/client.rs
changeset 12141 78925eff02c2
parent 12139 f3121d7dedec
child 12142 4d7d41be1993
equal deleted inserted replaced
12140:7e259e47eb0d 12141:78925eff02c2
    14 use log;
    14 use log;
    15 
    15 
    16 pub struct HWClient {
    16 pub struct HWClient {
    17     sock: TcpStream,
    17     sock: TcpStream,
    18     decoder: ProtocolDecoder,
    18     decoder: ProtocolDecoder,
    19     buf_out: netbuf::Buf
    19     buf_out: netbuf::Buf,
       
    20     pub nick: String,
       
    21     roomId: Token,
    20 }
    22 }
    21 
    23 
    22 impl HWClient {
    24 impl HWClient {
    23     pub fn new(sock: TcpStream) -> HWClient {
    25     pub fn new(sock: TcpStream, roomId: &Token) -> HWClient {
    24         HWClient {
    26         HWClient {
    25             sock: sock,
    27             sock: sock,
    26             decoder: ProtocolDecoder::new(),
    28             decoder: ProtocolDecoder::new(),
    27             buf_out: netbuf::Buf::new(),
    29             buf_out: netbuf::Buf::new(),
       
    30             nick: String::new(),
       
    31             roomId: roomId.clone(),
    28         }
    32         }
    29     }
    33     }
    30 
    34 
    31     pub fn register(&mut self, poll: &Poll, token: Token) {
    35     pub fn register(&mut self, poll: &Poll, token: Token) {
    32         poll.register(&self.sock, token, Ready::all(),
    36         poll.register(&self.sock, token, Ready::all(),
    67             for msg in msgs {
    71             for msg in msgs {
    68                 match msg {
    72                 match msg {
    69                     Ping => response.push(SendMe(Pong.to_raw_protocol())),
    73                     Ping => response.push(SendMe(Pong.to_raw_protocol())),
    70                     Quit(Some(msg)) => response.push(ByeClient("User quit: ".to_string() + msg)),
    74                     Quit(Some(msg)) => response.push(ByeClient("User quit: ".to_string() + msg)),
    71                     Quit(None) => response.push(ByeClient("User quit".to_string())),
    75                     Quit(None) => response.push(ByeClient("User quit".to_string())),
       
    76                     Nick(nick) => if self.nick.len() == 0 {
       
    77                         response.push(SetNick(nick.to_string()));
       
    78                     },
    72                     Malformed => warn!("Malformed/unknown message"),
    79                     Malformed => warn!("Malformed/unknown message"),
    73                     Empty => warn!("Empty message"),
    80                     Empty => warn!("Empty message"),
    74                     _ => unimplemented!(),
    81                     _ => unimplemented!(),
    75                 }
    82                 }
    76             }
    83             }