mirror of
https://git.robbyzambito.me/zaprus
synced 2025-12-21 16:54:49 +00:00
117 lines
3.4 KiB
Zig
117 lines
3.4 KiB
Zig
const c = @cImport({
|
|
@cInclude("zaprus.h");
|
|
});
|
|
|
|
fn zigToCMessage(msg: ?*zaprus.Message) ?*c.SaprusMessage {
|
|
if (msg) |m| {
|
|
var res = c.SaprusMessage{
|
|
.packet_type = @intFromEnum(m.*),
|
|
};
|
|
switch (m.*) {
|
|
.relay => |r| {
|
|
res.unnamed_0 = .{ .relay = .{
|
|
.unnamed_0 = .{
|
|
.dest = r.header.dest,
|
|
},
|
|
.payload_len = r.payload.len,
|
|
.payload = (allocator.alloc(u8, r.payload.len) catch return null).ptr,
|
|
} };
|
|
},
|
|
.connection => |con| {
|
|
res.unnamed_0 = .{
|
|
.connection = .{
|
|
.unnamed_0 = .{
|
|
.src_port = con.header.src_port,
|
|
.dest_port = con.header.dest_port,
|
|
.seq_num = con.header.seq_num,
|
|
.msg_id = con.header.msg_id,
|
|
._reserved = con.header.reserved,
|
|
.options = @bitCast(con.header.options),
|
|
},
|
|
.payload_len = con.payload.len,
|
|
.payload = (allocator.alloc(u8, con.payload.len) catch return null).ptr,
|
|
},
|
|
};
|
|
},
|
|
else => return null,
|
|
}
|
|
return &res;
|
|
} else return null;
|
|
}
|
|
|
|
fn cToZigMessage(msg: ?*c.SaprusMessage) ?*zaprus.Message {
|
|
_ = msg;
|
|
return @constCast(&zaprus.Message{
|
|
.relay = .{
|
|
.header = .{ .dest = @splat(0) },
|
|
.payload = &.{0},
|
|
},
|
|
});
|
|
}
|
|
|
|
// 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: [*]u8) c_int {
|
|
SaprusClient.sendRelay(payload[0..len], dest[0..4].*, 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) ?*c.SaprusMessage {
|
|
if (SaprusClient.connect(payload[0..len], allocator)) |msg| {
|
|
return zigToCMessage(@constCast(&(msg.?)));
|
|
} else |_| {
|
|
return null;
|
|
}
|
|
}
|
|
|
|
// message
|
|
|
|
/// ptr should be freed by the caller.
|
|
export fn zaprus_message_to_bytes(msg: c.SaprusMessage, ptr: *[*]u8, len: *usize) c_int {
|
|
if (cToZigMessage(@constCast(&msg))) |m| {
|
|
const bytes = m.toBytes(allocator) catch return 1;
|
|
ptr.* = bytes.ptr;
|
|
len.* = bytes.len;
|
|
return 0;
|
|
} else return 1;
|
|
}
|
|
|
|
/// Return value should be destroyed with zaprus_message_deinit.
|
|
export fn zaprus_message_from_bytes(bytes: [*]const u8, len: usize) ?*c.SaprusMessage {
|
|
if (zaprus.Message.fromBytes(bytes[0..len], allocator)) |msg| {
|
|
return zigToCMessage(@constCast(&msg));
|
|
} else |_| return null;
|
|
}
|
|
|
|
export fn zaprus_message_deinit(msg: *c.SaprusMessage) void {
|
|
// noop
|
|
_ = msg;
|
|
// 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());
|
|
}
|