Pingresp

Struct Pingresp 

Source
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

Source

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

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

pub fn packet_type() -> PacketType

Returns the packet type for PINGRESP packets

§Returns

PacketType::Pingresp (value 13)

§Examples
use mqtt_protocol_core::mqtt;

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

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);
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 PINGRESP packet in wire format.

§Returns

A vector of IoSlice objects for vectored I/O operations

§Examples
use mqtt_protocol_core::mqtt;

let pingresp = mqtt::packet::v5_0::Pingresp::new();
let buffers = pingresp.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 PINGRESP 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 pingresp = mqtt::packet::v5_0::Pingresp::new();
let buffer = pingresp.to_continuous_buffer();
// buffer contains all packet bytes
Source

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

Source§

fn clone(&self) -> Pingresp

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 Pingresp

Implements debug formatting for PINGRESP packets

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

§Examples

use mqtt_protocol_core::mqtt;

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

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

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

impl Display for Pingresp

Implements display formatting for PINGRESP packets

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

§Examples

use mqtt_protocol_core::mqtt;

let pingresp = mqtt::packet::v5_0::Pingresp::new();
println!("PINGRESP: {}", pingresp);
// Output: {"type":"pingresp"}
Source§

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

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

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

Source§

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

Converts to this type from the input type.
Source§

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 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 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 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 Pingresp

Source§

const IS_PINGRESP: bool = true

true if this is a PINGRESP 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_PINGREQ: bool = false

true if this is a PINGREQ 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 Pingresp

Source§

fn eq(&self, other: &Pingresp) -> 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 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§

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<Pingresp> 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<Pingresp, <Self as TryInto<Pingresp>>::Error>

Performs the conversion.
Source§

impl Eq for Pingresp

Source§

impl SendableRole<Any> for Pingresp

Source§

impl SendableRole<Server> for Pingresp

Source§

impl StructuralPartialEq for Pingresp

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.