Bindings: first draft (context init & deinit).

master
Philippe Pittoli 2023-01-16 22:26:15 +01:00
parent a6cfa88f79
commit a8224c1cf9
3 changed files with 37 additions and 12 deletions

View File

@ -1,12 +1,27 @@
const Builder = @import("std").build.Builder;
const std = @import("std");
pub fn build(b: *Builder) void {
const mode = b.standardReleaseOptions();
const lib = b.addStaticLibrary("ipc", "src/main.zig");
const build_dir = "build";
pub fn build(b: *std.build.Builder) void {
// Standard release options allow the person running `zig build` to select
// between Debug, ReleaseSafe, ReleaseFast, and ReleaseSmall.
// const mode = b.standardReleaseOptions();
const mode = std.builtin.Mode.Debug;
// const mode = std.builtin.Mode.ReleaseSmall;
const lib = b.addStaticLibrary("ipc", "src/bindings.zig");
lib.setOutputDir(build_dir);
lib.linkLibC();
lib.setBuildMode(mode);
lib.install();
var main_tests = b.addTest("src/main.zig");
const solib = b.addSharedLibrary("ipc", "src/bindings.zig", b.version(0, 1, 0));
solib.setOutputDir(build_dir);
solib.linkLibC();
solib.setBuildMode(mode);
solib.install();
const main_tests = b.addTest("src/main.zig");
main_tests.setBuildMode(mode);
const test_step = b.step("test", "Run library tests");

View File

@ -183,11 +183,8 @@ pub const Context = struct {
return error.IndexNotFound;
}
// Return the new fd. Can be useful to the caller.
pub fn connect(self: *Self, path: []const u8) !i32 {
return self.connect_ (Connection.Type.IPC, path);
}
/// Connect to the service directly, without reaching IPCd first.
/// Return the connection FD.
pub fn connect_service (self: *Self, service_name: []const u8) !i32 {
var buffer: [1000]u8 = undefined;
var fbs = std.io.fixedBufferStream(&buffer);
@ -196,9 +193,11 @@ pub const Context = struct {
try self.server_path(service_name, writer);
var path = fbs.getWritten();
return self.connect(path);
return self.connect_ (Connection.Type.IPC, path);
}
/// Tries to connect to IPCd first, then the service (if needed).
/// Return the connection FD.
pub fn connect_ipc (self: *Self, service_name: []const u8) !i32 {
// First, try ipcd.
if (try self.connect_ipcd (service_name, Connection.Type.IPC)) |fd| {
@ -217,7 +216,7 @@ pub const Context = struct {
try self.add_ (newcon, newfd);
}
pub fn accept_new_client(self: *Self, event: *Event, server_index: usize) !void {
fn accept_new_client(self: *Self, event: *Event, server_index: usize) !void {
// net.StreamServer
var serverfd = self.pollfd.items[server_index].fd;
var path = self.connections.items[server_index].path orelse return error.ServerWithNoPath;

View File

@ -1,4 +1,5 @@
#include <stdio.h>
#include <stdlib.h>
int main(void) {
int ret = 0;
@ -12,6 +13,9 @@ int main(void) {
return 1;
}
printf ("Context initiated.\n");
#if 0
printf ("Connect to a 'pong' service.\n");
ret = ipc_connect_service (ctx, &servicefd, "pong");
@ -47,6 +51,13 @@ int main(void) {
message[size] = '\0';
printf ("Response: %s.\n", message);
#endif
printf ("Deinit context\n");
ipc_context_deinit (ctx);
free(ctx);
printf ("Context freed.\n");
return 0;
}