diff options
author | Jurek <git@jurek.io> | 2024-05-21 22:22:00 +0200 |
---|---|---|
committer | Jurek <git@jurek.io> | 2024-05-21 22:22:00 +0200 |
commit | 4e686babc2c36726c45de0a4e71d39cccaa18c9d (patch) | |
tree | 3e82f9fcc33f5628ec6deb3029ec8beaa96d2704 | |
parent | d500ad2b72f693c331b2b467d305206de98b0213 (diff) |
[serialize] move short disconnect to disconnect.rs where it belongs
Signed-off-by: Jurek <git@jurek.io>
-rw-r--r-- | mqtt-format/src/v5/packets/disconnect.rs | 13 | ||||
-rw-r--r-- | mqtt-format/src/v5/packets/mod.rs | 9 |
2 files changed, 12 insertions, 10 deletions
diff --git a/mqtt-format/src/v5/packets/disconnect.rs b/mqtt-format/src/v5/packets/disconnect.rs index ea275cd..a433428 100644 --- a/mqtt-format/src/v5/packets/disconnect.rs +++ b/mqtt-format/src/v5/packets/disconnect.rs @@ -102,10 +102,21 @@ impl<'i> MDisconnect<'i> { } pub fn binary_size(&self) -> u32 { + if self.is_short_packet() { + return 0; + } self.reason_code.binary_size() + self.properties.binary_size() } - + #[inline] + fn is_short_packet(&self) -> bool { + // if reason code is NormalDisconnection AND properties are empty, we can skip writing the payload + self.reason_code == DisconnectReasonCode::NormalDisconnection + && self.properties == DisconnectProperties::new() + } pub fn write<W: WriteMqttPacket>(&self, buffer: &mut W) -> WResult<W> { + if self.is_short_packet() { + return Ok(()); + } self.reason_code.write(buffer)?; self.properties.write(buffer) } diff --git a/mqtt-format/src/v5/packets/mod.rs b/mqtt-format/src/v5/packets/mod.rs index 55b39b8..9c7172a 100644 --- a/mqtt-format/src/v5/packets/mod.rs +++ b/mqtt-format/src/v5/packets/mod.rs @@ -29,7 +29,6 @@ use super::write::WResult; use super::write::WriteMqttPacket; use crate::v5::fixed_header::MFixedHeader; use crate::v5::packets::connack::MConnack; -use crate::v5::packets::disconnect::{DisconnectProperties, DisconnectReasonCode}; use crate::v5::MResult; pub mod auth; @@ -163,14 +162,6 @@ impl<'i> MqttPacket<'i> { packet_type: PacketType::Disconnect, }; fixed_header.write(buffer)?; - // if reason code is NormalDisconnection AND properties are empty, we can skip writing the payload - if p.reason_code == DisconnectReasonCode::NormalDisconnection - && p.properties == DisconnectProperties::new() - { - // writing length of packet 0 - crate::v5::integers::write_variable_u32(buffer, 0)?; - return Ok(()); - } crate::v5::integers::write_variable_u32(buffer, p.binary_size())?; p.write(buffer)?; } |