Handle several JSON msg in 1 read

Signed-off-by: Francis Bouvier <francis@lightpanda.io>
This commit is contained in:
Francis Bouvier
2024-04-16 16:38:15 +02:00
parent 67bbd9957d
commit 211fa3d947

View File

@@ -35,7 +35,11 @@ pub const Cmd = struct {
return; return;
}; };
const input = self.buf[0..size]; // input
var input = self.buf[0..size];
if (std.log.defaultLogEnabled(.debug)) {
std.debug.print("\ninput {s}\n", .{input});
}
// close on exit command // close on exit command
if (std.mem.eql(u8, input, "exit")) { if (std.mem.eql(u8, input, "exit")) {
@@ -43,13 +47,21 @@ pub const Cmd = struct {
return; return;
} }
// input // cmds
if (std.log.defaultLogEnabled(.debug)) { var cmd: []const u8 = undefined;
std.debug.print("\ninput {s}\n", .{input}); while (true) {
// handle several JSON msg in 1 read
const pos = std.mem.indexOf(u8, input, "}{");
if (pos) |p| {
cmd = input[0 .. p + 1];
input = input[p + 1 ..];
} else {
cmd = input;
} }
// cdp // cdp
const res = cdp.do(self.alloc(), input, self) catch |err| { const res = cdp.do(self.alloc(), cmd, self) catch |err| {
if (cdp.isCdpError(err)) |e| { if (cdp.isCdpError(err)) |e| {
self.err = e; self.err = e;
return; return;
@@ -60,6 +72,11 @@ pub const Cmd = struct {
sendAsync(self, res) catch unreachable; sendAsync(self, res) catch unreachable;
if (pos == null) break;
// TODO: handle 1 read smaller than a complete JSON msg
}
// continue receving incomming messages asynchronously // continue receving incomming messages asynchronously
self.loop().io.recv(*Cmd, self, cbk, completion, self.socket, self.buf); self.loop().io.recv(*Cmd, self, cbk, completion, self.socket, self.buf);
} }