Obsolete
/
libipc-old
Archived
3
0
Fork 0

Bindings: read from fd.

master
Philippe Pittoli 2023-01-17 00:16:58 +01:00
parent 125a960816
commit 71aa496501
2 changed files with 24 additions and 5 deletions

View File

@ -32,6 +32,7 @@ export fn ipc_context_deinit (ctx: *Context) callconv(.C) void {
export fn ipc_write (ctx: *Context, servicefd: i32, mcontent: [*]const u8, mlen: u32) i32 {
// TODO: better default length.
var buffer: [100000]u8 = undefined;
var fba = std.heap.FixedBufferAllocator.init(&buffer);
@ -40,8 +41,19 @@ export fn ipc_write (ctx: *Context, servicefd: i32, mcontent: [*]const u8, mlen:
return 0;
}
export fn ipc_read_fd (ctx: *Context, fd: i32, buffer: [*]u8, buflen: *usize) i32 {
var m = ctx.read_fd (fd) catch {return -1;} orelse return -2;
if (m.payload.len > buflen.*) return -3;
buflen.* = m.payload.len;
var fbs = std.io.fixedBufferStream(buffer[0..buflen.*]);
var writer = fbs.writer();
_ = writer.write(m.payload) catch return -4;
return 0;
}
// pub fn schedule (ctx: *Context, m: Message) !void
// pub fn read_fd (ctx: *Context, fd: i32) !?Message
// pub fn read (ctx: *Context, index: usize) !?Message
//
// pub fn wait_event(ctx: *Context) !Event

View File

@ -35,11 +35,18 @@ int main(void) {
return 1;
}
#if 0
char message[10000];
unsigned int size = 10000;
#if 1
ret = ipc_read_fd (ctx, servicefd, message, &size);
if (ret != 0) {
printf ("Cannot read from the service fd: %d.\n", ret);
return 1;
}
#else
// TODO: loop over ipc_wait
int event_type, index, originfd = 0;
unsigned int size = 0;
char message[10000];
printf ("Wait for a response.\n", ret);
ret = ipc_wait (&event_type, &index, &originfd, &size, message);
@ -47,6 +54,7 @@ int main(void) {
printf ("Error while waiting for an event.\n");
return 1;
}
#endif
if (size == 0) {
printf ("No message returned.\n");
@ -55,7 +63,6 @@ int main(void) {
message[size] = '\0';
printf ("Response: %s.\n", message);
#endif
printf ("Deinit context\n");
ipc_context_deinit (ctx);