Table of Contents

Class PacketCompression

Namespace
FishMMO.Shared
Assembly
FishMMO.Shared.dll

Whole-packet compression between the game client and every server, with static Huffman codes trained on real game traffic (PacketCompressionTables). Lossless: the receiver gets exactly the bytes FishNet wrote.

public static class PacketCompression
Inheritance
PacketCompression
Inherited Members

Remarks

Why a static code. Game packets are small (tens to a few hundred bytes): a general compressor has no history to work with inside one, and a shared adaptive model cannot survive unreliable, unordered delivery. A fixed code both peers already hold needs neither: each packet is compressed on its own, with nothing on the wire but its bits.

Wire format. One header byte: RawPacket followed by the packet unchanged, or HuffmanPacket followed by the packet's length (a 7-bit varint) and its code bits. The sender takes whichever is shorter, so a packet never grows by more than MaximumGrowth.

Switched on by the tables. Installed only when both trained tables exist, and then on every peer of the same build (client, login, world and scene servers), so the two ends always agree. Until a table has been trained from a sample (the simulate report's compression lines), nothing is installed and the wire is unchanged.

Fields

HuffmanPacket

Header: the packet's length and its Huffman bits follow.

public const byte HuffmanPacket = 1

Field Value

byte

MaximumGrowth

The most a packet can grow by: the header byte of a raw packet.

public const int MaximumGrowth = 1

Field Value

int

RawPacket

Header: the packet follows unchanged.

public const byte RawPacket = 0

Field Value

byte

Properties

Available

True when this build has trained tables, so compression is installed on every connection.

public static bool Available { get; }

Property Value

bool

ToClients

The code for packets from a server to its clients, or null when untrained.

public static HuffmanCode ToClients { get; }

Property Value

HuffmanCode

ToServer

The code for packets from a client to a server, or null when untrained.

public static HuffmanCode ToServer { get; }

Property Value

HuffmanCode

Methods

Compress(HuffmanCode, ReadOnlySpan<byte>, Span<byte>)

Compresses source into destination (header included), choosing raw when the code would not be shorter. Returns the bytes written; destination must hold source + growth.

public static int Compress(HuffmanCode code, ReadOnlySpan<byte> source, Span<byte> destination)

Parameters

code HuffmanCode
source ReadOnlySpan<byte>
destination Span<byte>

Returns

int

Decompress(HuffmanCode, ReadOnlySpan<byte>, Span<byte>)

Restores a packet written by Compress(HuffmanCode, ReadOnlySpan<byte>, Span<byte>). Returns its length in destination, or -1 for a damaged or unknown packet (dropped by the caller, as a corrupt datagram would be).

public static int Decompress(HuffmanCode code, ReadOnlySpan<byte> source, Span<byte> destination)

Parameters

code HuffmanCode
source ReadOnlySpan<byte>
destination Span<byte>

Returns

int

TryInstall(NetworkManager)

Installs the compression layer on a network manager that has not started yet. Does nothing (and returns false) when the build has no trained tables; true when installed now or already.

public static bool TryInstall(NetworkManager networkManager)

Parameters

networkManager NetworkManager

Returns

bool

VarintLength(int)

Bytes a 7-bit varint of value takes.

public static int VarintLength(int value)

Parameters

value int

Returns

int