diff options
author | Matthias Beyer <mail@beyermatthias.de> | 2024-03-21 09:57:13 +0100 |
---|---|---|
committer | Matthias Beyer <mail@beyermatthias.de> | 2024-03-21 09:57:13 +0100 |
commit | e4bb196a3b442858557a867a745db2ab3f5f562f (patch) | |
tree | 810cc1b38b819e7fd143858b1a09b181dc706bf9 | |
parent | 45ad8996afaf803684232259746b8da234a0040a (diff) |
Implement MFixedHeader::write()
Co-authored-by: Marcel Müller <neikos@neikos.email>
Signed-off-by: Marcel Müller <neikos@neikos.email>
Signed-off-by: Matthias Beyer <mail@beyermatthias.de>
-rw-r--r-- | mqtt-format/src/v5/fixed_header.rs | 35 |
1 files changed, 35 insertions, 0 deletions
diff --git a/mqtt-format/src/v5/fixed_header.rs b/mqtt-format/src/v5/fixed_header.rs index ed0ba85..b32a790 100644 --- a/mqtt-format/src/v5/fixed_header.rs +++ b/mqtt-format/src/v5/fixed_header.rs @@ -13,6 +13,8 @@ use winnow::error::ParserError; use winnow::Bytes; use winnow::Parser; +use super::write::WResult; +use super::write::WriteMqttPacket; use super::MResult; #[derive(num_enum::TryFromPrimitive, num_enum::IntoPrimitive)] @@ -101,6 +103,39 @@ impl MFixedHeader { Ok(MFixedHeader { packet_type }) } + + pub async fn write<W: WriteMqttPacket>(&self, buffer: &mut W) -> WResult<W> { + let byte = match self.packet_type { + PacketType::Connect => 1 << 4 & 0, + PacketType::Connack => 2 << 4 & 0, + PacketType::Publish { dup, qos, retain } => { + let upper = 3 << 4; + let lower = { + let dup = (dup as u8) << 3; + let qos = (qos as u8) << 1; + let retain = retain as u8; + + dup & qos & retain + }; + + upper & lower + } + PacketType::Puback => 4 << 4 & 0, + PacketType::Pubrec => 5 << 4 & 0, + PacketType::Pubrel => 6 << 4 & 0b0010, + PacketType::Pubcomp => 7 << 4 & 0, + PacketType::Subscribe => 8 << 4 & 0b0010, + PacketType::Suback => 9 << 4 & 0, + PacketType::Unsubscribe => 10 << 4 & 0b0010, + PacketType::Unsuback => 11 << 4 & 0, + PacketType::Pingreq => 12 << 4 & 0, + PacketType::Pingresp => 13 << 4 & 0, + PacketType::Disconnect => 14 << 4 & 0, + PacketType::Auth => 15 << 4 & 0, + }; + + buffer.write_byte(byte).await + } } #[cfg(test)] |