summaryrefslogtreecommitdiff
path: root/src/server/main.zig
diff options
context:
space:
mode:
Diffstat (limited to 'src/server/main.zig')
-rw-r--r--src/server/main.zig29
1 files changed, 18 insertions, 11 deletions
diff --git a/src/server/main.zig b/src/server/main.zig
index 0d90e55..4e47c10 100644
--- a/src/server/main.zig
+++ b/src/server/main.zig
@@ -10,6 +10,11 @@ const Subscription = struct {
subject: []const u8,
client_id: usize,
sid: []const u8,
+
+ fn deinit(self: Subscription, alloc: std.mem.Allocator) void {
+ alloc.free(self.subject);
+ alloc.free(self.sid);
+ }
};
info: ServerInfo,
@@ -29,12 +34,10 @@ pub fn deinit(server: *Server, io: std.Io, alloc: std.mem.Allocator) void {
server.subs_lock.lockUncancelable(io);
defer server.subs_lock.unlock(io);
for (server.subscriptions.items) |sub| {
- alloc.free(sub.sid);
- alloc.free(sub.subject);
+ sub.deinit(alloc);
}
- server.subscriptions.shrinkAndFree(alloc, 0);
-
- server.clients.clearAndFree(alloc);
+ server.subscriptions.deinit(alloc);
+ server.clients.deinit(alloc);
}
pub fn main(alloc: std.mem.Allocator, server_config: ServerInfo) !void {
@@ -118,8 +121,7 @@ fn removeClient(server: *Server, io: std.Io, allocator: std.mem.Allocator, id: u
const i = len - from_end - 1;
const sub = server.subscriptions.items[i];
if (sub.client_id == id) {
- allocator.free(sub.sid);
- allocator.free(sub.subject);
+ sub.deinit(allocator);
_ = server.subscriptions.swapRemove(i);
}
}
@@ -205,9 +207,11 @@ fn handleConnection(
try server.publishMessage(io, server_allocator, &client, msg);
},
.sub => |sub| {
+ defer sub.deinit(server_allocator);
try server.subscribe(io, server_allocator, id, sub);
},
.unsub => |unsub| {
+ defer unsub.deinit(server_allocator);
try server.unsubscribe(io, server_allocator, id, unsub);
},
.connect => |connect| {
@@ -313,10 +317,14 @@ fn publishMessage(server: *Server, io: std.Io, alloc: std.mem.Allocator, source_
fn subscribe(server: *Server, io: std.Io, gpa: std.mem.Allocator, id: usize, msg: Message.Sub) !void {
try server.subs_lock.lock(io);
defer server.subs_lock.unlock(io);
+ const subject = try gpa.dupe(u8, msg.subject);
+ errdefer gpa.free(subject);
+ const sid = try gpa.dupe(u8, msg.sid);
+ errdefer gpa.free(sid);
try server.subscriptions.append(gpa, .{
- .subject = msg.subject,
+ .subject = subject,
.client_id = id,
- .sid = msg.sid,
+ .sid = sid,
});
}
@@ -328,8 +336,7 @@ fn unsubscribe(server: *Server, io: std.Io, gpa: std.mem.Allocator, id: usize, m
const i = len - from_end - 1;
const sub = server.subscriptions.items[i];
if (sub.client_id == id and std.mem.eql(u8, sub.sid, msg.sid)) {
- gpa.free(sub.sid);
- gpa.free(sub.subject);
+ sub.deinit(gpa);
_ = server.subscriptions.swapRemove(i);
}
}