Basic skeleton.

This commit is contained in:
Philippe PITTOLI 2024-06-15 12:15:53 +02:00
parent 09fbd37adb
commit 7415cca852

View File

@ -6,24 +6,55 @@ const ipc = @cImport({
@cInclude("libipc.h"); @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 { pub fn main() !void {
var context : ?*anyopaque = undefined; var context : ?*anyopaque = undefined;
var sockfd : i32 = 0; var service_socket_fd : i32 = 0;
const stdout_file = std.io.getStdOut().writer(); const stdout_file = std.io.getStdOut().writer();
var bw = std.io.bufferedWriter(stdout_file); var bw = std.io.bufferedWriter(stdout_file);
const stdout = bw.writer(); const stdout = bw.writer();
_ = ipc.ipc_context_init(@as([*c]?*anyopaque, &context)); switch(ipc.ipc_context_init(&context)) {
const service_init = ipc.ipc_service_init(context, &sockfd, "proxy", 5); 0 => try stdout.print("Context initialized\n", .{}),
switch(service_init) { else => try stdout.print("Problem while initializing the context\n", .{}),
0 => try stdout.print("Service initialized\n", .{}),
else => try stdout.print("Problem while initializing the service!\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! 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" { test "simple test" {