pub struct Pingresp { /* private fields */ }
Expand description
MQTT 5.0 PINGRESP packet representation
The PINGRESP packet is a heartbeat response packet sent by an MQTT server to a client in response to a PINGREQ packet. This packet acknowledges that the server has received the client’s keep-alive request and confirms that the connection is still active.
According to MQTT 5.0 specification, the PINGRESP packet:
- Has no variable header
- Has no payload
- Has a remaining length of 0
- Must be sent by the server in response to a PINGREQ packet
§Protocol Information
- Packet Type: 13 (0xD0)
- Remaining Length: 0 (no variable header or payload)
- Direction: Server to Client
§Keep Alive Mechanism
The PINGRESP packet is used to:
- Acknowledge the client’s PINGREQ packet
- Confirm to the client that the server is alive and responsive
- Maintain the network connection’s keep-alive state
- Prevent connection timeouts due to network inactivity
The server must send a PINGRESP packet in response to each PINGREQ packet received. If the client does not receive a PINGRESP within a reasonable time after sending a PINGREQ, it should consider the connection broken and close the network connection.
§Timing Requirements
The server should send a PINGRESP packet:
- Immediately upon receiving a PINGREQ packet
- With minimal delay to ensure timely keep-alive acknowledgment
- Before processing other non-urgent packets
§Examples
use mqtt_protocol_core::mqtt;
// Create a PINGRESP packet
let pingresp = mqtt::packet::v5_0::Pingresp::new();
assert_eq!(pingresp.packet_type(), mqtt::packet::packet_type::PacketType::Pingresp);
assert_eq!(pingresp.size(), 2); // Fixed header (1 byte) + remaining length (1 byte)
// Build using builder pattern
let pingresp = mqtt::packet::v5_0::Pingresp::builder()
.build()
.unwrap();
// Serialize to bytes for network transmission
let buffers = pingresp.to_buffers();
assert_eq!(buffers.len(), 2); // Fixed header + remaining length
Implementations§
Source§impl Pingresp
impl Pingresp
Sourcepub fn new() -> Self
pub fn new() -> Self
Creates a new PINGRESP packet
This method creates a PINGRESP packet with the standard fixed header and zero remaining length, as specified by the MQTT 5.0 protocol. The PINGRESP packet is typically created by the server in response to a PINGREQ packet from the client.
§Returns
A new Pingresp
instance ready for transmission
§Examples
use mqtt_protocol_core::mqtt;
let pingresp = mqtt::packet::v5_0::Pingresp::new();
assert_eq!(pingresp.size(), 2);
Sourcepub fn builder() -> PingrespBuilder
pub fn builder() -> PingrespBuilder
Creates a new builder for constructing a PINGRESP packet
The builder pattern provides a consistent interface for packet creation, even though PINGRESP packets have no configurable parameters.
§Returns
A PingrespBuilder
instance
§Examples
use mqtt_protocol_core::mqtt;
let pingresp = mqtt::packet::v5_0::Pingresp::builder()
.build()
.unwrap();
Sourcepub fn packet_type() -> PacketType
pub fn packet_type() -> PacketType
Sourcepub fn size(&self) -> usize
pub fn size(&self) -> usize
Returns the total size of the PINGRESP packet in bytes
The size includes the fixed header (1 byte) and the remaining length field (1 byte). Since PINGRESP has no variable header or payload, the total size is always 2 bytes.
§Returns
The packet size in bytes (always 2 for PINGRESP)
§Examples
use mqtt_protocol_core::mqtt;
let pingresp = mqtt::packet::v5_0::Pingresp::new();
assert_eq!(pingresp.size(), 2);
Sourcepub fn to_buffers(&self) -> Vec<IoSlice<'_>>
pub fn to_buffers(&self) -> Vec<IoSlice<'_>>
Converts the PINGRESP packet to a vector of I/O slices for efficient network transmission
This method provides zero-copy serialization by returning references to the internal packet data as I/O slices, which can be used directly with vectored I/O operations.
§Returns
A vector containing:
- Fixed header slice (1 byte)
- Remaining length slice (1 byte)
§Examples
use mqtt_protocol_core::mqtt;
let pingresp = mqtt::packet::v5_0::Pingresp::new();
let buffers = pingresp.to_buffers();
assert_eq!(buffers.len(), 2);
// Can be used with vectored write operations
// stream.write_vectored(&buffers).await?;
Sourcepub fn parse(_data: &[u8]) -> Result<(Self, usize), MqttError>
pub fn parse(_data: &[u8]) -> Result<(Self, usize), MqttError>
Parses a PINGRESP packet from raw bytes
Since PINGRESP packets have no variable header or payload, this method simply creates a new PINGRESP packet instance. The data parameter is not used but is kept for consistency with other packet types.
§Parameters
_data
- Raw byte data (unused for PINGRESP)
§Returns
A tuple containing:
- The created
Pingresp
instance - The number of bytes consumed (always 0 for PINGRESP)
§Errors
This method always succeeds for PINGRESP packets.
§Examples
use mqtt_protocol_core::mqtt;
let data = &[];
let (pingresp, consumed) = mqtt::packet::v5_0::Pingresp::parse(data).unwrap();
assert_eq!(consumed, 0);
assert_eq!(pingresp.size(), 2);
Trait Implementations§
Source§impl AsConcrete<Pingresp> for Packet
impl AsConcrete<Pingresp> for Packet
fn as_concrete(&self) -> Option<&Pingresp>
Source§impl Debug for Pingresp
Implements debug formatting for PINGRESP packets
impl Debug for Pingresp
Implements debug formatting for PINGRESP packets
Source§impl Display for Pingresp
Implements display formatting for PINGRESP packets
impl Display for Pingresp
Implements display formatting for PINGRESP packets
Source§impl<PacketIdType> From<Pingresp> for GenericPacket<PacketIdType>where
PacketIdType: IsPacketId + Serialize,
impl<PacketIdType> From<Pingresp> for GenericPacket<PacketIdType>where
PacketIdType: IsPacketId + Serialize,
Source§fn from(v: Pingresp) -> GenericPacket<PacketIdType>
fn from(v: Pingresp) -> GenericPacket<PacketIdType>
Source§impl GenericPacketDisplay for Pingresp
Implements the generic packet display trait for PINGRESP packets
impl GenericPacketDisplay for Pingresp
Implements the generic packet display trait for PINGRESP packets
This trait provides a common interface for formatting MQTT packets, allowing them to be displayed consistently across different packet types.
§Methods
fmt_debug()
: Debug formattingfmt_display()
: Display formatting
Source§impl GenericPacketTrait for Pingresp
Implements the generic packet trait for PINGRESP packets
impl GenericPacketTrait for Pingresp
Implements the generic packet trait for PINGRESP packets
This trait provides a common interface for all MQTT packet types, allowing them to be used polymorphically in generic contexts.
§Methods
size()
: Returns the packet size in bytesto_buffers()
: Returns I/O slices for network transmission
Source§impl IntoConcreteOwned<Pingresp> for Packet
impl IntoConcreteOwned<Pingresp> for Packet
fn into_concrete_owned(self) -> Option<Pingresp>
Source§impl PacketKind for Pingresp
impl PacketKind for Pingresp
Source§const IS_PINGRESP: bool = true
const IS_PINGRESP: bool = true
true
if this is a PINGRESP packetSource§const IS_CONNECT: bool = false
const IS_CONNECT: bool = false
true
if this is a CONNECT packetSource§const IS_CONNACK: bool = false
const IS_CONNACK: bool = false
true
if this is a CONNACK 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_DISCONNECT: bool = false
const IS_DISCONNECT: bool = false
true
if this is a DISCONNECT packetSource§impl Serialize for Pingresp
Implements JSON serialization for PINGRESP packets
impl Serialize for Pingresp
Implements JSON serialization for PINGRESP packets
This implementation allows PINGRESP packets to be serialized to JSON format, which is useful for debugging, logging, and protocol analysis.
The serialized format includes:
type
: The packet type as a string (“pingresp”)
§Examples
use mqtt_protocol_core::mqtt;
use serde_json;
let pingresp = mqtt::packet::v5_0::Pingresp::new();
let json = serde_json::to_string(&pingresp).unwrap();
assert!(json.contains("pingresp"));
Source§impl<PacketIdType> TryInto<Pingresp> for GenericPacket<PacketIdType>where
PacketIdType: IsPacketId + Serialize,
impl<PacketIdType> TryInto<Pingresp> for GenericPacket<PacketIdType>where
PacketIdType: IsPacketId + Serialize,
impl Eq for Pingresp
impl SendableRole<Any> for Pingresp
impl SendableRole<Server> for Pingresp
impl StructuralPartialEq for Pingresp
Auto Trait Implementations§
impl Freeze for Pingresp
impl RefUnwindSafe for Pingresp
impl Send for Pingresp
impl Sync for Pingresp
impl Unpin for Pingresp
impl UnwindSafe for Pingresp
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.