diff options
-rw-r--r-- | include/zaprus.h | 9 | ||||
-rw-r--r-- | src/c_api.zig | 41 |
2 files changed, 24 insertions, 26 deletions
diff --git a/include/zaprus.h b/include/zaprus.h index 6856a5e..51ac70c 100644 --- a/include/zaprus.h +++ b/include/zaprus.h @@ -20,13 +20,12 @@ struct SaprusMessage* zaprus_connect(const char* payload, size_t len); struct SaprusMessage { uint16_t packet_type; + uint16_t payload_len; union { struct { struct { char dest[4]; }; - size_t payload_len; - char *payload; } relay; struct { struct { @@ -37,10 +36,10 @@ struct SaprusMessage { char _reserved; char options; }; - size_t payload_len; - char *payload; + } connection; - }; + } headers; + char *payload; }; // ptr should be freed by the caller. diff --git a/src/c_api.zig b/src/c_api.zig index a0343ae..7be260e 100644 --- a/src/c_api.zig +++ b/src/c_api.zig @@ -2,38 +2,38 @@ const c = @cImport({ @cInclude("zaprus.h"); }); -fn zigToCMessage(msg: ?*zaprus.Message) ?*c.SaprusMessage { +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 = .{ + res.headers.relay = .{ .unnamed_0 = .{ .dest = r.header.dest, }, - .payload_len = r.payload.len, - .payload = (allocator.alloc(u8, r.payload.len) catch return null).ptr, - } }; + }; + res.payload_len = @intCast(r.payload.len); + res.payload = (try allocator.alloc(u8, r.payload.len)).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, + res.headers.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), }, }; + + res.payload_len = @intCast(con.payload.len); + res.payload = (try allocator.alloc(u8, con.payload.len)).ptr; }, - else => return null, + .file_transfer => return zaprus.Error.NotImplementedSaprusType, + else => return zaprus.Error.UnknownSaprusType, } return &res; } else return null; @@ -72,7 +72,7 @@ export fn zaprus_send_initial_connection(payload: [*]const u8, len: usize, initi export fn zaprus_connect(payload: [*]const u8, len: usize) ?*c.SaprusMessage { if (SaprusClient.connect(payload[0..len], allocator)) |msg| { - return zigToCMessage(@constCast(&(msg.?))); + return zigToCMessage(@constCast(&(msg.?))) catch null; } else |_| { return null; } @@ -93,7 +93,7 @@ export fn zaprus_message_to_bytes(msg: c.SaprusMessage, ptr: *[*]u8, len: *usize /// 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)); + return zigToCMessage(@constCast(&msg)) catch null; } else |_| return null; } @@ -107,7 +107,6 @@ const std = @import("std"); const zaprus = @import("./root.zig"); const SaprusClient = zaprus.Client; -// const SaprusMessage = zaprus.Message; const allocator = std.heap.c_allocator; |