equal
deleted
inserted
replaced
7 |
7 |
8 use utils; |
8 use utils; |
9 use protocol::ProtocolDecoder; |
9 use protocol::ProtocolDecoder; |
10 use protocol::messages; |
10 use protocol::messages; |
11 use protocol::messages::HWProtocolMessage::*; |
11 use protocol::messages::HWProtocolMessage::*; |
|
12 use log; |
12 |
13 |
13 pub struct HWClient { |
14 pub struct HWClient { |
14 sock: TcpStream, |
15 sock: TcpStream, |
15 decoder: ProtocolDecoder, |
16 decoder: ProtocolDecoder, |
16 buf_out: netbuf::Buf |
17 buf_out: netbuf::Buf |
24 buf_out: netbuf::Buf::new(), |
25 buf_out: netbuf::Buf::new(), |
25 } |
26 } |
26 } |
27 } |
27 |
28 |
28 pub fn register(&mut self, poll: &Poll, token: Token) { |
29 pub fn register(&mut self, poll: &Poll, token: Token) { |
29 poll.register(&self.sock, token, Ready::readable(), |
30 poll.register(&self.sock, token, Ready::all(), |
30 PollOpt::edge()) |
31 PollOpt::edge()) |
31 .ok().expect("could not register socket with event loop"); |
32 .ok().expect("could not register socket with event loop"); |
32 |
33 |
33 self.send_raw_msg( |
34 self.send_msg(Connected(utils::PROTOCOL_VERSION)); |
34 format!("CONNECTED\nHedgewars server http://www.hedgewars.org/\n{}\n\n" |
|
35 , utils::PROTOCOL_VERSION).as_bytes()); |
|
36 } |
35 } |
37 |
36 |
38 fn send_raw_msg(&mut self, msg: &[u8]) { |
37 fn send_raw_msg(&mut self, msg: &[u8]) { |
39 self.buf_out.write(msg).unwrap(); |
38 self.buf_out.write(msg).unwrap(); |
40 self.flush(); |
39 self.flush(); |
49 self.sock.flush(); |
48 self.sock.flush(); |
50 } |
49 } |
51 |
50 |
52 pub fn readable(&mut self, poll: &Poll) -> io::Result<()> { |
51 pub fn readable(&mut self, poll: &Poll) -> io::Result<()> { |
53 let v = self.decoder.read_from(&mut self.sock)?; |
52 let v = self.decoder.read_from(&mut self.sock)?; |
54 println!("Read {} bytes", v); |
53 debug!("Read {} bytes", v); |
55 let mut response = Vec::new(); |
54 let mut response = Vec::new(); |
56 { |
55 { |
57 let msgs = self.decoder.extract_messages(); |
56 let msgs = self.decoder.extract_messages(); |
58 for msg in msgs { |
57 for msg in msgs { |
59 match msg { |
58 match msg { |
60 Ping => response.push(Pong), |
59 Ping => response.push(Pong), |
61 _ => println!("Unknown message") |
60 Malformed => warn!("Malformed/unknown message"), |
|
61 Empty => warn!("Empty message"), |
|
62 _ => unimplemented!(), |
62 } |
63 } |
63 } |
64 } |
64 } |
65 } |
65 for r in response { |
66 for r in response { |
66 self.send_msg(r); |
67 self.send_msg(r); |
71 |
72 |
72 pub fn writable(&mut self, poll: &Poll) -> io::Result<()> { |
73 pub fn writable(&mut self, poll: &Poll) -> io::Result<()> { |
73 self.buf_out.write_to(&mut self.sock)?; |
74 self.buf_out.write_to(&mut self.sock)?; |
74 Ok(()) |
75 Ok(()) |
75 } |
76 } |
|
77 |
|
78 pub fn error(&mut self, poll: &Poll) -> io::Result<()> { |
|
79 debug!("Client error"); |
|
80 Ok(()) |
|
81 } |
76 } |
82 } |