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