From fd2719981367463f5795131f6add9aeb3e5811c4 Mon Sep 17 00:00:00 2001 From: jaina heartles Date: Sat, 9 Jul 2022 12:46:55 -0700 Subject: [PATCH] Fix ConnectionServer --- src/http/conn.zig | 17 +++++++++++++++-- 1 file changed, 15 insertions(+), 2 deletions(-) diff --git a/src/http/conn.zig b/src/http/conn.zig index 8975f8b..15a332f 100644 --- a/src/http/conn.zig +++ b/src/http/conn.zig @@ -17,8 +17,21 @@ pub const ConnectionServer = struct { next_conn_id: std.atomic.Atomic(Connection.Id) = std.atomic.Atomic(Connection.Id).init(1), handler: Handler, - // todo accept is a bad name - pub fn accept(self: *ConnectionServer, stream: std.net.Stream, address: std.net.Address) void { + pub fn run(self: *ConnectionServer, addr: std.net.Address) noreturn { + var srv = std.net.StreamServer.init(.{ .reuse_address = true }); + defer srv.deinit(); + + try srv.listen(addr); + + while (true) { + const conn = try srv.accept(); + self.handle(conn.stream, conn.address); + } + + unreachable; + } + + fn handle(self: *ConnectionServer, stream: std.net.Stream, address: std.net.Address) void { const conn = Connection{ .id = self.next_conn_id.fetchAdd(1, .SeqCst), .address = address,