gameServer2/src/server/client.rs
author unc0rr
Sat, 14 Jan 2017 22:30:09 +0300
changeset 12142 193dfdcb0620
parent 12141 e25a82ce2374
child 12143 e0bf51609062
permissions -rw-r--r--
- Use logging facilities instead of plain println! - Parse malformed messages, parser doesn't get stuck anymore
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
12133
f50876f3eff8 Refactor modules layout
unc0rr
parents:
diff changeset
     1
use slab;
f50876f3eff8 Refactor modules layout
unc0rr
parents:
diff changeset
     2
use mio::tcp::*;
f50876f3eff8 Refactor modules layout
unc0rr
parents:
diff changeset
     3
use mio::*;
f50876f3eff8 Refactor modules layout
unc0rr
parents:
diff changeset
     4
use std::io::Write;
f50876f3eff8 Refactor modules layout
unc0rr
parents:
diff changeset
     5
use std::io;
f50876f3eff8 Refactor modules layout
unc0rr
parents:
diff changeset
     6
use netbuf;
f50876f3eff8 Refactor modules layout
unc0rr
parents:
diff changeset
     7
f50876f3eff8 Refactor modules layout
unc0rr
parents:
diff changeset
     8
use utils;
12141
e25a82ce2374 - Render messages to string
unc0rr
parents: 12134
diff changeset
     9
use protocol::ProtocolDecoder;
e25a82ce2374 - Render messages to string
unc0rr
parents: 12134
diff changeset
    10
use protocol::messages;
e25a82ce2374 - Render messages to string
unc0rr
parents: 12134
diff changeset
    11
use protocol::messages::HWProtocolMessage::*;
12142
193dfdcb0620 - Use logging facilities instead of plain println!
unc0rr
parents: 12141
diff changeset
    12
use log;
12133
f50876f3eff8 Refactor modules layout
unc0rr
parents:
diff changeset
    13
f50876f3eff8 Refactor modules layout
unc0rr
parents:
diff changeset
    14
pub struct HWClient {
f50876f3eff8 Refactor modules layout
unc0rr
parents:
diff changeset
    15
    sock: TcpStream,
12141
e25a82ce2374 - Render messages to string
unc0rr
parents: 12134
diff changeset
    16
    decoder: ProtocolDecoder,
12133
f50876f3eff8 Refactor modules layout
unc0rr
parents:
diff changeset
    17
    buf_out: netbuf::Buf
f50876f3eff8 Refactor modules layout
unc0rr
parents:
diff changeset
    18
}
f50876f3eff8 Refactor modules layout
unc0rr
parents:
diff changeset
    19
f50876f3eff8 Refactor modules layout
unc0rr
parents:
diff changeset
    20
impl HWClient {
f50876f3eff8 Refactor modules layout
unc0rr
parents:
diff changeset
    21
    pub fn new(sock: TcpStream) -> HWClient {
f50876f3eff8 Refactor modules layout
unc0rr
parents:
diff changeset
    22
        HWClient {
f50876f3eff8 Refactor modules layout
unc0rr
parents:
diff changeset
    23
            sock: sock,
12141
e25a82ce2374 - Render messages to string
unc0rr
parents: 12134
diff changeset
    24
            decoder: ProtocolDecoder::new(),
12133
f50876f3eff8 Refactor modules layout
unc0rr
parents:
diff changeset
    25
            buf_out: netbuf::Buf::new(),
f50876f3eff8 Refactor modules layout
unc0rr
parents:
diff changeset
    26
        }
f50876f3eff8 Refactor modules layout
unc0rr
parents:
diff changeset
    27
    }
f50876f3eff8 Refactor modules layout
unc0rr
parents:
diff changeset
    28
f50876f3eff8 Refactor modules layout
unc0rr
parents:
diff changeset
    29
    pub fn register(&mut self, poll: &Poll, token: Token) {
12142
193dfdcb0620 - Use logging facilities instead of plain println!
unc0rr
parents: 12141
diff changeset
    30
        poll.register(&self.sock, token, Ready::all(),
12133
f50876f3eff8 Refactor modules layout
unc0rr
parents:
diff changeset
    31
                      PollOpt::edge())
f50876f3eff8 Refactor modules layout
unc0rr
parents:
diff changeset
    32
            .ok().expect("could not register socket with event loop");
f50876f3eff8 Refactor modules layout
unc0rr
parents:
diff changeset
    33
12142
193dfdcb0620 - Use logging facilities instead of plain println!
unc0rr
parents: 12141
diff changeset
    34
        self.send_msg(Connected(utils::PROTOCOL_VERSION));
12133
f50876f3eff8 Refactor modules layout
unc0rr
parents:
diff changeset
    35
    }
f50876f3eff8 Refactor modules layout
unc0rr
parents:
diff changeset
    36
f50876f3eff8 Refactor modules layout
unc0rr
parents:
diff changeset
    37
    fn send_raw_msg(&mut self, msg: &[u8]) {
f50876f3eff8 Refactor modules layout
unc0rr
parents:
diff changeset
    38
        self.buf_out.write(msg).unwrap();
f50876f3eff8 Refactor modules layout
unc0rr
parents:
diff changeset
    39
        self.flush();
f50876f3eff8 Refactor modules layout
unc0rr
parents:
diff changeset
    40
    }
f50876f3eff8 Refactor modules layout
unc0rr
parents:
diff changeset
    41
12141
e25a82ce2374 - Render messages to string
unc0rr
parents: 12134
diff changeset
    42
    fn send_msg(&mut self, msg: messages::HWProtocolMessage) {
e25a82ce2374 - Render messages to string
unc0rr
parents: 12134
diff changeset
    43
        self.send_raw_msg(&msg.to_raw_protocol().into_bytes());
e25a82ce2374 - Render messages to string
unc0rr
parents: 12134
diff changeset
    44
    }
e25a82ce2374 - Render messages to string
unc0rr
parents: 12134
diff changeset
    45
12133
f50876f3eff8 Refactor modules layout
unc0rr
parents:
diff changeset
    46
    fn flush(&mut self) {
f50876f3eff8 Refactor modules layout
unc0rr
parents:
diff changeset
    47
        self.buf_out.write_to(&mut self.sock).unwrap();
f50876f3eff8 Refactor modules layout
unc0rr
parents:
diff changeset
    48
        self.sock.flush();
f50876f3eff8 Refactor modules layout
unc0rr
parents:
diff changeset
    49
    }
f50876f3eff8 Refactor modules layout
unc0rr
parents:
diff changeset
    50
f50876f3eff8 Refactor modules layout
unc0rr
parents:
diff changeset
    51
    pub fn readable(&mut self, poll: &Poll) -> io::Result<()> {
12134
07972a8c2433 - Start protocol parser implementation
unc0rr
parents: 12133
diff changeset
    52
        let v = self.decoder.read_from(&mut self.sock)?;
12142
193dfdcb0620 - Use logging facilities instead of plain println!
unc0rr
parents: 12141
diff changeset
    53
        debug!("Read {} bytes", v);
12141
e25a82ce2374 - Render messages to string
unc0rr
parents: 12134
diff changeset
    54
        let mut response = Vec::new();
e25a82ce2374 - Render messages to string
unc0rr
parents: 12134
diff changeset
    55
        {
e25a82ce2374 - Render messages to string
unc0rr
parents: 12134
diff changeset
    56
            let msgs = self.decoder.extract_messages();
e25a82ce2374 - Render messages to string
unc0rr
parents: 12134
diff changeset
    57
            for msg in msgs {
e25a82ce2374 - Render messages to string
unc0rr
parents: 12134
diff changeset
    58
                match msg {
e25a82ce2374 - Render messages to string
unc0rr
parents: 12134
diff changeset
    59
                    Ping => response.push(Pong),
12142
193dfdcb0620 - Use logging facilities instead of plain println!
unc0rr
parents: 12141
diff changeset
    60
                    Malformed => warn!("Malformed/unknown message"),
193dfdcb0620 - Use logging facilities instead of plain println!
unc0rr
parents: 12141
diff changeset
    61
                    Empty => warn!("Empty message"),
193dfdcb0620 - Use logging facilities instead of plain println!
unc0rr
parents: 12141
diff changeset
    62
                    _ => unimplemented!(),
12141
e25a82ce2374 - Render messages to string
unc0rr
parents: 12134
diff changeset
    63
                }
e25a82ce2374 - Render messages to string
unc0rr
parents: 12134
diff changeset
    64
            }
e25a82ce2374 - Render messages to string
unc0rr
parents: 12134
diff changeset
    65
        }
e25a82ce2374 - Render messages to string
unc0rr
parents: 12134
diff changeset
    66
        for r in response {
e25a82ce2374 - Render messages to string
unc0rr
parents: 12134
diff changeset
    67
            self.send_msg(r);
e25a82ce2374 - Render messages to string
unc0rr
parents: 12134
diff changeset
    68
        }
e25a82ce2374 - Render messages to string
unc0rr
parents: 12134
diff changeset
    69
        self.decoder.sweep();
12133
f50876f3eff8 Refactor modules layout
unc0rr
parents:
diff changeset
    70
        Ok(())
f50876f3eff8 Refactor modules layout
unc0rr
parents:
diff changeset
    71
    }
f50876f3eff8 Refactor modules layout
unc0rr
parents:
diff changeset
    72
f50876f3eff8 Refactor modules layout
unc0rr
parents:
diff changeset
    73
    pub fn writable(&mut self, poll: &Poll) -> io::Result<()> {
f50876f3eff8 Refactor modules layout
unc0rr
parents:
diff changeset
    74
        self.buf_out.write_to(&mut self.sock)?;
f50876f3eff8 Refactor modules layout
unc0rr
parents:
diff changeset
    75
        Ok(())
f50876f3eff8 Refactor modules layout
unc0rr
parents:
diff changeset
    76
    }
12142
193dfdcb0620 - Use logging facilities instead of plain println!
unc0rr
parents: 12141
diff changeset
    77
193dfdcb0620 - Use logging facilities instead of plain println!
unc0rr
parents: 12141
diff changeset
    78
    pub fn error(&mut self, poll: &Poll) -> io::Result<()> {
193dfdcb0620 - Use logging facilities instead of plain println!
unc0rr
parents: 12141
diff changeset
    79
        debug!("Client error");
193dfdcb0620 - Use logging facilities instead of plain println!
unc0rr
parents: 12141
diff changeset
    80
        Ok(())
193dfdcb0620 - Use logging facilities instead of plain println!
unc0rr
parents: 12141
diff changeset
    81
    }
12133
f50876f3eff8 Refactor modules layout
unc0rr
parents:
diff changeset
    82
}