summaryrefslogtreecommitdiff
path: root/src/server/main.zig
diff options
context:
space:
mode:
authorRobby Zambito <contact@robbyzambito.me>2025-12-29 00:35:06 +0000
committerRobby Zambito <contact@robbyzambito.me>2025-12-29 00:35:06 +0000
commit335c4aa092b2ba1b8233c8c4b25d98c8f01f584e (patch)
tree9a0ac052a9cf41e60ed9a84e0e8144b6cf013ab2 /src/server/main.zig
parent18f4475aaf15df2e71fe0c568a77ecc00beb4d02 (diff)
Diffstat (limited to 'src/server/main.zig')
-rw-r--r--src/server/main.zig32
1 files changed, 26 insertions, 6 deletions
diff --git a/src/server/main.zig b/src/server/main.zig
index e1f9891..f3702e9 100644
--- a/src/server/main.zig
+++ b/src/server/main.zig
@@ -10,7 +10,24 @@ clients: std.AutoHashMapUnmanaged(usize, *ClientState) = .empty,
/// Map of subjects to a map of (client ID => SID)
subscriptions: std.StringHashMapUnmanaged(std.AutoHashMapUnmanaged(usize, []const u8)) = .empty,
+var keep_running = std.atomic.Value(bool).init(true);
+
+fn handleSigInt(sig: std.os.linux.SIG) callconv(.c) void {
+ _ = sig;
+ keep_running.store(false, .monotonic);
+}
+
pub fn main(gpa: std.mem.Allocator, server_config: ServerInfo) !void {
+ // Configure the signal action
+ // const act = std.posix.Sigaction{
+ // .handler = .{ .handler = handleSigInt },
+ // .mask = std.posix.sigemptyset(),
+ // .flags = 0,
+ // };
+
+ // // Register the handler for SIGINT (Ctrl+C)
+ // std.posix.sigaction(std.posix.SIG.INT, &act, null);
+
var server: Server = .{
.info = server_config,
};
@@ -26,7 +43,8 @@ pub fn main(gpa: std.mem.Allocator, server_config: ServerInfo) !void {
defer tcp_server.deinit(io);
var id: usize = 0;
- while (true) : (id +%= 1) {
+ // Run until SIGINT is handled, then exit gracefully
+ while (keep_running.load(.monotonic)) : (id +%= 1) {
std.debug.print("in server loop\n", .{});
if (server.clients.contains(id)) continue;
const stream = try tcp_server.accept(io);
@@ -36,6 +54,8 @@ pub fn main(gpa: std.mem.Allocator, server_config: ServerInfo) !void {
stream.close(io);
};
}
+
+ std.debug.print("Exiting gracefully\n", .{});
}
fn addClient(server: *Server, allocator: std.mem.Allocator, id: usize, client: *ClientState) !void {
@@ -58,8 +78,8 @@ fn handleConnection(
id: usize,
stream: std.Io.net.Stream,
) !void {
- _ = server_allocator;
var client_allocator: std.heap.DebugAllocator(.{}) = .init;
+ client_allocator.backing_allocator = server_allocator;
defer {
std.debug.print("deinitializing debug allocator\n", .{});
_ = client_allocator.deinit();
@@ -113,9 +133,6 @@ fn handleConnection(
.unsub => |unsub| {
try server.unsubscribe(client_state.id, unsub);
},
- .eos => {
- break;
- },
else => |e| {
std.debug.panic("Unimplemented message: {any}\n", .{e});
},
@@ -123,7 +140,10 @@ fn handleConnection(
std.debug.print("processed message from client\n", .{});
std.debug.print("awaiting next message from client\n", .{});
- } else |_| {}
+ } else |err| {
+ // This is probably going to be normal on disconnect
+ std.debug.print("Ran into error in client process loop: {}\n", .{err});
+ }
// client_state.task.await(io);
}