Fix ConnectionServer

This commit is contained in:
jaina heartles 2022-07-09 12:46:55 -07:00
parent 053de35d33
commit fd27199813

View file

@ -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,