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

Swift can call C functions directly via a bridging header or module map. No wrapper library needed.

Option 1: Bridging header

Add oxaccel.h to your Xcode project and reference it in Build Settings → Objective-C Bridging Header:
$(PROJECT_DIR)/ThirdParty/oxaccel.h

Option 2: Module map (SPM / frameworks)

Create a module map for the C library:
module COxAccel {
    header "oxaccel.h"
    link "oxaccel"
    export *
}
Link liboxaccel.a (static) or liboxaccel.dylib (shared) in your target’s build settings.

Usage

import Foundation

print("Oxidize Accel SDK v\(String(cString: ox_accel_version()))")

var config = OxAccelConfig(
    api_key: strdup("your-api-key"),
    relay_host: strdup("relay.oxd.sh"),
    relay_port: 51820,
    enable_fec: true,
    enable_compression: false,
    enable_multipath: true
)

guard let ctx = ox_accel_create(&config) else {
    fatalError("Failed to create context")
}
defer {
    ox_accel_destroy(ctx)
    free(UnsafeMutablePointer(mutating: config.api_key))
    free(UnsafeMutablePointer(mutating: config.relay_host))
}

let connectErr = ox_accel_connect(ctx)
guard connectErr == Ok else {
    fatalError("Connect failed: \(connectErr.rawValue)")
}

// Zero-copy send via withUnsafeBytes
let payload = "hello world".data(using: .utf8)!
let sendErr = payload.withUnsafeBytes { buf in
    ox_accel_send(ctx,
                  buf.baseAddress?.assumingMemoryBound(to: UInt8.self),
                  buf.count)
}
guard sendErr == Ok else {
    fatalError("Send failed: \(sendErr.rawValue)")
}

// Recv into pre-allocated buffer
var recvBuf = Data(count: 65536)
var received: Int = 0
let recvErr = recvBuf.withUnsafeMutableBytes { buf in
    ox_accel_recv(ctx,
                  buf.baseAddress?.assumingMemoryBound(to: UInt8.self),
                  buf.count,
                  &received)
}
if recvErr == Ok {
    print("Received \(received) bytes")
}

var stats = AccelStats(packets_sent: 0, packets_recv: 0, bytes_sent: 0, bytes_recv: 0)
ox_accel_stats(ctx, &stats)
print("Sent: \(stats.packets_sent) pkts / \(stats.bytes_sent) bytes")

iOS static linking

For iOS, use liboxaccel.a (static). Add to your target:
  1. Drag liboxaccel.a into Xcode → Frameworks, Libraries, and Embedded Content
  2. Set “Embed” to “Do Not Embed” (static libraries are linked at build time)
  3. Add linker flags in Build Settings → Other Linker Flags: -loxaccel

Performance tips

Swift’s C interop has near-zero overhead (~5-10ns per call). The withUnsafeBytes / withUnsafeMutableBytes pattern gives you a direct pointer to Data’s underlying buffer — no copies.
  • For maximum throughput, use UnsafeRawBufferPointer directly instead of Data
  • Reuse receive buffers across calls to avoid repeated allocation
  • On iOS, use the static library (liboxaccel.a) to avoid framework embedding overhead