CompressionMethod Class

Compression types for data compression.

Definition

Namespace: Alteruna.Trinity.PacketProcessing
Assembly: Alteruna.Trinity (in Alteruna.Trinity.dll) Version: 1.3.4
C#
public sealed class CompressionMethod : Enum
Inheritance
Object    ValueType    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;
    }
}

Fields

Base64 Base64 encoding.
GZip Zip compression. Less effective on small amounts of data.
NibbleZeroIndicator 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.
None No compression.
RunLengthEncoding Run-Length Encoding (RLE) compression. Effective form compressing data with many repeating bytes.
value__ 

See Also