author | Wuzzy <Wuzzy2@mail.ru> |
Fri, 27 Oct 2017 05:05:37 +0200 | |
changeset 12783 | 7849398ec0fa |
parent 12147 | 03ccb89820f3 |
child 12852 | bd35cb2302b3 |
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, |
12147 | 22 |
pub is_master: bool, |
23 |
pub is_ready: bool, |
|
24 |
pub is_joined_mid_game: bool, |
|
12128 | 25 |
} |
26 |
||
27 |
impl HWClient { |
|
12146
8d8fb85bc09c
SendAllButMe action, list all clients in lobby in LobbyJoined message to newcomers
unc0rr
parents:
12144
diff
changeset
|
28 |
pub fn new(sock: TcpStream) -> HWClient { |
12128 | 29 |
HWClient { |
30 |
sock: sock, |
|
12136 | 31 |
decoder: ProtocolDecoder::new(), |
12128 | 32 |
buf_out: netbuf::Buf::new(), |
12146
8d8fb85bc09c
SendAllButMe action, list all clients in lobby in LobbyJoined message to newcomers
unc0rr
parents:
12144
diff
changeset
|
33 |
room_id: None, |
8d8fb85bc09c
SendAllButMe action, list all clients in lobby in LobbyJoined message to newcomers
unc0rr
parents:
12144
diff
changeset
|
34 |
id: Token(0), |
12144 | 35 |
|
12141 | 36 |
nick: String::new(), |
12146
8d8fb85bc09c
SendAllButMe action, list all clients in lobby in LobbyJoined message to newcomers
unc0rr
parents:
12144
diff
changeset
|
37 |
protocol_number: 0, |
12147 | 38 |
is_master: false, |
39 |
is_ready: false, |
|
40 |
is_joined_mid_game: false, |
|
12128 | 41 |
} |
42 |
} |
|
43 |
||
44 |
pub fn register(&mut self, poll: &Poll, token: Token) { |
|
12137
193dfdcb0620
- Use logging facilities instead of plain println!
unc0rr
parents:
12136
diff
changeset
|
45 |
poll.register(&self.sock, token, Ready::all(), |
12128 | 46 |
PollOpt::edge()) |
47 |
.ok().expect("could not register socket with event loop"); |
|
48 |
||
12142
4d7d41be1993
Start refactoring path from getting message from client to reacting to it
unc0rr
parents:
12141
diff
changeset
|
49 |
self.send_msg(HWServerMessage::Connected(utils::PROTOCOL_VERSION)); |
12128 | 50 |
} |
51 |
||
12139 | 52 |
pub fn deregister(&mut self, poll: &Poll) { |
12147 | 53 |
poll.deregister(&self.sock) |
54 |
.ok().expect("could not deregister socket"); |
|
12139 | 55 |
} |
56 |
||
12138 | 57 |
pub fn send_raw_msg(&mut self, msg: &[u8]) { |
12128 | 58 |
self.buf_out.write(msg).unwrap(); |
59 |
self.flush(); |
|
60 |
} |
|
61 |
||
12138 | 62 |
pub fn send_string(&mut self, msg: &String) { |
63 |
self.send_raw_msg(&msg.as_bytes()); |
|
64 |
} |
|
65 |
||
12142
4d7d41be1993
Start refactoring path from getting message from client to reacting to it
unc0rr
parents:
12141
diff
changeset
|
66 |
pub fn send_msg(&mut self, msg: HWServerMessage) { |
12138 | 67 |
self.send_string(&msg.to_raw_protocol()); |
12136 | 68 |
} |
69 |
||
12128 | 70 |
fn flush(&mut self) { |
71 |
self.buf_out.write_to(&mut self.sock).unwrap(); |
|
72 |
self.sock.flush(); |
|
73 |
} |
|
74 |
||
12138 | 75 |
pub fn readable(&mut self, poll: &Poll) -> Vec<Action> { |
76 |
let v = self.decoder.read_from(&mut self.sock).unwrap(); |
|
12137
193dfdcb0620
- Use logging facilities instead of plain println!
unc0rr
parents:
12136
diff
changeset
|
77 |
debug!("Read {} bytes", v); |
12136 | 78 |
let mut response = Vec::new(); |
79 |
{ |
|
12142
4d7d41be1993
Start refactoring path from getting message from client to reacting to it
unc0rr
parents:
12141
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:
12141
diff
changeset
|
81 |
response.push(ReactProtocolMessage(msg)); |
12136 | 82 |
} |
83 |
} |
|
84 |
self.decoder.sweep(); |
|
12138 | 85 |
response |
12128 | 86 |
} |
87 |
||
88 |
pub fn writable(&mut self, poll: &Poll) -> io::Result<()> { |
|
89 |
self.buf_out.write_to(&mut self.sock)?; |
|
12139 | 90 |
|
12128 | 91 |
Ok(()) |
92 |
} |
|
12137
193dfdcb0620
- Use logging facilities instead of plain println!
unc0rr
parents:
12136
diff
changeset
|
93 |
|
12139 | 94 |
pub fn error(&mut self, poll: &Poll) -> Vec<Action> { |
95 |
return vec![ByeClient("Connection reset".to_string())] |
|
12137
193dfdcb0620
- Use logging facilities instead of plain println!
unc0rr
parents:
12136
diff
changeset
|
96 |
} |
12128 | 97 |
} |