author | unc0rr |
Tue, 24 Jan 2017 20:28:16 +0300 | |
changeset 12148 | 7e874846afe3 |
parent 12147 | 4d7d41be1993 |
child 12149 | 589a2d7d3dc5 |
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::*; |
12143 | 10 |
use server::actions::Action::*; |
11 |
use server::actions::Action; |
|
12133 | 12 |
|
13 |
pub struct HWClient { |
|
14 |
sock: TcpStream, |
|
12141 | 15 |
decoder: ProtocolDecoder, |
12146 | 16 |
buf_out: netbuf::Buf, |
17 |
pub nick: String, |
|
12148 | 18 |
room_id: Token, |
12133 | 19 |
} |
20 |
||
21 |
impl HWClient { |
|
12146 | 22 |
pub fn new(sock: TcpStream, roomId: &Token) -> HWClient { |
12133 | 23 |
HWClient { |
24 |
sock: sock, |
|
12141 | 25 |
decoder: ProtocolDecoder::new(), |
12133 | 26 |
buf_out: netbuf::Buf::new(), |
12146 | 27 |
nick: String::new(), |
12148 | 28 |
room_id: roomId.clone(), |
12133 | 29 |
} |
30 |
} |
|
31 |
||
32 |
pub fn register(&mut self, poll: &Poll, token: Token) { |
|
12142
193dfdcb0620
- Use logging facilities instead of plain println!
unc0rr
parents:
12141
diff
changeset
|
33 |
poll.register(&self.sock, token, Ready::all(), |
12133 | 34 |
PollOpt::edge()) |
35 |
.ok().expect("could not register socket with event loop"); |
|
36 |
||
12147
4d7d41be1993
Start refactoring path from getting message from client to reacting to it
unc0rr
parents:
12146
diff
changeset
|
37 |
self.send_msg(HWServerMessage::Connected(utils::PROTOCOL_VERSION)); |
12133 | 38 |
} |
39 |
||
12144 | 40 |
pub fn deregister(&mut self, poll: &Poll) { |
41 |
poll.deregister(&self.sock); |
|
42 |
} |
|
43 |
||
12143 | 44 |
pub fn send_raw_msg(&mut self, msg: &[u8]) { |
12133 | 45 |
self.buf_out.write(msg).unwrap(); |
46 |
self.flush(); |
|
47 |
} |
|
48 |
||
12143 | 49 |
pub fn send_string(&mut self, msg: &String) { |
50 |
self.send_raw_msg(&msg.as_bytes()); |
|
51 |
} |
|
52 |
||
12147
4d7d41be1993
Start refactoring path from getting message from client to reacting to it
unc0rr
parents:
12146
diff
changeset
|
53 |
pub fn send_msg(&mut self, msg: HWServerMessage) { |
12143 | 54 |
self.send_string(&msg.to_raw_protocol()); |
12141 | 55 |
} |
56 |
||
12133 | 57 |
fn flush(&mut self) { |
58 |
self.buf_out.write_to(&mut self.sock).unwrap(); |
|
59 |
self.sock.flush(); |
|
60 |
} |
|
61 |
||
12143 | 62 |
pub fn readable(&mut self, poll: &Poll) -> Vec<Action> { |
63 |
let v = self.decoder.read_from(&mut self.sock).unwrap(); |
|
12142
193dfdcb0620
- Use logging facilities instead of plain println!
unc0rr
parents:
12141
diff
changeset
|
64 |
debug!("Read {} bytes", v); |
12141 | 65 |
let mut response = Vec::new(); |
66 |
{ |
|
12147
4d7d41be1993
Start refactoring path from getting message from client to reacting to it
unc0rr
parents:
12146
diff
changeset
|
67 |
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
|
68 |
response.push(ReactProtocolMessage(msg)); |
12141 | 69 |
} |
70 |
} |
|
71 |
self.decoder.sweep(); |
|
12143 | 72 |
response |
12133 | 73 |
} |
74 |
||
75 |
pub fn writable(&mut self, poll: &Poll) -> io::Result<()> { |
|
76 |
self.buf_out.write_to(&mut self.sock)?; |
|
12144 | 77 |
|
12133 | 78 |
Ok(()) |
79 |
} |
|
12142
193dfdcb0620
- Use logging facilities instead of plain println!
unc0rr
parents:
12141
diff
changeset
|
80 |
|
12144 | 81 |
pub fn error(&mut self, poll: &Poll) -> Vec<Action> { |
82 |
return vec![ByeClient("Connection reset".to_string())] |
|
12142
193dfdcb0620
- Use logging facilities instead of plain println!
unc0rr
parents:
12141
diff
changeset
|
83 |
} |
12133 | 84 |
} |