packet
ModuleContents
7 submodules, and what the module defines itself.
use utopia_mqtt::Packet;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.
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;pub enum KindThe type of a control packet (the high nibble of its first byte).
use utopia_mqtt::packet::frames;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.
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;pub fn pingreq() -> [u8; 2]A PINGREQ.
use utopia_mqtt::packet::pingresp;pub fn pingresp() -> [u8; 2]A PINGRESP.
packet::auth
Own pageuse utopia_mqtt::packet::auth::Auth;pub struct AuthAn AUTH (5.0 only): one step of enhanced authentication, or a re-authentication on an open connection.
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 pageuse utopia_mqtt::packet::connack::Connack;pub struct ConnackA CONNACK: the broker's answer to a CONNECT.
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 pageuse utopia_mqtt::packet::connect::Connect;pub struct ConnectA CONNECT: what a client asks for when it opens a session.
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;pub struct WillThe 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 pageuse utopia_mqtt::packet::disconnect::Disconnect;pub struct DisconnectA DISCONNECT: a client leaving, or the broker closing a 5.0 client with a reason.
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 pageuse utopia_mqtt::packet::publish::Puback;pub struct PubackA PUBACK: a client acknowledging a QoS 1 delivery.
use utopia_mqtt::packet::publish::Publish;pub struct PublishA PUBLISH: a message on a topic.
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 pageuse utopia_mqtt::packet::subscribe::Filter;pub struct FilterOne topic filter of a SUBSCRIBE and the QoS asked for it.
use utopia_mqtt::packet::subscribe::Suback;pub struct SubackA SUBACK being built: one code per filter, in the SUBSCRIBE's order.
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;pub struct SubscribeA SUBSCRIBE: topic filters a client asks to receive.
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 pageuse utopia_mqtt::packet::unsubscribe::Unsuback;pub struct UnsubackAn UNSUBACK being built: one code per filter (5.0 sends them; 3.1.1 has none).
use utopia_mqtt::packet::unsubscribe::Unsubscribe;pub struct UnsubscribeAn UNSUBSCRIBE: topic filters a client no longer wants.
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()]));