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