Bindings: no more memory errors, fix typing.
parent
ed9cd24b22
commit
e80e99d47f
|
@ -14,11 +14,11 @@ enum event_types {
|
|||
};
|
||||
|
||||
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);
|
||||
int ipc_service_init (void* ctx, int* servicefd, const char* service_name, unsigned short service_name_len);
|
||||
int ipc_connect_service (void* ctx, int* servicefd, const char* service_name, unsigned short 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_write (void* ctx, int servicefd, char* mcontent, unsigned int mlen);
|
||||
int ipc_schedule (void* ctx, int servicefd, const char* mcontent, unsigned int 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);
|
||||
|
|
|
@ -28,14 +28,14 @@ export fn ipc_connect_service (ctx: *Context, servicefd: *i32, service_name: [*]
|
|||
return 0;
|
||||
}
|
||||
|
||||
export fn ipc_context_deinit (ctx: *Context) callconv(.C) callconv(.C) void {
|
||||
export fn ipc_context_deinit (ctx: *Context) callconv(.C) void {
|
||||
ctx.deinit();
|
||||
}
|
||||
|
||||
/// Write a message (no waiting).
|
||||
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 buffer = [_]u8{0} ** 100000;
|
||||
var fba = std.heap.FixedBufferAllocator.init(&buffer);
|
||||
|
||||
var message = Message.init(servicefd, fba.allocator(), mcontent[0..mlen]) catch return -1;
|
||||
|
@ -61,6 +61,7 @@ export fn ipc_read_fd (ctx: *Context, fd: i32, buffer: [*]u8, buflen: *usize) ca
|
|||
var fbs = std.io.fixedBufferStream(buffer[0..buflen.*]);
|
||||
var writer = fbs.writer();
|
||||
_ = writer.write(m.payload) catch return -4;
|
||||
m.deinit();
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
@ -75,6 +76,7 @@ export fn ipc_read (ctx: *Context, index: usize, buffer: [*]u8, buflen: *usize)
|
|||
var fbs = std.io.fixedBufferStream(buffer[0..buflen.*]);
|
||||
var writer = fbs.writer();
|
||||
_ = writer.write(m.payload) catch return -4;
|
||||
m.deinit();
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
@ -92,6 +94,7 @@ export fn ipc_wait_event(ctx: *Context, t: *u8, index: *usize, originfd: *i32, b
|
|||
var writer = fbs.writer();
|
||||
_ = writer.write(m.payload) catch return -4;
|
||||
buflen.* = m.payload.len;
|
||||
m.deinit();
|
||||
}
|
||||
else {
|
||||
buflen.* = 0;
|
||||
|
|
|
@ -25,8 +25,8 @@ const CBEventType = @import("./main.zig").CBEvent.Type;
|
|||
|
||||
pub const PollFD = std.ArrayList(std.os.pollfd);
|
||||
|
||||
pub const IPC_HEADER_SIZE = 5; // Size (5 bytes) then content.
|
||||
pub const IPC_BASE_SIZE = 2000000; // 2 MB, plenty enough space for messages
|
||||
pub const IPC_HEADER_SIZE = 4; // Size (4 bytes) then content.
|
||||
pub const IPC_BASE_SIZE = 100000; // 100 KB, plenty enough space for messages
|
||||
pub const IPC_MAX_MESSAGE_SIZE = IPC_BASE_SIZE-IPC_HEADER_SIZE;
|
||||
pub const IPC_VERSION = 1;
|
||||
|
||||
|
@ -265,7 +265,7 @@ pub const Context = struct {
|
|||
// a Stream from the fd.
|
||||
var stream = net.Stream { .handle = m.fd };
|
||||
|
||||
var buffer: [IPC_MAX_MESSAGE_SIZE]u8 = undefined;
|
||||
var buffer = [_]u8{0} ** IPC_MAX_MESSAGE_SIZE;
|
||||
var fbs = std.io.fixedBufferStream(&buffer);
|
||||
var writer = fbs.writer();
|
||||
|
||||
|
@ -304,7 +304,7 @@ pub const Context = struct {
|
|||
return error.IndexOutOfBounds;
|
||||
}
|
||||
|
||||
var buffer: [IPC_MAX_MESSAGE_SIZE]u8 = undefined; // TODO: FIXME??
|
||||
var buffer = [_]u8{0} ** IPC_MAX_MESSAGE_SIZE;
|
||||
var packet_size: usize = undefined;
|
||||
|
||||
// TODO: this is a problem from the network API in Zig,
|
||||
|
|
|
@ -1,5 +1,6 @@
|
|||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
#include "../libipc.h"
|
||||
|
||||
|
@ -32,6 +33,9 @@ int direct_write_then_read(void) {
|
|||
|
||||
if (ret != 0) {
|
||||
printf ("Cannot connect to a service.\n");
|
||||
printf ("Deinit context\n");
|
||||
ipc_context_deinit (ctx);
|
||||
free(ctx);
|
||||
return 1;
|
||||
}
|
||||
|
||||
|
@ -40,6 +44,9 @@ int direct_write_then_read(void) {
|
|||
|
||||
if (ret != 0) {
|
||||
printf ("Cannot write to the service.\n");
|
||||
printf ("Deinit context\n");
|
||||
ipc_context_deinit (ctx);
|
||||
free(ctx);
|
||||
return 1;
|
||||
}
|
||||
|
||||
|
@ -50,11 +57,17 @@ int direct_write_then_read(void) {
|
|||
|
||||
if (ret != 0) {
|
||||
printf ("Cannot read from the service fd: %d.\n", ret);
|
||||
printf ("Deinit context\n");
|
||||
ipc_context_deinit (ctx);
|
||||
free(ctx);
|
||||
return 1;
|
||||
}
|
||||
|
||||
if (size == 0) {
|
||||
printf ("No message returned.\n");
|
||||
printf ("Deinit context\n");
|
||||
ipc_context_deinit (ctx);
|
||||
free(ctx);
|
||||
return 1;
|
||||
}
|
||||
|
||||
|
@ -74,6 +87,7 @@ int wait_event(void) {
|
|||
int ret = 0;
|
||||
int servicefd = 0;
|
||||
char message[10000];
|
||||
memset (message, 0, 1000);
|
||||
size_t size = 10000;
|
||||
char event_type;
|
||||
size_t index = 0;
|
||||
|
@ -85,6 +99,9 @@ int wait_event(void) {
|
|||
|
||||
if (ret != 0) {
|
||||
printf ("Cannot init context.\n");
|
||||
printf ("Deinit context\n");
|
||||
ipc_context_deinit (ctx);
|
||||
free(ctx);
|
||||
return 1;
|
||||
}
|
||||
|
||||
|
@ -93,6 +110,9 @@ int wait_event(void) {
|
|||
|
||||
if (ret != 0) {
|
||||
printf ("Cannot connect to a service.\n");
|
||||
printf ("Deinit context\n");
|
||||
ipc_context_deinit (ctx);
|
||||
free(ctx);
|
||||
return 1;
|
||||
}
|
||||
|
||||
|
@ -101,6 +121,9 @@ int wait_event(void) {
|
|||
|
||||
if (ret != 0) {
|
||||
printf ("Cannot schedule a message.\n");
|
||||
printf ("Deinit context\n");
|
||||
ipc_context_deinit (ctx);
|
||||
free(ctx);
|
||||
return 1;
|
||||
}
|
||||
|
||||
|
@ -160,7 +183,7 @@ int wait_event(void) {
|
|||
}
|
||||
|
||||
message[size] = '\0';
|
||||
printf ("Response: %s.\n", message);
|
||||
printf ("Response (size %lu): %s.\n", size, message);
|
||||
// We received the response, quitting.
|
||||
should_continue = 0;
|
||||
break;
|
||||
|
|
|
@ -7,7 +7,7 @@
|
|||
#define SERVICE_LEN 4
|
||||
|
||||
|
||||
int main(void) {
|
||||
int main(int argc, char**argv) {
|
||||
int ret = 0;
|
||||
int servicefd = 0;
|
||||
char message[10000];
|
||||
|
@ -17,6 +17,13 @@ int main(void) {
|
|||
int originfd = 0;
|
||||
void *ctx = NULL;
|
||||
|
||||
int max_count = 0;
|
||||
|
||||
if (argc > 1) {
|
||||
max_count = atoi(argv[1]);
|
||||
printf ("Wait for %d timer events.\n", max_count);
|
||||
}
|
||||
|
||||
printf ("Init context.\n");
|
||||
ret = ipc_context_init (&ctx);
|
||||
|
||||
|
@ -80,6 +87,11 @@ int main(void) {
|
|||
case TIMER: {
|
||||
printf ("\rTIMER (%lu).", count_timer++);
|
||||
fflush(stdout);
|
||||
if (max_count && count_timer >= (size_t) max_count) {
|
||||
printf ("waited for %lu timer events: quitting\n", count_timer);
|
||||
should_continue = 0;
|
||||
}
|
||||
|
||||
break;
|
||||
}
|
||||
case TX: {
|
||||
|
@ -92,8 +104,8 @@ int main(void) {
|
|||
should_continue = 0;
|
||||
}
|
||||
else {
|
||||
message[size] = '\0';
|
||||
printf ("Message received: %s.\n", message);
|
||||
message[size+1] = '\0';
|
||||
printf ("Message received (size %lu): %s.\n", size, message);
|
||||
printf ("Scheduling this message.\n");
|
||||
|
||||
ret = ipc_schedule (ctx, originfd, message, size);
|
||||
|
|
Reference in New Issue