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