pub struct PacketCodec { /* private fields */ }Expand description
A codec for encoding and decoding MQTT packets.
The PacketCodec struct implements the Encoder and Decoder traits from the
tokio_util::codec module, allowing it to be used with asynchronous I/O frameworks.
§Examples
use mqute_codec::codec::PacketCodec;
use mqute_codec::protocol::{FixedHeader, PacketType};
use bytes::BytesMut;
let mut codec = PacketCodec::new(Some(1024), Some(1024));
let mut buffer = BytesMut::from(&[0x30, 0x02, 0x00, 0x01][..]); // Example raw packet
let packet = codec.try_decode(&mut buffer).unwrap();Implementations§
Source§impl PacketCodec
impl PacketCodec
Trait Implementations§
Source§impl Clone for PacketCodec
impl Clone for PacketCodec
Source§fn clone(&self) -> PacketCodec
fn clone(&self) -> PacketCodec
Returns a duplicate of the value. Read more
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
Performs copy-assignment from
source. Read moreSource§impl Debug for PacketCodec
impl Debug for PacketCodec
Source§impl Decoder for PacketCodec
impl Decoder for PacketCodec
Source§fn decode(
&mut self,
src: &mut BytesMut,
) -> Result<Option<Self::Item>, Self::Error>
fn decode( &mut self, src: &mut BytesMut, ) -> Result<Option<Self::Item>, Self::Error>
Decodes a raw packet from the provided buffer.
§Examples
use mqute_codec::codec::PacketCodec;
use mqute_codec::protocol::PacketType;
use tokio_util::codec::Decoder;
use bytes::BytesMut;
let mut codec = PacketCodec::new(Some(1024), Some(1024));
let mut buffer = BytesMut::from(&[0x30, 0x02, 0x00, 0x01][..]); // Example raw packet
let packet = codec.decode(&mut buffer).unwrap().unwrap();
assert_eq!(packet.header.packet_type(), PacketType::Publish);
assert_eq!(packet.payload.len(), 2);Source§impl<T> Encoder<T> for PacketCodecwhere
T: Encode,
impl<T> Encoder<T> for PacketCodecwhere
T: Encode,
Source§fn encode(&mut self, item: T, dst: &mut BytesMut) -> Result<(), Self::Error>
fn encode(&mut self, item: T, dst: &mut BytesMut) -> Result<(), Self::Error>
Encodes an item into the provided buffer.
§Examples
use mqute_codec::codec::{PacketCodec, Encode};
use tokio_util::codec::Encoder;
use bytes::BytesMut;
use mqute_codec::Error;
struct MyPacket;
impl Encode for MyPacket {
fn encode(&self, dst: &mut BytesMut) -> Result<(), Error> {
dst.extend_from_slice(&[0x30, 0x02, 0x00, 0x01]);
Ok(())
}
fn payload_len(&self) -> usize {
4
}
}
let mut codec = PacketCodec::new(Some(1024), Some(1024));
let mut buffer = BytesMut::new();
let packet = MyPacket {};
codec.encode(packet, &mut buffer).unwrap();
assert_eq!(buffer.as_ref(), &[0x30, 0x02, 0x00, 0x01]);Source§impl PartialEq for PacketCodec
impl PartialEq for PacketCodec
impl Copy for PacketCodec
impl Eq for PacketCodec
impl StructuralPartialEq for PacketCodec
Auto Trait Implementations§
impl Freeze for PacketCodec
impl RefUnwindSafe for PacketCodec
impl Send for PacketCodec
impl Sync for PacketCodec
impl Unpin for PacketCodec
impl UnwindSafe for PacketCodec
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more