12134
|
1 |
use netbuf;
|
|
2 |
use std::io::Read;
|
|
3 |
use std::io::Result;
|
12141
|
4 |
use nom::IResult;
|
12134
|
5 |
|
12141
|
6 |
pub mod messages;
|
13421
|
7 |
pub mod test;
|
12138
|
8 |
mod parser;
|
12134
|
9 |
|
12141
|
10 |
pub struct ProtocolDecoder {
|
12134
|
11 |
buf: netbuf::Buf,
|
12141
|
12 |
consumed: usize,
|
12134
|
13 |
}
|
|
14 |
|
12141
|
15 |
impl ProtocolDecoder {
|
|
16 |
pub fn new() -> ProtocolDecoder {
|
|
17 |
ProtocolDecoder {
|
|
18 |
buf: netbuf::Buf::new(),
|
|
19 |
consumed: 0,
|
12134
|
20 |
}
|
|
21 |
}
|
|
22 |
|
|
23 |
pub fn read_from<R: Read>(&mut self, stream: &mut R) -> Result<usize> {
|
|
24 |
self.buf.read_from(stream)
|
|
25 |
}
|
|
26 |
|
12141
|
27 |
pub fn extract_messages(&mut self) -> Vec<messages::HWProtocolMessage> {
|
|
28 |
let parse_result = parser::extract_messages(&self.buf[..]);
|
|
29 |
match parse_result {
|
|
30 |
IResult::Done(tail, msgs) => {
|
|
31 |
self.consumed = self.buf.len() - self.consumed - tail.len();
|
|
32 |
msgs
|
|
33 |
},
|
|
34 |
IResult::Incomplete(_) => unreachable!(),
|
|
35 |
IResult::Error(_) => unreachable!(),
|
|
36 |
}
|
|
37 |
}
|
|
38 |
|
|
39 |
pub fn sweep(&mut self) {
|
|
40 |
self.buf.consume(self.consumed);
|
|
41 |
self.consumed = 0;
|
12134
|
42 |
}
|
|
43 |
}
|