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

Kotlin uses the same JNI bridge as Java — see the Java binding for compiling liboxaccel_jni.so. Both languages share one native library.

Usage

import java.nio.ByteBuffer

object OxAccelNative {
    init { System.loadLibrary("oxaccel_jni") }

    @JvmStatic external fun nCreate(
        apiKey: String?, relayHost: String, relayPort: Int,
        fec: Boolean, compression: Boolean, multipath: Boolean
    ): Long
    @JvmStatic external fun nConnect(ctx: Long): Int
    @JvmStatic external fun nSend(ctx: Long, data: ByteBuffer, len: Int): Int
    @JvmStatic external fun nSendBytes(ctx: Long, data: ByteArray, len: Int): Int
    @JvmStatic external fun nRecv(ctx: Long, buf: ByteBuffer, bufLen: Int): Int
    @JvmStatic external fun nStats(ctx: Long): LongArray
    @JvmStatic external fun nDestroy(ctx: Long)
    @JvmStatic external fun nVersion(): String
}

fun main() {
    println("Oxidize Accel SDK v${OxAccelNative.nVersion()}")

    val ctx = OxAccelNative.nCreate(
        apiKey = "your-api-key",
        relayHost = "relay.oxd.sh",
        relayPort = 51820,
        fec = true, compression = false, multipath = true
    )
    check(ctx != 0L) { "Failed to create context" }

    try {
        val err = OxAccelNative.nConnect(ctx)
        check(err == 0) { "Connect failed: $err" }

        // Zero-copy via direct ByteBuffer
        val payload = "hello world".toByteArray()
        val direct = ByteBuffer.allocateDirect(payload.size).put(payload).flip()
        OxAccelNative.nSend(ctx, direct as ByteBuffer, payload.size)

        // Receive
        val recvBuf = ByteBuffer.allocateDirect(65536)
        val received = OxAccelNative.nRecv(ctx, recvBuf, 65536)
        if (received > 0) println("Received $received bytes")

        val stats = OxAccelNative.nStats(ctx)
        println("Sent: ${stats[0]} pkts / ${stats[2]} bytes")
    } finally {
        OxAccelNative.nDestroy(ctx)
    }
}

Android integration

Place native libraries in your project:
app/src/main/jniLibs/
  arm64-v8a/liboxaccel.so
  arm64-v8a/liboxaccel_jni.so
  armeabi-v7a/liboxaccel.so
  armeabi-v7a/liboxaccel_jni.so
  x86_64/liboxaccel.so
  x86_64/liboxaccel_jni.so
Or in build.gradle.kts:
android {
    sourceSets["main"].jniLibs.srcDirs("libs")
}

Coroutine support

The SDK’s send/recv block the calling thread. For coroutine-based code, dispatch to Dispatchers.IO:
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.withContext

suspend fun sendAsync(ctx: Long, data: ByteBuffer, len: Int): Int =
    withContext(Dispatchers.IO) { OxAccelNative.nSend(ctx, data, len) }

suspend fun recvAsync(ctx: Long, buf: ByteBuffer, bufLen: Int): Int =
    withContext(Dispatchers.IO) { OxAccelNative.nRecv(ctx, buf, bufLen) }
Kotlin shares the same JNI library as Java. You only need to compile liboxaccel_jni.so once for both languages.