author | unc0rr |
Wed, 18 Jan 2017 22:15:55 +0300 | |
changeset 12139 | f3121d7dedec |
parent 12138 | e0bf51609062 |
child 12141 | 78925eff02c2 |
permissions | -rw-r--r-- |
12128 | 1 |
use slab; |
2 |
use mio::tcp::*; |
|
3 |
use mio::*; |
|
4 |
use std::io::Write; |
|
5 |
use std::io; |
|
6 |
use netbuf; |
|
7 |
||
8 |
use utils; |
|
12136 | 9 |
use protocol::ProtocolDecoder; |
10 |
use protocol::messages; |
|
11 |
use protocol::messages::HWProtocolMessage::*; |
|
12138 | 12 |
use server::actions::Action::*; |
13 |
use server::actions::Action; |
|
12137
193dfdcb0620
- Use logging facilities instead of plain println!
unc0rr
parents:
12136
diff
changeset
|
14 |
use log; |
12128 | 15 |
|
16 |
pub struct HWClient { |
|
17 |
sock: TcpStream, |
|
12136 | 18 |
decoder: ProtocolDecoder, |
12128 | 19 |
buf_out: netbuf::Buf |
20 |
} |
|
21 |
||
22 |
impl HWClient { |
|
23 |
pub fn new(sock: TcpStream) -> HWClient { |
|
24 |
HWClient { |
|
25 |
sock: sock, |
|
12136 | 26 |
decoder: ProtocolDecoder::new(), |
12128 | 27 |
buf_out: netbuf::Buf::new(), |
28 |
} |
|
29 |
} |
|
30 |
||
31 |
pub fn register(&mut self, poll: &Poll, token: Token) { |
|
12137
193dfdcb0620
- Use logging facilities instead of plain println!
unc0rr
parents:
12136
diff
changeset
|
32 |
poll.register(&self.sock, token, Ready::all(), |
12128 | 33 |
PollOpt::edge()) |
34 |
.ok().expect("could not register socket with event loop"); |
|
35 |
||
12137
193dfdcb0620
- Use logging facilities instead of plain println!
unc0rr
parents:
12136
diff
changeset
|
36 |
self.send_msg(Connected(utils::PROTOCOL_VERSION)); |
12128 | 37 |
} |
38 |
||
12139 | 39 |
pub fn deregister(&mut self, poll: &Poll) { |
40 |
poll.deregister(&self.sock); |
|
41 |
} |
|
42 |
||
12138 | 43 |
pub fn send_raw_msg(&mut self, msg: &[u8]) { |
12128 | 44 |
self.buf_out.write(msg).unwrap(); |
45 |
self.flush(); |
|
46 |
} |
|
47 |
||
12138 | 48 |
pub fn send_string(&mut self, msg: &String) { |
49 |
self.send_raw_msg(&msg.as_bytes()); |
|
50 |
} |
|
51 |
||
52 |
pub fn send_msg(&mut self, msg: messages::HWProtocolMessage) { |
|
53 |
self.send_string(&msg.to_raw_protocol()); |
|
12136 | 54 |
} |
55 |
||
12128 | 56 |
fn flush(&mut self) { |
57 |
self.buf_out.write_to(&mut self.sock).unwrap(); |
|
58 |
self.sock.flush(); |
|
59 |
} |
|
60 |
||
12138 | 61 |
pub fn readable(&mut self, poll: &Poll) -> Vec<Action> { |
62 |
let v = self.decoder.read_from(&mut self.sock).unwrap(); |
|
12137
193dfdcb0620
- Use logging facilities instead of plain println!
unc0rr
parents:
12136
diff
changeset
|
63 |
debug!("Read {} bytes", v); |
12136 | 64 |
let mut response = Vec::new(); |
65 |
{ |
|
66 |
let msgs = self.decoder.extract_messages(); |
|
67 |
for msg in msgs { |
|
68 |
match msg { |
|
12138 | 69 |
Ping => response.push(SendMe(Pong.to_raw_protocol())), |
12139 | 70 |
Quit(Some(msg)) => response.push(ByeClient("User quit: ".to_string() + msg)), |
71 |
Quit(None) => response.push(ByeClient("User quit".to_string())), |
|
12137
193dfdcb0620
- Use logging facilities instead of plain println!
unc0rr
parents:
12136
diff
changeset
|
72 |
Malformed => warn!("Malformed/unknown message"), |
193dfdcb0620
- Use logging facilities instead of plain println!
unc0rr
parents:
12136
diff
changeset
|
73 |
Empty => warn!("Empty message"), |
193dfdcb0620
- Use logging facilities instead of plain println!
unc0rr
parents:
12136
diff
changeset
|
74 |
_ => unimplemented!(), |
12136 | 75 |
} |
76 |
} |
|
77 |
} |
|
78 |
self.decoder.sweep(); |
|
12138 | 79 |
response |
12128 | 80 |
} |
81 |
||
82 |
pub fn writable(&mut self, poll: &Poll) -> io::Result<()> { |
|
83 |
self.buf_out.write_to(&mut self.sock)?; |
|
12139 | 84 |
|
12128 | 85 |
Ok(()) |
86 |
} |
|
12137
193dfdcb0620
- Use logging facilities instead of plain println!
unc0rr
parents:
12136
diff
changeset
|
87 |
|
12139 | 88 |
pub fn error(&mut self, poll: &Poll) -> Vec<Action> { |
89 |
return vec![ByeClient("Connection reset".to_string())] |
|
12137
193dfdcb0620
- Use logging facilities instead of plain println!
unc0rr
parents:
12136
diff
changeset
|
90 |
} |
12128 | 91 |
} |