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