From e60a566a7c61cacc09213a23a708ab2f7d78a3ac Mon Sep 17 00:00:00 2001 From: Robby Zambito Date: Wed, 31 Dec 2025 01:05:05 +0000 Subject: 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. --- src/server/client.zig | 23 ++++++++++------------- 1 file changed, 10 insertions(+), 13 deletions(-) (limited to 'src/server/client.zig') diff --git a/src/server/client.zig b/src/server/client.zig index ed1d33e..70673c2 100644 --- a/src/server/client.zig +++ b/src/server/client.zig @@ -2,40 +2,36 @@ const Message = @import("message_parser.zig").Message; const std = @import("std"); pub const ClientState = struct { - id: usize, connect: Message.AllocatedConnect, /// 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, from_client: *std.Io.Reader, to_client: *std.Io.Writer, - task: std.Io.Future(void), + task: ?std.Io.Future(void) = null, pub fn init( - io: std.Io, - allocator: std.mem.Allocator, - id: usize, connect: Message.AllocatedConnect, in: *std.Io.Reader, out: *std.Io.Writer, ) !ClientState { var res: ClientState = .{ - .id = id, .connect = connect, - .recv_queue = try allocator.create(std.Io.Queue(Message)), .from_client = in, .to_client = out, - .task = undefined, }; - res.recv_queue.* = .init(&res.recv_queue_buffer); - res.task = try io.concurrent(processWrite, .{ &res, io }); + res.recv_queue = .init(&res.recv_queue_buffer); return res; } + pub fn start(self: *ClientState, io: std.Io) !void { + self.task = try io.concurrent(processWrite, .{ self, io }); + } + fn processWrite( self: *ClientState, 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 { - self.task.cancel(io); + if (self.task) |*t| { + t.cancel(io); + } self.connect.deinit(); _ = allocator; // allocator.destroy(self.recv_queue); -- cgit