mirror of
https://git.robbyzambito.me/zaprus
synced 2025-12-20 16:24:50 +00:00
Make C struct match the binary API more closely
Also make the internal conversion function return errors properly
This commit is contained in:
@@ -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;
|
||||
|
||||
|
||||
Reference in New Issue
Block a user