gameServer2/src/server/client.rs
author unc0rr
Wed, 25 Jan 2017 23:48:23 +0300
changeset 12149 589a2d7d3dc5
parent 12148 7e874846afe3
child 12151 8d8fb85bc09c
permissions -rw-r--r--
More refactoring
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
12133
f50876f3eff8 Refactor modules layout
unc0rr
parents:
diff changeset
     1
use mio::tcp::*;
f50876f3eff8 Refactor modules layout
unc0rr
parents:
diff changeset
     2
use mio::*;
f50876f3eff8 Refactor modules layout
unc0rr
parents:
diff changeset
     3
use std::io::Write;
f50876f3eff8 Refactor modules layout
unc0rr
parents:
diff changeset
     4
use std::io;
f50876f3eff8 Refactor modules layout
unc0rr
parents:
diff changeset
     5
use netbuf;
f50876f3eff8 Refactor modules layout
unc0rr
parents:
diff changeset
     6
f50876f3eff8 Refactor modules layout
unc0rr
parents:
diff changeset
     7
use utils;
12141
e25a82ce2374 - Render messages to string
unc0rr
parents: 12134
diff changeset
     8
use protocol::ProtocolDecoder;
12147
4d7d41be1993 Start refactoring path from getting message from client to reacting to it
unc0rr
parents: 12146
diff changeset
     9
use protocol::messages::*;
12149
589a2d7d3dc5 More refactoring
unc0rr
parents: 12148
diff changeset
    10
use super::actions::Action::*;
589a2d7d3dc5 More refactoring
unc0rr
parents: 12148
diff changeset
    11
use super::actions::Action;
12133
f50876f3eff8 Refactor modules layout
unc0rr
parents:
diff changeset
    12
f50876f3eff8 Refactor modules layout
unc0rr
parents:
diff changeset
    13
pub struct HWClient {
f50876f3eff8 Refactor modules layout
unc0rr
parents:
diff changeset
    14
    sock: TcpStream,
12141
e25a82ce2374 - Render messages to string
unc0rr
parents: 12134
diff changeset
    15
    decoder: ProtocolDecoder,
12146
78925eff02c2 Basic support for NICK message
unc0rr
parents: 12144
diff changeset
    16
    buf_out: netbuf::Buf,
12149
589a2d7d3dc5 More refactoring
unc0rr
parents: 12148
diff changeset
    17
    room_id: Token,
589a2d7d3dc5 More refactoring
unc0rr
parents: 12148
diff changeset
    18
12146
78925eff02c2 Basic support for NICK message
unc0rr
parents: 12144
diff changeset
    19
    pub nick: String,
12149
589a2d7d3dc5 More refactoring
unc0rr
parents: 12148
diff changeset
    20
    pub protocolNumber: u32,
12133
f50876f3eff8 Refactor modules layout
unc0rr
parents:
diff changeset
    21
}
f50876f3eff8 Refactor modules layout
unc0rr
parents:
diff changeset
    22
f50876f3eff8 Refactor modules layout
unc0rr
parents:
diff changeset
    23
impl HWClient {
12146
78925eff02c2 Basic support for NICK message
unc0rr
parents: 12144
diff changeset
    24
    pub fn new(sock: TcpStream, roomId: &Token) -> HWClient {
12133
f50876f3eff8 Refactor modules layout
unc0rr
parents:
diff changeset
    25
        HWClient {
f50876f3eff8 Refactor modules layout
unc0rr
parents:
diff changeset
    26
            sock: sock,
12141
e25a82ce2374 - Render messages to string
unc0rr
parents: 12134
diff changeset
    27
            decoder: ProtocolDecoder::new(),
12133
f50876f3eff8 Refactor modules layout
unc0rr
parents:
diff changeset
    28
            buf_out: netbuf::Buf::new(),
12149
589a2d7d3dc5 More refactoring
unc0rr
parents: 12148
diff changeset
    29
            room_id: roomId.clone(),
589a2d7d3dc5 More refactoring
unc0rr
parents: 12148
diff changeset
    30
12146
78925eff02c2 Basic support for NICK message
unc0rr
parents: 12144
diff changeset
    31
            nick: String::new(),
12149
589a2d7d3dc5 More refactoring
unc0rr
parents: 12148
diff changeset
    32
            protocolNumber: 0,
12133
f50876f3eff8 Refactor modules layout
unc0rr
parents:
diff changeset
    33
        }
f50876f3eff8 Refactor modules layout
unc0rr
parents:
diff changeset
    34
    }
f50876f3eff8 Refactor modules layout
unc0rr
parents:
diff changeset
    35
f50876f3eff8 Refactor modules layout
unc0rr
parents:
diff changeset
    36
    pub fn register(&mut self, poll: &Poll, token: Token) {
12142
193dfdcb0620 - Use logging facilities instead of plain println!
unc0rr
parents: 12141
diff changeset
    37
        poll.register(&self.sock, token, Ready::all(),
12133
f50876f3eff8 Refactor modules layout
unc0rr
parents:
diff changeset
    38
                      PollOpt::edge())
f50876f3eff8 Refactor modules layout
unc0rr
parents:
diff changeset
    39
            .ok().expect("could not register socket with event loop");
f50876f3eff8 Refactor modules layout
unc0rr
parents:
diff changeset
    40
12147
4d7d41be1993 Start refactoring path from getting message from client to reacting to it
unc0rr
parents: 12146
diff changeset
    41
        self.send_msg(HWServerMessage::Connected(utils::PROTOCOL_VERSION));
12133
f50876f3eff8 Refactor modules layout
unc0rr
parents:
diff changeset
    42
    }
f50876f3eff8 Refactor modules layout
unc0rr
parents:
diff changeset
    43
12144
f3121d7dedec - Handle errors
unc0rr
parents: 12143
diff changeset
    44
    pub fn deregister(&mut self, poll: &Poll) {
f3121d7dedec - Handle errors
unc0rr
parents: 12143
diff changeset
    45
        poll.deregister(&self.sock);
f3121d7dedec - Handle errors
unc0rr
parents: 12143
diff changeset
    46
    }
f3121d7dedec - Handle errors
unc0rr
parents: 12143
diff changeset
    47
12143
e0bf51609062 Introduce actions, just like in the old server
unc0rr
parents: 12142
diff changeset
    48
    pub fn send_raw_msg(&mut self, msg: &[u8]) {
12133
f50876f3eff8 Refactor modules layout
unc0rr
parents:
diff changeset
    49
        self.buf_out.write(msg).unwrap();
f50876f3eff8 Refactor modules layout
unc0rr
parents:
diff changeset
    50
        self.flush();
f50876f3eff8 Refactor modules layout
unc0rr
parents:
diff changeset
    51
    }
f50876f3eff8 Refactor modules layout
unc0rr
parents:
diff changeset
    52
12143
e0bf51609062 Introduce actions, just like in the old server
unc0rr
parents: 12142
diff changeset
    53
    pub fn send_string(&mut self, msg: &String) {
e0bf51609062 Introduce actions, just like in the old server
unc0rr
parents: 12142
diff changeset
    54
        self.send_raw_msg(&msg.as_bytes());
e0bf51609062 Introduce actions, just like in the old server
unc0rr
parents: 12142
diff changeset
    55
    }
e0bf51609062 Introduce actions, just like in the old server
unc0rr
parents: 12142
diff changeset
    56
12147
4d7d41be1993 Start refactoring path from getting message from client to reacting to it
unc0rr
parents: 12146
diff changeset
    57
    pub fn send_msg(&mut self, msg: HWServerMessage) {
12143
e0bf51609062 Introduce actions, just like in the old server
unc0rr
parents: 12142
diff changeset
    58
        self.send_string(&msg.to_raw_protocol());
12141
e25a82ce2374 - Render messages to string
unc0rr
parents: 12134
diff changeset
    59
    }
e25a82ce2374 - Render messages to string
unc0rr
parents: 12134
diff changeset
    60
12133
f50876f3eff8 Refactor modules layout
unc0rr
parents:
diff changeset
    61
    fn flush(&mut self) {
f50876f3eff8 Refactor modules layout
unc0rr
parents:
diff changeset
    62
        self.buf_out.write_to(&mut self.sock).unwrap();
f50876f3eff8 Refactor modules layout
unc0rr
parents:
diff changeset
    63
        self.sock.flush();
f50876f3eff8 Refactor modules layout
unc0rr
parents:
diff changeset
    64
    }
f50876f3eff8 Refactor modules layout
unc0rr
parents:
diff changeset
    65
12143
e0bf51609062 Introduce actions, just like in the old server
unc0rr
parents: 12142
diff changeset
    66
    pub fn readable(&mut self, poll: &Poll) -> Vec<Action> {
e0bf51609062 Introduce actions, just like in the old server
unc0rr
parents: 12142
diff changeset
    67
        let v = self.decoder.read_from(&mut self.sock).unwrap();
12142
193dfdcb0620 - Use logging facilities instead of plain println!
unc0rr
parents: 12141
diff changeset
    68
        debug!("Read {} bytes", v);
12141
e25a82ce2374 - Render messages to string
unc0rr
parents: 12134
diff changeset
    69
        let mut response = Vec::new();
e25a82ce2374 - Render messages to string
unc0rr
parents: 12134
diff changeset
    70
        {
12147
4d7d41be1993 Start refactoring path from getting message from client to reacting to it
unc0rr
parents: 12146
diff changeset
    71
            for msg in self.decoder.extract_messages() {
4d7d41be1993 Start refactoring path from getting message from client to reacting to it
unc0rr
parents: 12146
diff changeset
    72
                response.push(ReactProtocolMessage(msg));
12141
e25a82ce2374 - Render messages to string
unc0rr
parents: 12134
diff changeset
    73
            }
e25a82ce2374 - Render messages to string
unc0rr
parents: 12134
diff changeset
    74
        }
e25a82ce2374 - Render messages to string
unc0rr
parents: 12134
diff changeset
    75
        self.decoder.sweep();
12143
e0bf51609062 Introduce actions, just like in the old server
unc0rr
parents: 12142
diff changeset
    76
        response
12133
f50876f3eff8 Refactor modules layout
unc0rr
parents:
diff changeset
    77
    }
f50876f3eff8 Refactor modules layout
unc0rr
parents:
diff changeset
    78
f50876f3eff8 Refactor modules layout
unc0rr
parents:
diff changeset
    79
    pub fn writable(&mut self, poll: &Poll) -> io::Result<()> {
f50876f3eff8 Refactor modules layout
unc0rr
parents:
diff changeset
    80
        self.buf_out.write_to(&mut self.sock)?;
12144
f3121d7dedec - Handle errors
unc0rr
parents: 12143
diff changeset
    81
12133
f50876f3eff8 Refactor modules layout
unc0rr
parents:
diff changeset
    82
        Ok(())
f50876f3eff8 Refactor modules layout
unc0rr
parents:
diff changeset
    83
    }
12142
193dfdcb0620 - Use logging facilities instead of plain println!
unc0rr
parents: 12141
diff changeset
    84
12144
f3121d7dedec - Handle errors
unc0rr
parents: 12143
diff changeset
    85
    pub fn error(&mut self, poll: &Poll) -> Vec<Action> {
f3121d7dedec - Handle errors
unc0rr
parents: 12143
diff changeset
    86
        return vec![ByeClient("Connection reset".to_string())]
12142
193dfdcb0620 - Use logging facilities instead of plain println!
unc0rr
parents: 12141
diff changeset
    87
    }
12133
f50876f3eff8 Refactor modules layout
unc0rr
parents:
diff changeset
    88
}