12129
|
1 |
use netbuf;
|
|
2 |
use std::io::Read;
|
|
3 |
use std::io::Result;
|
12136
|
4 |
use nom::IResult;
|
12129
|
5 |
|
12136
|
6 |
pub mod messages;
|
13416
|
7 |
pub mod test;
|
12133
|
8 |
mod parser;
|
12129
|
9 |
|
12136
|
10 |
pub struct ProtocolDecoder {
|
12129
|
11 |
buf: netbuf::Buf,
|
12136
|
12 |
consumed: usize,
|
12129
|
13 |
}
|
|
14 |
|
12136
|
15 |
impl ProtocolDecoder {
|
|
16 |
pub fn new() -> ProtocolDecoder {
|
|
17 |
ProtocolDecoder {
|
|
18 |
buf: netbuf::Buf::new(),
|
|
19 |
consumed: 0,
|
12129
|
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 |
|
12136
|
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;
|
12129
|
42 |
}
|
|
43 |
}
|