Pingreq

Struct Pingreq 

Source
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

Source

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);
Source

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();
Source

pub fn packet_type() -> PacketType

Returns the packet type for PINGREQ packets

§Returns

PacketType::Pingreq (value 12)

§Examples
use mqtt_protocol_core::mqtt;

assert_eq!(
    mqtt::packet::v5_0::Pingreq::packet_type(),
    mqtt::packet::packet_type::PacketType::Pingreq
);
Source

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);
Source

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 PINGREQ packet in wire format.

§Returns

A vector of IoSlice objects for vectored I/O operations

§Examples
use mqtt_protocol_core::mqtt;

let pingreq = mqtt::packet::v5_0::Pingreq::new();
let buffers = pingreq.to_buffers();
// Use with vectored write: socket.write_vectored(&buffers)?;
Source

pub fn to_continuous_buffer(&self) -> Vec<u8>

Create a continuous buffer containing the complete packet data

Returns a vector containing all packet bytes in a single continuous buffer. This method provides an alternative to to_buffers() for no-std environments where vectored I/O is not available.

The returned buffer contains the complete PINGREQ packet serialized according to the MQTT v5.0 protocol specification.

§Returns

A vector containing the complete packet data

§Examples
use mqtt_protocol_core::mqtt;

let pingreq = mqtt::packet::v5_0::Pingreq::new();
let buffer = pingreq.to_continuous_buffer();
// buffer contains all packet bytes
Source

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 Clone for Pingreq

Source§

fn clone(&self) -> Pingreq

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for Pingreq

Implements debug formatting for PINGREQ packets

This implementation uses the same format as Display to provide consistent output for debugging purposes.

§Examples

use mqtt_protocol_core::mqtt;

let pingreq = mqtt::packet::v5_0::Pingreq::new();
println!("Debug: {:?}", pingreq);
// Output: {"type":"pingreq"}
Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Display for Pingreq

Implements display formatting for PINGREQ packets

This implementation provides a JSON representation of the PINGREQ packet for human-readable output, debugging, and logging purposes.

§Examples

use mqtt_protocol_core::mqtt;

let pingreq = mqtt::packet::v5_0::Pingreq::new();
println!("PINGREQ: {}", pingreq);
// Output: {"type":"pingreq"}
Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<PacketIdType> From<Pingreq> for GenericPacket<PacketIdType>
where PacketIdType: IsPacketId + Serialize,

Source§

fn from(v: Pingreq) -> GenericPacket<PacketIdType>

Converts to this type from the input type.
Source§

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 formatting
  • fmt_display(): Display formatting
Source§

fn fmt_debug(&self, f: &mut Formatter<'_>) -> Result

Source§

fn fmt_display(&self, f: &mut Formatter<'_>) -> Result

Source§

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 bytes
  • to_buffers(): Returns I/O slices for network transmission
Source§

fn size(&self) -> usize

Source§

fn to_buffers(&self) -> Vec<IoSlice<'_>>

Source§

fn to_continuous_buffer(&self) -> Vec<u8>

Create a continuous buffer containing the complete packet data Read more
Source§

impl PacketKind for Pingreq

PacketKind implementation for v5.0 PINGREQ packet

Source§

const IS_PINGREQ: bool = true

true if this is a PINGREQ packet
Source§

const IS_V5_0: bool = true

true if this is an MQTT v5.0 packet
Source§

const IS_CONNECT: bool = false

true if this is a CONNECT packet
Source§

const IS_CONNACK: bool = false

true if this is a CONNACK packet
Source§

const IS_PUBLISH: bool = false

true if this is a PUBLISH packet
Source§

const IS_PUBACK: bool = false

true if this is a PUBACK packet
Source§

const IS_PUBREC: bool = false

true if this is a PUBREC packet
Source§

const IS_PUBREL: bool = false

true if this is a PUBREL packet
Source§

const IS_PUBCOMP: bool = false

true if this is a PUBCOMP packet
Source§

const IS_SUBSCRIBE: bool = false

true if this is a SUBSCRIBE packet
Source§

const IS_SUBACK: bool = false

true if this is a SUBACK packet
Source§

const IS_UNSUBSCRIBE: bool = false

true if this is an UNSUBSCRIBE packet
Source§

const IS_UNSUBACK: bool = false

true if this is an UNSUBACK packet
Source§

const IS_PINGRESP: bool = false

true if this is a PINGRESP packet
Source§

const IS_DISCONNECT: bool = false

true if this is a DISCONNECT packet
Source§

const IS_AUTH: bool = false

true if this is an AUTH packet (v5.0 only)
Source§

const IS_V3_1_1: bool = false

true if this is an MQTT v3.1.1 packet
Source§

impl PartialEq for Pingreq

Source§

fn eq(&self, other: &Pingreq) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

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§

fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where S: Serializer,

Serialize this value into the given Serde serializer. Read more
Source§

impl<PacketIdType> TryInto<Pingreq> for GenericPacket<PacketIdType>
where PacketIdType: IsPacketId + Serialize,

Source§

type Error = &'static str

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<Pingreq, <Self as TryInto<Pingreq>>::Error>

Performs the conversion.
Source§

impl Eq for Pingreq

Source§

impl SendableRole<Any> for Pingreq

Source§

impl SendableRole<Client> for Pingreq

Source§

impl StructuralPartialEq for Pingreq

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<Q, K> Equivalent<K> for Q
where Q: Eq + ?Sized, K: Borrow<Q> + ?Sized,

Source§

fn equivalent(&self, key: &K) -> bool

Checks if this value is equivalent to the given key. Read more
Source§

impl<Q, K> Equivalent<K> for Q
where Q: Eq + ?Sized, K: Borrow<Q> + ?Sized,

Source§

fn equivalent(&self, key: &K) -> bool

Compare self to key and return true if they are equal.
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<Role, PacketIdType, T> Sendable<Role, PacketIdType> for T
where Role: RoleType, PacketIdType: IsPacketId, T: SendableRole<Role> + SendableVersion + Display + Debug + PacketKind + SendableHelper<Role, PacketIdType>,

Source§

fn dispatch_send( self, connection: &mut GenericConnection<Role, PacketIdType>, ) -> Vec<GenericEvent<PacketIdType>>

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T> ToString for T
where T: Display + ?Sized,

Source§

fn to_string(&self) -> String

Converts the given value to a String. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.