mqute_codec/protocol/v5/
ping.rs

1//! # Ping Request and Response Packets V5
2//!
3//! This module implements the MQTT PingReq (Ping Request) and PingResp (Ping Response) packets.
4//! These packets are used to maintain the connection between client and server when no other
5//! packets are being sent, and to verify that the connection is still active.
6//!
7//! The PingReq packet is sent by a client to the server to:
8//! 1. Indicate that the client is alive when no other packets are being sent
9//! 2. Verify that the server is available and responding
10//!
11//! The PingResp packet is sent by the server in response to a PingReq to:
12//! 1. Acknowledge the ping request
13//! 2. Confirm that the server is still alive and responsive
14//!
15//! The PingReq and PingResp packet have no payload or variable header - they consist only of a
16//! fixed header.
17
18use super::util;
19use crate::protocol::PacketType;
20
21/// Represents an MQTT PingReq (Ping Request) packet.
22///
23/// # Example
24///
25/// ```rust
26/// use mqute_codec::protocol::v5::PingReq;
27///
28/// let packet = PingReq { };
29/// ```
30#[derive(Debug, Clone, Copy, Default, PartialEq, Eq)]
31pub struct PingReq {}
32
33// Implements encoding/decoding using ping packet macros
34util::ping_packet_decode_impl!(PingReq, PacketType::PingReq);
35util::ping_packet_encode_impl!(PingReq, PacketType::PingReq);
36
37/// Represents an MQTT PingResp (Ping Response) packet.
38///
39/// # Example
40///
41/// ```rust
42/// use mqute_codec::protocol::v5::PingResp;
43///
44/// let packet = PingResp { };
45/// ```
46#[derive(Debug, Clone, Copy, Default, PartialEq, Eq)]
47pub struct PingResp {}
48
49// Implements encoding/decoding using ping packet macros
50util::ping_packet_decode_impl!(PingResp, PacketType::PingResp);
51util::ping_packet_encode_impl!(PingResp, PacketType::PingResp);
52
53#[cfg(test)]
54mod tests {
55    use super::*;
56    use crate::codec::PacketCodec;
57    use crate::codec::{Decode, Encode};
58    use bytes::BytesMut;
59    use tokio_util::codec::Decoder;
60
61    #[test]
62    fn pingreq_decode() {
63        let mut codec = PacketCodec::new(None, None);
64
65        let data = &[
66            (PacketType::PingReq as u8) << 4, // Packet type
67            0x00,                             // Remaining len
68        ];
69
70        let mut stream = BytesMut::new();
71
72        stream.extend_from_slice(&data[..]);
73
74        let raw_packet = codec.decode(&mut stream).unwrap().unwrap();
75        let packet = PingReq::decode(raw_packet).unwrap();
76
77        assert_eq!(packet, PingReq::default());
78    }
79
80    #[test]
81    fn pingreq_encode() {
82        let packet = PingReq::default();
83
84        let mut stream = BytesMut::new();
85        packet.encode(&mut stream).unwrap();
86        assert_eq!(stream, vec![(PacketType::PingReq as u8) << 4, 0x00]);
87    }
88
89    #[test]
90    fn pingresp_decode() {
91        let mut codec = PacketCodec::new(None, None);
92
93        let data = &[
94            (PacketType::PingResp as u8) << 4, // Packet type
95            0x00,                              // Remaining len
96        ];
97
98        let mut stream = BytesMut::new();
99
100        stream.extend_from_slice(&data[..]);
101
102        let raw_packet = codec.decode(&mut stream).unwrap().unwrap();
103        let packet = PingResp::decode(raw_packet).unwrap();
104
105        assert_eq!(packet, PingResp::default());
106    }
107
108    #[test]
109    fn pingresp_encode() {
110        let packet = PingResp::default();
111
112        let mut stream = BytesMut::new();
113        packet.encode(&mut stream).unwrap();
114        assert_eq!(stream, vec![(PacketType::PingResp as u8) << 4, 0x00]);
115    }
116}