Skip to main content

Module framing

Module framing 

Source
Expand description

Binary framing: message ids, fragmentation and reassembly.

After the Noise handshake every application message is one Noise transport message. Its plaintext starts with a one-byte binary message id: 0 carries a UTF-8 JSON body, 1 is the fragmentation channel, and each role owns its own ids (for example 4 for player audio). MAX_PLAINTEXT_LEN bounds one plaintext, so a larger payload has to be split.

Fragmentation keeps the byte layout of the published protocol:

first fragment   [1][flags with the first bit][original message id][data]
later fragments  [1][flags][data]

Bit 0 of the flags byte marks the last fragment, bit 1 the first one, and bits 2-7 must be zero. Only one fragmented message may be in flight per direction, so the receiver appends every fragment to one buffer and dispatches the result when the last flag arrives. Reassembler does that for a session reading frame by frame; reassemble_binary does it for a sequence already in hand. This module does no I/O.

Re-exports§

pub use crate::noise::MAX_PLAINTEXT_LEN;

Structs§

Reassembler
Incremental reassembler for one direction of a connection.

Enums§

Frame
One plaintext frame, classified by its leading message id.

Constants§

BINARY_MESSAGE_ID_FRAGMENT
Binary message id of a fragment.
BINARY_MESSAGE_ID_JSON
Binary message id of a JSON message body.
FRAGMENT_FLAGS_RESERVED
Fragment flag bits that are reserved and must be zero.
FRAGMENT_FLAG_FIRST
Fragment flag marking the first fragment of a message.
FRAGMENT_FLAG_LAST
Fragment flag marking the last fragment of a message.
MAX_REASSEMBLED_LEN
Largest reassembled message, bounding the buffer a peer can make the receiver allocate.

Functions§

decode_frame
Decodes one Noise transport plaintext into a Frame.
encode_binary
Encodes one binary message: the id byte followed by the payload.
max_audio_payload
Largest encoded-audio payload that fits one frame without fragmentation.
reassemble_binary
Reassembles a complete sequence of frames into the original message.
split_binary
Splits a binary message into plaintext frames that each fit max_plaintext.