This commit is contained in:
2025-12-02 22:37:50 -05:00
parent 4afdf32beb
commit 826da348a5
4 changed files with 244 additions and 87 deletions

View File

@@ -29,7 +29,10 @@ pub fn main(gpa: std.mem.Allocator, server_config: ServerInfo) !void {
while (true) : (id +%= 1) {
if (server.clients.contains(id)) continue;
const stream = try tcp_server.accept(io);
_ = io.async(handleConnection, .{ &server, gpa, io, id, stream });
_ = io.concurrent(handleConnection, .{ &server, gpa, io, id, stream }) catch {
std.debug.print("could not start concurrent handler for {d}\n", .{id});
stream.close(io);
};
}
}
@@ -66,7 +69,7 @@ fn handleConnection(
var connect_arena: std.heap.ArenaAllocator = .init(allocator);
defer connect_arena.deinit();
const connect = (Message.next(connect_arena.allocator(), in) catch return).connect;
var client_state: ClientState = .init(io, allocator, id, connect, in, out);
var client_state: ClientState = try .init(io, allocator, id, connect, in, out);
try server.addClient(allocator, id, client_state);
defer server.removeClient(allocator, id);
@@ -133,16 +136,24 @@ fn subscribe(server: *Server, gpa: std.mem.Allocator, id: usize, msg: Message.Su
try server.subscriptions.put(gpa, msg.subject, subs_for_subject);
}
fn processClient(server: *Server, gpa: std.mem.Allocator, io: std.Io, client_state: *ClientState) !void {
pub fn processClient(server: *Server, gpa: std.mem.Allocator, io: std.Io, client_state: *ClientState) !void {
defer std.debug.print("done processing client??\n", .{});
defer client_state.deinit(gpa);
std.debug.print("processing client: {d}\n", .{client_state.id});
while (true) {
switch (try client_state.next(io)) {
std.debug.print("awaiting next message from client\n", .{});
switch (client_state.next(io)) {
.ping => {
std.debug.print("got a ping! sending a pong.\n", .{});
for (0..5) |_| {
if (try client_state.send(io, .pong)) break;
if (try client_state.send(io, .pong)) {
std.debug.print("sent pong\n", .{});
break;
}
std.debug.print("trying to send a pong again.\n", .{});
} else {
std.debug.print("could not pong to client {}\n", .{client_state.id});
std.debug.print("could not pong to client {d}\n", .{client_state.id});
}
},
.@"pub" => |msg| {
@@ -158,6 +169,8 @@ fn processClient(server: *Server, gpa: std.mem.Allocator, io: std.Io, client_sta
std.debug.panic("Unimplemented message: {any}\n", .{msg});
},
}
std.debug.print("processed message from client\n", .{});
}
// while (!io.cancelRequested()) {
@@ -247,3 +260,42 @@ pub fn createId() []const u8 {
pub fn createName() []const u8 {
return "SERVERNAME";
}
// TESTING
// fn initTestServer() Server {
// return .{
// .info = .{
// .server_id = "ABCD",
// .server_name = "test server",
// .version = "0.1.2",
// .max_payload = 1234,
// },
// };
// }
// fn initTestClient(
// io: std.Io,
// allocator: std.mem.Allocator,
// id: usize,
// data_from: []const u8,
// ) !struct {
// Client,
// *std.Io.Reader,
// *std.Io.Writer,
// } {
// return .init(io, allocator, id, .{}, in, out);
// }
// test {
// const gpa = std.testing.allocator;
// const io = std.testing.io;
// const server = initTestServer();
// const client: Client = .init(
// io,
// gpa,
// 1,
// .{},
// );
// }