Rust

packet

Module

MQTT control packets: the fixed header (Packet, Kind), splitting a byte stream into packets (frames), and the decoded form of each packet a broker reads or answers with.

Contents

7 submodules, and what the module defines itself.

use utopia_mqtt::Packet;
Rust
pub struct Packet<'a>

A framed control packet: its type and flags (the fixed header) and the rest of it, borrowed from the bytes it was parsed from.

Rust
use utopia_mqtt::packet::{Kind, Packet};
use utopia_mqtt::spec::v3;

let bytes = v3::publish(b"sport/tennis", b"hi", 1, 7, false);
let packet = Packet::parse(&bytes)?;
assert_eq!(packet.kind, Kind::Publish);
assert_eq!(packet.qos(), 1);
assert_eq!(packet.name(), "publish");
use utopia_mqtt::packet::Kind;
Rust
pub enum Kind

The type of a control packet (the high nibble of its first byte).

use utopia_mqtt::packet::frames;
Rust
pub fn frames(buffer: &[u8]) -> (Vec<&[u8]>, &[u8])

Splits a byte stream into whole packets, and returns them with the trailing bytes that do not make a whole packet yet. Used where the carrier's framing does not follow MQTT's (a WebSocket message may hold several packets, or part of one): keep the remainder and prepend it to what arrives next. A remaining length longer than four bytes never completes.

Rust
use utopia_mqtt::packet::{frames, pingreq};

let mut stream = [pingreq(), pingreq()].concat();
stream.push(0xC0);
let (packets, rest) = frames(&stream);
assert_eq!(packets, [&pingreq()[..], &pingreq()[..]]);
assert_eq!(rest, [0xC0]);
use utopia_mqtt::packet::pingreq;
Rust
pub fn pingreq() -> [u8; 2]

A PINGREQ.

use utopia_mqtt::packet::pingresp;
Rust
pub fn pingresp() -> [u8; 2]

A PINGRESP.

packet::auth

Own page
use utopia_mqtt::packet::auth::Auth;
Rust
pub struct Auth

An AUTH (5.0 only): one step of enhanced authentication, or a re-authentication on an open connection.

Rust
use utopia_mqtt::packet::{Auth, Packet};
use utopia_mqtt::spec::v5;
use utopia_mqtt::{Properties, Property};

let properties = Properties::new().with(Property::bytes(Property::AUTHENTICATION_METHOD, "token"));
let bytes = v5::auth(Auth::REAUTH, Some(&properties))?;
let auth = Auth::decode(Packet::parse(&bytes)?.body)?;
assert_eq!((auth.reason_code, auth.method.as_slice()), (Auth::REAUTH, &b"token"[..]));

packet::connack

Own page
use utopia_mqtt::packet::connack::Connack;
Rust
pub struct Connack

A CONNACK: the broker's answer to a CONNECT.

Rust
use utopia_mqtt::packet::Connack;

assert!(Connack::accept(false, None).accepted());
assert_eq!(Connack::refuse(Connack::BAD_CREDENTIALS, None).reason_code, 0x86);

packet::connect

Own page
use utopia_mqtt::packet::connect::Connect;
Rust
pub struct Connect

A CONNECT: what a client asks for when it opens a session.

Rust
use utopia_mqtt::packet::{Connect, Packet};
use utopia_mqtt::spec::v3;

let bytes = v3::connect(b"device-tv", 60, true, b"user", b"secret");
let connect = Connect::decode(Packet::parse(&bytes)?.body)?;
assert_eq!(connect.client_id, b"device-tv");
assert_eq!(connect.password.as_deref(), Some(&b"secret"[..]));
use utopia_mqtt::packet::connect::Will;
Rust
pub struct Will

The Will of a CONNECT: the message the broker publishes for a client that goes away without a DISCONNECT. Its 5.0 properties are skipped.

packet::disconnect

Own page
use utopia_mqtt::packet::disconnect::Disconnect;
Rust
pub struct Disconnect

A DISCONNECT: a client leaving, or the broker closing a 5.0 client with a reason.

Rust
use utopia_mqtt::Property;
use utopia_mqtt::packet::Disconnect;

let refused = Disconnect::refuse(Disconnect::NOT_AUTHORIZED, Some(b"Session expired"));
let reason = refused.properties.as_ref().and_then(|p| p.bytes(Property::REASON_STRING));
assert_eq!(reason, Some(&b"Session expired"[..]));
assert_eq!(Disconnect::normal(Some(b"")).properties, None);

packet::publish

Own page
use utopia_mqtt::packet::publish::Puback;
Rust
pub struct Puback

A PUBACK: a client acknowledging a QoS 1 delivery.

use utopia_mqtt::packet::publish::Publish;
Rust
pub struct Publish

A PUBLISH: a message on a topic.

Rust
use utopia_mqtt::packet::{Packet, Publish};
use utopia_mqtt::spec::v5;
use utopia_mqtt::{Properties, Property};

let properties = Properties::new().with(Property::user("unit", "C"));
let bytes = v5::publish(b"sensors/room1/temp", b"21.5", 1, 100, Some(&properties), false)?;
let publish = Publish::decode(&Packet::parse(&bytes)?, 5)?;
assert_eq!((publish.topic.as_slice(), publish.payload.as_slice()), (&b"sensors/room1/temp"[..], &b"21.5"[..]));
assert_eq!((publish.qos, publish.packet_id), (1, 100));
assert_eq!(publish.user_properties(), [(b"unit".to_vec(), b"C".to_vec())]);

packet::subscribe

Own page
use utopia_mqtt::packet::subscribe::Filter;
Rust
pub struct Filter

One topic filter of a SUBSCRIBE and the QoS asked for it.

use utopia_mqtt::packet::subscribe::Suback;
Rust
pub struct Suback

A SUBACK being built: one code per filter, in the SUBSCRIBE's order.

Rust
use utopia_mqtt::packet::Suback;

let mut suback = Suback::new();
suback.grant(1).deny(Suback::DENIED);
assert_eq!(suback.codes(), [1, 0x80]);
use utopia_mqtt::packet::subscribe::Subscribe;
Rust
pub struct Subscribe

A SUBSCRIBE: topic filters a client asks to receive.

Rust
use utopia_mqtt::packet::{Filter, Packet, Subscribe};
use utopia_mqtt::spec::v5;

let bytes = v5::subscribe(1, &[b"sensors/+/temp"], 1, None)?;
let subscribe = Subscribe::decode(Packet::parse(&bytes)?.body, 5)?;
assert_eq!(subscribe.packet_id, 1);
assert_eq!(subscribe.filters, [Filter { topic: b"sensors/+/temp".to_vec(), qos: 1 }]);

packet::unsubscribe

Own page
use utopia_mqtt::packet::unsubscribe::Unsuback;
Rust
pub struct Unsuback

An UNSUBACK being built: one code per filter (5.0 sends them; 3.1.1 has none).

use utopia_mqtt::packet::unsubscribe::Unsubscribe;
Rust
pub struct Unsubscribe

An UNSUBSCRIBE: topic filters a client no longer wants.

Rust
use utopia_mqtt::packet::{Packet, Unsubscribe};
use utopia_mqtt::spec::v3;

let bytes = v3::unsubscribe(2, &[b"news/tech"]);
let unsubscribe = Unsubscribe::decode(Packet::parse(&bytes)?.body, 4)?;
assert_eq!((unsubscribe.packet_id, unsubscribe.filters), (2, vec![b"news/tech".to_vec()]));