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