mirror of
https://git.robbyzambito.me/zits
synced 2026-02-04 11:44:48 +00:00
This commit is contained in:
25
src/main.zig
25
src/main.zig
@@ -3,6 +3,7 @@ const zits = @import("zits");
|
|||||||
const clap = @import("clap");
|
const clap = @import("clap");
|
||||||
|
|
||||||
const MessageType = zits.MessageParser.MessageType;
|
const MessageType = zits.MessageParser.MessageType;
|
||||||
|
const parseNextMessage = zits.MessageParser.parseNextMessage;
|
||||||
|
|
||||||
const SubCommands = enum {
|
const SubCommands = enum {
|
||||||
help,
|
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 {
|
fn processClient(in: *std.Io.Reader, out: *std.Io.Writer, info: ServerInfo) !void {
|
||||||
try writeInfo(out, info);
|
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
|
// move this inside client_state declaration
|
||||||
var json_parse_buf: [4096]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_fb: std.heap.FixedBufferAllocator = std.heap.FixedBufferAllocator.init(&json_parse_buf);
|
||||||
var json_parse_alloc = json_parse_alloc_fb.allocator();
|
var json_parse_alloc = json_parse_alloc_fb.allocator();
|
||||||
var json_reader: std.json.Reader = .init(json_parse_alloc, in);
|
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(), .{});
|
// var client_state = try std.json.parseFromSliceLeaky(ClientState, json_parse_alloc, in.buffered(), .{});
|
||||||
// in.toss(in.buffered().len);
|
// 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});
|
std.debug.print("client_state: {}\n", .{client_state});
|
||||||
|
|
||||||
while (true) {
|
while (true) {
|
||||||
// Rebase the next message to the start of the buffer
|
const next_message_type = parseNextMessage(json_parse_alloc, in) orelse return;
|
||||||
// in.rebase(in.buffer.len);
|
|
||||||
const next_message_type = MessageType.parse((in.takeDelimiter(' ') catch return error.InvalidMessageType) orelse "") orelse return error.InvalidMessageType;
|
|
||||||
|
|
||||||
switch (next_message_type) {
|
switch (next_message_type) {
|
||||||
.connect => {
|
.connect => |connect| {
|
||||||
json_parse_alloc_fb = std.heap.FixedBufferAllocator.init(&json_parse_buf);
|
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_parse_alloc = json_parse_alloc_fb.allocator();
|
||||||
json_reader = .init(json_parse_alloc, in);
|
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});
|
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}),
|
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 {
|
fn writeInfo(out: *std.Io.Writer, info: ServerInfo) !void {
|
||||||
_ = try out.write("INFO ");
|
_ = try out.write("INFO ");
|
||||||
try std.json.Stringify.value(info, .{}, out);
|
try std.json.Stringify.value(info, .{}, out);
|
||||||
|
|||||||
@@ -123,15 +123,24 @@ fn parsePub(in: *std.Io.Reader) !Message.Pub {
|
|||||||
// try returning error in debug mode, only null in release?
|
// try returning error in debug mode, only null in release?
|
||||||
pub fn parseNextMessage(alloc: std.mem.Allocator, in: *std.Io.Reader) ?Message {
|
pub fn parseNextMessage(alloc: std.mem.Allocator, in: *std.Io.Reader) ?Message {
|
||||||
const message_type: MessageType = blk: {
|
const message_type: MessageType = blk: {
|
||||||
const word: []const u8 = (in.takeDelimiter(' ') catch return null) orelse return null;
|
var word: ["CONNECT".len]u8 = undefined;
|
||||||
std.debug.print("word: {s}\n", .{word});
|
var len: usize = 0;
|
||||||
break :blk MessageType.parse(word) orelse return null;
|
for (&word, 0..) |*b, i| {
|
||||||
|
const byte = in.takeByte() catch return null;
|
||||||
|
if (std.ascii.isUpper(byte)) {
|
||||||
|
b.* = byte;
|
||||||
|
len = i + 1;
|
||||||
|
} else break;
|
||||||
|
}
|
||||||
|
std.debug.print("word: '{s}'\n", .{word[0..len]});
|
||||||
|
break :blk MessageType.parse(word[0..len]) orelse return null;
|
||||||
};
|
};
|
||||||
// defer in.toss(2); // CRLF
|
std.debug.print("buffered: '{s}'\n", .{in.buffered()});
|
||||||
|
defer in.toss(2); // CRLF
|
||||||
return switch (message_type) {
|
return switch (message_type) {
|
||||||
.connect => .{ .connect = parseJsonMessage(Message.Connect, alloc, in) catch return null },
|
.connect => .{ .connect = parseJsonMessage(Message.Connect, alloc, in) catch return null },
|
||||||
.@"pub" => .{ .@"pub" = parsePub(in) catch |err| std.debug.panic("{}", .{err}) },
|
.@"pub" => .{ .@"pub" = parsePub(in) catch |err| std.debug.panic("{}", .{err}) },
|
||||||
.ping => .{ .ping = {} },
|
.ping => .ping,
|
||||||
else => null,
|
else => null,
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user