pub struct Connack {
pub props: Properties,
/* private fields */
}
Expand description
MQTT 5.0 CONNACK packet representation
The CONNACK packet is sent by the MQTT server (broker) in response to a CONNECT packet from a client. It indicates whether the connection attempt was successful and provides various connection-related parameters and capabilities.
According to MQTT 5.0 specification, the CONNACK packet contains:
- Fixed header with packet type and remaining length
- Variable header with acknowledgment flags, reason code, and properties
- No payload
§Acknowledgment Flags
The acknowledgment flags byte contains:
- Session Present flag (bit 0): Indicates whether the server has stored session state from a previous connection. Set to 1 if the server has session state, 0 if starting a clean session.
- Bits 1-7: Reserved and must be set to 0
§Reason Codes
The reason code indicates the result of the connection attempt:
0x00
Success - Connection accepted0x80
Unspecified error0x81
Malformed packet0x82
Protocol error0x83
Implementation specific error0x84
Unsupported protocol version0x85
Client identifier not valid0x86
Bad username or password0x87
Not authorized0x88
Server unavailable0x89
Server busy0x8A
Banned0x8C
Bad authentication method0x90
Topic name invalid0x95
Packet too large0x97
Quota exceeded0x99
Payload format invalid0x9A
Retain not supported0x9B
QoS not supported0x9C
Use another server0x9D
Server moved0x9F
Connection rate exceeded
§Properties
MQTT 5.0 CONNACK packets can include various properties:
- Session Expiry Interval
- Receive Maximum
- Maximum QoS
- Retain Available
- Maximum Packet Size
- Assigned Client Identifier
- Topic Alias Maximum
- Reason String
- User Properties
- Wildcard Subscription Available
- Subscription Identifiers Available
- Shared Subscription Available
- Server Keep Alive
- Response Information
- Server Reference
- Authentication Method
- Authentication Data
§Examples
use mqtt_protocol_core::mqtt;
use mqtt_protocol_core::mqtt::result_code::ConnectReasonCode;
// Create a successful CONNACK with clean session
let connack = mqtt::packet::v5_0::Connack::builder()
.session_present(false)
.reason_code(ConnectReasonCode::Success)
.build()
.unwrap();
assert_eq!(connack.reason_code(), ConnectReasonCode::Success);
assert!(!connack.session_present());
// Create CONNACK with session present and properties
let props = mqtt::packet::Properties::new();
let connack = mqtt::packet::v5_0::Connack::builder()
.session_present(true)
.reason_code(ConnectReasonCode::Success)
.props(props)
.build()
.unwrap();
// Serialize to bytes for network transmission
let buffers = connack.to_buffers();
Fields§
§props: Properties
Implementations§
Source§impl Connack
impl Connack
Sourcepub fn builder() -> ConnackBuilder
pub fn builder() -> ConnackBuilder
Create a new ConnackBuilder for constructing CONNACK packets
Returns a builder instance that allows setting the various fields of a CONNACK packet in a fluent interface style. The builder ensures all required fields are set before creating the final packet.
§Returns
A new ConnackBuilder
instance
§Examples
use mqtt_protocol_core::mqtt;
use mqtt_protocol_core::mqtt::result_code::ConnectReasonCode;
let connack = mqtt::packet::v5_0::Connack::builder()
.session_present(false)
.reason_code(ConnectReasonCode::Success)
.build()
.unwrap();
Sourcepub fn packet_type() -> PacketType
pub fn packet_type() -> PacketType
Get the packet type for CONNACK packets
Returns the constant packet type identifier for CONNACK packets.
This is always PacketType::Connack
for CONNACK packets.
§Returns
The packet type PacketType::Connack
§Examples
use mqtt_protocol_core::mqtt;
use mqtt_protocol_core::mqtt::packet::packet_type::PacketType;
assert_eq!(mqtt::packet::v5_0::Connack::packet_type(), PacketType::Connack);
Sourcepub fn session_present(&self) -> bool
pub fn session_present(&self) -> bool
Get the session present flag
Returns true
if the server has stored session state for this client from a
previous connection, false
if this is a clean session or no session state exists.
This corresponds to bit 0 of the acknowledgment flags byte.
§Returns
true
if session state is present, false
for a clean session
§Examples
use mqtt_protocol_core::mqtt;
use mqtt_protocol_core::mqtt::result_code::ConnectReasonCode;
let connack = mqtt::packet::v5_0::Connack::builder()
.session_present(true)
.reason_code(ConnectReasonCode::Success)
.build()
.unwrap();
assert!(connack.session_present());
Sourcepub fn reason_code(&self) -> ConnectReasonCode
pub fn reason_code(&self) -> ConnectReasonCode
Get the reason code from the CONNACK packet
Returns the reason code that indicates the result of the connection attempt. The reason code provides detailed information about whether the connection was successful or failed, and if failed, the specific reason for failure.
§Returns
The ConnectReasonCode
indicating the connection result
§Examples
use mqtt_protocol_core::mqtt;
use mqtt_protocol_core::mqtt::result_code::ConnectReasonCode;
let connack = mqtt::packet::v5_0::Connack::builder()
.session_present(false)
.reason_code(ConnectReasonCode::Success)
.build()
.unwrap();
assert_eq!(connack.reason_code(), ConnectReasonCode::Success);
Sourcepub fn size(&self) -> usize
pub fn size(&self) -> usize
Get the total size of the CONNACK packet in bytes
Returns the complete size of the CONNACK packet including the fixed header, variable header, and all properties. This is useful for memory allocation and buffer management.
§Returns
The total packet size in bytes
§Examples
use mqtt_protocol_core::mqtt;
use mqtt_protocol_core::mqtt::result_code::ConnectReasonCode;
let connack = mqtt::packet::v5_0::Connack::builder()
.session_present(false)
.reason_code(ConnectReasonCode::Success)
.build()
.unwrap();
let size = connack.size();
// Size includes fixed header + variable header + properties
Sourcepub fn to_buffers(&self) -> Vec<IoSlice<'_>>
pub fn to_buffers(&self) -> Vec<IoSlice<'_>>
Create IoSlice buffers for efficient network I/O
Returns a vector of IoSlice
objects that can be used for vectored I/O
operations, allowing zero-copy writes to network sockets. The buffers
represent the complete CONNACK packet in wire format.
The returned buffers contain:
- Fixed header (packet type and flags)
- Remaining length field
- Acknowledgment flags
- Reason code
- Property length field
- Properties data (if any)
§Returns
A vector of IoSlice
objects for vectored I/O operations
§Examples
use mqtt_protocol_core::mqtt;
use mqtt_protocol_core::mqtt::result_code::ConnectReasonCode;
let connack = mqtt::packet::v5_0::Connack::builder()
.session_present(false)
.reason_code(ConnectReasonCode::Success)
.build()
.unwrap();
let buffers = connack.to_buffers();
// Use with vectored write: socket.write_vectored(&buffers)?;
Sourcepub fn parse(data: &[u8]) -> Result<(Self, usize), MqttError>
pub fn parse(data: &[u8]) -> Result<(Self, usize), MqttError>
Parse a CONNACK packet from raw bytes
Decodes a CONNACK packet from a byte buffer according to the MQTT 5.0 protocol specification. The buffer should contain the variable header and any properties, but not the fixed header (which should have been processed separately).
The parsing process:
- Reads acknowledgment flags (1 byte)
- Reads reason code (1 byte)
- Parses properties section
- Validates the packet structure and properties
§Parameters
data
- Byte buffer containing the CONNACK packet data (without fixed header)
§Returns
Ok((Connack, bytes_consumed))
- Successfully parsed packet and number of bytes consumedErr(MqttError::MalformedPacket)
- If the buffer is malformed or too shortErr(MqttError::ProtocolError)
- If the packet contains invalid properties
§Examples
use mqtt_protocol_core::mqtt;
// Buffer contains: [flags, reason_code, prop_length, ...properties]
let buffer = &[0x00, 0x00, 0x00]; // No session, success, no properties
let (connack, consumed) = mqtt::packet::v5_0::Connack::parse(buffer).unwrap();
assert!(!connack.session_present());
assert_eq!(consumed, 3);
Trait Implementations§
Source§impl AsConcrete<Connack> for Packet
impl AsConcrete<Connack> for Packet
fn as_concrete(&self) -> Option<&Connack>
Source§impl Debug for Connack
Implementation of Debug
trait for debugging output
impl Debug for Connack
Implementation of Debug
trait for debugging output
Uses the same JSON formatting as Display
for consistent debugging output.
This provides detailed, structured information about the CONNACK packet state.
§Examples
use mqtt_protocol_core::mqtt;
use mqtt_protocol_core::mqtt::result_code::ConnectReasonCode;
let connack = mqtt::packet::v5_0::Connack::builder()
.session_present(false)
.reason_code(ConnectReasonCode::BadUserNameOrPassword)
.build()
.unwrap();
println!("{:?}", connack);
// Output: {"type":"CONNACK","session_present":false,"reason_code":"BadUserNameOrPassword","props":{}}
Source§impl Display for Connack
Implementation of Display
trait for human-readable output
impl Display for Connack
Implementation of Display
trait for human-readable output
Formats the CONNACK packet as a JSON string for easy reading and debugging. If serialization fails, it returns an error message in JSON format.
§Examples
use mqtt_protocol_core::mqtt;
use mqtt_protocol_core::mqtt::result_code::ConnectReasonCode;
let connack = mqtt::packet::v5_0::Connack::builder()
.session_present(true)
.reason_code(ConnectReasonCode::Success)
.build()
.unwrap();
println!("{}", connack);
// Output: {"type":"CONNACK","session_present":true,"reason_code":"Success","props":{}}
Source§impl<PacketIdType> From<Connack> for GenericPacket<PacketIdType>where
PacketIdType: IsPacketId + Serialize,
impl<PacketIdType> From<Connack> for GenericPacket<PacketIdType>where
PacketIdType: IsPacketId + Serialize,
Source§fn from(v: Connack) -> GenericPacket<PacketIdType>
fn from(v: Connack) -> GenericPacket<PacketIdType>
Source§impl GenericPacketDisplay for Connack
Implementation of GenericPacketDisplay
for generic formatting operations
impl GenericPacketDisplay for Connack
Implementation of GenericPacketDisplay
for generic formatting operations
Provides a common interface for formatting operations that can be used generically across different MQTT packet types. This enables uniform display handling in collections and generic contexts.
§Methods
fmt_debug()
: Delegates to the Debug trait implementationfmt_display()
: Delegates to the Display trait implementation
§Examples
use mqtt_protocol_core::mqtt;
use mqtt_protocol_core::mqtt::packet::GenericPacketDisplay;
use mqtt_protocol_core::mqtt::result_code::ConnectReasonCode;
let connack = mqtt::packet::v5_0::Connack::builder()
.session_present(false)
.reason_code(ConnectReasonCode::Success)
.build()
.unwrap();
// Use generic display methods
println!("{}", format_args!("{}", connack)); // Uses fmt_display
Source§impl GenericPacketTrait for Connack
Implementation of GenericPacketTrait
for generic packet operations
impl GenericPacketTrait for Connack
Implementation of GenericPacketTrait
for generic packet operations
Provides a common interface for packet operations that can be used generically across different MQTT packet types. This allows for uniform handling of packets in collections and generic contexts.
§Methods
size()
: Returns the total packet size in bytesto_buffers()
: Returns IoSlice buffers for efficient network I/O
§Examples
use mqtt_protocol_core::mqtt;
use mqtt_protocol_core::mqtt::packet::GenericPacketTrait;
use mqtt_protocol_core::mqtt::result_code::ConnectReasonCode;
let connack = mqtt::packet::v5_0::Connack::builder()
.session_present(false)
.reason_code(ConnectReasonCode::Success)
.build()
.unwrap();
// Use generic trait methods
let size = connack.size();
let buffers = connack.to_buffers();
Source§impl IntoConcreteOwned<Connack> for Packet
impl IntoConcreteOwned<Connack> for Packet
fn into_concrete_owned(self) -> Option<Connack>
Source§impl PacketKind for Connack
PacketKind
implementation for v5.0 CONNACK packet
impl PacketKind for Connack
PacketKind
implementation for v5.0 CONNACK packet
Source§const IS_CONNACK: bool = true
const IS_CONNACK: bool = true
true
if this is a CONNACK packetSource§const IS_CONNECT: bool = false
const IS_CONNECT: bool = false
true
if this is a CONNECT packetSource§const IS_PUBLISH: bool = false
const IS_PUBLISH: bool = false
true
if this is a PUBLISH packetSource§const IS_PUBCOMP: bool = false
const IS_PUBCOMP: bool = false
true
if this is a PUBCOMP packetSource§const IS_SUBSCRIBE: bool = false
const IS_SUBSCRIBE: bool = false
true
if this is a SUBSCRIBE packetSource§const IS_UNSUBSCRIBE: bool = false
const IS_UNSUBSCRIBE: bool = false
true
if this is an UNSUBSCRIBE packetSource§const IS_UNSUBACK: bool = false
const IS_UNSUBACK: bool = false
true
if this is an UNSUBACK packetSource§const IS_PINGREQ: bool = false
const IS_PINGREQ: bool = false
true
if this is a PINGREQ packetSource§const IS_PINGRESP: bool = false
const IS_PINGRESP: bool = false
true
if this is a PINGRESP packetSource§const IS_DISCONNECT: bool = false
const IS_DISCONNECT: bool = false
true
if this is a DISCONNECT packetSource§impl Serialize for Connack
Implementation of Serialize
trait for JSON serialization
impl Serialize for Connack
Implementation of Serialize
trait for JSON serialization
Serializes the CONNACK packet to a structured format containing:
type
: The packet type as a string (“CONNACK”)session_present
: Boolean indicating session statereason_code
: The connection result reason codeprops
: MQTT 5.0 properties (if any)
§Examples
use mqtt_protocol_core::mqtt;
use mqtt_protocol_core::mqtt::result_code::ConnectReasonCode;
let connack = mqtt::packet::v5_0::Connack::builder()
.session_present(false)
.reason_code(ConnectReasonCode::Success)
.build()
.unwrap();
let json = serde_json::to_string(&connack).unwrap();
// Produces: {"type":"CONNACK","session_present":false,"reason_code":"Success","props":{}}
Source§impl<PacketIdType> TryInto<Connack> for GenericPacket<PacketIdType>where
PacketIdType: IsPacketId + Serialize,
impl<PacketIdType> TryInto<Connack> for GenericPacket<PacketIdType>where
PacketIdType: IsPacketId + Serialize,
impl Eq for Connack
impl SendableRole<Any> for Connack
impl SendableRole<Server> for Connack
impl StructuralPartialEq for Connack
Auto Trait Implementations§
impl Freeze for Connack
impl RefUnwindSafe for Connack
impl Send for Connack
impl Sync for Connack
impl Unpin for Connack
impl UnwindSafe for Connack
Blanket Implementations§
Source§impl<T> AsConcrete<T> for T
impl<T> AsConcrete<T> for T
fn as_concrete(&self) -> Option<&T>
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
Source§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
Source§fn equivalent(&self, key: &K) -> bool
fn equivalent(&self, key: &K) -> bool
key
and return true
if they are equal.