8 Commits

Author SHA1 Message Date
87c9d921d4 startiing to clean up c api 2025-04-30 17:04:32 -04:00
f212454dfb Big changes to the C api implementations
Should map directly to the zig struct instead of mallocing
2025-04-27 18:03:06 -04:00
983544facf Add C to Zig converter
Further simplify struct
2025-04-27 18:03:06 -04:00
67818ed9d6 Make C struct match the binary API more closely
Also make the internal conversion function return errors properly
2025-04-27 18:03:06 -04:00
d459dd60ef Convert from Zig struct to C struct 2025-04-27 18:03:06 -04:00
ce21b94a43 use InstallHeader function to install the header 2025-04-27 18:03:06 -04:00
c0e466b28a successfully build c interface 2025-04-27 18:03:06 -04:00
ee6062334b Initial C api 2025-04-27 18:03:06 -04:00
5 changed files with 70 additions and 4 deletions

View File

@@ -1,4 +1,5 @@
const std = @import("std"); const std = @import("std");
const Step = std.Build.Step;
// Although this function looks imperative, note that its job is to // Although this function looks imperative, note that its job is to
// declaratively construct a build graph that will be executed by an external // declaratively construct a build graph that will be executed by an external
@@ -37,13 +38,29 @@ pub fn build(b: *std.Build) void {
exe_mod.addImport("zaprus", lib_mod); exe_mod.addImport("zaprus", lib_mod);
exe_mod.addImport("clap", b.dependency("clap", .{}).module("clap")); exe_mod.addImport("clap", b.dependency("clap", .{}).module("clap"));
const lib = b.addLibrary(.{ const static_lib = b.addLibrary(.{
.linkage = .static, .linkage = .static,
.name = "zaprus", .name = "zaprus",
.root_module = lib_mod, .root_module = lib_mod,
}); });
static_lib.addIncludePath(.{ .cwd_relative = "include" });
static_lib.linkLibC();
b.installArtifact(lib); b.installArtifact(static_lib);
const dynamic_lib = b.addLibrary(.{
.linkage = .dynamic,
.name = "zaprus",
.root_module = lib_mod,
});
dynamic_lib.addIncludePath(.{ .cwd_relative = "include" });
dynamic_lib.linkLibC();
b.installArtifact(dynamic_lib);
// C Headers
const c_header = b.addInstallHeaderFile(b.path("include/zaprus.h"), "zaprus.h");
b.getInstallStep().dependOn(&c_header.step);
// This creates another `std.Build.Step.Compile`, but this one builds an executable // This creates another `std.Build.Step.Compile`, but this one builds an executable
// rather than a static library. // rather than a static library.

15
include/zaprus.h Normal file
View File

@@ -0,0 +1,15 @@
// client
#include<stdint.h>
#include<stdlib.h>
int zaprus_init(void);
int zaprus_deinit(void);
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);
void* zaprus_connect(const char* payload, size_t len);

View File

@@ -76,7 +76,7 @@ pub fn sendInitialConnection(payload: []const u8, initial_port: u16, allocator:
return msg; return msg;
} }
pub fn connect(payload: []const u8, allocator: Allocator) !?SaprusMessage { pub fn connect(payload: []const u8, allocator: Allocator) !SaprusMessage {
var initial_port: u16 = 0; var initial_port: u16 = 0;
if (rand) |r| { if (rand) |r| {
initial_port = r.intRangeAtMost(u16, 1024, 65000); initial_port = r.intRangeAtMost(u16, 1024, 65000);
@@ -109,7 +109,7 @@ pub fn connect(payload: []const u8, allocator: Allocator) !?SaprusMessage {
// Complete handshake after awaiting response // Complete handshake after awaiting response
try broadcastSaprusMessage(msg, randomPort(), allocator); try broadcastSaprusMessage(msg, randomPort(), allocator);
return initial_conn_res; return initial_conn_res.?;
} }
const SaprusMessage = @import("message.zig").Message; const SaprusMessage = @import("message.zig").Message;

32
src/c_api.zig Normal file
View File

@@ -0,0 +1,32 @@
// client
export fn zaprus_init() c_int {
SaprusClient.init() catch return 1;
return 0;
}
export fn zaprus_deinit() c_int {
SaprusClient.deinit();
return 0;
}
export fn zaprus_send_relay(payload: [*]const u8, len: usize, dest: [*]u8) c_int {
SaprusClient.sendRelay(payload[0..len], dest[0..4].*, allocator) catch return 1;
return 0;
}
export fn zaprus_connect(payload: [*]const u8, len: usize, output: *SaprusConnection) c_int {
output.* = (SaprusClient.connect(payload[0..len], allocator) catch return 1).?;
return 0;
}
const std = @import("std");
const zaprus = @import("./root.zig");
const SaprusClient = zaprus.Client;
const SaprusConnection = zaprus.Connection;
const allocator = std.heap.c_allocator;
test {
std.testing.refAllDeclsRecursively(@This());
}

View File

@@ -1,2 +1,4 @@
pub const Client = @import("Client.zig"); pub const Client = @import("Client.zig");
pub usingnamespace @import("message.zig"); pub usingnamespace @import("message.zig");
pub usingnamespace @import("c_api.zig");