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
|
use serde::de::DeserializeOwned;
use serde::{Deserialize, Serialize};
use std::fmt::Debug;
use std::io;
use std::mem;
#[derive(Debug, Deserialize, Serialize)]
pub(super) enum Packet<P: 'static + Send + Debug> {
Cargo(P),
Disconnect,
}
#[derive(Debug, thiserror::Error)]
pub enum PacketRwError {
#[error("packet could not be properly deserialised: {0}")]
DeserialiseError(bincode::Error),
#[error("packet could not be properly serialised: {0}")]
SerialiseError(bincode::Error),
#[error("unable to read packet from stream: {0}")]
IOError(io::Error),
#[error("connection was closed from the remote end")]
Closed,
}
impl<P: 'static + Send + Debug + DeserializeOwned + Serialize> Packet<P> {
pub fn write_to_stream(&self, stream: &mut impl io::Write) -> Result<(), PacketRwError> {
let data: Vec<u8> =
bincode::serialize(&self).map_err(|err| PacketRwError::SerialiseError(err))?;
// Write head with packet length
assert!(data.len() as u64 <= u32::MAX as u64);
let len = data.len() as u32;
let len = bincode::serialize(&len).map_err(|err| PacketRwError::SerialiseError(err))?;
stream
.write_all(&len)
.map_err(|err| PacketRwError::IOError(err))?;
// Write the data of the packet and pray all errors are caught.
Ok(stream
.write_all(&data)
.map_err(|err| PacketRwError::IOError(err))?)
}
pub fn read_from_stream(stream: &mut impl io::Read) -> Result<Self, PacketRwError> {
// Read packet head which informs us of the length.
let mut len = vec![0; mem::size_of::<u32>()];
stream.read_exact(&mut len).map_err(|err| {
if err.kind() == io::ErrorKind::UnexpectedEof {
PacketRwError::Closed
} else {
PacketRwError::IOError(err)
}
})?;
let len: u32 = bincode::deserialize(&len)
.expect("Unable to deserialise length of packet. Stream is corrupted.");
// Read all data from the packet according to the length.
let mut data = vec![0; len as usize];
match stream.read_exact(&mut data) {
Ok(()) => {
let res: Result<Self, bincode::Error> = bincode::deserialize(&data);
Ok(res.map_err(|err| PacketRwError::DeserialiseError(err))?)
}
Err(err) => {
if err.kind() == io::ErrorKind::UnexpectedEof {
Err(PacketRwError::Closed)
} else {
Err(PacketRwError::IOError(err))
}
}
}
}
}
|