gameServer2/src/server/client.rs
author unc0rr
Sun, 15 Jan 2017 00:34:36 +0300
changeset 12138 e0bf51609062
parent 12137 193dfdcb0620
child 12139 f3121d7dedec
permissions -rw-r--r--
Introduce actions, just like in the old server
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
12128
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;
12136
e25a82ce2374 - Render messages to string
unc0rr
parents: 12129
diff changeset
     9
use protocol::ProtocolDecoder;
e25a82ce2374 - Render messages to string
unc0rr
parents: 12129
diff changeset
    10
use protocol::messages;
e25a82ce2374 - Render messages to string
unc0rr
parents: 12129
diff changeset
    11
use protocol::messages::HWProtocolMessage::*;
12138
e0bf51609062 Introduce actions, just like in the old server
unc0rr
parents: 12137
diff changeset
    12
use server::actions::Action::*;
e0bf51609062 Introduce actions, just like in the old server
unc0rr
parents: 12137
diff changeset
    13
use server::actions::Action;
12137
193dfdcb0620 - Use logging facilities instead of plain println!
unc0rr
parents: 12136
diff changeset
    14
use log;
12128
f50876f3eff8 Refactor modules layout
unc0rr
parents:
diff changeset
    15
f50876f3eff8 Refactor modules layout
unc0rr
parents:
diff changeset
    16
pub struct HWClient {
f50876f3eff8 Refactor modules layout
unc0rr
parents:
diff changeset
    17
    sock: TcpStream,
12136
e25a82ce2374 - Render messages to string
unc0rr
parents: 12129
diff changeset
    18
    decoder: ProtocolDecoder,
12128
f50876f3eff8 Refactor modules layout
unc0rr
parents:
diff changeset
    19
    buf_out: netbuf::Buf
f50876f3eff8 Refactor modules layout
unc0rr
parents:
diff changeset
    20
}
f50876f3eff8 Refactor modules layout
unc0rr
parents:
diff changeset
    21
f50876f3eff8 Refactor modules layout
unc0rr
parents:
diff changeset
    22
impl HWClient {
f50876f3eff8 Refactor modules layout
unc0rr
parents:
diff changeset
    23
    pub fn new(sock: TcpStream) -> HWClient {
f50876f3eff8 Refactor modules layout
unc0rr
parents:
diff changeset
    24
        HWClient {
f50876f3eff8 Refactor modules layout
unc0rr
parents:
diff changeset
    25
            sock: sock,
12136
e25a82ce2374 - Render messages to string
unc0rr
parents: 12129
diff changeset
    26
            decoder: ProtocolDecoder::new(),
12128
f50876f3eff8 Refactor modules layout
unc0rr
parents:
diff changeset
    27
            buf_out: netbuf::Buf::new(),
f50876f3eff8 Refactor modules layout
unc0rr
parents:
diff changeset
    28
        }
f50876f3eff8 Refactor modules layout
unc0rr
parents:
diff changeset
    29
    }
f50876f3eff8 Refactor modules layout
unc0rr
parents:
diff changeset
    30
f50876f3eff8 Refactor modules layout
unc0rr
parents:
diff changeset
    31
    pub fn register(&mut self, poll: &Poll, token: Token) {
12137
193dfdcb0620 - Use logging facilities instead of plain println!
unc0rr
parents: 12136
diff changeset
    32
        poll.register(&self.sock, token, Ready::all(),
12128
f50876f3eff8 Refactor modules layout
unc0rr
parents:
diff changeset
    33
                      PollOpt::edge())
f50876f3eff8 Refactor modules layout
unc0rr
parents:
diff changeset
    34
            .ok().expect("could not register socket with event loop");
f50876f3eff8 Refactor modules layout
unc0rr
parents:
diff changeset
    35
12137
193dfdcb0620 - Use logging facilities instead of plain println!
unc0rr
parents: 12136
diff changeset
    36
        self.send_msg(Connected(utils::PROTOCOL_VERSION));
12128
f50876f3eff8 Refactor modules layout
unc0rr
parents:
diff changeset
    37
    }
f50876f3eff8 Refactor modules layout
unc0rr
parents:
diff changeset
    38
12138
e0bf51609062 Introduce actions, just like in the old server
unc0rr
parents: 12137
diff changeset
    39
    pub fn send_raw_msg(&mut self, msg: &[u8]) {
12128
f50876f3eff8 Refactor modules layout
unc0rr
parents:
diff changeset
    40
        self.buf_out.write(msg).unwrap();
f50876f3eff8 Refactor modules layout
unc0rr
parents:
diff changeset
    41
        self.flush();
f50876f3eff8 Refactor modules layout
unc0rr
parents:
diff changeset
    42
    }
f50876f3eff8 Refactor modules layout
unc0rr
parents:
diff changeset
    43
12138
e0bf51609062 Introduce actions, just like in the old server
unc0rr
parents: 12137
diff changeset
    44
    pub fn send_string(&mut self, msg: &String) {
e0bf51609062 Introduce actions, just like in the old server
unc0rr
parents: 12137
diff changeset
    45
        self.send_raw_msg(&msg.as_bytes());
e0bf51609062 Introduce actions, just like in the old server
unc0rr
parents: 12137
diff changeset
    46
    }
e0bf51609062 Introduce actions, just like in the old server
unc0rr
parents: 12137
diff changeset
    47
e0bf51609062 Introduce actions, just like in the old server
unc0rr
parents: 12137
diff changeset
    48
    pub fn send_msg(&mut self, msg: messages::HWProtocolMessage) {
e0bf51609062 Introduce actions, just like in the old server
unc0rr
parents: 12137
diff changeset
    49
        self.send_string(&msg.to_raw_protocol());
12136
e25a82ce2374 - Render messages to string
unc0rr
parents: 12129
diff changeset
    50
    }
e25a82ce2374 - Render messages to string
unc0rr
parents: 12129
diff changeset
    51
12128
f50876f3eff8 Refactor modules layout
unc0rr
parents:
diff changeset
    52
    fn flush(&mut self) {
f50876f3eff8 Refactor modules layout
unc0rr
parents:
diff changeset
    53
        self.buf_out.write_to(&mut self.sock).unwrap();
f50876f3eff8 Refactor modules layout
unc0rr
parents:
diff changeset
    54
        self.sock.flush();
f50876f3eff8 Refactor modules layout
unc0rr
parents:
diff changeset
    55
    }
f50876f3eff8 Refactor modules layout
unc0rr
parents:
diff changeset
    56
12138
e0bf51609062 Introduce actions, just like in the old server
unc0rr
parents: 12137
diff changeset
    57
    pub fn readable(&mut self, poll: &Poll) -> Vec<Action> {
e0bf51609062 Introduce actions, just like in the old server
unc0rr
parents: 12137
diff changeset
    58
        let v = self.decoder.read_from(&mut self.sock).unwrap();
12137
193dfdcb0620 - Use logging facilities instead of plain println!
unc0rr
parents: 12136
diff changeset
    59
        debug!("Read {} bytes", v);
12136
e25a82ce2374 - Render messages to string
unc0rr
parents: 12129
diff changeset
    60
        let mut response = Vec::new();
e25a82ce2374 - Render messages to string
unc0rr
parents: 12129
diff changeset
    61
        {
e25a82ce2374 - Render messages to string
unc0rr
parents: 12129
diff changeset
    62
            let msgs = self.decoder.extract_messages();
e25a82ce2374 - Render messages to string
unc0rr
parents: 12129
diff changeset
    63
            for msg in msgs {
e25a82ce2374 - Render messages to string
unc0rr
parents: 12129
diff changeset
    64
                match msg {
12138
e0bf51609062 Introduce actions, just like in the old server
unc0rr
parents: 12137
diff changeset
    65
                    Ping => response.push(SendMe(Pong.to_raw_protocol())),
12137
193dfdcb0620 - Use logging facilities instead of plain println!
unc0rr
parents: 12136
diff changeset
    66
                    Malformed => warn!("Malformed/unknown message"),
193dfdcb0620 - Use logging facilities instead of plain println!
unc0rr
parents: 12136
diff changeset
    67
                    Empty => warn!("Empty message"),
193dfdcb0620 - Use logging facilities instead of plain println!
unc0rr
parents: 12136
diff changeset
    68
                    _ => unimplemented!(),
12136
e25a82ce2374 - Render messages to string
unc0rr
parents: 12129
diff changeset
    69
                }
e25a82ce2374 - Render messages to string
unc0rr
parents: 12129
diff changeset
    70
            }
e25a82ce2374 - Render messages to string
unc0rr
parents: 12129
diff changeset
    71
        }
e25a82ce2374 - Render messages to string
unc0rr
parents: 12129
diff changeset
    72
        self.decoder.sweep();
12138
e0bf51609062 Introduce actions, just like in the old server
unc0rr
parents: 12137
diff changeset
    73
        response
12128
f50876f3eff8 Refactor modules layout
unc0rr
parents:
diff changeset
    74
    }
f50876f3eff8 Refactor modules layout
unc0rr
parents:
diff changeset
    75
f50876f3eff8 Refactor modules layout
unc0rr
parents:
diff changeset
    76
    pub fn writable(&mut self, poll: &Poll) -> io::Result<()> {
f50876f3eff8 Refactor modules layout
unc0rr
parents:
diff changeset
    77
        self.buf_out.write_to(&mut self.sock)?;
f50876f3eff8 Refactor modules layout
unc0rr
parents:
diff changeset
    78
        Ok(())
f50876f3eff8 Refactor modules layout
unc0rr
parents:
diff changeset
    79
    }
12137
193dfdcb0620 - Use logging facilities instead of plain println!
unc0rr
parents: 12136
diff changeset
    80
193dfdcb0620 - Use logging facilities instead of plain println!
unc0rr
parents: 12136
diff changeset
    81
    pub fn error(&mut self, poll: &Poll) -> io::Result<()> {
193dfdcb0620 - Use logging facilities instead of plain println!
unc0rr
parents: 12136
diff changeset
    82
        debug!("Client error");
193dfdcb0620 - Use logging facilities instead of plain println!
unc0rr
parents: 12136
diff changeset
    83
        Ok(())
193dfdcb0620 - Use logging facilities instead of plain println!
unc0rr
parents: 12136
diff changeset
    84
    }
12128
f50876f3eff8 Refactor modules layout
unc0rr
parents:
diff changeset
    85
}