This commit is contained in:
2025-11-18 13:49:39 -05:00
parent d6d177aede
commit 51008cd7e1
4 changed files with 201 additions and 101 deletions

View File

@@ -2,6 +2,8 @@ const std = @import("std");
const zits = @import("zits");
const clap = @import("clap");
const MessageType = zits.MessageParser.MessageType;
const SubCommands = enum {
help,
serve,
@@ -135,12 +137,12 @@ fn handleConnection(io: std.Io, stream: std.Io.net.Stream, info: ServerInfo) voi
var writer = stream.writer(io, &w_buffer);
const out = &writer.interface;
var r_buffer: [1024]u8 = undefined;
var r_buffer: [8192]u8 = undefined;
var reader = stream.reader(io, &r_buffer);
const in = &reader.interface;
processClient(in, out, info) catch |err| {
std.debug.print("Error processing client: {}\n", .{err});
std.debug.panic("Error processing client: {}\n", .{err});
};
// var stdout_buffer: [1024]u8 = undefined;
@@ -171,102 +173,36 @@ 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 ClientState = struct {
verbose: bool = false,
pedantic: bool = false,
tls_required: bool = false,
auth_token: ?[]const u8 = null,
user: ?[]const u8 = null,
pass: ?[]const u8 = null,
name: ?[]const u8 = null,
lang: []const u8,
version: []const u8,
protocol: u32,
echo: ?bool = null,
sig: ?[]const u8 = null,
jwt: ?[]const u8 = null,
no_responders: ?bool = null,
headers: ?bool = null,
nkey: ?[]const u8 = null,
};
const MessageType = enum {
info,
connect,
@"pub",
hpub,
sub,
unsub,
msg,
hmsg,
ping,
pong,
@"+ok",
@"-err",
// fn parse(input: []u8) !MessageType {
// // if (std.mem.eql(u8, "INFO", input)) return .info;
// if (std.mem.eql(u8, "CONNECT", input)) return .connect;
// if (std.mem.eql(u8, "PUB", input)) return .@"pub";
// if (std.mem.eql(u8, "HPUB", input)) return .hpub;
// if (std.mem.eql(u8, "SUB", input)) return .sub;
// if (std.mem.eql(u8, "UNSUB", input)) return .unsub;
// // if (std.mem.eql(u8, "MSG", input)) return .msg;
// // if (std.mem.eql(u8, "HMSG", input)) return .hmsg;
// if (std.mem.eql(u8, "PING", input)) return .ping;
// if (std.mem.eql(u8, "PONG", input)) return .pong;
// // if (std.mem.eql(u8, "@"+OK"", input)) return .@"+ok";
// // if (std.mem.eql(u8, "@"-ERR"", input)) return .@"-err";
// return error.InvalidMessageType;
// }
const client_types = std.StaticStringMap(@This()).initComptime(
.{
// {"INFO", .info},
.{ "CONNECT", .connect },
.{ "PUB", .@"pub" },
.{ "HPUB", .hpub },
.{ "SUB", .sub },
.{ "UNSUB", .unsub },
// {"MSG", .msg},
// {"HMSG", .hmsg},
.{ "PING", .ping },
.{ "PONG", .pong },
// {"+OK", .@"+ok"},
// {"-ERR", .@"-err"},
},
);
fn parse(input: []u8) !@This() {
return client_types.get(input) orelse return error.InvalidMessageType;
}
};
const initial_message_type = try MessageType.parse((in.takeDelimiter(' ') catch return error.InvalidMessageType) orelse return error.InvalidMessageType);
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: [1024]u8 = undefined;
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.parseFromTokenSourceLeaky(ClientState, json_parse_alloc, &json_reader, .{});
// var client_state = try std.json.parseFromSliceLeaky(ClientState, json_parse_alloc, in.buffered(), .{});
// in.toss(in.buffered().len);
std.debug.print("client_state: {any}\n", .{client_state});
// var client_state = try std.json.parseFromTokenSourceLeaky(ClientState, json_parse_alloc, &json_reader, .{});
const client_state = 0;
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 = try MessageType.parse((in.takeDelimiter(' ') catch return error.InvalidMessageType) orelse return error.InvalidMessageType);
const next_message_type = MessageType.parse((in.takeDelimiter(' ') catch return error.InvalidMessageType) orelse "") orelse return error.InvalidMessageType;
switch (next_message_type) {
.connect => {
json_parse_alloc_fb = std.heap.FixedBufferAllocator.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, .{});
// client_state = try std.json.parseFromTokenSourceLeaky(ClientState, json_parse_alloc, &json_reader, .{});
std.debug.print("client_state: {any}\n", .{client_state});
},
else => |msg| std.debug.print("received {}\n", .{msg}),