PacketCodec

Struct PacketCodec 

Source
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

Source

pub fn new( inbound_max_size: Option<usize>, outbound_max_size: Option<usize>, ) -> Self

Creates a new PacketCodec with the specified size limits.

Source

pub fn try_decode(&self, dst: &mut BytesMut) -> Result<RawPacket, Error>

Attempts to decode a raw packet from the provided buffer.

Trait Implementations§

Source§

impl Clone for PacketCodec

Source§

fn clone(&self) -> PacketCodec

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for PacketCodec

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Decoder for PacketCodec

Source§

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§

type Item = RawPacket

The type of decoded frames.
Source§

type Error = Error

The type of unrecoverable frame decoding errors. Read more
Source§

fn decode_eof( &mut self, buf: &mut BytesMut, ) -> Result<Option<Self::Item>, Self::Error>

A default method available to be called when there are no more bytes available to be read from the underlying I/O. Read more
Source§

fn framed<T>(self, io: T) -> Framed<T, Self>
where T: AsyncRead + AsyncWrite, Self: Sized,

Provides a Stream and Sink interface for reading and writing to this Io object, using Decode and Encode to read and write the raw data. Read more
Source§

impl<T> Encoder<T> for PacketCodec
where T: Encode,

Source§

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§

type Error = Error

The type of encoding errors. Read more
Source§

impl PartialEq for PacketCodec

Source§

fn eq(&self, other: &PacketCodec) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl Copy for PacketCodec

Source§

impl Eq for PacketCodec

Source§

impl StructuralPartialEq for PacketCodec

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.