Bindings: libipc.h almost complete, correct types, fix callconv.
This commit is contained in:
parent
178f205d44
commit
ed9cd24b22
@ -13,4 +13,19 @@ enum event_types {
|
||||
, TX = 8 // Message sent.
|
||||
};
|
||||
|
||||
int ipc_context_init (void** ptr);
|
||||
int ipc_service_init (void* ctx, int* servicefd, const char* service_name, size_t service_name_len);
|
||||
int ipc_connect_service (void* ctx, int* servicefd, const char* service_name, size_t service_name_len);
|
||||
void ipc_context_deinit (void* ctx);
|
||||
int ipc_write (void* ctx, int servicefd, char* mcontent, size_t mlen);
|
||||
int ipc_schedule (void* ctx, int servicefd, const char* mcontent, size_t mlen);
|
||||
int ipc_read_fd (void* ctx, int fd, char* buffer, size_t* buflen);
|
||||
int ipc_read (void* ctx, size_t index, char* buffer, size_t* buflen);
|
||||
int ipc_wait_event(void* ctx, char* t, size_t* index, int* originfd, char* buffer, size_t* buflen);
|
||||
void ipc_context_timer (void* ctx, int timer);
|
||||
int ipc_close_fd (void* ctx, int fd);
|
||||
int ipc_close (void* ctx, size_t index);
|
||||
int ipc_add_external (void* ctx, int newfd);
|
||||
int ipc_add_switch (void* ctx, int fd1, int fd2);
|
||||
|
||||
#endif
|
||||
|
@ -22,18 +22,18 @@ export fn ipc_service_init(ctx: *Context, servicefd: *i32, service_name: [*]cons
|
||||
}
|
||||
|
||||
/// Connect to a libipc service, possibly through IPCd.
|
||||
export fn ipc_connect_service (ctx: *Context, servicefd: *i32, service_name: [*]const u8, service_name_len: u16) i32 {
|
||||
export fn ipc_connect_service (ctx: *Context, servicefd: *i32, service_name: [*]const u8, service_name_len: u16) callconv(.C) i32 {
|
||||
var fd = ctx.connect_ipc (service_name[0..service_name_len]) catch return -1;
|
||||
servicefd.* = fd;
|
||||
return 0;
|
||||
}
|
||||
|
||||
export fn ipc_context_deinit (ctx: *Context) callconv(.C) void {
|
||||
export fn ipc_context_deinit (ctx: *Context) callconv(.C) callconv(.C) void {
|
||||
ctx.deinit();
|
||||
}
|
||||
|
||||
/// Write a message (no waiting).
|
||||
export fn ipc_write (ctx: *Context, servicefd: i32, mcontent: [*]const u8, mlen: u32) i32 {
|
||||
export fn ipc_write (ctx: *Context, servicefd: i32, mcontent: [*]const u8, mlen: u32) callconv(.C) i32 {
|
||||
// TODO: better default length.
|
||||
var buffer: [100000]u8 = undefined;
|
||||
var fba = std.heap.FixedBufferAllocator.init(&buffer);
|
||||
@ -45,7 +45,7 @@ export fn ipc_write (ctx: *Context, servicefd: i32, mcontent: [*]const u8, mlen:
|
||||
|
||||
/// Schedule a message.
|
||||
/// Use the same allocator as the context.
|
||||
export fn ipc_schedule (ctx: *Context, servicefd: i32, mcontent: [*]const u8, mlen: u32) i32 {
|
||||
export fn ipc_schedule (ctx: *Context, servicefd: i32, mcontent: [*]const u8, mlen: u32) callconv(.C) i32 {
|
||||
var message = Message.init(servicefd, ctx.allocator, mcontent[0..mlen]) catch return -1;
|
||||
ctx.schedule(message) catch return -2;
|
||||
return 0;
|
||||
@ -53,7 +53,7 @@ export fn ipc_schedule (ctx: *Context, servicefd: i32, mcontent: [*]const u8, ml
|
||||
|
||||
/// Read a message from a file descriptor.
|
||||
/// Buffer length will be changed to the size of the received message.
|
||||
export fn ipc_read_fd (ctx: *Context, fd: i32, buffer: [*]u8, buflen: *usize) i32 {
|
||||
export fn ipc_read_fd (ctx: *Context, fd: i32, buffer: [*]u8, buflen: *usize) callconv(.C) i32 {
|
||||
var m = ctx.read_fd (fd) catch {return -1;} orelse return -2;
|
||||
if (m.payload.len > buflen.*) return -3;
|
||||
buflen.* = m.payload.len;
|
||||
@ -67,7 +67,7 @@ export fn ipc_read_fd (ctx: *Context, fd: i32, buffer: [*]u8, buflen: *usize) i3
|
||||
|
||||
/// Read a message.
|
||||
/// Buffer length will be changed to the size of the received message.
|
||||
export fn ipc_read (ctx: *Context, index: usize, buffer: [*]u8, buflen: *usize) i32 {
|
||||
export fn ipc_read (ctx: *Context, index: usize, buffer: [*]u8, buflen: *usize) callconv(.C) i32 {
|
||||
var m = ctx.read (index) catch {return -1;} orelse return -2;
|
||||
if (m.payload.len > buflen.*) return -3;
|
||||
buflen.* = m.payload.len;
|
||||
@ -81,7 +81,7 @@ export fn ipc_read (ctx: *Context, index: usize, buffer: [*]u8, buflen: *usize)
|
||||
|
||||
/// Wait for an event.
|
||||
/// Buffer length will be changed to the size of the received message.
|
||||
export fn ipc_wait_event(ctx: *Context, t: *u8, index: *usize, originfd: *i32, buffer: [*]u8, buflen: *usize) i32 {
|
||||
export fn ipc_wait_event(ctx: *Context, t: *u8, index: *usize, originfd: *i32, buffer: [*]u8, buflen: *usize) callconv(.C) i32 {
|
||||
var event = ctx.wait_event() catch return -1;
|
||||
t.* = @enumToInt(event.t);
|
||||
index.* = event.index;
|
||||
@ -101,16 +101,16 @@ export fn ipc_wait_event(ctx: *Context, t: *u8, index: *usize, originfd: *i32, b
|
||||
}
|
||||
|
||||
/// Change the timer (ms).
|
||||
export fn ipc_context_timer (ctx: *Context, timer: i32) void {
|
||||
export fn ipc_context_timer (ctx: *Context, timer: i32) callconv(.C) void {
|
||||
ctx.timer = timer;
|
||||
}
|
||||
|
||||
export fn ipc_close_fd (ctx: *Context, fd: i32) i32 {
|
||||
export fn ipc_close_fd (ctx: *Context, fd: i32) callconv(.C) i32 {
|
||||
ctx.close_fd (fd) catch return -1;
|
||||
return 0;
|
||||
}
|
||||
|
||||
export fn ipc_close (ctx: *Context, index: usize) i32 {
|
||||
export fn ipc_close (ctx: *Context, index: usize) callconv(.C) i32 {
|
||||
ctx.close (index) catch return -1;
|
||||
return 0;
|
||||
}
|
||||
@ -118,12 +118,12 @@ export fn ipc_close (ctx: *Context, index: usize) i32 {
|
||||
/// Add a new file descriptor to listen to.
|
||||
/// The FD is marked as "external"; it isn't a simple libipc connection.
|
||||
/// You may want to handle any operation on it by yourself.
|
||||
export fn ipc_add_external (ctx: *Context, newfd: i32) i32 {
|
||||
export fn ipc_add_external (ctx: *Context, newfd: i32) callconv(.C) i32 {
|
||||
ctx.add_external (newfd) catch return -1;
|
||||
return 0;
|
||||
}
|
||||
|
||||
export fn ipc_add_switch (ctx: *Context, fd1: i32, fd2: i32) i32 {
|
||||
export fn ipc_add_switch (ctx: *Context, fd1: i32, fd2: i32) callconv(.C) i32 {
|
||||
ctx.add_switch (fd1, fd2) catch return -1;
|
||||
return 0;
|
||||
}
|
||||
|
@ -6,6 +6,8 @@
|
||||
#define SERVICE "pong"
|
||||
#define SERVICE_LEN 4
|
||||
|
||||
int direct_write_then_read(void);
|
||||
int wait_event(void);
|
||||
|
||||
int main(void) {
|
||||
direct_write_then_read();
|
||||
@ -42,7 +44,7 @@ int direct_write_then_read(void) {
|
||||
}
|
||||
|
||||
char message[10000];
|
||||
unsigned int size = 10000;
|
||||
size_t size = 10000;
|
||||
|
||||
ret = ipc_read_fd (ctx, servicefd, message, &size);
|
||||
|
||||
@ -72,9 +74,9 @@ int wait_event(void) {
|
||||
int ret = 0;
|
||||
int servicefd = 0;
|
||||
char message[10000];
|
||||
unsigned int size = 10000;
|
||||
size_t size = 10000;
|
||||
char event_type;
|
||||
unsigned int index = 0;
|
||||
size_t index = 0;
|
||||
int originfd = 0;
|
||||
void *ctx = NULL;
|
||||
|
||||
@ -107,7 +109,7 @@ int wait_event(void) {
|
||||
|
||||
printf ("Let's loop over events.\n");
|
||||
char should_continue = 1;
|
||||
unsigned int count = 0;
|
||||
size_t count = 0;
|
||||
while(should_continue) {
|
||||
size = 10000;
|
||||
ret = ipc_wait_event (ctx, &event_type, &index, &originfd, message, &size);
|
||||
@ -116,7 +118,7 @@ int wait_event(void) {
|
||||
return 1;
|
||||
}
|
||||
|
||||
printf ("EVENT %u\t", count++);
|
||||
printf ("EVENT %lu\t", count++);
|
||||
|
||||
switch ((enum event_types) event_type) {
|
||||
case ERROR: {
|
||||
|
@ -11,9 +11,9 @@ int main(void) {
|
||||
int ret = 0;
|
||||
int servicefd = 0;
|
||||
char message[10000];
|
||||
unsigned int size = 10000;
|
||||
size_t size = 10000;
|
||||
char event_type;
|
||||
unsigned int index = 0;
|
||||
size_t index = 0;
|
||||
int originfd = 0;
|
||||
void *ctx = NULL;
|
||||
|
||||
@ -38,8 +38,8 @@ int main(void) {
|
||||
|
||||
printf ("Loop over events.\n");
|
||||
char should_continue = 1;
|
||||
unsigned int count = 0;
|
||||
unsigned int count_timer = 0;
|
||||
size_t count = 0;
|
||||
size_t count_timer = 0;
|
||||
while(should_continue) {
|
||||
size = 10000;
|
||||
ret = ipc_wait_event (ctx, &event_type, &index, &originfd, message, &size);
|
||||
@ -49,7 +49,7 @@ int main(void) {
|
||||
}
|
||||
|
||||
if ((enum event_types) event_type != TIMER) {
|
||||
printf ("EVENT %u\t", count++);
|
||||
printf ("EVENT %lu\t", count++);
|
||||
}
|
||||
|
||||
switch ((enum event_types) event_type) {
|
||||
@ -78,7 +78,7 @@ int main(void) {
|
||||
break;
|
||||
}
|
||||
case TIMER: {
|
||||
printf ("\rTIMER (%d).", count_timer++);
|
||||
printf ("\rTIMER (%lu).", count_timer++);
|
||||
fflush(stdout);
|
||||
break;
|
||||
}
|
||||
|
Reference in New Issue
Block a user