12129
|
1 |
use netbuf;
|
14796
|
2 |
use nom::{Err, ErrorKind, IResult};
|
14478
|
3 |
use std::io::{Read, Result};
|
12129
|
4 |
|
12136
|
5 |
pub mod messages;
|
14478
|
6 |
mod parser;
|
13713
|
7 |
#[cfg(test)]
|
13416
|
8 |
pub mod test;
|
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 {
|
13438
|
30 |
Ok((tail, msgs)) => {
|
12136
|
31 |
self.consumed = self.buf.len() - self.consumed - tail.len();
|
|
32 |
msgs
|
14478
|
33 |
}
|
14796
|
34 |
_ => unreachable!(),
|
12136
|
35 |
}
|
|
36 |
}
|
|
37 |
|
|
38 |
pub fn sweep(&mut self) {
|
|
39 |
self.buf.consume(self.consumed);
|
|
40 |
self.consumed = 0;
|
12129
|
41 |
}
|
|
42 |
}
|