diff --git a/src/main.zig b/src/main.zig index 00e80e0..c0c3e75 100644 --- a/src/main.zig +++ b/src/main.zig @@ -6,24 +6,55 @@ const ipc = @cImport({ @cInclude("libipc.h"); }); -const Context = opaque {}; +// TODO: noreturn error function: print a error message then crash. +// TODO: connection to the proxied service +// TODO: handle connections and disconnections +// TODO: handle messages (proxy) pub fn main() !void { var context : ?*anyopaque = undefined; - var sockfd : i32 = 0; + var service_socket_fd : i32 = 0; const stdout_file = std.io.getStdOut().writer(); var bw = std.io.bufferedWriter(stdout_file); const stdout = bw.writer(); - _ = ipc.ipc_context_init(@as([*c]?*anyopaque, &context)); - const service_init = ipc.ipc_service_init(context, &sockfd, "proxy", 5); - switch(service_init) { - 0 => try stdout.print("Service initialized\n", .{}), - else => try stdout.print("Problem while initializing the service!\n", .{}), + switch(ipc.ipc_context_init(&context)) { + 0 => try stdout.print("Context initialized\n", .{}), + else => try stdout.print("Problem while initializing the context\n", .{}), } + defer { _ = ipc.ipc_context_deinit(&context); } + switch(ipc.ipc_service_init(context, &service_socket_fd, "proxy", 5)) { + 0 => try stdout.print("Service initialized\n", .{}), + else => try stdout.print("Problem while initializing the service\n", .{}), + } + defer { _ = ipc.ipc_close_all(context); } try bw.flush(); // don't forget to flush! + + // TODO: do some stuff + while(true) { + var t : u8 = undefined; + var index : u64 = undefined; + var buffer : [100_000]u8 = undefined; + var buflen : u64 = 100_000; + var origin_fd : i32 = 0; + + _ = ipc.ipc_wait_event(context, &t, &index, &origin_fd, &buffer, &buflen); + switch(t) { + ipc.ERROR => try stdout.print("ERROR\n", .{}), // A problem occured. + ipc.CONNECTION => try stdout.print("CONNECTION\n", .{}), // New user. + ipc.DISCONNECTION => try stdout.print("DISCONNECTION\n", .{}), // User disconnected. + ipc.MESSAGE_RX => try stdout.print("MESSAGE_RX\n", .{}), // New message. + ipc.MESSAGE_TX => try stdout.print("MESSAGE_TX\n", .{}), // Message sent. + ipc.TIMER => try stdout.print("TIMER\n", .{}), // Timeout in the poll(2) function. + ipc.EXTERNAL => try stdout.print("EXTERNAL\n", .{}), // Message received from a non IPC socket. + ipc.SWITCH_RX => try stdout.print("SWITCH_RX\n", .{}), // Message received from a switched FD. + ipc.SWITCH_TX => try stdout.print("SWITCH_TX\n", .{}), // Message sent to a switched fd. + else => try stdout.print("ELSE CASE\n", .{}), // Should never happen. + } + try bw.flush(); // don't forget to flush! + } } test "simple test" {