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
pub const TOPIC_SEPARATOR: char = '/';
pub const MULTI_LEVEL_WILDCARD: char = '#';
pub const MULTI_LEVEL_WILDCARD_STR: &str = "#";
pub const SINGLE_LEVEL_WILDCARD: char = '+';
pub const SINGLE_LEVEL_WILDCARD_STR: &str = "+";
pub const SHARED_SUBSCRIPTION_PREFIX: &str = "$share/";
pub const MAX_TOPIC_LEN_BYTES: usize = 65_535;
pub mod decoder;
pub mod encoder;
pub mod topic;
pub mod types;
#[cfg(feature = "codec")]
pub mod codec {
use crate::{
decoder, encoder,
types::{DecodeError, Packet, ProtocolVersion},
};
use bytes::BytesMut;
use tokio_util::codec::{Decoder, Encoder};
pub struct MqttCodec {
version: ProtocolVersion,
}
impl MqttCodec {
pub fn new() -> Self {
MqttCodec { version: ProtocolVersion::V311 }
}
pub fn decode(&mut self, buf: &mut BytesMut) -> Result<Option<Packet>, DecodeError> {
let packet = decoder::decode_mqtt(buf, self.version);
if let Ok(Some(Packet::Connect(packet))) = &packet {
self.version = packet.protocol_version;
}
packet
}
pub fn encode(&mut self, packet: Packet, bytes: &mut BytesMut) -> Result<(), DecodeError> {
encoder::encode_mqtt(&packet, bytes);
Ok(())
}
}
impl Decoder for MqttCodec {
type Error = DecodeError;
type Item = Packet;
fn decode(&mut self, buf: &mut BytesMut) -> Result<Option<Self::Item>, Self::Error> {
self.decode(buf)
}
}
impl Encoder for MqttCodec {
type Error = DecodeError;
type Item = Packet;
fn encode(&mut self, packet: Self::Item, bytes: &mut BytesMut) -> Result<(), DecodeError> {
self.encode(packet, bytes)
}
}
}