equal
deleted
inserted
replaced
1 use self::parser::message; |
1 use hedgewars_network_protocol::{ |
|
2 messages::HwProtocolMessage, |
|
3 parser::{malformed_message, message}, |
|
4 }; |
2 use log::*; |
5 use log::*; |
3 use netbuf; |
6 use netbuf; |
4 use std::io::{Read, Result}; |
7 use std::io::{Read, Result}; |
5 |
|
6 pub mod messages; |
|
7 mod parser; |
|
8 #[cfg(test)] |
|
9 pub mod test; |
|
10 |
8 |
11 pub struct ProtocolDecoder { |
9 pub struct ProtocolDecoder { |
12 buf: netbuf::Buf, |
10 buf: netbuf::Buf, |
13 is_recovering: bool, |
11 is_recovering: bool, |
14 } |
12 } |
20 is_recovering: false, |
18 is_recovering: false, |
21 } |
19 } |
22 } |
20 } |
23 |
21 |
24 fn recover(&mut self) -> bool { |
22 fn recover(&mut self) -> bool { |
25 self.is_recovering = match parser::malformed_message(&self.buf[..]) { |
23 self.is_recovering = match malformed_message(&self.buf[..]) { |
26 Ok((tail, ())) => { |
24 Ok((tail, ())) => { |
27 let length = tail.len(); |
25 let length = tail.len(); |
28 self.buf.consume(self.buf.len() - length); |
26 self.buf.consume(self.buf.len() - length); |
29 false |
27 false |
30 } |
28 } |
42 self.recover(); |
40 self.recover(); |
43 } |
41 } |
44 Ok(count) |
42 Ok(count) |
45 } |
43 } |
46 |
44 |
47 pub fn extract_messages(&mut self) -> Vec<messages::HwProtocolMessage> { |
45 pub fn extract_messages(&mut self) -> Vec<HwProtocolMessage> { |
48 let mut messages = vec![]; |
46 let mut messages = vec![]; |
49 if !self.is_recovering { |
47 if !self.is_recovering { |
50 while !self.buf.is_empty() { |
48 while !self.buf.is_empty() { |
51 match parser::message(&self.buf[..]) { |
49 match message(&self.buf[..]) { |
52 Ok((tail, message)) => { |
50 Ok((tail, message)) => { |
53 messages.push(message); |
51 messages.push(message); |
54 let length = tail.len(); |
52 let length = tail.len(); |
55 self.buf.consume(self.buf.len() - length); |
53 self.buf.consume(self.buf.len() - length); |
56 } |
54 } |