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