author | nemo |
Thu, 23 Nov 2017 16:02:29 -0500 | |
changeset 12847 | f72c6f64c94c |
parent 12152 | 03ccb89820f3 |
child 12857 | bd35cb2302b3 |
permissions | -rw-r--r-- |
12133 | 1 |
use mio::tcp::*; |
2 |
use mio::*; |
|
3 |
use std::io::Write; |
|
4 |
use std::io; |
|
5 |
use netbuf; |
|
6 |
||
7 |
use utils; |
|
12141 | 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 | 10 |
use super::actions::Action::*; |
11 |
use super::actions::Action; |
|
12133 | 12 |
|
13 |
pub struct HWClient { |
|
14 |
sock: TcpStream, |
|
12141 | 15 |
decoder: ProtocolDecoder, |
12146 | 16 |
buf_out: netbuf::Buf, |
12149 | 17 |
|
12151
8d8fb85bc09c
SendAllButMe action, list all clients in lobby in LobbyJoined message to newcomers
unc0rr
parents:
12149
diff
changeset
|
18 |
pub id: Token, |
8d8fb85bc09c
SendAllButMe action, list all clients in lobby in LobbyJoined message to newcomers
unc0rr
parents:
12149
diff
changeset
|
19 |
pub room_id: Option<Token>, |
12146 | 20 |
pub nick: String, |
12151
8d8fb85bc09c
SendAllButMe action, list all clients in lobby in LobbyJoined message to newcomers
unc0rr
parents:
12149
diff
changeset
|
21 |
pub protocol_number: u32, |
12152 | 22 |
pub is_master: bool, |
23 |
pub is_ready: bool, |
|
24 |
pub is_joined_mid_game: bool, |
|
12133 | 25 |
} |
26 |
||
27 |
impl HWClient { |
|
12151
8d8fb85bc09c
SendAllButMe action, list all clients in lobby in LobbyJoined message to newcomers
unc0rr
parents:
12149
diff
changeset
|
28 |
pub fn new(sock: TcpStream) -> HWClient { |
12133 | 29 |
HWClient { |
30 |
sock: sock, |
|
12141 | 31 |
decoder: ProtocolDecoder::new(), |
12133 | 32 |
buf_out: netbuf::Buf::new(), |
12151
8d8fb85bc09c
SendAllButMe action, list all clients in lobby in LobbyJoined message to newcomers
unc0rr
parents:
12149
diff
changeset
|
33 |
room_id: None, |
8d8fb85bc09c
SendAllButMe action, list all clients in lobby in LobbyJoined message to newcomers
unc0rr
parents:
12149
diff
changeset
|
34 |
id: Token(0), |
12149 | 35 |
|
12146 | 36 |
nick: String::new(), |
12151
8d8fb85bc09c
SendAllButMe action, list all clients in lobby in LobbyJoined message to newcomers
unc0rr
parents:
12149
diff
changeset
|
37 |
protocol_number: 0, |
12152 | 38 |
is_master: false, |
39 |
is_ready: false, |
|
40 |
is_joined_mid_game: false, |
|
12133 | 41 |
} |
42 |
} |
|
43 |
||
44 |
pub fn register(&mut self, poll: &Poll, token: Token) { |
|
12142
193dfdcb0620
- Use logging facilities instead of plain println!
unc0rr
parents:
12141
diff
changeset
|
45 |
poll.register(&self.sock, token, Ready::all(), |
12133 | 46 |
PollOpt::edge()) |
47 |
.ok().expect("could not register socket with event loop"); |
|
48 |
||
12147
4d7d41be1993
Start refactoring path from getting message from client to reacting to it
unc0rr
parents:
12146
diff
changeset
|
49 |
self.send_msg(HWServerMessage::Connected(utils::PROTOCOL_VERSION)); |
12133 | 50 |
} |
51 |
||
12144 | 52 |
pub fn deregister(&mut self, poll: &Poll) { |
12152 | 53 |
poll.deregister(&self.sock) |
54 |
.ok().expect("could not deregister socket"); |
|
12144 | 55 |
} |
56 |
||
12143 | 57 |
pub fn send_raw_msg(&mut self, msg: &[u8]) { |
12133 | 58 |
self.buf_out.write(msg).unwrap(); |
59 |
self.flush(); |
|
60 |
} |
|
61 |
||
12143 | 62 |
pub fn send_string(&mut self, msg: &String) { |
63 |
self.send_raw_msg(&msg.as_bytes()); |
|
64 |
} |
|
65 |
||
12147
4d7d41be1993
Start refactoring path from getting message from client to reacting to it
unc0rr
parents:
12146
diff
changeset
|
66 |
pub fn send_msg(&mut self, msg: HWServerMessage) { |
12143 | 67 |
self.send_string(&msg.to_raw_protocol()); |
12141 | 68 |
} |
69 |
||
12133 | 70 |
fn flush(&mut self) { |
71 |
self.buf_out.write_to(&mut self.sock).unwrap(); |
|
72 |
self.sock.flush(); |
|
73 |
} |
|
74 |
||
12143 | 75 |
pub fn readable(&mut self, poll: &Poll) -> Vec<Action> { |
76 |
let v = self.decoder.read_from(&mut self.sock).unwrap(); |
|
12142
193dfdcb0620
- Use logging facilities instead of plain println!
unc0rr
parents:
12141
diff
changeset
|
77 |
debug!("Read {} bytes", v); |
12141 | 78 |
let mut response = Vec::new(); |
79 |
{ |
|
12147
4d7d41be1993
Start refactoring path from getting message from client to reacting to it
unc0rr
parents:
12146
diff
changeset
|
80 |
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
|
81 |
response.push(ReactProtocolMessage(msg)); |
12141 | 82 |
} |
83 |
} |
|
84 |
self.decoder.sweep(); |
|
12143 | 85 |
response |
12133 | 86 |
} |
87 |
||
88 |
pub fn writable(&mut self, poll: &Poll) -> io::Result<()> { |
|
89 |
self.buf_out.write_to(&mut self.sock)?; |
|
12144 | 90 |
|
12133 | 91 |
Ok(()) |
92 |
} |
|
12142
193dfdcb0620
- Use logging facilities instead of plain println!
unc0rr
parents:
12141
diff
changeset
|
93 |
|
12144 | 94 |
pub fn error(&mut self, poll: &Poll) -> Vec<Action> { |
95 |
return vec![ByeClient("Connection reset".to_string())] |
|
12142
193dfdcb0620
- Use logging facilities instead of plain println!
unc0rr
parents:
12141
diff
changeset
|
96 |
} |
12133 | 97 |
} |