authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2025-10-23 20:33:34-07:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2025-10-29 06:20:51-07:00
logecdc00466c34424ec8467d05efe0421f868c739a
tree69c4874db516beacd20388640ab70314ac92a433
parentc87fbd58787823da6db48e0bc488b5344fe52d43

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


2 files changed, 12 insertions(+), 2 deletions(-)

lib/std/Io/Threaded.zig+1-1
...@@ -5009,7 +5009,7 @@ fn lookupDns(...@@ -5009,7 +5009,7 @@ fn lookupDns(
5009 } };5009 } };
50105010
5011 while (true) {5011 while (true) {
5012 var message_buffer: [max_messages]Io.net.IncomingMessage = undefined;5012 var message_buffer: [max_messages]Io.net.IncomingMessage = @splat(.init);
5013 const buf = answer_buffer[answer_buffer_i..];5013 const buf = answer_buffer[answer_buffer_i..];
5014 const recv_err, const recv_n = socket.receiveManyTimeout(t_io, &message_buffer, buf, .{}, timeout);5014 const recv_err, const recv_n = socket.receiveManyTimeout(t_io, &message_buffer, buf, .{}, timeout);
5015 for (message_buffer[0..recv_n]) |*received_message| {5015 for (message_buffer[0..recv_n]) |*received_message| {
lib/std/Io/net.zig+11-1
...@@ -904,10 +904,18 @@ pub const IncomingMessage = struct {...@@ -904,10 +904,18 @@ pub const IncomingMessage = struct {
904 data: []u8,904 data: []u8,
905 /// Supplied by caller before calling receive functions; mutated by receive905 /// Supplied by caller before calling receive functions; mutated by receive
906 /// functions.906 /// functions.
907 control: []u8 = &.{},907 control: []u8,
908 /// Populated by receive functions.908 /// Populated by receive functions.
909 flags: Flags,909 flags: Flags,
910910
911 /// Useful for initializing before calling `receiveManyTimeout`.
912 pub const init: IncomingMessage = .{
913 .from = undefined,
914 .data = undefined,
915 .control = &.{},
916 .flags = undefined,
917 };
918
911 pub const Flags = packed struct(u8) {919 pub const Flags = packed struct(u8) {
912 /// indicates end-of-record; the data returned completed a record920 /// indicates end-of-record; the data returned completed a record
913 /// (generally used with sockets of type SOCK_SEQPACKET).921 /// (generally used with sockets of type SOCK_SEQPACKET).
...@@ -1146,6 +1154,8 @@ pub const Socket = struct {...@@ -1146,6 +1154,8 @@ pub const Socket = struct {
1146 pub fn receiveManyTimeout(1154 pub fn receiveManyTimeout(
1147 s: *const Socket,1155 s: *const Socket,
1148 io: Io,1156 io: Io,
1157 /// Function assumes each element has initialized `control` field.
1158 /// Initializing with `IncomingMessage.init` may be helpful.
1149 message_buffer: []IncomingMessage,1159 message_buffer: []IncomingMessage,
1150 data_buffer: []u8,1160 data_buffer: []u8,
1151 flags: ReceiveFlags,1161 flags: ReceiveFlags,