std.Io.net: make it easier to use netReceiveMany correctly

This commit is contained in:
Andrew Kelley 2025-10-23 20:33:34 -07:00
parent c87fbd5878
commit ecdc00466c
2 changed files with 12 additions and 2 deletions

View File

@ -5009,7 +5009,7 @@ fn lookupDns(
} };
while (true) {
var message_buffer: [max_messages]Io.net.IncomingMessage = undefined;
var message_buffer: [max_messages]Io.net.IncomingMessage = @splat(.init);
const buf = answer_buffer[answer_buffer_i..];
const recv_err, const recv_n = socket.receiveManyTimeout(t_io, &message_buffer, buf, .{}, timeout);
for (message_buffer[0..recv_n]) |*received_message| {

View File

@ -904,10 +904,18 @@ pub const IncomingMessage = struct {
data: []u8,
/// Supplied by caller before calling receive functions; mutated by receive
/// functions.
control: []u8 = &.{},
control: []u8,
/// Populated by receive functions.
flags: Flags,
/// Useful for initializing before calling `receiveManyTimeout`.
pub const init: IncomingMessage = .{
.from = undefined,
.data = undefined,
.control = &.{},
.flags = undefined,
};
pub const Flags = packed struct(u8) {
/// indicates end-of-record; the data returned completed a record
/// (generally used with sockets of type SOCK_SEQPACKET).
@ -1146,6 +1154,8 @@ pub const Socket = struct {
pub fn receiveManyTimeout(
s: *const Socket,
io: Io,
/// Function assumes each element has initialized `control` field.
/// Initializing with `IncomingMessage.init` may be helpful.
message_buffer: []IncomingMessage,
data_buffer: []u8,
flags: ReceiveFlags,