[][src]Crate mqttrs

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 mqttrs::*;
use bytes::BytesMut;

// Allocate buffer.
let mut buf = BytesMut::with_capacity(1024);

// Encode an MQTT Connect packet.
let pkt = Packet::Connect(Connect { protocol: Protocol::MQTT311,
                                    keep_alive: 30,
                                    client_id: "doc_client".into(),
                                    clean_session: true,
                                    last_will: None,
                                    username: None,
                                    password: None });
assert!(encode(&pkt, &mut buf).is_ok());
assert_eq!(&buf[14..], "doc_client".as_bytes());
let mut encoded = buf.clone();

// Decode one packet. The buffer will advance to the next packet.
assert_eq!(Ok(Some(pkt)), decode(&mut buf));

// Example decode failures.
let mut incomplete = encoded.split_to(10);
assert_eq!(Ok(None), decode(&mut incomplete));
let mut garbage = BytesMut::from(&[0u8,0,0,0] as &[u8]);
assert_eq!(Err(Error::InvalidHeader), decode(&mut garbage));

Structs

Connack

Connack packet (MQTT 3.2).

Connect

Connect packet (MQTT 3.1).

LastWill

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).

SubscribeTopic

Subscribe topic.

Unsubscribe

Unsubscribe packet (MQTT 3.10).

Enums

ConnectReturnCode

Sucess value of a Connack packet.

Error

Errors returned by encode() and decode().

Packet

Base enum for all MQTT packet types.

PacketType

Packet type variant, without the associated data.

Protocol

Protocol version.

QoS

Packet delivery Quality of Service level.

QosPid

Combined QoS/Pid.

SubscribeReturnCodes

Subscribe return value.

Functions

decode

Decode bytes from a BytesMut buffer as a Packet enum.

encode

Encode a Packet enum into a BufMut buffer.