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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
use std::{fmt, io, num::NonZeroU16};
use ntex::util::Either;
use crate::v5::codec::DisconnectReasonCode;
#[derive(Debug, thiserror::Error)]
pub enum MqttError<E> {
#[error("Service error")]
Service(E),
#[error("Mqtt protocol error: {}", _0)]
Protocol(#[from] ProtocolError),
#[error("Handshake timeout")]
HandshakeTimeout,
#[error("Peer is disconnected, error: {:?}", _0)]
Disconnected(Option<io::Error>),
#[error("Server error: {}", _0)]
ServerError(&'static str),
}
#[derive(Debug, thiserror::Error)]
pub enum ProtocolError {
#[error("Decoding error: {0:?}")]
Decode(#[from] DecodeError),
#[error("Encoding error: {0:?}")]
Encode(#[from] EncodeError),
#[error("Protocol violation: {0}")]
ProtocolViolation(#[from] ProtocolViolationError),
#[error("Keep Alive timeout")]
KeepAliveTimeout,
}
#[derive(Debug, thiserror::Error)]
#[error(transparent)]
pub struct ProtocolViolationError {
inner: ViolationInner,
}
#[derive(Debug, thiserror::Error)]
enum ViolationInner {
#[error("{message}")]
Common { reason: DisconnectReasonCode, message: &'static str },
#[error("{message}; received packet with type `{packet_type:b}`")]
UnexpectedPacket { packet_type: u8, message: &'static str },
}
impl ProtocolViolationError {
pub(crate) fn reason(&self) -> DisconnectReasonCode {
match self.inner {
ViolationInner::Common { reason, .. } => reason,
ViolationInner::UnexpectedPacket { .. } => DisconnectReasonCode::ProtocolError,
}
}
}
impl ProtocolError {
pub(crate) fn violation(reason: DisconnectReasonCode, message: &'static str) -> Self {
Self::ProtocolViolation(ProtocolViolationError {
inner: ViolationInner::Common { reason, message },
})
}
pub(crate) fn generic_violation(message: &'static str) -> Self {
Self::violation(DisconnectReasonCode::ProtocolError, message)
}
pub(crate) fn unexpected_packet(packet_type: u8, message: &'static str) -> ProtocolError {
Self::ProtocolViolation(ProtocolViolationError {
inner: ViolationInner::UnexpectedPacket { packet_type, message },
})
}
pub(crate) fn packet_id_mismatch() -> Self {
Self::generic_violation(
"Packet id of PUBACK packet does not match expected next value according to sending order of PUBLISH packets [MQTT-4.6.0-2]"
)
}
}
impl<E> From<io::Error> for MqttError<E> {
fn from(err: io::Error) -> Self {
MqttError::Disconnected(Some(err))
}
}
impl<E> From<Either<io::Error, io::Error>> for MqttError<E> {
fn from(err: Either<io::Error, io::Error>) -> Self {
MqttError::Disconnected(Some(err.into_inner()))
}
}
impl<E> From<Either<DecodeError, io::Error>> for MqttError<E> {
fn from(err: Either<DecodeError, io::Error>) -> Self {
match err {
Either::Left(err) => MqttError::Protocol(ProtocolError::Decode(err)),
Either::Right(err) => MqttError::Disconnected(Some(err)),
}
}
}
impl<E> From<Either<EncodeError, io::Error>> for MqttError<E> {
fn from(err: Either<EncodeError, io::Error>) -> Self {
match err {
Either::Left(err) => MqttError::Protocol(ProtocolError::Encode(err)),
Either::Right(err) => MqttError::Disconnected(Some(err)),
}
}
}
#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash, thiserror::Error)]
pub enum DecodeError {
#[error("Invalid protocol")]
InvalidProtocol,
#[error("Invalid length")]
InvalidLength,
#[error("Malformed packet")]
MalformedPacket,
#[error("Unsupported protocol level")]
UnsupportedProtocolLevel,
#[error("Connect frame's reserved flag is set")]
ConnectReservedFlagSet,
#[error("ConnectAck frame's reserved flag is set")]
ConnAckReservedFlagSet,
#[error("Invalid client id")]
InvalidClientId,
#[error("Unsupported packet type")]
UnsupportedPacketType,
#[error("Packet id is required")]
PacketIdRequired,
#[error("Max size exceeded")]
MaxSizeExceeded,
#[error("utf8 error")]
Utf8Error,
}
#[derive(Copy, Clone, Debug, PartialEq, Eq, Hash, thiserror::Error)]
pub enum EncodeError {
#[error("Packet is bigger than peer's Maximum Packet Size")]
OverMaxPacketSize,
#[error("Invalid length")]
InvalidLength,
#[error("Malformed packet")]
MalformedPacket,
#[error("Packet id is required")]
PacketIdRequired,
#[error("Unsupported version")]
UnsupportedVersion,
}
#[derive(Debug, PartialEq, Eq, Copy, Clone, thiserror::Error)]
pub enum SendPacketError {
#[error("Encoding error {:?}", _0)]
Encode(#[from] EncodeError),
#[error("Provided packet id is in use")]
PacketIdInUse(NonZeroU16),
#[error("Peer is disconnected")]
Disconnected,
}
#[derive(Debug, thiserror::Error)]
pub enum ClientError<T: fmt::Debug> {
#[error("Connect ack failed: {:?}", _0)]
Ack(T),
#[error("Protocol error: {:?}", _0)]
Protocol(#[from] ProtocolError),
#[error("Handshake timeout")]
HandshakeTimeout,
#[error("Peer disconnected")]
Disconnected(Option<std::io::Error>),
#[error("Connect error: {}", _0)]
Connect(#[from] ntex::connect::ConnectError),
}
impl<T: fmt::Debug> From<Either<EncodeError, std::io::Error>> for ClientError<T> {
fn from(err: Either<EncodeError, std::io::Error>) -> Self {
match err {
Either::Left(err) => ClientError::Protocol(ProtocolError::Encode(err)),
Either::Right(err) => ClientError::Disconnected(Some(err)),
}
}
}
impl<T: fmt::Debug> From<Either<DecodeError, std::io::Error>> for ClientError<T> {
fn from(err: Either<DecodeError, std::io::Error>) -> Self {
match err {
Either::Left(err) => ClientError::Protocol(ProtocolError::Decode(err)),
Either::Right(err) => ClientError::Disconnected(Some(err)),
}
}
}