Cleanup client

Break up creating and starting the client process.
I think this should simplify storing the std.Io.Queue on the stack.
Before I was storing it on the heap because it was hard to make it point to the same location if I was initializing the client on the stack.
This commit is contained in:
2025-12-31 01:05:05 +00:00
parent 1cbd030037
commit e60a566a7c
2 changed files with 14 additions and 16 deletions

View File

@@ -2,40 +2,36 @@ const Message = @import("message_parser.zig").Message;
const std = @import("std"); const std = @import("std");
pub const ClientState = struct { pub const ClientState = struct {
id: usize,
connect: Message.AllocatedConnect, connect: Message.AllocatedConnect,
/// Messages that this client should receive. /// Messages that this client should receive.
recv_queue: *std.Io.Queue(Message), recv_queue: std.Io.Queue(Message) = undefined,
recv_queue_buffer: [1024]Message = undefined, recv_queue_buffer: [1024]Message = undefined,
from_client: *std.Io.Reader, from_client: *std.Io.Reader,
to_client: *std.Io.Writer, to_client: *std.Io.Writer,
task: std.Io.Future(void), task: ?std.Io.Future(void) = null,
pub fn init( pub fn init(
io: std.Io,
allocator: std.mem.Allocator,
id: usize,
connect: Message.AllocatedConnect, connect: Message.AllocatedConnect,
in: *std.Io.Reader, in: *std.Io.Reader,
out: *std.Io.Writer, out: *std.Io.Writer,
) !ClientState { ) !ClientState {
var res: ClientState = .{ var res: ClientState = .{
.id = id,
.connect = connect, .connect = connect,
.recv_queue = try allocator.create(std.Io.Queue(Message)),
.from_client = in, .from_client = in,
.to_client = out, .to_client = out,
.task = undefined,
}; };
res.recv_queue.* = .init(&res.recv_queue_buffer); res.recv_queue = .init(&res.recv_queue_buffer);
res.task = try io.concurrent(processWrite, .{ &res, io });
return res; return res;
} }
pub fn start(self: *ClientState, io: std.Io) !void {
self.task = try io.concurrent(processWrite, .{ self, io });
}
fn processWrite( fn processWrite(
self: *ClientState, self: *ClientState,
io: std.Io, io: std.Io,
@@ -60,11 +56,12 @@ pub const ClientState = struct {
}, },
} }
} }
self.task.cancel(io);
} }
pub fn deinit(self: *ClientState, io: std.Io, allocator: std.mem.Allocator) void { pub fn deinit(self: *ClientState, io: std.Io, allocator: std.mem.Allocator) void {
self.task.cancel(io); if (self.task) |*t| {
t.cancel(io);
}
self.connect.deinit(); self.connect.deinit();
_ = allocator; _ = allocator;
// allocator.destroy(self.recv_queue); // allocator.destroy(self.recv_queue);

View File

@@ -100,7 +100,8 @@ fn handleConnection(
var connect_arena: std.heap.ArenaAllocator = .init(allocator); var connect_arena: std.heap.ArenaAllocator = .init(allocator);
defer connect_arena.deinit(); defer connect_arena.deinit();
const connect = (Message.next(connect_arena.allocator(), in) catch return).connect; const connect = (Message.next(connect_arena.allocator(), in) catch return).connect;
var client_state: ClientState = try .init(io, allocator, id, connect, in, out); var client_state: ClientState = try .init(connect, in, out);
try client_state.start(io);
defer client_state.deinit(io, allocator); defer client_state.deinit(io, allocator);
try server.addClient(allocator, id, &client_state); try server.addClient(allocator, id, &client_state);
@@ -123,10 +124,10 @@ fn handleConnection(
} }
}, },
.sub => |sub| { .sub => |sub| {
try server.subscribe(allocator, client_state.id, sub); try server.subscribe(allocator, id, sub);
}, },
.unsub => |unsub| { .unsub => |unsub| {
try server.unsubscribe(client_state.id, unsub); try server.unsubscribe(id, unsub);
}, },
else => |e| { else => |e| {
std.debug.panic("Unimplemented message: {any}\n", .{e}); std.debug.panic("Unimplemented message: {any}\n", .{e});