mqute_codec/protocol/v4/
ping.rs

1//! # PingReq and PingResp Packets V4
2//!
3//! This module defines the `PingReq` and `PingResp` packets, which are used in the MQTT protocol
4//! for connection keep-alive and heartbeat functionality. Both packets have no payload and are
5//! represented by simple structs.
6
7use super::util;
8use crate::protocol::PacketType;
9
10/// Represents an MQTT `PingReq` packet.
11///
12/// The `PingReq` packet is sent by the client to the server to indicate that the connection
13/// is still alive.
14///
15/// # Example
16///
17/// ```rust
18/// use mqute_codec::protocol::v4::PingReq;
19///
20/// let packet = PingReq { };
21/// ```
22#[derive(Debug, Clone, Copy, Default, PartialEq, Eq)]
23pub struct PingReq {}
24
25// Implement the `Decode` trait for `PingReq`.
26util::header_packet_decode_impl!(PingReq, PacketType::PingReq);
27
28// Implement the `Encode` trait for `PingReq`.
29util::header_packet_encode_impl!(PingReq, PacketType::PingReq);
30
31/// Represents an MQTT `PingResp` packet.
32///
33/// The `PingResp` packet is sent by the server to the client in response to a `PingReq` packet.
34///
35/// # Example
36///
37/// ```rust
38/// use mqute_codec::protocol::v4::PingResp;
39///
40/// let packet = PingResp { };
41/// ```
42#[derive(Debug, Clone, Copy, Default, PartialEq, Eq)]
43pub struct PingResp {}
44
45// Implement the `Decode` trait for `PingResp`.
46util::header_packet_decode_impl!(PingResp, PacketType::PingResp);
47
48// Implement the `Encode` trait for `PingResp`.
49util::header_packet_encode_impl!(PingResp, PacketType::PingResp);
50
51#[cfg(test)]
52mod tests {
53    use super::*;
54    use crate::codec::PacketCodec;
55    use crate::codec::{Decode, Encode};
56    use bytes::BytesMut;
57    use tokio_util::codec::Decoder;
58
59    #[test]
60    fn pingreq_decode() {
61        let mut codec = PacketCodec::new(None, None);
62
63        let data = &[
64            (PacketType::PingReq as u8) << 4, // Packet type
65            0x00,                             // Remaining len
66        ];
67
68        let mut stream = BytesMut::new();
69
70        stream.extend_from_slice(&data[..]);
71
72        let raw_packet = codec.decode(&mut stream).unwrap().unwrap();
73        let packet = PingReq::decode(raw_packet).unwrap();
74
75        assert_eq!(packet, PingReq::default());
76    }
77
78    #[test]
79    fn pingreq_encode() {
80        let packet = PingReq::default();
81
82        let mut stream = BytesMut::new();
83        packet.encode(&mut stream).unwrap();
84        assert_eq!(stream, vec![(PacketType::PingReq as u8) << 4, 0x00]);
85    }
86
87    #[test]
88    fn pingresp_decode() {
89        let mut codec = PacketCodec::new(None, None);
90
91        let data = &[
92            (PacketType::PingResp as u8) << 4, // Packet type
93            0x00,                              // Remaining len
94        ];
95
96        let mut stream = BytesMut::new();
97
98        stream.extend_from_slice(&data[..]);
99
100        let raw_packet = codec.decode(&mut stream).unwrap().unwrap();
101        let packet = PingResp::decode(raw_packet).unwrap();
102
103        assert_eq!(packet, PingResp::default());
104    }
105
106    #[test]
107    fn pingresp_encode() {
108        let packet = PingResp::default();
109
110        let mut stream = BytesMut::new();
111        packet.encode(&mut stream).unwrap();
112        assert_eq!(stream, vec![(PacketType::PingResp as u8) << 4, 0x00]);
113    }
114}