mirror of
https://git.robbyzambito.me/zits
synced 2026-02-04 11:44:48 +00:00
The issue was the writer is not working to send pong ??
This commit is contained in:
@@ -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 {
|
||||
|
||||
Reference in New Issue
Block a user