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 {
|
struct SaprusMessage {
|
||||||
uint16_t packet_type;
|
uint16_t packet_type;
|
||||||
|
uint16_t payload_len;
|
||||||
union {
|
union {
|
||||||
struct {
|
struct {
|
||||||
struct {
|
struct {
|
||||||
char dest[4];
|
char dest[4];
|
||||||
};
|
};
|
||||||
size_t payload_len;
|
|
||||||
char *payload;
|
|
||||||
} relay;
|
} relay;
|
||||||
struct {
|
struct {
|
||||||
struct {
|
struct {
|
||||||
@@ -37,10 +36,10 @@ struct SaprusMessage {
|
|||||||
char _reserved;
|
char _reserved;
|
||||||
char options;
|
char options;
|
||||||
};
|
};
|
||||||
size_t payload_len;
|
|
||||||
char *payload;
|
|
||||||
} connection;
|
} connection;
|
||||||
};
|
} headers;
|
||||||
|
char *payload;
|
||||||
};
|
};
|
||||||
|
|
||||||
// ptr should be freed by the caller.
|
// ptr should be freed by the caller.
|
||||||
|
|||||||
@@ -2,24 +2,23 @@ const c = @cImport({
|
|||||||
@cInclude("zaprus.h");
|
@cInclude("zaprus.h");
|
||||||
});
|
});
|
||||||
|
|
||||||
fn zigToCMessage(msg: ?*zaprus.Message) ?*c.SaprusMessage {
|
fn zigToCMessage(msg: ?*zaprus.Message) !?*c.SaprusMessage {
|
||||||
if (msg) |m| {
|
if (msg) |m| {
|
||||||
var res = c.SaprusMessage{
|
var res = c.SaprusMessage{
|
||||||
.packet_type = @intFromEnum(m.*),
|
.packet_type = @intFromEnum(m.*),
|
||||||
};
|
};
|
||||||
switch (m.*) {
|
switch (m.*) {
|
||||||
.relay => |r| {
|
.relay => |r| {
|
||||||
res.unnamed_0 = .{ .relay = .{
|
res.headers.relay = .{
|
||||||
.unnamed_0 = .{
|
.unnamed_0 = .{
|
||||||
.dest = r.header.dest,
|
.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| {
|
.connection => |con| {
|
||||||
res.unnamed_0 = .{
|
res.headers.connection = .{
|
||||||
.connection = .{
|
|
||||||
.unnamed_0 = .{
|
.unnamed_0 = .{
|
||||||
.src_port = con.header.src_port,
|
.src_port = con.header.src_port,
|
||||||
.dest_port = con.header.dest_port,
|
.dest_port = con.header.dest_port,
|
||||||
@@ -28,12 +27,13 @@ fn zigToCMessage(msg: ?*zaprus.Message) ?*c.SaprusMessage {
|
|||||||
._reserved = con.header.reserved,
|
._reserved = con.header.reserved,
|
||||||
.options = @bitCast(con.header.options),
|
.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;
|
return &res;
|
||||||
} else return null;
|
} 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 {
|
export fn zaprus_connect(payload: [*]const u8, len: usize) ?*c.SaprusMessage {
|
||||||
if (SaprusClient.connect(payload[0..len], allocator)) |msg| {
|
if (SaprusClient.connect(payload[0..len], allocator)) |msg| {
|
||||||
return zigToCMessage(@constCast(&(msg.?)));
|
return zigToCMessage(@constCast(&(msg.?))) catch null;
|
||||||
} else |_| {
|
} else |_| {
|
||||||
return null;
|
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.
|
/// Return value should be destroyed with zaprus_message_deinit.
|
||||||
export fn zaprus_message_from_bytes(bytes: [*]const u8, len: usize) ?*c.SaprusMessage {
|
export fn zaprus_message_from_bytes(bytes: [*]const u8, len: usize) ?*c.SaprusMessage {
|
||||||
if (zaprus.Message.fromBytes(bytes[0..len], allocator)) |msg| {
|
if (zaprus.Message.fromBytes(bytes[0..len], allocator)) |msg| {
|
||||||
return zigToCMessage(@constCast(&msg));
|
return zigToCMessage(@constCast(&msg)) catch null;
|
||||||
} else |_| return null;
|
} else |_| return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -107,7 +107,6 @@ const std = @import("std");
|
|||||||
|
|
||||||
const zaprus = @import("./root.zig");
|
const zaprus = @import("./root.zig");
|
||||||
const SaprusClient = zaprus.Client;
|
const SaprusClient = zaprus.Client;
|
||||||
// const SaprusMessage = zaprus.Message;
|
|
||||||
|
|
||||||
const allocator = std.heap.c_allocator;
|
const allocator = std.heap.c_allocator;
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user