summaryrefslogtreecommitdiff
path: root/src/server/Client.zig
diff options
context:
space:
mode:
authorRobby Zambito <contact@robbyzambito.me>2026-01-06 10:03:03 -0500
committerRobby Zambito <contact@robbyzambito.me>2026-01-06 10:04:10 -0500
commit6e9f6998bd15f3c4dabd0ba28a1973573acdc765 (patch)
tree98ac70533f4880f9ec0ed310fe205e32b020aa78 /src/server/Client.zig
parent318d467f5c692e78d16df9c0c32b5e7999343e65 (diff)
Use client allocator to own incoming messages to a client
Diffstat (limited to 'src/server/Client.zig')
-rw-r--r--src/server/Client.zig14
1 files changed, 9 insertions, 5 deletions
diff --git a/src/server/Client.zig b/src/server/Client.zig
index d099ecb..690cabf 100644
--- a/src/server/Client.zig
+++ b/src/server/Client.zig
@@ -10,6 +10,8 @@ pub const Msgs = union(enum) {
};
connect: ?Message.Connect,
+// Used to own messages that we receive in our queues.
+alloc: std.mem.Allocator,
// Messages for this client to receive.
recv_queue: *Queue(Message),
@@ -20,6 +22,7 @@ to_client: *std.Io.Writer,
pub fn init(
connect: ?Message.Connect,
+ alloc: std.mem.Allocator,
recv_queue: *Queue(Message),
msg_queue: *Queue(Msgs),
in: *std.Io.Reader,
@@ -27,6 +30,7 @@ pub fn init(
) Client {
return .{
.connect = connect,
+ .alloc = alloc,
.recv_queue = recv_queue,
.msg_queue = msg_queue,
.from_client = in,
@@ -41,7 +45,7 @@ pub fn deinit(self: *Client, alloc: std.mem.Allocator) void {
self.* = undefined;
}
-pub fn start(self: *Client, io: std.Io, alloc: std.mem.Allocator) !void {
+pub fn start(self: *Client, io: std.Io) !void {
var msgs_buf: [1024]Msgs = undefined;
var recv_msgs_task = io.concurrent(Queue(Msgs).get, .{ self.msg_queue, io, &msgs_buf, 1 }) catch @panic("Concurrency unavailable");
@@ -58,15 +62,15 @@ pub fn start(self: *Client, io: std.Io, alloc: std.mem.Allocator) !void {
for (0..msgs.len) |i| {
const msg = msgs[i];
defer switch (msg) {
- .MSG => |m| m.deinit(alloc),
- .HMSG => |h| h.deinit(alloc),
+ .MSG => |m| m.deinit(self.alloc),
+ .HMSG => |h| h.deinit(self.alloc),
};
errdefer for (msgs[i + 1 ..]) |mg| switch (mg) {
.MSG => |m| {
- m.deinit(alloc);
+ m.deinit(self.alloc);
},
.HMSG => |h| {
- h.deinit(alloc);
+ h.deinit(self.alloc);
},
};
switch (msg) {