author | unc0rr |
Fri, 27 Jan 2017 23:03:31 +0300 | |
changeset 12146 | 8d8fb85bc09c |
parent 12144 | 589a2d7d3dc5 |
child 12147 | 03ccb89820f3 |
permissions | -rw-r--r-- |
12128 | 1 |
use mio::tcp::*; |
2 |
use mio::*; |
|
3 |
use std::io::Write; |
|
4 |
use std::io; |
|
5 |
use netbuf; |
|
6 |
||
7 |
use utils; |
|
12136 | 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 | 10 |
use super::actions::Action::*; |
11 |
use super::actions::Action; |
|
12128 | 12 |
|
13 |
pub struct HWClient { |
|
14 |
sock: TcpStream, |
|
12136 | 15 |
decoder: ProtocolDecoder, |
12141 | 16 |
buf_out: netbuf::Buf, |
12144 | 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 | 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 | 22 |
} |
23 |
||
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 | 26 |
HWClient { |
27 |
sock: sock, |
|
12136 | 28 |
decoder: ProtocolDecoder::new(), |
12128 | 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 | 32 |
|
12141 | 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 | 35 |
} |
36 |
} |
|
37 |
||
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 | 40 |
PollOpt::edge()) |
41 |
.ok().expect("could not register socket with event loop"); |
|
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 | 44 |
} |
45 |
||
12139 | 46 |
pub fn deregister(&mut self, poll: &Poll) { |
47 |
poll.deregister(&self.sock); |
|
48 |
} |
|
49 |
||
12138 | 50 |
pub fn send_raw_msg(&mut self, msg: &[u8]) { |
12128 | 51 |
self.buf_out.write(msg).unwrap(); |
52 |
self.flush(); |
|
53 |
} |
|
54 |
||
12138 | 55 |
pub fn send_string(&mut self, msg: &String) { |
56 |
self.send_raw_msg(&msg.as_bytes()); |
|
57 |
} |
|
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 | 60 |
self.send_string(&msg.to_raw_protocol()); |
12136 | 61 |
} |
62 |
||
12128 | 63 |
fn flush(&mut self) { |
64 |
self.buf_out.write_to(&mut self.sock).unwrap(); |
|
65 |
self.sock.flush(); |
|
66 |
} |
|
67 |
||
12138 | 68 |
pub fn readable(&mut self, poll: &Poll) -> Vec<Action> { |
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 | 71 |
let mut response = Vec::new(); |
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 | 75 |
} |
76 |
} |
|
77 |
self.decoder.sweep(); |
|
12138 | 78 |
response |
12128 | 79 |
} |
80 |
||
81 |
pub fn writable(&mut self, poll: &Poll) -> io::Result<()> { |
|
82 |
self.buf_out.write_to(&mut self.sock)?; |
|
12139 | 83 |
|
12128 | 84 |
Ok(()) |
85 |
} |
|
12137
193dfdcb0620
- Use logging facilities instead of plain println!
unc0rr
parents:
12136
diff
changeset
|
86 |
|
12139 | 87 |
pub fn error(&mut self, poll: &Poll) -> Vec<Action> { |
88 |
return vec![ByeClient("Connection reset".to_string())] |
|
12137
193dfdcb0620
- Use logging facilities instead of plain println!
unc0rr
parents:
12136
diff
changeset
|
89 |
} |
12128 | 90 |
} |