use super::property::check_property_type_list;
use super::{Properties, PropertyType, ReasonCode};
use crate::{
ByteArray, DecodeError, DecodePacket, EncodeError, EncodePacket, FixedHeader, Packet,
PacketType, VarIntError,
};
#[allow(clippy::module_name_repetitions)]
#[derive(Clone, Debug, Default, PartialEq, Eq)]
pub struct DisconnectPacket {
reason_code: ReasonCode,
properties: Properties,
}
impl DisconnectPacket {
#[must_use]
pub fn new() -> Self {
Self::default()
}
pub fn set_reason_code(&mut self, reason_code: ReasonCode) -> &mut Self {
self.reason_code = reason_code;
self
}
#[must_use]
pub const fn reason_code(&self) -> ReasonCode {
self.reason_code
}
pub fn properties_mut(&mut self) -> &mut Properties {
&mut self.properties
}
#[must_use]
pub const fn properties(&self) -> &Properties {
&self.properties
}
fn get_fixed_header(&self) -> Result<FixedHeader, VarIntError> {
let remaining_length = ReasonCode::bytes() + self.properties.bytes();
FixedHeader::new(PacketType::Disconnect, remaining_length)
}
}
pub const DISCONNECT_REASONS: &[ReasonCode] = &[
ReasonCode::Success,
ReasonCode::DisconnectWithWillMessage,
ReasonCode::UnspecifiedError,
ReasonCode::MalformedPacket,
ReasonCode::ProtocolError,
ReasonCode::ImplementationSpecificError,
ReasonCode::NotAuthorized,
ReasonCode::ServerBusy,
ReasonCode::ServerShuttingDown,
ReasonCode::KeepAliveTimeout,
ReasonCode::SessionTakenOver,
ReasonCode::TopicFilterInvalid,
ReasonCode::TopicNameInvalid,
ReasonCode::ReceiveMaximumExceeded,
ReasonCode::TopicAliasInvalid,
];
pub const DISCONNECT_PROPERTIES: &[PropertyType] = &[
PropertyType::SessionExpiryInterval,
PropertyType::ReasonString,
PropertyType::UserProperty,
PropertyType::ServerReference,
];
impl EncodePacket for DisconnectPacket {
fn encode(&self, buf: &mut Vec<u8>) -> Result<usize, EncodeError> {
let old_len = buf.len();
let fixed_header = self.get_fixed_header()?;
fixed_header.encode(buf)?;
self.reason_code.encode(buf)?;
self.properties.encode(buf)?;
Ok(buf.len() - old_len)
}
}
impl DecodePacket for DisconnectPacket {
fn decode(ba: &mut ByteArray) -> Result<Self, DecodeError> {
let fixed_header = FixedHeader::decode(ba)?;
if fixed_header.packet_type() != PacketType::Disconnect {
return Err(DecodeError::InvalidPacketType);
}
if fixed_header.remaining_length() == 0 {
return Ok(Self::default());
}
let reason_code = ReasonCode::decode(ba)?;
if !DISCONNECT_REASONS.contains(&reason_code) {
log::error!("Invalid reason code {:?}", reason_code);
return Err(DecodeError::InvalidReasonCode);
}
let properties = Properties::decode(ba)?;
if let Err(property_type) =
check_property_type_list(properties.props(), DISCONNECT_PROPERTIES)
{
log::error!(
"v5/DisconnectPacket: property type {:?} cannot be used in properties!",
property_type
);
return Err(DecodeError::InvalidPropertyType);
}
Ok(Self {
reason_code,
properties,
})
}
}
impl Packet for DisconnectPacket {
fn packet_type(&self) -> PacketType {
PacketType::Disconnect
}
fn bytes(&self) -> Result<usize, VarIntError> {
let fixed_header = self.get_fixed_header()?;
Ok(fixed_header.bytes() + fixed_header.remaining_length())
}
}