equal
deleted
inserted
replaced
|
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; |
|
9 |
|
10 pub struct HWClient { |
|
11 sock: TcpStream, |
|
12 buf_in: netbuf::Buf, |
|
13 buf_out: netbuf::Buf |
|
14 } |
|
15 |
|
16 impl HWClient { |
|
17 pub fn new(sock: TcpStream) -> HWClient { |
|
18 HWClient { |
|
19 sock: sock, |
|
20 buf_in: netbuf::Buf::new(), |
|
21 buf_out: netbuf::Buf::new(), |
|
22 } |
|
23 } |
|
24 |
|
25 pub fn register(&mut self, poll: &Poll, token: Token) { |
|
26 poll.register(&self.sock, token, Ready::readable(), |
|
27 PollOpt::edge()) |
|
28 .ok().expect("could not register socket with event loop"); |
|
29 |
|
30 self.send_raw_msg( |
|
31 format!("CONNECTED\nHedgewars server http://www.hedgewars.org/\n{}\n\n" |
|
32 , utils::PROTOCOL_VERSION).as_bytes()); |
|
33 } |
|
34 |
|
35 fn send_raw_msg(&mut self, msg: &[u8]) { |
|
36 self.buf_out.write(msg).unwrap(); |
|
37 self.flush(); |
|
38 } |
|
39 |
|
40 fn flush(&mut self) { |
|
41 self.buf_out.write_to(&mut self.sock).unwrap(); |
|
42 self.sock.flush(); |
|
43 } |
|
44 |
|
45 pub fn readable(&mut self, poll: &Poll) -> io::Result<()> { |
|
46 self.buf_in.read_from(&mut self.sock)?; |
|
47 println!("Incoming buffer size: {}", self.buf_in.len()); |
|
48 Ok(()) |
|
49 } |
|
50 |
|
51 pub fn writable(&mut self, poll: &Poll) -> io::Result<()> { |
|
52 self.buf_out.write_to(&mut self.sock)?; |
|
53 Ok(()) |
|
54 } |
|
55 } |