rust_mqtt/packet/v5/
pingreq_packet.rs

1/*
2 * MIT License
3 *
4 * Copyright (c) [2022] [Ondrej Babec <ond.babec@gmail.com>]
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included in all
14 * copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22 * SOFTWARE.
23 */
24
25use crate::packet::v5::mqtt_packet::Packet;
26use crate::utils::buffer_reader::BuffReader;
27use crate::utils::buffer_writer::BuffWriter;
28use crate::utils::types::BufferError;
29
30use super::packet_type::PacketType;
31use super::property::Property;
32
33pub struct PingreqPacket {
34    pub fixed_header: u8,
35    pub remain_len: u32,
36}
37
38impl PingreqPacket {}
39
40impl<'a> Packet<'a> for PingreqPacket {
41    fn new() -> Self {
42        Self {
43            fixed_header: PacketType::Pingreq.into(),
44            remain_len: 0,
45        }
46    }
47
48    fn encode(&mut self, buffer: &mut [u8], buffer_len: usize) -> Result<usize, BufferError> {
49        let mut buff_writer = BuffWriter::new(buffer, buffer_len);
50        buff_writer.write_u8(self.fixed_header)?;
51        buff_writer.write_variable_byte_int(0)?;
52        Ok(buff_writer.position)
53    }
54
55    fn decode(&mut self, _buff_reader: &mut BuffReader<'a>) -> Result<(), BufferError> {
56        error!("Pingreq Packet packet does not support decode funtion on client!");
57        Err(BufferError::WrongPacketToDecode)
58    }
59
60    fn set_property_len(&mut self, _value: u32) {
61        error!("PINGREQ packet does not contain any properties!");
62    }
63
64    fn get_property_len(&mut self) -> u32 {
65        error!("PINGREQ packet does not contain any properties!");
66        0
67    }
68
69    fn push_to_properties(&mut self, _property: Property<'a>) {
70        error!("PINGREQ packet does not contain any properties!");
71    }
72
73    fn property_allowed(&mut self, property: &Property<'a>) -> bool {
74        property.pingreq_property()
75    }
76
77    fn set_fixed_header(&mut self, header: u8) {
78        self.fixed_header = header;
79    }
80
81    fn set_remaining_len(&mut self, remaining_len: u32) {
82        self.remain_len = remaining_len;
83    }
84}