This commit is contained in:
2025-11-19 09:49:32 -05:00
parent 5471cbe155
commit dbf189adc0
2 changed files with 29 additions and 15 deletions

View File

@@ -3,6 +3,7 @@ const zits = @import("zits");
const clap = @import("clap");
const MessageType = zits.MessageParser.MessageType;
const parseNextMessage = zits.MessageParser.parseNextMessage;
const SubCommands = enum {
help,
@@ -173,17 +174,12 @@ fn handleConnection(io: std.Io, stream: std.Io.net.Stream, info: ServerInfo) voi
fn processClient(in: *std.Io.Reader, out: *std.Io.Writer, info: ServerInfo) !void {
try writeInfo(out, info);
const initial_message_type = MessageType.parse((in.takeDelimiter(' ') catch return error.InvalidMessageType) orelse "") orelse return error.InvalidMessageType;
if (initial_message_type != .connect) return error.InvalidMessageType;
// move this inside client_state declaration
var json_parse_buf: [4096]u8 = undefined;
var json_parse_alloc_fb: std.heap.FixedBufferAllocator = std.heap.FixedBufferAllocator.init(&json_parse_buf);
var json_parse_alloc = json_parse_alloc_fb.allocator();
var json_reader: std.json.Reader = .init(json_parse_alloc, in);
std.debug.print("buffered:{s}\n", .{in.buffered()});
// var client_state = try std.json.parseFromSliceLeaky(ClientState, json_parse_alloc, in.buffered(), .{});
// in.toss(in.buffered().len);
@@ -193,23 +189,32 @@ fn processClient(in: *std.Io.Reader, out: *std.Io.Writer, info: ServerInfo) !voi
std.debug.print("client_state: {}\n", .{client_state});
while (true) {
// Rebase the next message to the start of the buffer
// in.rebase(in.buffer.len);
const next_message_type = MessageType.parse((in.takeDelimiter(' ') catch return error.InvalidMessageType) orelse "") orelse return error.InvalidMessageType;
const next_message_type = parseNextMessage(json_parse_alloc, in) orelse return;
switch (next_message_type) {
.connect => {
json_parse_alloc_fb = std.heap.FixedBufferAllocator.init(&json_parse_buf);
.connect => |connect| {
std.debug.print("connect: {s}\n", .{connect.name orelse "\"\""});
json_parse_alloc_fb = .init(&json_parse_buf);
json_parse_alloc = json_parse_alloc_fb.allocator();
json_reader = .init(json_parse_alloc, in);
// client_state = try std.json.parseFromTokenSourceLeaky(ClientState, json_parse_alloc, &json_reader, .{});
std.debug.print("client_state: {any}\n", .{client_state});
},
.ping => writePong(out) catch |err| {
std.debug.panic("failed to pong: {any}\n", .{err});
},
else => |msg| std.debug.print("received {}\n", .{msg}),
}
}
}
fn writePong(out: *std.Io.Writer) !void {
std.debug.print("in writePong\n", .{});
_ = try out.write("PONG");
_ = try out.write("\r\n");
try out.flush();
}
fn writeInfo(out: *std.Io.Writer, info: ServerInfo) !void {
_ = try out.write("INFO ");
try std.json.Stringify.value(info, .{}, out);