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:
@@ -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.
|
||||
|
||||
@@ -2,24 +2,23 @@ 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 = .{
|
||||
res.headers.connection = .{
|
||||
.unnamed_0 = .{
|
||||
.src_port = con.header.src_port,
|
||||
.dest_port = con.header.dest_port,
|
||||
@@ -28,12 +27,13 @@ fn zigToCMessage(msg: ?*zaprus.Message) ?*c.SaprusMessage {
|
||||
._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.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