diff options
| author | Robby Zambito <contact@robbyzambito.me> | 2026-01-01 21:41:53 +0000 |
|---|---|---|
| committer | Robby Zambito <contact@robbyzambito.me> | 2026-01-01 22:14:36 +0000 |
| commit | 5a395bdadfb05c3acc1a23605cf6762404c5d367 (patch) | |
| tree | ae26963d2273ced7aa2965da39162b7f388ff980 /src | |
| parent | a3e026ebf1434279bae431aa463a2690b471a4c2 (diff) | |
Use group to make it easier to clean up client tasks on exit
Diffstat (limited to 'src')
| -rw-r--r-- | src/server/main.zig | 15 |
1 files changed, 14 insertions, 1 deletions
diff --git a/src/server/main.zig b/src/server/main.zig index f3728ea..044f551 100644 --- a/src/server/main.zig +++ b/src/server/main.zig @@ -83,6 +83,9 @@ pub fn start(server: *Server, io: std.Io, gpa: std.mem.Allocator) !void { ), io, .{ .reuse_address = true }); defer tcp_server.deinit(io); + var client_group: std.Io.Group = .init; + defer client_group.cancel(io); + var id: usize = 0; // Run until SIGINT is handled, then exit gracefully while (true) : (id +%= 1) { @@ -90,7 +93,7 @@ pub fn start(server: *Server, io: std.Io, gpa: std.mem.Allocator) !void { if (server.clients.contains(id)) continue; const stream = try tcp_server.accept(io); std.debug.print("accepted connection\n", .{}); - _ = io.concurrent(handleConnection, .{ server, gpa, io, id, stream }) catch { + _ = client_group.concurrent(io, handleConnectionInfallible, .{ server, gpa, io, id, stream }) catch { std.debug.print("could not start concurrent handler for {d}\n", .{id}); stream.close(io); }; @@ -118,6 +121,16 @@ fn removeClient(server: *Server, io: std.Io, allocator: std.mem.Allocator, id: u } } +fn handleConnectionInfallible( + server: *Server, + server_allocator: std.mem.Allocator, + io: std.Io, + id: usize, + stream: std.Io.net.Stream, +) void { + handleConnection(server, server_allocator, io, id, stream) catch {}; +} + fn handleConnection( server: *Server, server_allocator: std.mem.Allocator, |
