Expand description
mqttrs is a codec for the MQTT protocol.
The API aims to be straightforward and composable, usable with plain std or with a framework
like tokio. The decoded packet is help in a Packet struct, and the encoded bytes in a
bytes::BytesMut struct. Convert between the two using [encode()] and [decode()]. Almost all
struct fields can be accessed directly, to create or read packets.
It currently targets MQTT 3.1, with MQTT 5 support planned.
use mqttrs2::*;
use bytes::BytesMut;
// Allocate buffer.
let mut buf = [0u8; 1024];
// Encode an MQTT Connect packet.
let pkt = Packet::Connect(Connect { protocol: Protocol::MQTT311,
keep_alive: 30,
client_id: "doc_client",
clean_session: true,
last_will: None,
username: None,
password: None });
let len = encode_slice(&pkt, &mut buf).unwrap();
assert_eq!(&buf[14..len], b"doc_client");
let mut encoded = buf.clone();
// Decode one packet. The buffer will advance to the next packet.
assert_eq!(Ok(Some(pkt)), decode_slice(&mut buf));
// Example decode failures.
let mut incomplete = encoded.split_at(10).0;
assert_eq!(Ok(None), decode_slice(&mut incomplete));
let mut garbage = BytesMut::from(&[0u8,0,0,0] as &[u8]);
assert_eq!(Err(Error::InvalidHeader), decode_slice(&mut garbage));Structs§
- Connack
- Connack packet (MQTT 3.2).
- Connect
- Connect packet (MQTT 3.1).
- Last
Will - Message that the server should publish when the client disconnects.
- Pid
- Packet Identifier.
- Publish
- Publish packet (MQTT 3.3).
- Suback
- Subsack packet (MQTT 3.9).
- Subscribe
- Subscribe packet (MQTT 3.8).
- Subscribe
Topic - Subscribe topic.
- Unsubscribe
- Unsubscribe packet (MQTT 3.10).
Enums§
- Connect
Return Code - Sucess value of a Connack packet.
- Error
- Errors returned by
encode()anddecode(). - Packet
- Base enum for all MQTT packet types.
- Packet
Type - Packet type variant, without the associated data.
- Protocol
- Protocol version.
- QoS
- Packet delivery Quality of Service level.
- QosPid
- Combined
QoS/Pid. - Subscribe
Return Codes - Subscribe return value.
Functions§
- clone_
packet - decode_
slice - Decode bytes from a BytesMut buffer as a Packet enum.
- decode_
slice_ with_ len - Decode bytes from a BytesMut buffer as a Packet enum, returning a tuple containing the number of bytes read from the buffer and the Packet.
- encode_
slice - Encode a Packet enum into a BufMut buffer.