Struct VariedLengthDelimitedCodec

Source
pub struct VariedLengthDelimitedCodec { /* private fields */ }
Expand description

Length delimited bytes-to-frame encoding/decoding scheme that uses first n bits to represent header bytes’ length. (1 <= n <= 3)

The implementation of the codec is similar to tokio_util’s implementation of length_delimited; however, the differentiation is that tokio_util’s implementation uses a fixed type (u32, unless specified otherwise using a builder) for frame length, whereas this crate’s implementation uses varying types (u8 - u32) depending on the length of the frame.

§Limitations

  • This codec allows limited customizations (with_max_frame_length), as it is a minimal implementation; if users would like to customize things such as length_adjustment, length_encoding_endianness, etc., they should use tokio_util::codec::length_delimited::VariedLengthDelimitedCodec instead.
  • This codec can encode and decode frames up to 2^29 bytes (536,870,912 bytes = 536GB), and if users would like to send frames that are greater than 536GB, they should break it into multiple byte objects before encoding with this codec.

§Getting started

§Implementation details

Length byte’s first n bits are reserved to indicate whether or not there exists (n + 1)th length byte (1 <= n <= 3).

§Example 1

Say our frame payload is 100 bytes; it would be wasteful to use 2 or 3 bytes to represent the number 100, when it could just be done in a single byte. In this case, our frame will look like below:

+---- header bytes in binary ----+--------------------------------+
|           0 1100100            |          frame payload         |
+--------------------------------+--------------------------------+

First bit 0 indicates that there is no subsequent byte to represent the frame length, meaning the frame header is a single byte. Subsequent 7 bits makes number value 100 in binary.

Since the first bit is used as a flag, a single header byte can only represent a value up to 127; greater values would need an extra byte to represent it.

§Example 2

In this example, let’s use a frame with payload of 500 bytes. In this case, it will require at least 2 bytes to represent the value:

+---- header bytes in binary ----+--------------------------------+
|       10 000001 11110100       |          frame payload         |
+--------------------------------+--------------------------------+

Firt bit 1 indicates that there exists a second byte to represent the frame length, and the second bit 0 indicates that there doesn’t exist a third byte in the frame header, meaning that the header frame is represented with 2 bytes. Subsequent 14 bits represent the number value 500.

Since the first 2 bits are used as flags, the 2 header bytes can only represent the byte length up to 16,383. Values greater than this would need another byte.

§Example 3

This time, say our frame payload is made of 2,000,000 bytes. In this case, 2 bytes (14 bits more specifically) is not able to capture this value; in this case, it would need at least 3 bytes.

+------ header bytes in binary ------+--------------------------------+
|     110 11110 10000100 10000000    |          frame payload         |
+--------------------------+------------------------------------------+

First two bits 11 indicate that there are at least 3 bytes in the frame header; subsequent bit 0 indicates that there doesn’t exist a 4th byte in the header bytes. And the subsequent 21 bits are used to represent the value 2,000,000. Since 3 bits are used as flags, 3 bytes will only be able to represent values up to 2,097,151 bytes (~2MB).

But what if we wanted to send a frame that was 50GB?

§Example 4

Finally, say we want to send a frame payload of length 50GB = 50,000,000 bytes. In this case, 3 bytes is not even close to enough to represent this value. So, we need 4 bytes for it.

+---------- header bytes in binary -----------+--------------------------------+
|     111 00010 11111010 11110000 10000000    |          frame payload         |
+--------------------------+---------------------------------------------------+

First three bits 111 indicate that there are 4 bytes used in the frame header; in this case, we do not add another flag bit because 4 bytes (32 - 3 = 29 bits) are enough to represent values up to 536,870,911 bytes (536GB), which should be more than enough for most cases.

Implementations§

Source§

impl VariedLengthDelimitedCodec

Source

pub fn new() -> Self

Creates a new VariedLengthDelimitedCodec.

Source

pub fn with_max_frame_length(max_frame_length: usize) -> Self

Returns Self with custom max_frame_length

Defaults to MAX_FRAME_LENGTH = 8 * 1024 * 1024

Source

pub fn into_framed<T>(self, inner: T) -> Framed<T, VariedLengthDelimitedCodec>
where T: AsyncRead + AsyncWrite,

Create a configured length delimited Framed

§Example
use tokio::io::{AsyncRead, AsyncWrite};
use tsyncp::util::frame_codec::VariedLengthDelimitedCodec;

fn write_frame<T: AsyncRead + AsyncWrite>(io: T) {
    let framed = VariedLengthDelimitedCodec::new().into_framed(io);
}

Trait Implementations§

Source§

impl Clone for VariedLengthDelimitedCodec

Source§

fn clone(&self) -> VariedLengthDelimitedCodec

Returns a copy 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 VariedLengthDelimitedCodec

Source§

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

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

impl Decoder for VariedLengthDelimitedCodec

Source§

type Item = BytesMut

The type of decoded frames.
Source§

type Error = CodecError

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

fn decode( &mut self, src: &mut BytesMut, ) -> Result<Option<Self::Item>, CodecError>

Attempts to decode a frame from the provided buffer of bytes. 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 Default for VariedLengthDelimitedCodec

Source§

fn default() -> Self

Returns the “default value” for a type. Read more
Source§

impl Encoder<Bytes> for VariedLengthDelimitedCodec

Source§

type Error = CodecError

The type of encoding errors. Read more
Source§

fn encode(&mut self, data: Bytes, dst: &mut BytesMut) -> Result<(), CodecError>

Encodes a frame into the buffer provided. Read more
Source§

impl Ord for VariedLengthDelimitedCodec

Source§

fn cmp(&self, other: &VariedLengthDelimitedCodec) -> Ordering

This method returns an Ordering between self and other. Read more
1.21.0 · Source§

fn max(self, other: Self) -> Self
where Self: Sized,

Compares and returns the maximum of two values. Read more
1.21.0 · Source§

fn min(self, other: Self) -> Self
where Self: Sized,

Compares and returns the minimum of two values. Read more
1.50.0 · Source§

fn clamp(self, min: Self, max: Self) -> Self
where Self: Sized,

Restrict a value to a certain interval. Read more
Source§

impl PartialEq for VariedLengthDelimitedCodec

Source§

fn eq(&self, other: &VariedLengthDelimitedCodec) -> 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 PartialOrd for VariedLengthDelimitedCodec

Source§

fn partial_cmp(&self, other: &VariedLengthDelimitedCodec) -> Option<Ordering>

This method returns an ordering between self and other values if one exists. Read more
1.0.0 · Source§

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

Tests less than (for self and other) and is used by the < operator. Read more
1.0.0 · Source§

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

Tests less than or equal to (for self and other) and is used by the <= operator. Read more
1.0.0 · Source§

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

Tests greater than (for self and other) and is used by the > operator. Read more
1.0.0 · Source§

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

Tests greater than or equal to (for self and other) and is used by the >= operator. Read more
Source§

impl Eq for VariedLengthDelimitedCodec

Source§

impl StructuralPartialEq for VariedLengthDelimitedCodec

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.