1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
use std::io::{self, Cursor, Read, Write};
use byteorder::{LittleEndian, ReadBytesExt, WriteBytesExt};
use crate::command::HEADER_SIZE;
use super::{Command, Header};
#[derive(Debug)]
pub enum StreamError {
Bincode(bincode::Error),
Io(io::Error),
}
impl From<bincode::Error> for StreamError {
fn from(err: bincode::Error) -> Self {
Self::Bincode(err)
}
}
impl From<io::Error> for StreamError {
fn from(err: io::Error) -> Self {
Self::Io(err)
}
}
pub struct CommandCodec<S> {
stream: S,
current_header: Option<(Header, u32)>,
}
impl<S> CommandCodec<S> {
pub fn new(stream: S) -> Self {
Self {
stream,
current_header: None,
}
}
pub fn stream(&self) -> &S {
&self.stream
}
pub fn stream_mut(&mut self) -> &mut S {
&mut self.stream
}
pub fn unwrap(self) -> S {
self.stream
}
}
impl<S: Write> CommandCodec<S> {
pub fn write(&mut self, command: &Command) -> Result<usize, StreamError> {
let header = bincode::serialize(&command.header)?;
self.stream
.write_all(&header)
.and(
self.stream
.write_u32::<LittleEndian>(command.data.len() as u32),
)
.and(self.stream.write_all(&command.data))?;
Ok(command.data.len() + 22)
}
}
impl<S: Read> CommandCodec<S> {
pub fn read(&mut self) -> Result<(u32, Command), StreamError> {
let (header, data_size) = match self.current_header.take() {
Some(tup) => tup,
None => {
let mut buf = [0u8; HEADER_SIZE + 4];
self.stream.read_exact(&mut buf)?;
let header = bincode::deserialize::<Header>(&buf[..HEADER_SIZE])?;
let data_size = Cursor::new(&buf[HEADER_SIZE..]).read_u32::<LittleEndian>()?;
(header, data_size)
}
};
let mut data = vec![0_u8; data_size as usize];
if let Err(err) = self.stream.read_exact(&mut data) {
self.current_header = Some((header, data_size));
return Err(StreamError::from(err));
}
Ok((
HEADER_SIZE as u32 + 4 + data_size as u32,
Command { header, data },
))
}
}