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, traits};
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
31impl traits::PingReq for PingReq {}
32
33/// Represents an MQTT `PingResp` packet.
34///
35/// The `PingResp` packet is sent by the server to the client in response to a `PingReq` packet.
36///
37/// # Example
38///
39/// ```rust
40/// use mqute_codec::protocol::v4::PingResp;
41///
42/// let packet = PingResp { };
43/// ```
44#[derive(Debug, Clone, Copy, Default, PartialEq, Eq)]
45pub struct PingResp {}
46
47// Implement the `Decode` trait for `PingResp`.
48util::header_packet_decode_impl!(PingResp, PacketType::PingResp);
49
50// Implement the `Encode` trait for `PingResp`.
51util::header_packet_encode_impl!(PingResp, PacketType::PingResp);
52
53impl traits::PingResp for PingResp {}
54
55#[cfg(test)]
56mod tests {
57    use super::*;
58    use crate::codec::PacketCodec;
59    use crate::codec::{Decode, Encode};
60    use bytes::BytesMut;
61    use tokio_util::codec::Decoder;
62
63    #[test]
64    fn pingreq_decode() {
65        let mut codec = PacketCodec::new(None, None);
66
67        let data = &[
68            (PacketType::PingReq as u8) << 4, // Packet type
69            0x00,                             // Remaining len
70        ];
71
72        let mut stream = BytesMut::new();
73
74        stream.extend_from_slice(&data[..]);
75
76        let raw_packet = codec.decode(&mut stream).unwrap().unwrap();
77        let packet = PingReq::decode(raw_packet).unwrap();
78
79        assert_eq!(packet, PingReq::default());
80    }
81
82    #[test]
83    fn pingreq_encode() {
84        let packet = PingReq::default();
85
86        let mut stream = BytesMut::new();
87        packet.encode(&mut stream).unwrap();
88        assert_eq!(stream, vec![(PacketType::PingReq as u8) << 4, 0x00]);
89    }
90
91    #[test]
92    fn pingresp_decode() {
93        let mut codec = PacketCodec::new(None, None);
94
95        let data = &[
96            (PacketType::PingResp as u8) << 4, // Packet type
97            0x00,                              // Remaining len
98        ];
99
100        let mut stream = BytesMut::new();
101
102        stream.extend_from_slice(&data[..]);
103
104        let raw_packet = codec.decode(&mut stream).unwrap().unwrap();
105        let packet = PingResp::decode(raw_packet).unwrap();
106
107        assert_eq!(packet, PingResp::default());
108    }
109
110    #[test]
111    fn pingresp_encode() {
112        let packet = PingResp::default();
113
114        let mut stream = BytesMut::new();
115        packet.encode(&mut stream).unwrap();
116        assert_eq!(stream, vec![(PacketType::PingResp as u8) << 4, 0x00]);
117    }
118}