Skip to main content

Documentation Index

Fetch the complete documentation index at: https://docs.oxd.sh/llms.txt

Use this file to discover all available pages before exploring further.

Setup

  1. Place oxaccel.dll (Windows) or liboxaccel.dylib (macOS) in Assets/Plugins/
  2. Unity auto-discovers native plugins from this directory

P/Invoke declarations

using System;
using System.Runtime.InteropServices;

public static class OxAccel
{
    [StructLayout(LayoutKind.Sequential)]
    public struct Config
    {
        public IntPtr api_key;
        public IntPtr relay_host;
        public ushort relay_port;
        [MarshalAs(UnmanagedType.U1)] public bool enable_fec;
        [MarshalAs(UnmanagedType.U1)] public bool enable_compression;
        [MarshalAs(UnmanagedType.U1)] public bool enable_multipath;
    }

    [StructLayout(LayoutKind.Sequential)]
    public struct Stats
    {
        public ulong packets_sent;
        public ulong packets_recv;
        public ulong bytes_sent;
        public ulong bytes_recv;
    }

    [DllImport("oxaccel")] public static extern IntPtr ox_accel_create(ref Config config);
    [DllImport("oxaccel")] public static extern int ox_accel_connect(IntPtr ctx);
    [DllImport("oxaccel")] public static extern void ox_accel_destroy(IntPtr ctx);
    [DllImport("oxaccel")] public static extern int ox_accel_send(IntPtr ctx, byte[] data, nuint len);
    [DllImport("oxaccel")] public static extern int ox_accel_recv(IntPtr ctx, byte[] buf, nuint bufLen, out nuint outLen);
    [DllImport("oxaccel")] public static extern int ox_accel_stats(IntPtr ctx, out Stats stats);
    [DllImport("oxaccel")] public static extern IntPtr ox_accel_version();
}

Usage in a MonoBehaviour

using UnityEngine;
using System;
using System.Runtime.InteropServices;

public class AccelNetwork : MonoBehaviour
{
    private IntPtr _ctx;

    void Start()
    {
        var apiKey = Marshal.StringToHGlobalAnsi("your-api-key");
        var relayHost = Marshal.StringToHGlobalAnsi("relay.oxd.sh");

        var config = new OxAccel.Config
        {
            api_key = apiKey,
            relay_host = relayHost,
            relay_port = 51820,
            enable_fec = true,
            enable_compression = true,
            enable_multipath = true,
        };

        _ctx = OxAccel.ox_accel_create(ref config);
        Marshal.FreeHGlobal(apiKey);
        Marshal.FreeHGlobal(relayHost);

        if (_ctx == IntPtr.Zero)
        {
            Debug.LogError("Failed to create Accel context");
            return;
        }

        int err = OxAccel.ox_accel_connect(_ctx);
        if (err != 0)
            Debug.LogError($"Accel connect failed: {err}");
    }

    public void Send(byte[] data)
    {
        OxAccel.ox_accel_send(_ctx, data, (nuint)data.Length);
    }

    public byte[] Receive()
    {
        var buf = new byte[65536];
        int err = OxAccel.ox_accel_recv(_ctx, buf, (nuint)buf.Length, out nuint received);
        if (err != 0) return null;

        var result = new byte[received];
        Array.Copy(buf, result, (int)received);
        return result;
    }

    void OnDestroy()
    {
        if (_ctx != IntPtr.Zero)
        {
            OxAccel.ox_accel_destroy(_ctx);
            _ctx = IntPtr.Zero;
        }
    }
}
For IL2CPP builds (iOS, consoles), use the static library (liboxaccel.a) instead. Add it to Xcode via the Unity post-build script.