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