Skip to content

Commit c196c27

Browse files
committed
recvfrom
Signed-off-by: Loris Cro <[email protected]>
1 parent 419aea5 commit c196c27

File tree

3 files changed

+23
-7
lines changed

3 files changed

+23
-7
lines changed

lib/std/event/loop.zig

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1109,6 +1109,24 @@ pub const Loop = struct {
11091109
}
11101110
}
11111111

1112+
pub fn recvfrom(
1113+
sockfd: os.fd_t,
1114+
buf: []u8,
1115+
flags: u32,
1116+
src_addr: ?*os.sockaddr,
1117+
addrlen: ?*os.socklen_t,
1118+
) os.RecvFromError!usize {
1119+
while (true) {
1120+
return os.recvfrom(sockfd, buf, flags, src_addr, addrlen) catch |err| switch (err) {
1121+
error.WouldBlock => {
1122+
self.waitUntilFdReadable(sockfd);
1123+
continue;
1124+
},
1125+
else => return err,
1126+
};
1127+
}
1128+
}
1129+
11121130
/// Performs an async `os.faccessatZ` using a separate thread.
11131131
/// `fd` must block and not return EAGAIN.
11141132
pub fn faccessatZ(

lib/std/net.zig

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1454,7 +1454,10 @@ fn resMSendRc(
14541454

14551455
while (true) {
14561456
var sl_copy = sl;
1457-
const rlen = os.recvfrom(fd, answer_bufs[next], 0, &sa.any, &sl_copy) catch break;
1457+
const rlen = if (std.io.is_async)
1458+
std.event.Loop.instance.?.recvfrom(fd, answer_bufs[next], 0, &sa.any, &sl_copy) catch break
1459+
else
1460+
os.recvfrom(fd, answer_bufs[next], 0, &sa.any, &sl_copy) catch break;
14581461

14591462
// Ignore non-identifiable packets
14601463
if (rlen < 4) continue;

lib/std/os.zig

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5068,12 +5068,7 @@ pub fn recvfrom(
50685068
ENOTCONN => unreachable,
50695069
ENOTSOCK => unreachable,
50705070
EINTR => continue,
5071-
EAGAIN => if (std.event.Loop.instance) |loop| {
5072-
loop.waitUntilFdReadable(sockfd);
5073-
continue;
5074-
} else {
5075-
return error.WouldBlock;
5076-
},
5071+
EAGAIN => return error.WouldBlock,
50775072
ENOMEM => return error.SystemResources,
50785073
ECONNREFUSED => return error.ConnectionRefused,
50795074
else => |err| return unexpectedErrno(err),

0 commit comments

Comments
 (0)