Switch: callbacks can be set to "NULL" to keep default operations.

master
Philippe PITTOLI 2024-06-18 13:19:30 +02:00
parent 45614deacb
commit f59eb58e0b
4 changed files with 9 additions and 8 deletions

View File

@ -106,6 +106,7 @@ int ipc_add_switch (void* ctx, int fd1, int fd2);
// Set IO callbacks for a file descriptor.
// Returned "char" is a cb_event_types enum.
// One of the callbacks can be "NULL" to keep the default callback, thus changing only input or output operations.
int ipc_set_switch_callbacks (void* ctx, int fd
, char (*in (int orig, char *payload, size_t *mlen))
, char (*out(int dest, const char *payload, size_t mlen)));

View File

@ -152,8 +152,8 @@ export fn ipc_add_switch(ctx: *Context, fd1: i32, fd2: i32) callconv(.C) i32 {
}
export fn ipc_set_switch_callbacks(ctx: *Context, fd: i32,
in: *const fn (origin: i32, mcontent: [*]u8, mlen: *usize) callconv(.C) u8,
out: *const fn (origin: i32, mcontent: [*]const u8, mlen: usize) callconv(.C) u8) callconv(.C) i32 {
in: ?*const fn (origin: i32, mcontent: [*]u8, mlen: *usize) callconv(.C) u8,
out: ?*const fn (origin: i32, mcontent: [*]const u8, mlen: usize) callconv(.C) u8) callconv(.C) i32 {
ctx.set_switch_callbacks(fd, in, out) catch return -1;
return 0;
}

View File

@ -317,8 +317,8 @@ pub const Context = struct {
}
pub fn set_switch_callbacks(self: *Self, fd: i32,
in: *const fn (origin: i32, mcontent: [*]u8, mlen: *usize) callconv(.C) u8,
out: *const fn (origin: i32, mcontent: [*]const u8, mlen: usize) callconv(.C) u8) !void {
in: ?*const fn (origin: i32, mcontent: [*]u8, mlen: *usize) callconv(.C) u8,
out: ?*const fn (origin: i32, mcontent: [*]const u8, mlen: usize) callconv(.C) u8) !void {
try self.switchdb.set_callbacks(fd, in, out);
}

View File

@ -65,11 +65,11 @@ pub const SwitchDB = struct {
}
pub fn set_callbacks(self: *Self, fd: i32,
in: *const fn (origin: i32, mcontent: [*]u8, mlen: *usize) callconv(.C) u8,
out: *const fn (origin: i32, mcontent: [*]const u8, mlen: usize) callconv(.C) u8) !void {
in: ?*const fn (origin: i32, mcontent: [*]u8, mlen: *usize) callconv(.C) u8,
out: ?*const fn (origin: i32, mcontent: [*]const u8, mlen: usize) callconv(.C) u8) !void {
var managedconnection = self.db.get(fd) orelse return error.unregisteredFD;
managedconnection.in = in;
managedconnection.out = out;
if (in) |f| { managedconnection.in = f; }
if (out) |f| { managedconnection.out = f; }
try self.db.put(fd, managedconnection);
}