mirror of
https://git.robbyzambito.me/zaprus
synced 2025-12-20 16:24:50 +00:00
Compare commits
1 Commits
push-sqrvt
...
fe26cb002d
| Author | SHA1 | Date | |
|---|---|---|---|
| fe26cb002d |
22
build.zig
22
build.zig
@@ -1,4 +1,5 @@
|
||||
const std = @import("std");
|
||||
const Step = std.Build.Step;
|
||||
|
||||
// Although this function looks imperative, note that its job is to
|
||||
// declaratively construct a build graph that will be executed by an external
|
||||
@@ -37,13 +38,30 @@ pub fn build(b: *std.Build) void {
|
||||
exe_mod.addImport("zaprus", lib_mod);
|
||||
exe_mod.addImport("clap", b.dependency("clap", .{}).module("clap"));
|
||||
|
||||
const lib = b.addLibrary(.{
|
||||
const static_lib = b.addLibrary(.{
|
||||
.linkage = .static,
|
||||
.name = "zaprus",
|
||||
.root_module = lib_mod,
|
||||
});
|
||||
|
||||
b.installArtifact(lib);
|
||||
b.installArtifact(static_lib);
|
||||
|
||||
const dynamic_lib = b.addLibrary(.{
|
||||
.linkage = .dynamic,
|
||||
.name = "zaprus",
|
||||
.root_module = lib_mod,
|
||||
});
|
||||
|
||||
b.installArtifact(dynamic_lib);
|
||||
|
||||
// C Headers
|
||||
const c_header = b.addInstallFileWithDir(
|
||||
b.path("include/zaprus.h"),
|
||||
.header,
|
||||
"zaprus.h",
|
||||
);
|
||||
|
||||
b.getInstallStep().dependOn(&c_header.step);
|
||||
|
||||
// This creates another `std.Build.Step.Compile`, but this one builds an executable
|
||||
// rather than a static library.
|
||||
|
||||
24
include/zaprus.h
Normal file
24
include/zaprus.h
Normal file
@@ -0,0 +1,24 @@
|
||||
// client
|
||||
|
||||
int zaprus_init(void);
|
||||
|
||||
int zaprus_deinit(void);
|
||||
|
||||
int zaprus_send_relay(const char* payload, usize len, char[4] dest);
|
||||
|
||||
int zaprus_send_initial_connection(const char* payload, usize len, uint16_t initial_port);
|
||||
|
||||
struct SaprusMessage* zaprus_connect(const char* payload, usize len);
|
||||
|
||||
// message
|
||||
struct SaprusMessage {
|
||||
|
||||
};
|
||||
|
||||
// ptr should be freed by the caller.
|
||||
int zaprus_message_to_bytes(struct SaprusMessage msg, char** ptr, usize* len);
|
||||
|
||||
// Return value should be destroyed with zaprus_message_deinit.
|
||||
struct SaprusMessage* zaprus_message_from_bytes(const char* bytes, usize len);
|
||||
|
||||
void zaprus_message_deinit(struct SaprusMessage* msg);
|
||||
56
src/c_api.zig
Normal file
56
src/c_api.zig
Normal file
@@ -0,0 +1,56 @@
|
||||
// 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: [4]u8) c_int {
|
||||
SaprusClient.sendRelay(payload[0..len], dest, allocator) catch return 1;
|
||||
return 0;
|
||||
}
|
||||
|
||||
export fn zaprus_send_initial_connection(payload: [*]const u8, len: usize, initial_port: u16) c_int {
|
||||
SaprusClient.sendInitialConnection(payload[0..len], initial_port, allocator) catch return 1;
|
||||
return 0;
|
||||
}
|
||||
|
||||
export fn zaprus_connect(payload: [*]const u8, len: usize) ?*SaprusMessage {
|
||||
return SaprusClient.connect(payload[0..len], allocator) catch null;
|
||||
}
|
||||
|
||||
// message
|
||||
|
||||
/// ptr should be freed by the caller.
|
||||
export fn zaprus_message_to_bytes(msg: SaprusMessage, ptr: *[*]u8, len: *usize) c_int {
|
||||
const bytes = msg.toBytes(allocator) catch return 1;
|
||||
ptr.* = bytes[0..].*;
|
||||
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) ?*SaprusMessage {
|
||||
return SaprusMessage.fromBytes(bytes[0..len], allocator) catch null;
|
||||
}
|
||||
|
||||
export fn zaprus_message_deinit(msg: *SaprusMessage) void {
|
||||
msg.deinit(allocator);
|
||||
}
|
||||
|
||||
const std = @import("std");
|
||||
|
||||
const zaprus = @import("./root.zig");
|
||||
const SaprusClient = zaprus.Client;
|
||||
const SaprusMessage = zaprus.Message;
|
||||
|
||||
const allocator = std.heap.c_allocator;
|
||||
|
||||
test {
|
||||
std.testing.refAllDeclsRecursively(@This());
|
||||
}
|
||||
@@ -1,2 +1,4 @@
|
||||
pub const Client = @import("Client.zig");
|
||||
pub usingnamespace @import("message.zig");
|
||||
|
||||
pub usingnamespace @import("c_api.zig");
|
||||
|
||||
Reference in New Issue
Block a user