diff options
author | Robby Zambito <contact@robbyzambito.me> | 2025-04-19 23:56:47 -0400 |
---|---|---|
committer | Robby Zambito <contact@robbyzambito.me> | 2025-04-19 23:59:06 -0400 |
commit | fe26cb002db4b7bd9a53265690aba19a54ebe50f (patch) | |
tree | e139cfef34ffef1b5f0d936100a4e412aa5bc342 /src | |
parent | c34748dab3def4dbb1deea7240ab3d4ef17ef161 (diff) |
Initial C api
Diffstat (limited to 'src')
-rw-r--r-- | src/c_api.zig | 56 | ||||
-rw-r--r-- | src/root.zig | 2 |
2 files changed, 58 insertions, 0 deletions
diff --git a/src/c_api.zig b/src/c_api.zig new file mode 100644 index 0000000..bb233e0 --- /dev/null +++ b/src/c_api.zig @@ -0,0 +1,56 @@ +// client + +export fn zaprus_init() c_int { + SaprusClient.init() catch return 1; + return 0; +} + +export fn zaprus_deinit() c_int { + SaprusClient.deinit(); + return 0; +} + +export fn zaprus_send_relay(payload: [*]const u8, len: usize, dest: [4]u8) c_int { + SaprusClient.sendRelay(payload[0..len], dest, allocator) catch return 1; + return 0; +} + +export fn zaprus_send_initial_connection(payload: [*]const u8, len: usize, initial_port: u16) c_int { + SaprusClient.sendInitialConnection(payload[0..len], initial_port, allocator) catch return 1; + return 0; +} + +export fn zaprus_connect(payload: [*]const u8, len: usize) ?*SaprusMessage { + return SaprusClient.connect(payload[0..len], allocator) catch null; +} + +// message + +/// ptr should be freed by the caller. +export fn zaprus_message_to_bytes(msg: SaprusMessage, ptr: *[*]u8, len: *usize) c_int { + const bytes = msg.toBytes(allocator) catch return 1; + ptr.* = bytes[0..].*; + len.* = bytes.len; + return 0; +} + +/// Return value should be destroyed with zaprus_message_deinit. +export fn zaprus_message_from_bytes(bytes: [*]const u8, len: usize) ?*SaprusMessage { + return SaprusMessage.fromBytes(bytes[0..len], allocator) catch null; +} + +export fn zaprus_message_deinit(msg: *SaprusMessage) void { + msg.deinit(allocator); +} + +const std = @import("std"); + +const zaprus = @import("./root.zig"); +const SaprusClient = zaprus.Client; +const SaprusMessage = zaprus.Message; + +const allocator = std.heap.c_allocator; + +test { + std.testing.refAllDeclsRecursively(@This()); +} diff --git a/src/root.zig b/src/root.zig index 5d93578..dfa4984 100644 --- a/src/root.zig +++ b/src/root.zig @@ -1,2 +1,4 @@ pub const Client = @import("Client.zig"); pub usingnamespace @import("message.zig"); + +pub usingnamespace @import("c_api.zig"); |