gameServer2/src/server/client.rs
author unc0rr
Mon, 23 Jan 2017 23:43:29 +0300
changeset 12142 4d7d41be1993
parent 12141 78925eff02c2
child 12143 7e874846afe3
permissions -rw-r--r--
Start refactoring path from getting message from client to reacting to it
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;
12142
4d7d41be1993 Start refactoring path from getting message from client to reacting to it
unc0rr
parents: 12141
diff changeset
    10
use protocol::messages::*;
12138
e0bf51609062 Introduce actions, just like in the old server
unc0rr
parents: 12137
diff changeset
    11
use server::actions::Action::*;
e0bf51609062 Introduce actions, just like in the old server
unc0rr
parents: 12137
diff changeset
    12
use server::actions::Action;
12137
193dfdcb0620 - Use logging facilities instead of plain println!
unc0rr
parents: 12136
diff changeset
    13
use log;
12128
f50876f3eff8 Refactor modules layout
unc0rr
parents:
diff changeset
    14
f50876f3eff8 Refactor modules layout
unc0rr
parents:
diff changeset
    15
pub struct HWClient {
f50876f3eff8 Refactor modules layout
unc0rr
parents:
diff changeset
    16
    sock: TcpStream,
12136
e25a82ce2374 - Render messages to string
unc0rr
parents: 12129
diff changeset
    17
    decoder: ProtocolDecoder,
12141
78925eff02c2 Basic support for NICK message
unc0rr
parents: 12139
diff changeset
    18
    buf_out: netbuf::Buf,
78925eff02c2 Basic support for NICK message
unc0rr
parents: 12139
diff changeset
    19
    pub nick: String,
78925eff02c2 Basic support for NICK message
unc0rr
parents: 12139
diff changeset
    20
    roomId: Token,
12128
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 {
12141
78925eff02c2 Basic support for NICK message
unc0rr
parents: 12139
diff changeset
    24
    pub fn new(sock: TcpStream, roomId: &Token) -> HWClient {
12128
f50876f3eff8 Refactor modules layout
unc0rr
parents:
diff changeset
    25
        HWClient {
f50876f3eff8 Refactor modules layout
unc0rr
parents:
diff changeset
    26
            sock: sock,
12136
e25a82ce2374 - Render messages to string
unc0rr
parents: 12129
diff changeset
    27
            decoder: ProtocolDecoder::new(),
12128
f50876f3eff8 Refactor modules layout
unc0rr
parents:
diff changeset
    28
            buf_out: netbuf::Buf::new(),
12141
78925eff02c2 Basic support for NICK message
unc0rr
parents: 12139
diff changeset
    29
            nick: String::new(),
78925eff02c2 Basic support for NICK message
unc0rr
parents: 12139
diff changeset
    30
            roomId: roomId.clone(),
12128
f50876f3eff8 Refactor modules layout
unc0rr
parents:
diff changeset
    31
        }
f50876f3eff8 Refactor modules layout
unc0rr
parents:
diff changeset
    32
    }
f50876f3eff8 Refactor modules layout
unc0rr
parents:
diff changeset
    33
f50876f3eff8 Refactor modules layout
unc0rr
parents:
diff changeset
    34
    pub fn register(&mut self, poll: &Poll, token: Token) {
12137
193dfdcb0620 - Use logging facilities instead of plain println!
unc0rr
parents: 12136
diff changeset
    35
        poll.register(&self.sock, token, Ready::all(),
12128
f50876f3eff8 Refactor modules layout
unc0rr
parents:
diff changeset
    36
                      PollOpt::edge())
f50876f3eff8 Refactor modules layout
unc0rr
parents:
diff changeset
    37
            .ok().expect("could not register socket with event loop");
f50876f3eff8 Refactor modules layout
unc0rr
parents:
diff changeset
    38
12142
4d7d41be1993 Start refactoring path from getting message from client to reacting to it
unc0rr
parents: 12141
diff changeset
    39
        self.send_msg(HWServerMessage::Connected(utils::PROTOCOL_VERSION));
12128
f50876f3eff8 Refactor modules layout
unc0rr
parents:
diff changeset
    40
    }
f50876f3eff8 Refactor modules layout
unc0rr
parents:
diff changeset
    41
12139
f3121d7dedec - Handle errors
unc0rr
parents: 12138
diff changeset
    42
    pub fn deregister(&mut self, poll: &Poll) {
f3121d7dedec - Handle errors
unc0rr
parents: 12138
diff changeset
    43
        poll.deregister(&self.sock);
f3121d7dedec - Handle errors
unc0rr
parents: 12138
diff changeset
    44
    }
f3121d7dedec - Handle errors
unc0rr
parents: 12138
diff changeset
    45
12138
e0bf51609062 Introduce actions, just like in the old server
unc0rr
parents: 12137
diff changeset
    46
    pub fn send_raw_msg(&mut self, msg: &[u8]) {
12128
f50876f3eff8 Refactor modules layout
unc0rr
parents:
diff changeset
    47
        self.buf_out.write(msg).unwrap();
f50876f3eff8 Refactor modules layout
unc0rr
parents:
diff changeset
    48
        self.flush();
f50876f3eff8 Refactor modules layout
unc0rr
parents:
diff changeset
    49
    }
f50876f3eff8 Refactor modules layout
unc0rr
parents:
diff changeset
    50
12138
e0bf51609062 Introduce actions, just like in the old server
unc0rr
parents: 12137
diff changeset
    51
    pub fn send_string(&mut self, msg: &String) {
e0bf51609062 Introduce actions, just like in the old server
unc0rr
parents: 12137
diff changeset
    52
        self.send_raw_msg(&msg.as_bytes());
e0bf51609062 Introduce actions, just like in the old server
unc0rr
parents: 12137
diff changeset
    53
    }
e0bf51609062 Introduce actions, just like in the old server
unc0rr
parents: 12137
diff changeset
    54
12142
4d7d41be1993 Start refactoring path from getting message from client to reacting to it
unc0rr
parents: 12141
diff changeset
    55
    pub fn send_msg(&mut self, msg: HWServerMessage) {
12138
e0bf51609062 Introduce actions, just like in the old server
unc0rr
parents: 12137
diff changeset
    56
        self.send_string(&msg.to_raw_protocol());
12136
e25a82ce2374 - Render messages to string
unc0rr
parents: 12129
diff changeset
    57
    }
e25a82ce2374 - Render messages to string
unc0rr
parents: 12129
diff changeset
    58
12128
f50876f3eff8 Refactor modules layout
unc0rr
parents:
diff changeset
    59
    fn flush(&mut self) {
f50876f3eff8 Refactor modules layout
unc0rr
parents:
diff changeset
    60
        self.buf_out.write_to(&mut self.sock).unwrap();
f50876f3eff8 Refactor modules layout
unc0rr
parents:
diff changeset
    61
        self.sock.flush();
f50876f3eff8 Refactor modules layout
unc0rr
parents:
diff changeset
    62
    }
f50876f3eff8 Refactor modules layout
unc0rr
parents:
diff changeset
    63
12138
e0bf51609062 Introduce actions, just like in the old server
unc0rr
parents: 12137
diff changeset
    64
    pub fn readable(&mut self, poll: &Poll) -> Vec<Action> {
e0bf51609062 Introduce actions, just like in the old server
unc0rr
parents: 12137
diff changeset
    65
        let v = self.decoder.read_from(&mut self.sock).unwrap();
12137
193dfdcb0620 - Use logging facilities instead of plain println!
unc0rr
parents: 12136
diff changeset
    66
        debug!("Read {} bytes", v);
12136
e25a82ce2374 - Render messages to string
unc0rr
parents: 12129
diff changeset
    67
        let mut response = Vec::new();
e25a82ce2374 - Render messages to string
unc0rr
parents: 12129
diff changeset
    68
        {
12142
4d7d41be1993 Start refactoring path from getting message from client to reacting to it
unc0rr
parents: 12141
diff changeset
    69
            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
    70
                response.push(ReactProtocolMessage(msg));
4d7d41be1993 Start refactoring path from getting message from client to reacting to it
unc0rr
parents: 12141
diff changeset
    71
/*                match msg {
12138
e0bf51609062 Introduce actions, just like in the old server
unc0rr
parents: 12137
diff changeset
    72
                    Ping => response.push(SendMe(Pong.to_raw_protocol())),
12142
4d7d41be1993 Start refactoring path from getting message from client to reacting to it
unc0rr
parents: 12141
diff changeset
    73
                    Quit(Some(msg)) => response.push(ByeClient("User quit: ".to_string() + &msg)),
12139
f3121d7dedec - Handle errors
unc0rr
parents: 12138
diff changeset
    74
                    Quit(None) => response.push(ByeClient("User quit".to_string())),
12141
78925eff02c2 Basic support for NICK message
unc0rr
parents: 12139
diff changeset
    75
                    Nick(nick) => if self.nick.len() == 0 {
78925eff02c2 Basic support for NICK message
unc0rr
parents: 12139
diff changeset
    76
                        response.push(SetNick(nick.to_string()));
78925eff02c2 Basic support for NICK message
unc0rr
parents: 12139
diff changeset
    77
                    },
12137
193dfdcb0620 - Use logging facilities instead of plain println!
unc0rr
parents: 12136
diff changeset
    78
                    Malformed => warn!("Malformed/unknown message"),
193dfdcb0620 - Use logging facilities instead of plain println!
unc0rr
parents: 12136
diff changeset
    79
                    Empty => warn!("Empty message"),
193dfdcb0620 - Use logging facilities instead of plain println!
unc0rr
parents: 12136
diff changeset
    80
                    _ => unimplemented!(),
12142
4d7d41be1993 Start refactoring path from getting message from client to reacting to it
unc0rr
parents: 12141
diff changeset
    81
                }*/
12136
e25a82ce2374 - Render messages to string
unc0rr
parents: 12129
diff changeset
    82
            }
e25a82ce2374 - Render messages to string
unc0rr
parents: 12129
diff changeset
    83
        }
e25a82ce2374 - Render messages to string
unc0rr
parents: 12129
diff changeset
    84
        self.decoder.sweep();
12138
e0bf51609062 Introduce actions, just like in the old server
unc0rr
parents: 12137
diff changeset
    85
        response
12128
f50876f3eff8 Refactor modules layout
unc0rr
parents:
diff changeset
    86
    }
f50876f3eff8 Refactor modules layout
unc0rr
parents:
diff changeset
    87
f50876f3eff8 Refactor modules layout
unc0rr
parents:
diff changeset
    88
    pub fn writable(&mut self, poll: &Poll) -> io::Result<()> {
f50876f3eff8 Refactor modules layout
unc0rr
parents:
diff changeset
    89
        self.buf_out.write_to(&mut self.sock)?;
12139
f3121d7dedec - Handle errors
unc0rr
parents: 12138
diff changeset
    90
12128
f50876f3eff8 Refactor modules layout
unc0rr
parents:
diff changeset
    91
        Ok(())
f50876f3eff8 Refactor modules layout
unc0rr
parents:
diff changeset
    92
    }
12137
193dfdcb0620 - Use logging facilities instead of plain println!
unc0rr
parents: 12136
diff changeset
    93
12139
f3121d7dedec - Handle errors
unc0rr
parents: 12138
diff changeset
    94
    pub fn error(&mut self, poll: &Poll) -> Vec<Action> {
f3121d7dedec - Handle errors
unc0rr
parents: 12138
diff changeset
    95
        return vec![ByeClient("Connection reset".to_string())]
12137
193dfdcb0620 - Use logging facilities instead of plain println!
unc0rr
parents: 12136
diff changeset
    96
    }
12128
f50876f3eff8 Refactor modules layout
unc0rr
parents:
diff changeset
    97
}