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