msg: rename MsgBuffer -> Buffer

Signed-off-by: Francis Bouvier <francis@lightpanda.io>
This commit is contained in:
Francis Bouvier
2024-11-28 00:50:44 +01:00
parent b800d0eeb8
commit 8f297b83c1
2 changed files with 15 additions and 15 deletions

View File

@@ -32,33 +32,33 @@ pub const Msg = struct {
} }
}; };
/// MsgBuffer returns messages from a raw text read stream, /// Buffer returns messages from a raw text read stream,
/// according to the following format `<msg_size>:<msg>`. /// with the message size being encoded on the 2 first bytes (little endian)
/// It handles both: /// It handles both:
/// - combined messages in one read /// - combined messages in one read
/// - single message in several reads (multipart) /// - single message in several reads (multipart)
/// It's safe (and a good practice) to reuse the same MsgBuffer /// It's safe (and a good practice) to reuse the same Buffer
/// on several reads of the same stream. /// on several reads of the same stream.
pub const MsgBuffer = struct { pub const Buffer = struct {
buf: []u8, buf: []u8,
size: usize = 0, size: usize = 0,
pos: usize = 0, pos: usize = 0,
fn isFinished(self: *MsgBuffer) bool { fn isFinished(self: *const Buffer) bool {
return self.pos >= self.size; return self.pos >= self.size;
} }
fn isEmpty(self: MsgBuffer) bool { fn isEmpty(self: *const Buffer) bool {
return self.size == 0 and self.pos == 0; return self.size == 0 and self.pos == 0;
} }
fn reset(self: *MsgBuffer) void { fn reset(self: *Buffer) void {
self.size = 0; self.size = 0;
self.pos = 0; self.pos = 0;
} }
// read input // read input
pub fn read(self: *MsgBuffer, input: []const u8) !struct { pub fn read(self: *Buffer, input: []const u8) !struct {
msg: []const u8, msg: []const u8,
left: []const u8, left: []const u8,
} { } {
@@ -78,7 +78,7 @@ pub const MsgBuffer = struct {
const is_multipart = !self.isEmpty() or _input.len < msg_size; const is_multipart = !self.isEmpty() or _input.len < msg_size;
if (is_multipart) { if (is_multipart) {
// set msg size on empty MsgBuffer // set msg size on empty Buffer
if (self.isEmpty()) { if (self.isEmpty()) {
self.size = msg_size; self.size = msg_size;
} }
@@ -91,7 +91,7 @@ pub const MsgBuffer = struct {
return error.MsgTooBig; return error.MsgTooBig;
} }
// copy the current input into MsgBuffer // copy the current input into Buffer
// NOTE: we could use @memcpy but it's not Thread-safe (alias problem) // NOTE: we could use @memcpy but it's not Thread-safe (alias problem)
// see https://www.openmymind.net/Zigs-memcpy-copyForwards-and-copyBackwards/ // see https://www.openmymind.net/Zigs-memcpy-copyForwards-and-copyBackwards/
// Intead we just use std.mem.copyForwards // Intead we just use std.mem.copyForwards
@@ -113,7 +113,7 @@ pub const MsgBuffer = struct {
} }
}; };
test "MsgBuffer" { test "Buffer" {
const Case = struct { const Case = struct {
input: []const u8, input: []const u8,
nb: u8, nb: u8,
@@ -144,14 +144,14 @@ test "MsgBuffer" {
.{ .input = "part", .nb = 1 }, .{ .input = "part", .nb = 1 },
}; };
var buf: [MaxSize]u8 = undefined; var b: [MaxSize]u8 = undefined;
var msg_buf = MsgBuffer{ .buf = &buf }; var buf = Buffer{ .buf = &b };
for (cases) |case| { for (cases) |case| {
var nb: u8 = 0; var nb: u8 = 0;
var input = case.input; var input = case.input;
while (input.len > 0) { while (input.len > 0) {
const parts = msg_buf.read(input) catch |err| { const parts = buf.read(input) catch |err| {
if (err == error.MsgMultipart) break; // go to the next case input if (err == error.MsgMultipart) break; // go to the next case input
return err; return err;
}; };

View File

@@ -30,7 +30,7 @@ const CloseError = jsruntime.IO.CloseError;
const CancelError = jsruntime.IO.CancelError; const CancelError = jsruntime.IO.CancelError;
const TimeoutError = jsruntime.IO.TimeoutError; const TimeoutError = jsruntime.IO.TimeoutError;
const MsgBuffer = @import("msg.zig").MsgBuffer; const MsgBuffer = @import("msg.zig").Buffer;
const MaxSize = @import("msg.zig").MaxSize; const MaxSize = @import("msg.zig").MaxSize;
const Browser = @import("browser/browser.zig").Browser; const Browser = @import("browser/browser.zig").Browser;
const cdp = @import("cdp/cdp.zig"); const cdp = @import("cdp/cdp.zig");