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
#![doc = include_str!("../README.md")]
use std::io;
use bincode::Options;
#[cfg(test)]
mod tests;
mod duplex;
mod read;
mod write;
pub use duplex::*;
pub use read::*;
pub use write::*;
pub(crate) const U16_MARKER: u8 = 252;
pub(crate) const U32_MARKER: u8 = 253;
pub(crate) const U64_MARKER: u8 = 254;
pub(crate) const ZST_MARKER: u8 = 255;
pub(crate) const CHECKSUM_ENABLED: u8 = 2;
pub(crate) const CHECKSUM_DISABLED: u8 = 3;
pub(crate) const PROTOCOL_VERSION: u64 = 2;
#[derive(Debug, thiserror::Error)]
pub enum Error {
#[error("io error {0}")]
Io(#[from] io::Error),
#[error("bincode serialization/deserialization error {0}")]
Bincode(#[from] bincode::Error),
#[error("message sent exceeded configured length limit")]
SentMessageTooLarge,
#[error("message received exceeded configured length limit, terminating connection")]
ReceivedMessageTooLarge,
#[error("checksum mismatch, data corrupted or there was a protocol mismatch")]
ChecksumMismatch {
sent_checksum: u64,
computed_checksum: u64,
},
#[error("the peer is using an incompatible protocol version. Our version {our_version}, Their version {their_version}")]
ProtocolVersionMismatch {
our_version: u64,
their_version: u64,
},
#[error("checksum handshake failed, expected {CHECKSUM_ENABLED} or {CHECKSUM_DISABLED}, got {checksum_value}")]
ChecksumHandshakeFailed { checksum_value: u8 },
}
fn bincode_options(size_limit: u64) -> impl Options {
bincode::DefaultOptions::new()
.with_limit(size_limit)
.with_little_endian()
.with_varint_encoding()
.reject_trailing_bytes()
}