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