Skip to content

Protocol library

BlitzRelay.Protocol targets .NET Standard 2.1 and contains the wire-format types used by the server and official Unity transports. It has no networking loop; your transport is responsible for connecting, sending, receiving, and mapping delivery methods.

Constant Value Meaning
MessageCodec.RelayWireChannel 0 LiteNetLib channel used by every relay message.
MessageCodec.BroadcastVirtualClientId -1 Host-data target for every client in the room.
MessageCodec.ClientDataHeaderSize 2 Type and game-channel bytes.
MessageCodec.HostDataHeaderSize 6 Type, virtual client ID, and game-channel bytes.
MessageCodec.MaxRelayHeaderSize 6 Largest relay header added to a game payload.

Every message with variable data has a Create* method that returns a new byte array and a Write* method that writes into a caller-owned Span<byte>.

byte[] simple = MessageCodec.CreateClientJoin(roomCode);
int frameLength = MessageCodec.HostDataHeaderSize + gamePayload.Length;
Span<byte> frame = stackalloc byte[frameLength];
int written = MessageCodec.WriteHostData(
frame,
MessageCodec.BroadcastVirtualClientId,
(byte)GameChannel.Unreliable,
gamePayload);

Write* returns the number of bytes written and throws ArgumentException when the destination is too small. Numeric values that do not fit their wire field throw during checked conversion.

TryRead* methods validate the minimum frame size and expected message type before returning parsed values. Read* methods assume the caller has already selected and validated the message.

if (!MessageCodec.TryReadMessageType(packet, out MessageType type)) return;
if (type == MessageType.Data &&
MessageCodec.TryReadClientDataSpan(
packet,
out byte gameChannel,
out ReadOnlySpan<byte> gamePayload))
{
// Consume gamePayload before the packet's backing memory is recycled.
}

Use TryReadHostDataSpan and TryReadClientDataSpan on receive hot paths to avoid allocating a new game-payload array. The older TryReadHostData and TryReadClientData overloads return copied byte[] payloads.

GameChannel.Reliable is byte 0; GameChannel.Unreliable is byte 1. MessageCodec.IsUnreliableGameChannel(byte) recognises byte 1.

Send a data frame with reliable ordered transport delivery when it encodes channel 0, and unreliable transport delivery when it encodes channel 1. The server ignores a data frame when the encoded game channel and LiteNetLib delivery method do not match.

Control messages should use reliable ordered delivery.

A custom transport must:

  1. Connect to the relay with the configured connection key.
  2. Send control and data frames on relay wire channel 0.
  3. Track the virtual client IDs reported through Connected and Disconnected.
  4. Preserve the source or target virtual ID when converting game-framework packets.
  5. Acknowledge HostPromoted, reconnect, and send HostClaim within 10 seconds.
  6. Surface HostUnavailable, HostAvailable, and Error to the game lifecycle.
  7. Respect the underlying transport’s MTU for unreliable payloads.

See UDP protocol for the byte-level frames and state transitions.