CompressionMethod Enumeration

Compression types for data compression.

Definition

Namespace: Alteruna.Multiplayer.Core.PacketProcessing
Assembly: Alteruna (in Alteruna.dll) Version: 2.0.1+a1176e08a0b4a6bfd8fefeddde6163a16d29e5ab
C#
public enum CompressionMethod

Example

Here is an example of how to use compression in a Synchronizable.
C#
using Alteruna;
using UnityEngine;
public class SyncCompressed : Synchronizable
{
    public string SData = "Hello, World!";
    private string _oldSData;

    public byte[] Data = new byte[512];
    private byte[] _oldData;

    // Set the old data to the current data so that only new data is sent.
    private void Start()
    {
        _oldData = Data;
        _oldSData = SData;
    }

    // Check if the data has changed, and if so, commit it.
    private void Update()
    {
        if (Data != _oldData)
        {
            _oldData = Data;
            Commit();
        }
        else if (SData != _oldSData)
        {
            _oldSData = SData;
            Commit();
        }

        SyncUpdate();
    }

    // This is called when the data is being sent to the server.
    public override void AssembleData(Writer writer, byte LOD)
    {
        // Mark the start of the compressed data
        writer.StartCompress();

        // Write the data
        writer.Write(SData);
        writer.Write(Data);

        // Compress the data
        var data = writer.EndCompress(CompressionMethod.NibbleZeroIndicator);

        Debug.Log("We saved " + data + " bytes by compressing the data!");
    }

    // This is called when the data is being received from the server.
    public override void DisassembleData(Reader reader, byte LOD)
    {
        // Decompress the data.
        reader.Decompress(CompressionMethod.NibbleZeroIndicator);

        // Read the string data.
        SData = reader.ReadString();
        _oldSData = SData;

        // Read the byte array data.
        Data = reader.ReadByteArray();
        _oldData = Data;
    }
}

Members

None0 No compression.
GZip1 Zip compression. Less effective on small amounts of data.
RunLengthEncoding2 Run-Length Encoding (RLE) compression. Effective form compressing data with many repeating bytes.
Base643 Base64 encoding.
NibbleZeroIndicator4 Nibble Zero Indicator (NZI) compression. Inefficient for compressing data similar to ASCII and UTF8 strings. Works as well on small amounts of data as it does on large amounts of data.

See Also