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.
Public constants
Section titled “Public constants”| 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. |
Create and write APIs
Section titled “Create and write APIs”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.
Read APIs
Section titled “Read APIs”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.
Delivery mapping
Section titled “Delivery mapping”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.
Integration responsibilities
Section titled “Integration responsibilities”A custom transport must:
- Connect to the relay with the configured connection key.
- Send control and data frames on relay wire channel
0. - Track the virtual client IDs reported through
ConnectedandDisconnected. - Preserve the source or target virtual ID when converting game-framework packets.
- Acknowledge
HostPromoted, reconnect, and sendHostClaimwithin 10 seconds. - Surface
HostUnavailable,HostAvailable, andErrorto the game lifecycle. - Respect the underlying transport’s MTU for unreliable payloads.
See UDP protocol for the byte-level frames and state transitions.