author | unc0rr |
Sat, 14 Jan 2017 22:30:09 +0300 | |
changeset 12137 | 193dfdcb0620 |
parent 12136 | e25a82ce2374 |
child 12138 | e0bf51609062 |
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::*; |
|
12137
193dfdcb0620
- Use logging facilities instead of plain println!
unc0rr
parents:
12136
diff
changeset
|
12 |
use log; |
12128 | 13 |
|
14 |
pub struct HWClient { |
|
15 |
sock: TcpStream, |
|
12136 | 16 |
decoder: ProtocolDecoder, |
12128 | 17 |
buf_out: netbuf::Buf |
18 |
} |
|
19 |
||
20 |
impl HWClient { |
|
21 |
pub fn new(sock: TcpStream) -> HWClient { |
|
22 |
HWClient { |
|
23 |
sock: sock, |
|
12136 | 24 |
decoder: ProtocolDecoder::new(), |
12128 | 25 |
buf_out: netbuf::Buf::new(), |
26 |
} |
|
27 |
} |
|
28 |
||
29 |
pub fn register(&mut self, poll: &Poll, token: Token) { |
|
12137
193dfdcb0620
- Use logging facilities instead of plain println!
unc0rr
parents:
12136
diff
changeset
|
30 |
poll.register(&self.sock, token, Ready::all(), |
12128 | 31 |
PollOpt::edge()) |
32 |
.ok().expect("could not register socket with event loop"); |
|
33 |
||
12137
193dfdcb0620
- Use logging facilities instead of plain println!
unc0rr
parents:
12136
diff
changeset
|
34 |
self.send_msg(Connected(utils::PROTOCOL_VERSION)); |
12128 | 35 |
} |
36 |
||
37 |
fn send_raw_msg(&mut self, msg: &[u8]) { |
|
38 |
self.buf_out.write(msg).unwrap(); |
|
39 |
self.flush(); |
|
40 |
} |
|
41 |
||
12136 | 42 |
fn send_msg(&mut self, msg: messages::HWProtocolMessage) { |
43 |
self.send_raw_msg(&msg.to_raw_protocol().into_bytes()); |
|
44 |
} |
|
45 |
||
12128 | 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<()> { |
|
12129 | 52 |
let v = self.decoder.read_from(&mut self.sock)?; |
12137
193dfdcb0620
- Use logging facilities instead of plain println!
unc0rr
parents:
12136
diff
changeset
|
53 |
debug!("Read {} bytes", v); |
12136 | 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), |
|
12137
193dfdcb0620
- Use logging facilities instead of plain println!
unc0rr
parents:
12136
diff
changeset
|
60 |
Malformed => warn!("Malformed/unknown message"), |
193dfdcb0620
- Use logging facilities instead of plain println!
unc0rr
parents:
12136
diff
changeset
|
61 |
Empty => warn!("Empty message"), |
193dfdcb0620
- Use logging facilities instead of plain println!
unc0rr
parents:
12136
diff
changeset
|
62 |
_ => unimplemented!(), |
12136 | 63 |
} |
64 |
} |
|
65 |
} |
|
66 |
for r in response { |
|
67 |
self.send_msg(r); |
|
68 |
} |
|
69 |
self.decoder.sweep(); |
|
12128 | 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 |
} |
|
12137
193dfdcb0620
- Use logging facilities instead of plain println!
unc0rr
parents:
12136
diff
changeset
|
77 |
|
193dfdcb0620
- Use logging facilities instead of plain println!
unc0rr
parents:
12136
diff
changeset
|
78 |
pub fn error(&mut self, poll: &Poll) -> io::Result<()> { |
193dfdcb0620
- Use logging facilities instead of plain println!
unc0rr
parents:
12136
diff
changeset
|
79 |
debug!("Client error"); |
193dfdcb0620
- Use logging facilities instead of plain println!
unc0rr
parents:
12136
diff
changeset
|
80 |
Ok(()) |
193dfdcb0620
- Use logging facilities instead of plain println!
unc0rr
parents:
12136
diff
changeset
|
81 |
} |
12128 | 82 |
} |