Keyboard shortcuts

Press ← or → to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Framing codecs

Framing codecs are used by FramedTransport.

What framing means

FramedTransport wraps a lower-level transport implementing AsyncRead and AsyncWrite. That means the transport sends a continuous stream of bytes, which must first be split into frames if we want to decode them into separate messages on the receiving side.

Framing codecs are responsible for this split.

How framing codecs work

Framing codecs differ from normal codecs in that their error type implements NotEnoughData - this tells FramedTransport that the codec needs more bytes before it can decode the incoming message.

/// Codecs implementing this trait can be used in [FramedTransport].
pub trait Framing: DecodeWithMessageLength {}

impl<T> Framing for T
where
    T: Decode + DecodeWithMessageLength,
    <T as Decode>::Error: NotEnoughData,
{
}

Warning

The codec must preserve partial input (resulting in NotEnoughData error) in some internal buffer until a full frame can be decoded - FramedTransport won’t send earlier bytes to the codec again.

Note

DecodeWithMessageLength can be used to obtain the byte length of a message in addition to the decoded message itself. This functionality is currently used only for logging.

Framing schemes

Different frame splitting methods are possible. The dnet library currently includes one: