author | unc0rr |
Sat, 14 Jan 2017 22:30:09 +0300 | |
changeset 12142 | 193dfdcb0620 |
parent 12141 | e25a82ce2374 |
child 12143 | e0bf51609062 |
permissions | -rw-r--r-- |
12133 | 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; |
|
12141 | 9 |
use protocol::ProtocolDecoder; |
10 |
use protocol::messages; |
|
11 |
use protocol::messages::HWProtocolMessage::*; |
|
12142
193dfdcb0620
- Use logging facilities instead of plain println!
unc0rr
parents:
12141
diff
changeset
|
12 |
use log; |
12133 | 13 |
|
14 |
pub struct HWClient { |
|
15 |
sock: TcpStream, |
|
12141 | 16 |
decoder: ProtocolDecoder, |
12133 | 17 |
buf_out: netbuf::Buf |
18 |
} |
|
19 |
||
20 |
impl HWClient { |
|
21 |
pub fn new(sock: TcpStream) -> HWClient { |
|
22 |
HWClient { |
|
23 |
sock: sock, |
|
12141 | 24 |
decoder: ProtocolDecoder::new(), |
12133 | 25 |
buf_out: netbuf::Buf::new(), |
26 |
} |
|
27 |
} |
|
28 |
||
29 |
pub fn register(&mut self, poll: &Poll, token: Token) { |
|
12142
193dfdcb0620
- Use logging facilities instead of plain println!
unc0rr
parents:
12141
diff
changeset
|
30 |
poll.register(&self.sock, token, Ready::all(), |
12133 | 31 |
PollOpt::edge()) |
32 |
.ok().expect("could not register socket with event loop"); |
|
33 |
||
12142
193dfdcb0620
- Use logging facilities instead of plain println!
unc0rr
parents:
12141
diff
changeset
|
34 |
self.send_msg(Connected(utils::PROTOCOL_VERSION)); |
12133 | 35 |
} |
36 |
||
37 |
fn send_raw_msg(&mut self, msg: &[u8]) { |
|
38 |
self.buf_out.write(msg).unwrap(); |
|
39 |
self.flush(); |
|
40 |
} |
|
41 |
||
12141 | 42 |
fn send_msg(&mut self, msg: messages::HWProtocolMessage) { |
43 |
self.send_raw_msg(&msg.to_raw_protocol().into_bytes()); |
|
44 |
} |
|
45 |
||
12133 | 46 |
fn flush(&mut self) { |
47 |
self.buf_out.write_to(&mut self.sock).unwrap(); |
|
48 |
self.sock.flush(); |
|
49 |
} |
|
50 |
||
51 |
pub fn readable(&mut self, poll: &Poll) -> io::Result<()> { |
|
12134 | 52 |
let v = self.decoder.read_from(&mut self.sock)?; |
12142
193dfdcb0620
- Use logging facilities instead of plain println!
unc0rr
parents:
12141
diff
changeset
|
53 |
debug!("Read {} bytes", v); |
12141 | 54 |
let mut response = Vec::new(); |
55 |
{ |
|
56 |
let msgs = self.decoder.extract_messages(); |
|
57 |
for msg in msgs { |
|
58 |
match msg { |
|
59 |
Ping => response.push(Pong), |
|
12142
193dfdcb0620
- Use logging facilities instead of plain println!
unc0rr
parents:
12141
diff
changeset
|
60 |
Malformed => warn!("Malformed/unknown message"), |
193dfdcb0620
- Use logging facilities instead of plain println!
unc0rr
parents:
12141
diff
changeset
|
61 |
Empty => warn!("Empty message"), |
193dfdcb0620
- Use logging facilities instead of plain println!
unc0rr
parents:
12141
diff
changeset
|
62 |
_ => unimplemented!(), |
12141 | 63 |
} |
64 |
} |
|
65 |
} |
|
66 |
for r in response { |
|
67 |
self.send_msg(r); |
|
68 |
} |
|
69 |
self.decoder.sweep(); |
|
12133 | 70 |
Ok(()) |
71 |
} |
|
72 |
||
73 |
pub fn writable(&mut self, poll: &Poll) -> io::Result<()> { |
|
74 |
self.buf_out.write_to(&mut self.sock)?; |
|
75 |
Ok(()) |
|
76 |
} |
|
12142
193dfdcb0620
- Use logging facilities instead of plain println!
unc0rr
parents:
12141
diff
changeset
|
77 |
|
193dfdcb0620
- Use logging facilities instead of plain println!
unc0rr
parents:
12141
diff
changeset
|
78 |
pub fn error(&mut self, poll: &Poll) -> io::Result<()> { |
193dfdcb0620
- Use logging facilities instead of plain println!
unc0rr
parents:
12141
diff
changeset
|
79 |
debug!("Client error"); |
193dfdcb0620
- Use logging facilities instead of plain println!
unc0rr
parents:
12141
diff
changeset
|
80 |
Ok(()) |
193dfdcb0620
- Use logging facilities instead of plain println!
unc0rr
parents:
12141
diff
changeset
|
81 |
} |
12133 | 82 |
} |