The issue was the writer is not working to send pong ??

This commit is contained in:
2025-12-09 17:39:29 -05:00
parent 1eeb55ff4d
commit 30cc017081
2 changed files with 151 additions and 192 deletions

View File

@@ -4,21 +4,18 @@ const std = @import("std");
pub const ClientState = struct {
id: usize,
connect: Message.AllocatedConnect,
/// Messages that this client is trying to send.
send_queue: std.Io.Queue(Message),
send_queue_buffer: [1024]Message = undefined,
/// Messages that this client should receive.
recv_queue: std.Io.Queue(Message),
recv_queue_buffer: [1024]Message = undefined,
io_group: std.Io.Group = .init,
from_client: *std.Io.Reader,
to_client: *std.Io.Writer,
task: std.Io.Future(void),
pub fn init(
io: std.Io,
allocator: std.mem.Allocator,
id: usize,
connect: Message.AllocatedConnect,
in: *std.Io.Reader,
@@ -27,15 +24,13 @@ pub const ClientState = struct {
var res: ClientState = .{
.id = id,
.connect = connect,
.send_queue = undefined,
.recv_queue = undefined,
.from_client = in,
.to_client = out,
.task = undefined,
};
res.send_queue = .init(&res.send_queue_buffer);
res.recv_queue = .init(&res.recv_queue_buffer);
try res.io_group.concurrent(io, processWrite, .{ &res, io, out });
try res.io_group.concurrent(io, processRead, .{ &res, io, allocator, in });
res.task = try io.concurrent(processWrite, .{ &res, io, out });
return res;
}
@@ -47,6 +42,7 @@ pub const ClientState = struct {
) void {
while (true) {
const message = self.recv_queue.getOne(io) catch continue;
std.debug.print("got message in write loop to send to client: {any}\n", .{message});
switch (message) {
.@"+ok" => {
writeOk(out) catch break;
@@ -67,63 +63,23 @@ pub const ClientState = struct {
}
}
fn processRead(
self: *ClientState,
io: std.Io,
allocator: std.mem.Allocator,
in: *std.Io.Reader,
) void {
// io.sleep(.fromMilliseconds(100), .real) catch @panic("couldn't sleep");
while (true) {
// std.debug.print("waiting for message\n", .{});
const next_message = Message.next(allocator, in) catch |err| switch (err) {
error.EndOfStream => {
self.send_queue.putOne(io, .eos) catch {};
break;
},
else => {
std.debug.panic("guh: {any}\n", .{err});
break;
},
};
std.debug.print("next_message: {any}\n", .{next_message});
switch (next_message) {
.eos => {
self.send_queue.putOne(io, next_message) catch {};
break;
},
else => {
self.send_queue.putOne(io, next_message) catch break;
},
}
}
std.debug.print("no more messages\n", .{});
}
pub fn deinit(self: *ClientState, alloc: std.mem.Allocator) void {
_ = self;
// self.write_task.cancel() catch {
// std.debug.print("failed to cancel write task of {}\n", .{self.id});
// };
// self.read_taks.cancel() catch {
// std.debug.print("failed to cancel read task of {}\n", .{self.id});
// };
// TODO: dealloc things.
_ = alloc;
pub fn deinit(self: *ClientState, io: std.Io) void {
self.task.cancel(io);
self.connect.deinit();
}
/// Return true if the value was put in the clients buffer to process, else false.
pub fn send(self: *ClientState, io: std.Io, msg: Message) std.Io.Cancelable!bool {
return (try self.recv_queue.put(io, &.{msg}, 0)) > 0;
try self.recv_queue.putOne(io, msg);
return true;
}
pub fn next(self: *ClientState, io: std.Io) !Message {
std.debug.print("in client awaiting next message\n", .{});
errdefer std.debug.print("actually it was canceled\n", .{});
defer std.debug.print("client returning next message!\n", .{});
return self.send_queue.getOne(io);
pub fn next(self: *ClientState, allocator: std.mem.Allocator) !Message {
// std.debug.print("in client awaiting next message\n", .{});
// errdefer std.debug.print("actually it was canceled\n", .{});
// defer std.debug.print("client returning next message!\n", .{});
return Message.next(allocator, self.from_client);
// return self.send_queue.getOne(io);
}
};
@@ -132,10 +88,12 @@ fn writeOk(out: *std.Io.Writer) !void {
try out.flush();
}
fn writePong(out: *std.Io.Writer) !void {
pub fn writePong(out: *std.Io.Writer) !void {
std.debug.print("writing pong\n", .{});
_ = try out.write("PONG\r\n");
try out.flush();
for (0..10000) |_| {
_ = try out.write("PONG\r\n");
try out.flush();
}
}
pub fn writeInfo(out: *std.Io.Writer, info: Message.ServerInfo) !void {

View File

@@ -71,7 +71,8 @@ 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 = try .init(io, allocator, id, connect, in, out);
var client_state: ClientState = try .init(io, id, connect, in, out);
defer client_state.deinit(io);
try server.addClient(allocator, id, &client_state);
defer server.removeClient(allocator, id);
@@ -96,9 +97,133 @@ fn handleConnection(
// server.subscriptions.unlockPointers();
// }
server.processClient(allocator, io, &client_state) catch |err| {
std.debug.panic("Error processing client: {}\n", .{err});
};
{
defer std.debug.print("done processing client??\n", .{});
std.debug.print("processing client: {d}\n", .{client_state.id});
std.debug.print("awaiting next message from client\n", .{});
while (client_state.next(allocator)) |msg| {
std.debug.print("message from client!: {any}\n", .{msg});
switch (msg) {
.ping => {
std.debug.print("got a ping! sending a pong.\n", .{});
@import("./client.zig").writePong(out) catch return;
for (0..5) |_| {
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 {d}\n", .{client_state.id});
}
},
.@"pub" => |@"pub"| {
std.debug.print("pub: {any}\n", .{@"pub"});
try server.publishMessage(io, @"pub");
if (client_state.connect.connect.verbose) {
std.debug.print("server IS sending +ok\n", .{});
_ = try client_state.send(io, .@"+ok");
} else {
std.debug.print("server NOT sending +ok\n", .{});
}
},
.sub => |sub| {
try server.subscribe(allocator, client_state.id, sub);
},
.eos => {
break;
},
else => |e| {
std.debug.panic("Unimplemented message: {any}\n", .{e});
},
}
std.debug.print("processed message from client\n", .{});
std.debug.print("awaiting next message from client\n", .{});
} else |_| {}
// while (!io.cancelRequested()) {
// if (client_state.send_queue.getOne(io)) |msg| {
// switch (msg) {
// // Respond to ping with pong.
// .ping => {
// try client_state.recv_queue.putOne(io, .pong);
// },
// .@"pub" => |p| {
// std.debug.print("subs (in pub): {any}\n", .{server.subscriptions});
// std.debug.print("subs size: {d}\n", .{server.subscriptions.size});
// std.debug.print("subs subjects:\n", .{});
// var key_iter = server.subscriptions.keyIterator();
// while (key_iter.next()) |k| {
// std.debug.print("- {s}\n", .{k.*});
// } else std.debug.print("<none>\n", .{});
// std.debug.print("pub subject: '{s}'\n", .{p.subject});
// std.debug.print("pub: {any}\n", .{p});
// errdefer _ = client_state.recv_queue.put(io, &.{.@"-err"}, 1) catch {};
// // Just publishing to exact matches right now.
// // TODO: Publish to pattern matching subjects.
// if (server.subscriptions.get(p.subject)) |subs| {
// var subs_iter = subs.iterator();
// while (subs_iter.next()) |sub| {
// var client = server.clients.get(sub.key_ptr.*) orelse std.debug.panic("Trying to pub to a client that doesn't exist!\n", .{});
// std.debug.print("{d} is pubbing to {d}\n", .{ client_state.id, client.id });
// if ((try client.recv_queue.put(
// io,
// &.{
// .{
// .msg = .{
// .subject = p.subject,
// .sid = sub.value_ptr.*,
// .reply_to = p.reply_to,
// .payload = p.payload,
// },
// },
// },
// 0,
// )) > 0) {
// std.debug.print("published message!\n", .{});
// } else {
// std.debug.print("skipped publishing for some reason\n", .{});
// }
// }
// try client_state.recv_queue.putOne(io, .@"+ok");
// } else {
// std.debug.print("no subs with subject\n", .{});
// }
// },
// .sub => |s| {
// var subscribers = try server.subscriptions.getOrPut(gpa, s.subject);
// if (!subscribers.found_existing) {
// subscribers.value_ptr.* = .empty;
// }
// try subscribers.value_ptr.*.put(gpa, client_state.id, s.sid);
// std.debug.print("subs: {any}\n", .{server.subscriptions});
// },
// .info => |info| {
// std.debug.panic("got an info message? : {any}\n", .{info});
// },
// else => |m| {
// std.debug.panic("Unimplemented: {any}\n", .{m});
// },
// }
// } else |err| return err;
// }
// while (true) {
// switch (next_message) {
// .connect => |connect| {
// std.debug.panic("Connection message after already connected: {any}\n", .{connect});
// },
// .ping => try writePong(out),
// .@"pub" => try writeOk(out),
// else => |msg| std.debug.panic("Message type not implemented: {any}\n", .{msg}),
// }
// }
}
client_state.task.await(io);
}
// // Result is owned by the caller
@@ -138,130 +263,6 @@ fn subscribe(server: *Server, gpa: std.mem.Allocator, id: usize, msg: Message.Su
try server.subscriptions.put(gpa, msg.subject, subs_for_subject);
}
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});
std.debug.print("awaiting next message from client\n", .{});
while (client_state.next(io)) |msg| {
std.debug.print("message from client!: {any}\n", .{msg});
switch (msg) {
.ping => {
std.debug.print("got a ping! sending a pong.\n", .{});
for (0..5) |_| {
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 {d}\n", .{client_state.id});
}
},
.@"pub" => |@"pub"| {
std.debug.print("pub: {any}\n", .{@"pub"});
try server.publishMessage(io, @"pub");
if (client_state.connect.connect.verbose) {
_ = try client_state.send(io, .@"+ok");
}
},
.sub => |sub| {
try server.subscribe(gpa, client_state.id, sub);
},
.eos => {
client_state.io_group.wait(io);
break;
},
else => |e| {
std.debug.panic("Unimplemented message: {any}\n", .{e});
},
}
std.debug.print("processed message from client\n", .{});
std.debug.print("awaiting next message from client\n", .{});
} else |_| {}
// while (!io.cancelRequested()) {
// if (client_state.send_queue.getOne(io)) |msg| {
// switch (msg) {
// // Respond to ping with pong.
// .ping => {
// try client_state.recv_queue.putOne(io, .pong);
// },
// .@"pub" => |p| {
// std.debug.print("subs (in pub): {any}\n", .{server.subscriptions});
// std.debug.print("subs size: {d}\n", .{server.subscriptions.size});
// std.debug.print("subs subjects:\n", .{});
// var key_iter = server.subscriptions.keyIterator();
// while (key_iter.next()) |k| {
// std.debug.print("- {s}\n", .{k.*});
// } else std.debug.print("<none>\n", .{});
// std.debug.print("pub subject: '{s}'\n", .{p.subject});
// std.debug.print("pub: {any}\n", .{p});
// errdefer _ = client_state.recv_queue.put(io, &.{.@"-err"}, 1) catch {};
// // Just publishing to exact matches right now.
// // TODO: Publish to pattern matching subjects.
// if (server.subscriptions.get(p.subject)) |subs| {
// var subs_iter = subs.iterator();
// while (subs_iter.next()) |sub| {
// var client = server.clients.get(sub.key_ptr.*) orelse std.debug.panic("Trying to pub to a client that doesn't exist!\n", .{});
// std.debug.print("{d} is pubbing to {d}\n", .{ client_state.id, client.id });
// if ((try client.recv_queue.put(
// io,
// &.{
// .{
// .msg = .{
// .subject = p.subject,
// .sid = sub.value_ptr.*,
// .reply_to = p.reply_to,
// .payload = p.payload,
// },
// },
// },
// 0,
// )) > 0) {
// std.debug.print("published message!\n", .{});
// } else {
// std.debug.print("skipped publishing for some reason\n", .{});
// }
// }
// try client_state.recv_queue.putOne(io, .@"+ok");
// } else {
// std.debug.print("no subs with subject\n", .{});
// }
// },
// .sub => |s| {
// var subscribers = try server.subscriptions.getOrPut(gpa, s.subject);
// if (!subscribers.found_existing) {
// subscribers.value_ptr.* = .empty;
// }
// try subscribers.value_ptr.*.put(gpa, client_state.id, s.sid);
// std.debug.print("subs: {any}\n", .{server.subscriptions});
// },
// .info => |info| {
// std.debug.panic("got an info message? : {any}\n", .{info});
// },
// else => |m| {
// std.debug.panic("Unimplemented: {any}\n", .{m});
// },
// }
// } else |err| return err;
// }
// while (true) {
// switch (next_message) {
// .connect => |connect| {
// std.debug.panic("Connection message after already connected: {any}\n", .{connect});
// },
// .ping => try writePong(out),
// .@"pub" => try writeOk(out),
// else => |msg| std.debug.panic("Message type not implemented: {any}\n", .{msg}),
// }
// }
}
pub fn createId() []const u8 {
return "SERVERID";
}