server: rename buf in read_buf

Signed-off-by: Francis Bouvier <francis@lightpanda.io>
This commit is contained in:
Francis Bouvier
2024-10-09 00:07:49 +02:00
parent c8a91d4cf6
commit cea38a10e9

View File

@@ -49,10 +49,9 @@ pub const Ctx = struct {
// internal fields // internal fields
accept_socket: std.posix.socket_t, accept_socket: std.posix.socket_t,
conn_socket: std.posix.socket_t = undefined, conn_socket: std.posix.socket_t = undefined,
buf: []u8, // only for read operations read_buf: []u8, // only for read operations
err: ?Error = null,
msg_buf: *MsgBuffer, msg_buf: *MsgBuffer,
err: ?Error = null,
// I/O fields // I/O fields
conn_completion: *Completion, conn_completion: *Completion,
@@ -86,7 +85,7 @@ pub const Ctx = struct {
self.loop.io.timeout(*Ctx, self, Ctx.timeoutCbk, self.timeout_completion, TimeoutCheck); self.loop.io.timeout(*Ctx, self, Ctx.timeoutCbk, self.timeout_completion, TimeoutCheck);
// receving incomming messages asynchronously // receving incomming messages asynchronously
self.loop.io.recv(*Ctx, self, Ctx.readCbk, self.conn_completion, self.conn_socket, self.buf); self.loop.io.recv(*Ctx, self, Ctx.readCbk, self.conn_completion, self.conn_socket, self.read_buf);
} }
fn readCbk(self: *Ctx, completion: *Completion, result: RecvError!usize) void { fn readCbk(self: *Ctx, completion: *Completion, result: RecvError!usize) void {
@@ -99,16 +98,16 @@ pub const Ctx = struct {
if (size == 0) { if (size == 0) {
// continue receving incomming messages asynchronously // continue receving incomming messages asynchronously
self.loop.io.recv(*Ctx, self, Ctx.readCbk, self.conn_completion, self.conn_socket, self.buf); self.loop.io.recv(*Ctx, self, Ctx.readCbk, self.conn_completion, self.conn_socket, self.read_buf);
return; return;
} }
// input // input
const input = self.buf[0..size];
if (std.log.defaultLogEnabled(.debug)) { if (std.log.defaultLogEnabled(.debug)) {
const content = input[0..@min(MaxStdOutSize, size)]; const content = input[0..@min(MaxStdOutSize, size)];
std.debug.print("\ninput size: {d}, content: {s}\n", .{ size, content }); std.debug.print("\ninput size: {d}, content: {s}\n", .{ size, content });
} }
const input = self.read_buf[0..size];
// read and execute input // read and execute input
self.msg_buf.read(self.alloc(), input, self, Ctx.do) catch |err| { self.msg_buf.read(self.alloc(), input, self, Ctx.do) catch |err| {
@@ -125,7 +124,7 @@ pub const Ctx = struct {
}; };
// continue receving incomming messages asynchronously // continue receving incomming messages asynchronously
self.loop.io.recv(*Ctx, self, Ctx.readCbk, self.conn_completion, self.conn_socket, self.buf); self.loop.io.recv(*Ctx, self, Ctx.readCbk, self.conn_completion, self.conn_socket, self.read_buf);
} }
fn timeoutCbk(self: *Ctx, completion: *Completion, result: TimeoutError!void) void { fn timeoutCbk(self: *Ctx, completion: *Completion, result: TimeoutError!void) void {
@@ -224,14 +223,16 @@ pub const Ctx = struct {
if (self.sessionNew) self.sessionNew = false; if (self.sessionNew) self.sessionNew = false;
// cdp end cmd
const res = cdp.do(self.alloc(), cmd, self) catch |err| { const res = cdp.do(self.alloc(), cmd, self) catch |err| {
// cdp end cmd
if (err == error.DisposeBrowserContext) { if (err == error.DisposeBrowserContext) {
// restart a new browser session // restart a new browser session
std.log.debug("cdp end cmd", .{}); std.log.debug("cdp end cmd", .{});
try self.newSession(); try self.newSession();
return; return;
} }
return err; return err;
}; };
@@ -366,7 +367,8 @@ pub fn sendSync(ctx: *Ctx, msg: []const u8) !void {
pub fn listen(browser: *Browser, loop: *public.Loop, server_socket: std.posix.socket_t) anyerror!void { pub fn listen(browser: *Browser, loop: *public.Loop, server_socket: std.posix.socket_t) anyerror!void {
// MsgBuffer // create buffers
var read_buf: [BufReadSize]u8 = undefined;
var msg_buf = try MsgBuffer.init(loop.alloc, BufReadSize * 256); // 256KB var msg_buf = try MsgBuffer.init(loop.alloc, BufReadSize * 256); // 256KB
defer msg_buf.deinit(loop.alloc); defer msg_buf.deinit(loop.alloc);
@@ -376,12 +378,11 @@ pub fn listen(browser: *Browser, loop: *public.Loop, server_socket: std.posix.so
// create I/O contexts and callbacks // create I/O contexts and callbacks
// for accepting connections and receving messages // for accepting connections and receving messages
var ctxInput: [BufReadSize]u8 = undefined;
var ctx = Ctx{ var ctx = Ctx{
.loop = loop, .loop = loop,
.browser = browser, .browser = browser,
.sessionNew = true, .sessionNew = true,
.buf = &ctxInput, .read_buf = &read_buf,
.msg_buf = &msg_buf, .msg_buf = &msg_buf,
.accept_socket = server_socket, .accept_socket = server_socket,
.conn_completion = &conn_completion, .conn_completion = &conn_completion,