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
NotEnoughDataerror) in some internal buffer until a full frame can be decoded -FramedTransportwon’t send earlier bytes to the codec again.
Note
DecodeWithMessageLengthcan 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:
length-delimited codec- which writes the length of each frame in bytes at the beginning of the frame.