diff options
| author | Loris Cro <kappaloris@gmail.com> | 2020-06-20 00:45:51 +0200 |
|---|---|---|
| committer | Loris Cro <kappaloris@gmail.com> | 2020-09-24 22:06:41 +0200 |
| commit | c196c27af86f0f10b2f53240a68477391c4b9820 (patch) | |
| tree | 2933eee628b0a106184a0ea9dbb76f063e222921 | |
| parent | 419aea54cb30b394191778fcc70effaf5181bf33 (diff) | |
| download | zig-c196c27af86f0f10b2f53240a68477391c4b9820.tar.gz zig-c196c27af86f0f10b2f53240a68477391c4b9820.zip | |
recvfrom
Signed-off-by: Loris Cro <kappaloris@gmail.com>
| -rw-r--r-- | lib/std/event/loop.zig | 18 | ||||
| -rw-r--r-- | lib/std/net.zig | 5 | ||||
| -rw-r--r-- | lib/std/os.zig | 7 |
3 files changed, 23 insertions, 7 deletions
diff --git a/lib/std/event/loop.zig b/lib/std/event/loop.zig index 3a79d36a10..c3bf2495ff 100644 --- a/lib/std/event/loop.zig +++ b/lib/std/event/loop.zig @@ -1109,6 +1109,24 @@ pub const Loop = struct { } } + pub fn recvfrom( + sockfd: os.fd_t, + buf: []u8, + flags: u32, + src_addr: ?*os.sockaddr, + addrlen: ?*os.socklen_t, + ) os.RecvFromError!usize { + while (true) { + return os.recvfrom(sockfd, buf, flags, src_addr, addrlen) catch |err| switch (err) { + error.WouldBlock => { + self.waitUntilFdReadable(sockfd); + continue; + }, + else => return err, + }; + } + } + /// Performs an async `os.faccessatZ` using a separate thread. /// `fd` must block and not return EAGAIN. pub fn faccessatZ( diff --git a/lib/std/net.zig b/lib/std/net.zig index 8fe19f955d..fe7d0fafe6 100644 --- a/lib/std/net.zig +++ b/lib/std/net.zig @@ -1454,7 +1454,10 @@ fn resMSendRc( while (true) { var sl_copy = sl; - const rlen = os.recvfrom(fd, answer_bufs[next], 0, &sa.any, &sl_copy) catch break; + const rlen = if (std.io.is_async) + std.event.Loop.instance.?.recvfrom(fd, answer_bufs[next], 0, &sa.any, &sl_copy) catch break + else + os.recvfrom(fd, answer_bufs[next], 0, &sa.any, &sl_copy) catch break; // Ignore non-identifiable packets if (rlen < 4) continue; diff --git a/lib/std/os.zig b/lib/std/os.zig index 2f354e33d6..2c5f3065b2 100644 --- a/lib/std/os.zig +++ b/lib/std/os.zig @@ -5068,12 +5068,7 @@ pub fn recvfrom( ENOTCONN => unreachable, ENOTSOCK => unreachable, EINTR => continue, - EAGAIN => if (std.event.Loop.instance) |loop| { - loop.waitUntilFdReadable(sockfd); - continue; - } else { - return error.WouldBlock; - }, + EAGAIN => return error.WouldBlock, ENOMEM => return error.SystemResources, ECONNREFUSED => return error.ConnectionRefused, else => |err| return unexpectedErrno(err), |
