Skip to content

Commit bcdcfee

Browse files
Merge pull request #323 from lightpanda-io/cdp_msg_size
Cdp msg size
2 parents 4b8c3cb + 913d3af commit bcdcfee

File tree

2 files changed

+19
-16
lines changed

2 files changed

+19
-16
lines changed

src/handler.zig

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ pub const Stream = struct {
4949
}
5050

5151
fn closeCDP(self: *const Stream) void {
52-
const close_msg: []const u8 = .{ 5, 0 } ++ "close";
52+
const close_msg: []const u8 = .{ 5, 0, 0, 0 } ++ "close";
5353
self.recv(close_msg) catch |err| {
5454
log.err("stream close error: {any}", .{err});
5555
};
@@ -87,7 +87,7 @@ pub const Handler = struct {
8787
}
8888

8989
pub fn clientMessage(self: *Handler, data: []const u8) !void {
90-
var header: [2]u8 = undefined;
90+
var header: [4]u8 = undefined;
9191
Msg.setSize(data.len, &header);
9292
try self.stream.recv(&header);
9393
try self.stream.recv(data);

src/msg.zig

Lines changed: 17 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -18,17 +18,20 @@
1818

1919
const std = @import("std");
2020

21-
pub const MsgSize = 16 * 1204; // 16KB
22-
pub const HeaderSize = 2;
21+
pub const HeaderSize = 4;
22+
pub const MsgSize = 256 * 1204; // 256KB
23+
// NOTE: Theorically we could go up to 4GB with a 4 bytes binary encoding
24+
// but we prefer to put a lower hard limit for obvious memory size reasons.
25+
2326
pub const MaxSize = HeaderSize + MsgSize;
2427

2528
pub const Msg = struct {
2629
pub fn getSize(data: []const u8) usize {
27-
return std.mem.readInt(u16, data[0..HeaderSize], .little);
30+
return std.mem.readInt(u32, data[0..HeaderSize], .little);
2831
}
2932

30-
pub fn setSize(len: usize, header: *[2]u8) void {
31-
std.mem.writeInt(u16, header, @intCast(len), .little);
33+
pub fn setSize(len: usize, header: *[4]u8) void {
34+
std.mem.writeInt(u32, header, @intCast(len), .little);
3235
}
3336
};
3437

@@ -121,26 +124,26 @@ test "Buffer" {
121124

122125
const cases = [_]Case{
123126
// simple
124-
.{ .input = .{ 2, 0 } ++ "ok", .nb = 1 },
127+
.{ .input = .{ 2, 0, 0, 0 } ++ "ok", .nb = 1 },
125128
// combined
126-
.{ .input = .{ 2, 0 } ++ "ok" ++ .{ 3, 0 } ++ "foo", .nb = 2 },
129+
.{ .input = .{ 2, 0, 0, 0 } ++ "ok" ++ .{ 3, 0, 0, 0 } ++ "foo", .nb = 2 },
127130
// multipart
128-
.{ .input = .{ 9, 0 } ++ "multi", .nb = 0 },
131+
.{ .input = .{ 9, 0, 0, 0 } ++ "multi", .nb = 0 },
129132
.{ .input = "part", .nb = 1 },
130133
// multipart & combined
131-
.{ .input = .{ 9, 0 } ++ "multi", .nb = 0 },
132-
.{ .input = "part" ++ .{ 2, 0 } ++ "ok", .nb = 2 },
134+
.{ .input = .{ 9, 0, 0, 0 } ++ "multi", .nb = 0 },
135+
.{ .input = "part" ++ .{ 2, 0, 0, 0 } ++ "ok", .nb = 2 },
133136
// multipart & combined with other multipart
134-
.{ .input = .{ 9, 0 } ++ "multi", .nb = 0 },
135-
.{ .input = "part" ++ .{ 8, 0 } ++ "co", .nb = 1 },
137+
.{ .input = .{ 9, 0, 0, 0 } ++ "multi", .nb = 0 },
138+
.{ .input = "part" ++ .{ 8, 0, 0, 0 } ++ "co", .nb = 1 },
136139
.{ .input = "mbined", .nb = 1 },
137140
// several multipart
138-
.{ .input = .{ 23, 0 } ++ "multi", .nb = 0 },
141+
.{ .input = .{ 23, 0, 0, 0 } ++ "multi", .nb = 0 },
139142
.{ .input = "several", .nb = 0 },
140143
.{ .input = "complex", .nb = 0 },
141144
.{ .input = "part", .nb = 1 },
142145
// combined & multipart
143-
.{ .input = .{ 2, 0 } ++ "ok" ++ .{ 9, 0 } ++ "multi", .nb = 1 },
146+
.{ .input = .{ 2, 0, 0, 0 } ++ "ok" ++ .{ 9, 0, 0, 0 } ++ "multi", .nb = 1 },
144147
.{ .input = "part", .nb = 1 },
145148
};
146149

0 commit comments

Comments
 (0)