Table of Contents

Fragmentation

MOSH fragmentation allows a logical message payload to be larger than a single on-wire frame.

Why Fragmentation Exists

The MOSH protocol standard limits the maximum encoded frame size to 1470 bytes. That full frame includes:

That means the largest unfragmented payload is:

1470 - 13 - 2 = 1455 bytes

Any payload larger than 1455 bytes must be split into multiple fragment frames before transmission.

Fragment Format

A fragment is still a normal MOSH frame, but it has the `FLAG_IS_FRAGMENT` bit set and uses the first 4 bytes of its payload as a fragment header:

The remaining payload bytes in the fragment are fragment data.

For a transport with a 1470-byte frame limit:

Send Path

When a frame is sent, MOSH first checks whether the encoded frame would exceed the transport's maximum frame length.

If it fits:

If it does not fit:

This means a large logical payload is transmitted as a sequence of smaller wire-safe frames.

Receive Path

On receipt, each frame is decoded normally.

If the fragment flag is not set:

If the fragment flag is set:

Reassembly Rules

A fragment set is considered valid only if:

If the fragment metadata changes mid-stream, or the final payload length does not match, the reassembly buffer is discarded.

Limits

There are two important limits:

In practice, the true upper limit is also constrained by available RAM, especially on embedded devices such as the ESP32.

Current Behavior

On the current MOSH implementation:

As a result, application code can work with large payloads without needing to manually split or reassemble them.