diff options
Diffstat (limited to 'src/c_api.zig')
| -rw-r--r-- | src/c_api.zig | 116 |
1 files changed, 3 insertions, 113 deletions
diff --git a/src/c_api.zig b/src/c_api.zig index d3ad168..11ff763 100644 --- a/src/c_api.zig +++ b/src/c_api.zig @@ -1,81 +1,3 @@ -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.headers.relay = .{ - .dest = r.header.dest, - }; - res.payload_len = @intCast(r.payload.len); - res.payload = (try allocator.alloc(u8, r.payload.len)).ptr; - }, - .connection => |con| { - res.headers.connection = .{ - .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), - }; - - res.payload_len = @intCast(con.payload.len); - res.payload = (try allocator.alloc(u8, con.payload.len)).ptr; - }, - .file_transfer => return zaprus.Error.NotImplementedSaprusType, - else => return zaprus.Error.UnknownSaprusType, - } - return &res; - } else return null; -} - -fn cToZigMessage(msg: ?*c.SaprusMessage) !?*zaprus.Message { - if (msg) |m| { - const msg_type: zaprus.PacketType = @enumFromInt(m.*.packet_type); - var res: zaprus.Message = switch (msg_type) { - .relay => .{ - .relay = .{ - .header = .{ - .dest = m.*.headers.relay.dest[0..4].*, - }, - .payload = blk: { - const p = try allocator.alloc(u8, m.payload_len); - @memcpy(p, m.payload[0..m.payload_len]); - break :blk p; - }, - }, - }, - .connection => .{ - .connection = .{ - .header = .{ - .src_port = m.*.headers.connection.src_port, - .dest_port = m.*.headers.connection.dest_port, - .seq_num = m.*.headers.connection.seq_num, - .msg_id = m.*.headers.connection.msg_id, - .reserved = m.*.headers.connection._reserved, - .options = @bitCast(m.*.headers.connection.options), - }, - .payload = blk: { - const p = try allocator.alloc(u8, m.payload_len); - @memcpy(p, m.payload[0..m.payload_len]); - break :blk p; - }, - }, - }, - .file_transfer => return zaprus.Error.NotImplementedSaprusType, - else => return zaprus.Error.UnknownSaprusType, - }; - - return &res; - } else return null; -} - // client export fn zaprus_init() c_int { SaprusClient.init() catch return 1; @@ -92,48 +14,16 @@ export fn zaprus_send_relay(payload: [*]const u8, len: usize, dest: [*]u8) c_int 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; +export fn zaprus_connect(payload: [*]const u8, len: usize, output: *SaprusConnection) c_int { + output.* = (SaprusClient.connect(payload[0..len], 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.?))) catch null; - } 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)) catch return 1) |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)) catch null; - } 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 SaprusConnection = zaprus.Connection; const allocator = std.heap.c_allocator; |
