diff options
Diffstat (limited to 'src/main.zig')
| -rw-r--r-- | src/main.zig | 20 |
1 files changed, 16 insertions, 4 deletions
diff --git a/src/main.zig b/src/main.zig index d1acc32..d80a7d3 100644 --- a/src/main.zig +++ b/src/main.zig @@ -1,4 +1,5 @@ const std = @import("std"); +const zio = @import("zio"); const Io = std.Io; const WebSocket = std.http.Server.WebSocket; @@ -21,7 +22,12 @@ fn handle_websocket(alloc: std.mem.Allocator, websocket: *WebSocket) void { m.unlock(); defer { - for (0..clients.items.len) |i| { if (std.mem.eql(u8, websocket.key, clients.items[i].key)) { _ = clients.swapRemove(i); break; } } + for (0..clients.items.len) |i| { + if (std.mem.eql(u8, websocket.key, clients.items[i].key)) { + _ = clients.swapRemove(i); + break; + } + } std.debug.print("{} clients\n", .{clients.items.len}); } @@ -105,6 +111,7 @@ pub fn main(init: std.process.Init) !void { // This is appropriate for anything that lives as long as the process. const arena: std.mem.Allocator = init.arena.allocator(); + defer init.arena.deinit(); clients = try .initCapacity(arena, 10); @@ -114,8 +121,9 @@ pub fn main(init: std.process.Init) !void { std.log.info("arg: {s}", .{arg}); } - // In order to do I/O operations need an `Io` instance. - const io = init.io; + const rt = try zio.Runtime.init(arena, .{}); + defer rt.deinit(); + const io = rt.io(); var port: u16 = 10010; if (init.environ_map.get("PORT")) |s| { @@ -129,10 +137,14 @@ pub fn main(init: std.process.Init) !void { const address = try std.Io.net.IpAddress.parseIp4("0.0.0.0", port); var net_server = try address.listen(io, .{ .reuse_address = true }); + var group: Io.Group = .init; + defer group.cancel(io); + while (true) { const stream = try net_server.accept(io); + errdefer stream.close(io); - _ = io.async(handle_request, .{ arena, io, stream }); + try group.concurrent(io, handle_request, .{ arena, io, stream }); // std.debug.print("created http thread\n", .{}); } |
