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;
|
12133
|
7 |
mod parser;
|
12129
|
8 |
|
12136
|
9 |
pub struct ProtocolDecoder {
|
12129
|
10 |
buf: netbuf::Buf,
|
12136
|
11 |
consumed: usize,
|
12129
|
12 |
}
|
|
13 |
|
12136
|
14 |
impl ProtocolDecoder {
|
|
15 |
pub fn new() -> ProtocolDecoder {
|
|
16 |
ProtocolDecoder {
|
|
17 |
buf: netbuf::Buf::new(),
|
|
18 |
consumed: 0,
|
12129
|
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 |
|
12136
|
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;
|
12129
|
41 |
}
|
|
42 |
}
|