1 use mio::net::TcpStream; |
1 pub type ClientId = usize; |
2 use mio::*; |
|
3 use std::io::Write; |
|
4 use std::io; |
|
5 use netbuf; |
|
6 |
|
7 use utils; |
|
8 use protocol::ProtocolDecoder; |
|
9 use protocol::messages::*; |
|
10 use super::actions::Action::*; |
|
11 use super::actions::Action; |
|
12 |
2 |
13 pub struct HWClient { |
3 pub struct HWClient { |
14 sock: TcpStream, |
4 pub id: ClientId, |
15 decoder: ProtocolDecoder, |
|
16 buf_out: netbuf::Buf, |
|
17 |
|
18 pub id: usize, |
|
19 pub room_id: Option<usize>, |
5 pub room_id: Option<usize>, |
20 pub nick: String, |
6 pub nick: String, |
21 pub protocol_number: u32, |
7 pub protocol_number: u32, |
22 pub is_master: bool, |
8 pub is_master: bool, |
23 pub is_ready: bool, |
9 pub is_ready: bool, |
24 pub is_joined_mid_game: bool, |
10 pub is_joined_mid_game: bool, |
25 } |
11 } |
26 |
12 |
27 impl HWClient { |
13 impl HWClient { |
28 pub fn new(sock: TcpStream) -> HWClient { |
14 pub fn new(id: ClientId) -> HWClient { |
29 HWClient { |
15 HWClient { |
30 sock: sock, |
16 id, |
31 decoder: ProtocolDecoder::new(), |
|
32 buf_out: netbuf::Buf::new(), |
|
33 room_id: None, |
17 room_id: None, |
34 id: 0, |
|
35 |
|
36 nick: String::new(), |
18 nick: String::new(), |
37 protocol_number: 0, |
19 protocol_number: 0, |
38 is_master: false, |
20 is_master: false, |
39 is_ready: false, |
21 is_ready: false, |
40 is_joined_mid_game: false, |
22 is_joined_mid_game: false, |
41 } |
23 } |
42 } |
24 } |
43 |
|
44 pub fn register(&mut self, poll: &Poll, token: Token) { |
|
45 poll.register(&self.sock, token, Ready::readable() | Ready::writable(), |
|
46 PollOpt::edge()) |
|
47 .ok().expect("could not register socket with event loop"); |
|
48 |
|
49 self.send_msg(HWServerMessage::Connected(utils::PROTOCOL_VERSION)); |
|
50 } |
|
51 |
|
52 pub fn deregister(&mut self, poll: &Poll) { |
|
53 poll.deregister(&self.sock) |
|
54 .ok().expect("could not deregister socket"); |
|
55 } |
|
56 |
|
57 pub fn send_raw_msg(&mut self, msg: &[u8]) { |
|
58 self.buf_out.write(msg).unwrap(); |
|
59 self.flush(); |
|
60 } |
|
61 |
|
62 pub fn send_string(&mut self, msg: &String) { |
|
63 self.send_raw_msg(&msg.as_bytes()); |
|
64 } |
|
65 |
|
66 pub fn send_msg(&mut self, msg: HWServerMessage) { |
|
67 self.send_string(&msg.to_raw_protocol()); |
|
68 } |
|
69 |
|
70 fn flush(&mut self) { |
|
71 self.buf_out.write_to(&mut self.sock).unwrap(); |
|
72 self.sock.flush(); |
|
73 } |
|
74 |
|
75 pub fn readable(&mut self, _poll: &Poll) -> Vec<Action> { |
|
76 let v = self.decoder.read_from(&mut self.sock).unwrap(); |
|
77 debug!("Read {} bytes", v); |
|
78 let mut response = Vec::new(); |
|
79 { |
|
80 for msg in self.decoder.extract_messages() { |
|
81 response.push(ReactProtocolMessage(msg)); |
|
82 } |
|
83 } |
|
84 self.decoder.sweep(); |
|
85 response |
|
86 } |
|
87 |
|
88 pub fn writable(&mut self, _poll: &Poll) -> io::Result<()> { |
|
89 self.buf_out.write_to(&mut self.sock)?; |
|
90 |
|
91 Ok(()) |
|
92 } |
|
93 |
|
94 pub fn error(&mut self, _poll: &Poll) -> Vec<Action> { |
|
95 return vec![ByeClient("Connection reset".to_string())] |
|
96 } |
|
97 } |
25 } |