mirror of
https://git.robbyzambito.me/zaprus
synced 2025-12-20 16:24:50 +00:00
startiing to clean up c api
This commit is contained in:
@@ -11,36 +11,5 @@ int zaprus_send_relay(const char* payload, size_t len, char dest[4]);
|
|||||||
|
|
||||||
int zaprus_send_initial_connection(const char* payload, size_t len, uint16_t initial_port);
|
int zaprus_send_initial_connection(const char* payload, size_t len, uint16_t initial_port);
|
||||||
|
|
||||||
struct SaprusMessage* zaprus_connect(const char* payload, size_t len);
|
void* zaprus_connect(const char* payload, size_t len);
|
||||||
|
|
||||||
// message
|
|
||||||
#define SAPRUS_RELAY_MESSAGE_TYPE 0x003C
|
|
||||||
#define SAPRUS_FILE_TRANSFER_MESSAGE_TYPE 0x8888
|
|
||||||
#define SAPRUS_CONNECTION_MESSAGE_TYPE 0x00E9
|
|
||||||
|
|
||||||
struct SaprusMessage {
|
|
||||||
uint16_t packet_type;
|
|
||||||
uint16_t payload_len;
|
|
||||||
union {
|
|
||||||
struct {
|
|
||||||
char dest[4];
|
|
||||||
} relay;
|
|
||||||
struct {
|
|
||||||
uint16_t src_port;
|
|
||||||
uint16_t dest_port;
|
|
||||||
uint32_t seq_num;
|
|
||||||
uint32_t msg_id;
|
|
||||||
char _reserved;
|
|
||||||
char options;
|
|
||||||
} connection;
|
|
||||||
} headers;
|
|
||||||
char *payload;
|
|
||||||
};
|
|
||||||
|
|
||||||
// ptr should be freed by the caller.
|
|
||||||
int zaprus_message_to_bytes(struct SaprusMessage msg, char** ptr, size_t* len);
|
|
||||||
|
|
||||||
// Return value should be destroyed with zaprus_message_deinit.
|
|
||||||
struct SaprusMessage* zaprus_message_from_bytes(const char* bytes, size_t len);
|
|
||||||
|
|
||||||
void zaprus_message_deinit(struct SaprusMessage* msg);
|
|
||||||
|
|||||||
100
src/c_api.zig
100
src/c_api.zig
@@ -1,64 +1,3 @@
|
|||||||
const c = @cImport({
|
|
||||||
@cInclude("zaprus.h");
|
|
||||||
});
|
|
||||||
|
|
||||||
fn zigToCMessage(msg: zaprus.Message) !c.SaprusMessage {
|
|
||||||
return switch (msg) {
|
|
||||||
.relay => |r| .{
|
|
||||||
.packet_type = @intFromEnum(msg),
|
|
||||||
.payload_len = @intCast(r.payload.len),
|
|
||||||
.headers = .{ .relay = .{
|
|
||||||
.dest = r.header.dest,
|
|
||||||
} },
|
|
||||||
.payload = @constCast(r.payload.ptr),
|
|
||||||
},
|
|
||||||
.connection => |con| .{
|
|
||||||
.packet_type = @intFromEnum(msg),
|
|
||||||
.payload_len = @intCast(con.payload.len),
|
|
||||||
.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),
|
|
||||||
} },
|
|
||||||
.payload = @constCast(con.payload.ptr),
|
|
||||||
},
|
|
||||||
.file_transfer => return zaprus.Error.NotImplementedSaprusType,
|
|
||||||
else => return zaprus.Error.UnknownSaprusType,
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
fn cToZigMessage(msg: c.SaprusMessage) !zaprus.Message {
|
|
||||||
const msg_type: zaprus.PacketType = @enumFromInt(msg.packet_type);
|
|
||||||
return switch (msg_type) {
|
|
||||||
.relay => .{
|
|
||||||
.relay = .{
|
|
||||||
.header = .{
|
|
||||||
.dest = msg.headers.relay.dest[0..4].*,
|
|
||||||
},
|
|
||||||
.payload = msg.payload[0..msg.payload_len],
|
|
||||||
},
|
|
||||||
},
|
|
||||||
.connection => .{
|
|
||||||
.connection = .{
|
|
||||||
.header = .{
|
|
||||||
.src_port = msg.headers.connection.src_port,
|
|
||||||
.dest_port = msg.headers.connection.dest_port,
|
|
||||||
.seq_num = msg.headers.connection.seq_num,
|
|
||||||
.msg_id = msg.headers.connection.msg_id,
|
|
||||||
.reserved = msg.headers.connection._reserved,
|
|
||||||
.options = @bitCast(msg.headers.connection.options),
|
|
||||||
},
|
|
||||||
.payload = msg.payload[0..msg.payload_len],
|
|
||||||
},
|
|
||||||
},
|
|
||||||
.file_transfer => return zaprus.Error.NotImplementedSaprusType,
|
|
||||||
else => return zaprus.Error.UnknownSaprusType,
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
// client
|
// client
|
||||||
export fn zaprus_init() c_int {
|
export fn zaprus_init() c_int {
|
||||||
SaprusClient.init() catch return 1;
|
SaprusClient.init() catch return 1;
|
||||||
@@ -75,49 +14,16 @@ export fn zaprus_send_relay(payload: [*]const u8, len: usize, dest: [*]u8) c_int
|
|||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
export fn zaprus_send_initial_connection(payload: [*]const u8, len: usize, initial_port: u16) c_int {
|
export fn zaprus_connect(payload: [*]const u8, len: usize, output: *SaprusConnection) c_int {
|
||||||
_ = SaprusClient.sendInitialConnection(payload[0..len], initial_port, allocator) catch return 1;
|
output.* = (SaprusClient.connect(payload[0..len], allocator) catch return 1).?;
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
export fn zaprus_connect(payload: [*]const u8, len: usize) ?*c.SaprusMessage {
|
|
||||||
if (SaprusClient.connect(payload[0..len], allocator)) |msg| {
|
|
||||||
var m = zigToCMessage(msg) catch return null;
|
|
||||||
return &m;
|
|
||||||
} 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 {
|
|
||||||
var m = cToZigMessage(msg) catch return 1;
|
|
||||||
const bytes = m.toBytes(allocator) catch return 1;
|
|
||||||
ptr.* = bytes.ptr;
|
|
||||||
len.* = bytes.len;
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
/// 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| {
|
|
||||||
var m = zigToCMessage(msg) catch return null;
|
|
||||||
return &m;
|
|
||||||
} else |_| return null;
|
|
||||||
}
|
|
||||||
|
|
||||||
export fn zaprus_message_deinit(msg: *c.SaprusMessage) void {
|
|
||||||
if (cToZigMessage(msg.*)) |m| {
|
|
||||||
m.deinit(allocator);
|
|
||||||
} else |_| unreachable;
|
|
||||||
}
|
|
||||||
|
|
||||||
const std = @import("std");
|
const std = @import("std");
|
||||||
|
|
||||||
const zaprus = @import("./root.zig");
|
const zaprus = @import("./root.zig");
|
||||||
const SaprusClient = zaprus.Client;
|
const SaprusClient = zaprus.Client;
|
||||||
|
const SaprusConnection = zaprus.Connection;
|
||||||
|
|
||||||
const allocator = std.heap.c_allocator;
|
const allocator = std.heap.c_allocator;
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user