Skip to content

Commit

Permalink
[serialize] move short disconnect to disconnect.rs where it belongs
Browse files Browse the repository at this point in the history
Signed-off-by: Jurek <[email protected]>
  • Loading branch information
dscso committed May 21, 2024
1 parent d500ad2 commit 4e686ba
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 10 deletions.
13 changes: 12 additions & 1 deletion mqtt-format/src/v5/packets/disconnect.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down
9 changes: 0 additions & 9 deletions mqtt-format/src/v5/packets/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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)?;
}
Expand Down

0 comments on commit 4e686ba

Please sign in to comment.