pub struct Pingreq { /* private fields */ }
Expand description
MQTT 5.0 PINGREQ packet representation
The PINGREQ packet is a heartbeat packet sent by an MQTT client to the server to keep the connection alive and ensure that the connection is still active. This packet is part of the keep-alive mechanism in MQTT protocol.
According to MQTT 5.0 specification, the PINGREQ packet:
- Has no variable header
- Has no payload
- Has a remaining length of 0
- Is typically sent by the client at intervals specified by the Keep Alive value
§Protocol Information
- Packet Type: 12 (0xC0)
- Remaining Length: 0 (no variable header or payload)
- Direction: Client to Server
§Keep Alive Mechanism
The PINGREQ packet is used to:
- Indicate to the server that the client is alive
- Exercise the network to confirm connectivity
- Ensure that the network connection is active
The server must respond with a PINGRESP packet when it receives a PINGREQ. If the client does not receive a PINGRESP within a reasonable time, it should close the network connection.
§Timing
The client should send a PINGREQ packet when:
- The Keep Alive time period has elapsed since the last packet was sent
- No other packets need to be sent during the Keep Alive period
§Examples
use mqtt_protocol_core::mqtt;
// Create a PINGREQ packet
let pingreq = mqtt::packet::v5_0::Pingreq::new();
assert_eq!(pingreq.packet_type(), mqtt::packet::packet_type::PacketType::Pingreq);
assert_eq!(pingreq.size(), 2); // Fixed header (1 byte) + remaining length (1 byte)
// Build using builder pattern
let pingreq = mqtt::packet::v5_0::Pingreq::builder()
.build()
.unwrap();
// Serialize to bytes for network transmission
let buffers = pingreq.to_buffers();
assert_eq!(buffers.len(), 2); // Fixed header + remaining length
Implementations§
Source§impl Pingreq
impl Pingreq
Sourcepub fn new() -> Self
pub fn new() -> Self
Creates a new PINGREQ packet
This method creates a PINGREQ packet with the standard fixed header and zero remaining length, as specified by the MQTT 5.0 protocol.
§Returns
A new Pingreq
instance ready for transmission
§Examples
use mqtt_protocol_core::mqtt;
let pingreq = mqtt::packet::v5_0::Pingreq::new();
assert_eq!(pingreq.size(), 2);
Sourcepub fn builder() -> PingreqBuilder
pub fn builder() -> PingreqBuilder
Creates a new builder for constructing a PINGREQ packet
The builder pattern provides a consistent interface for packet creation, even though PINGREQ packets have no configurable parameters.
§Returns
A PingreqBuilder
instance
§Examples
use mqtt_protocol_core::mqtt;
let pingreq = mqtt::packet::v5_0::Pingreq::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 PINGREQ packet in bytes
The size includes the fixed header (1 byte) and the remaining length field (1 byte). Since PINGREQ has no variable header or payload, the total size is always 2 bytes.
§Returns
The packet size in bytes (always 2 for PINGREQ)
§Examples
use mqtt_protocol_core::mqtt;
let pingreq = mqtt::packet::v5_0::Pingreq::new();
assert_eq!(pingreq.size(), 2);
Sourcepub fn to_buffers(&self) -> Vec<IoSlice<'_>>
pub fn to_buffers(&self) -> Vec<IoSlice<'_>>
Converts the PINGREQ 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 pingreq = mqtt::packet::v5_0::Pingreq::new();
let buffers = pingreq.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 PINGREQ packet from raw bytes
Since PINGREQ packets have no variable header or payload, this method simply creates a new PINGREQ packet instance. The data parameter is not used but is kept for consistency with other packet types.
§Parameters
_data
- Raw byte data (unused for PINGREQ)
§Returns
A tuple containing:
- The created
Pingreq
instance - The number of bytes consumed (always 0 for PINGREQ)
§Errors
This method always succeeds for PINGREQ packets.
§Examples
use mqtt_protocol_core::mqtt;
let data = &[];
let (pingreq, consumed) = mqtt::packet::v5_0::Pingreq::parse(data).unwrap();
assert_eq!(consumed, 0);
assert_eq!(pingreq.size(), 2);
Trait Implementations§
Source§impl AsConcrete<Pingreq> for Packet
impl AsConcrete<Pingreq> for Packet
fn as_concrete(&self) -> Option<&Pingreq>
Source§impl Debug for Pingreq
Implements debug formatting for PINGREQ packets
impl Debug for Pingreq
Implements debug formatting for PINGREQ packets
Source§impl Display for Pingreq
Implements display formatting for PINGREQ packets
impl Display for Pingreq
Implements display formatting for PINGREQ packets
Source§impl<PacketIdType> From<Pingreq> for GenericPacket<PacketIdType>where
PacketIdType: IsPacketId + Serialize,
impl<PacketIdType> From<Pingreq> for GenericPacket<PacketIdType>where
PacketIdType: IsPacketId + Serialize,
Source§fn from(v: Pingreq) -> GenericPacket<PacketIdType>
fn from(v: Pingreq) -> GenericPacket<PacketIdType>
Source§impl GenericPacketDisplay for Pingreq
Implements the generic packet display trait for PINGREQ packets
impl GenericPacketDisplay for Pingreq
Implements the generic packet display trait for PINGREQ 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 Pingreq
Implements the generic packet trait for PINGREQ packets
impl GenericPacketTrait for Pingreq
Implements the generic packet trait for PINGREQ 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<Pingreq> for Packet
impl IntoConcreteOwned<Pingreq> for Packet
fn into_concrete_owned(self) -> Option<Pingreq>
Source§impl PacketKind for Pingreq
PacketKind
implementation for v5.0 PINGREQ packet
impl PacketKind for Pingreq
PacketKind
implementation for v5.0 PINGREQ packet
Source§const IS_PINGREQ: bool = true
const IS_PINGREQ: bool = true
true
if this is a PINGREQ 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_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 Pingreq
Implements JSON serialization for PINGREQ packets
impl Serialize for Pingreq
Implements JSON serialization for PINGREQ packets
This implementation allows PINGREQ 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 (“pingreq”)
§Examples
use mqtt_protocol_core::mqtt;
use serde_json;
let pingreq = mqtt::packet::v5_0::Pingreq::new();
let json = serde_json::to_string(&pingreq).unwrap();
assert!(json.contains("pingreq"));
Source§impl<PacketIdType> TryInto<Pingreq> for GenericPacket<PacketIdType>where
PacketIdType: IsPacketId + Serialize,
impl<PacketIdType> TryInto<Pingreq> for GenericPacket<PacketIdType>where
PacketIdType: IsPacketId + Serialize,
impl Eq for Pingreq
impl SendableRole<Any> for Pingreq
impl SendableRole<Client> for Pingreq
impl StructuralPartialEq for Pingreq
Auto Trait Implementations§
impl Freeze for Pingreq
impl RefUnwindSafe for Pingreq
impl Send for Pingreq
impl Sync for Pingreq
impl Unpin for Pingreq
impl UnwindSafe for Pingreq
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.