mirror of
https://git.robbyzambito.me/zits
synced 2026-02-04 03:34:48 +00:00
Use group to make it easier to clean up client tasks on exit
This commit is contained in:
@@ -83,6 +83,9 @@ pub fn start(server: *Server, io: std.Io, gpa: std.mem.Allocator) !void {
|
|||||||
), io, .{ .reuse_address = true });
|
), io, .{ .reuse_address = true });
|
||||||
defer tcp_server.deinit(io);
|
defer tcp_server.deinit(io);
|
||||||
|
|
||||||
|
var client_group: std.Io.Group = .init;
|
||||||
|
defer client_group.cancel(io);
|
||||||
|
|
||||||
var id: usize = 0;
|
var id: usize = 0;
|
||||||
// Run until SIGINT is handled, then exit gracefully
|
// Run until SIGINT is handled, then exit gracefully
|
||||||
while (true) : (id +%= 1) {
|
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;
|
if (server.clients.contains(id)) continue;
|
||||||
const stream = try tcp_server.accept(io);
|
const stream = try tcp_server.accept(io);
|
||||||
std.debug.print("accepted connection\n", .{});
|
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});
|
std.debug.print("could not start concurrent handler for {d}\n", .{id});
|
||||||
stream.close(io);
|
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(
|
fn handleConnection(
|
||||||
server: *Server,
|
server: *Server,
|
||||||
server_allocator: std.mem.Allocator,
|
server_allocator: std.mem.Allocator,
|
||||||
|
|||||||
Reference in New Issue
Block a user