treesummaryrefslogcommitdiff
path: root/src/main.zig
diff options
context:
space:
mode:
authorPatrick Schönberger2026-05-18 12:27:54 +0200
committerPatrick Schönberger2026-05-18 12:27:54 +0200
commite75dbcb13fa36b13b5b71ff6d9622d3cb0fd54b5 (patch)
tree73f8ed50d968f285af9d0d72386e12e673f122fd /src/main.zig
parentecf1efb4a84a90f8c2b5a0d3c28cba25ce3666c4 (diff)
downloadzhttpws-e75dbcb13fa36b13b5b71ff6d9622d3cb0fd54b5.tar.gz
zhttpws-e75dbcb13fa36b13b5b71ff6d9622d3cb0fd54b5.zip
change to ziozio
Diffstat (limited to 'src/main.zig')
-rw-r--r--src/main.zig20
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", .{});
}