12129
|
1 |
use netbuf;
|
14457
|
2 |
use nom::{Err, IResult};
|
|
3 |
use std::io::{Read, Result};
|
12129
|
4 |
|
12136
|
5 |
pub mod messages;
|
14457
|
6 |
mod parser;
|
13796
|
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
|
14457
|
33 |
}
|
13438
|
34 |
Err(Err::Incomplete(_)) => unreachable!(),
|
|
35 |
Err(Err::Error(_)) | Err(Err::Failure(_)) => unreachable!(),
|
12136
|
36 |
}
|
|
37 |
}
|
|
38 |
|
|
39 |
pub fn sweep(&mut self) {
|
|
40 |
self.buf.consume(self.consumed);
|
|
41 |
self.consumed = 0;
|
12129
|
42 |
}
|
|
43 |
}
|