aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLoris Cro <kappaloris@gmail.com>2020-06-20 00:35:08 +0200
committerLoris Cro <kappaloris@gmail.com>2020-09-24 22:06:41 +0200
commit419aea54cb30b394191778fcc70effaf5181bf33 (patch)
treeac929fd736e0f970c78be45402a2bae012b02af9
parent7fec5b3def36bc73e9e48a777bcca838c4b86770 (diff)
downloadzig-419aea54cb30b394191778fcc70effaf5181bf33.tar.gz
zig-419aea54cb30b394191778fcc70effaf5181bf33.zip
sendto
Signed-off-by: Loris Cro <kappaloris@gmail.com>
-rw-r--r--lib/std/event/loop.zig21
-rw-r--r--lib/std/net.zig12
-rw-r--r--lib/std/os.zig8
3 files changed, 32 insertions, 9 deletions
diff --git a/lib/std/event/loop.zig b/lib/std/event/loop.zig
index ae2d2f1499..3a79d36a10 100644
--- a/lib/std/event/loop.zig
+++ b/lib/std/event/loop.zig
@@ -1088,6 +1088,27 @@ pub const Loop = struct {
}
}
+ pub fn sendto(
+ self: *Loop,
+ /// The file descriptor of the sending socket.
+ sockfd: os.fd_t,
+ /// Message to send.
+ buf: []const u8,
+ flags: u32,
+ dest_addr: ?*const os.sockaddr,
+ addrlen: os.socklen_t,
+ ) os.SendError!usize {
+ while (true) {
+ return os.sendto(sockfd, buf, flags, dest_addr, addrlen) catch |err| switch (err) {
+ error.WouldBlock => {
+ self.waitUntilFdWritable(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 928ebbbce5..8fe19f955d 100644
--- a/lib/std/net.zig
+++ b/lib/std/net.zig
@@ -1435,7 +1435,11 @@ fn resMSendRc(
if (answers[i].len == 0) {
var j: usize = 0;
while (j < ns.len) : (j += 1) {
- _ = os.sendto(fd, queries[i], os.MSG_NOSIGNAL, &ns[j].any, sl) catch undefined;
+ if (std.io.is_async) {
+ _ = std.event.Loop.instance.?.sendto(fd, queries[i], os.MSG_NOSIGNAL, &ns[j].any, sl) catch undefined;
+ } else {
+ _ = os.sendto(fd, queries[i], os.MSG_NOSIGNAL, &ns[j].any, sl) catch undefined;
+ }
}
}
}
@@ -1476,7 +1480,11 @@ fn resMSendRc(
0, 3 => {},
2 => if (servfail_retry != 0) {
servfail_retry -= 1;
- _ = os.sendto(fd, queries[i], os.MSG_NOSIGNAL, &ns[j].any, sl) catch undefined;
+ if (std.io.is_async) {
+ _ = std.event.Loop.instance.?.sendto(fd, queries[i], os.MSG_NOSIGNAL, &ns[j].any, sl) catch undefined;
+ } else {
+ _ = os.sendto(fd, queries[i], os.MSG_NOSIGNAL, &ns[j].any, sl) catch undefined;
+ }
},
else => continue,
}
diff --git a/lib/std/os.zig b/lib/std/os.zig
index 1138013e8c..2f354e33d6 100644
--- a/lib/std/os.zig
+++ b/lib/std/os.zig
@@ -4571,14 +4571,8 @@ pub fn sendto(
const rc = system.sendto(sockfd, buf.ptr, buf.len, flags, dest_addr, addrlen);
switch (errno(rc)) {
0 => return @intCast(usize, rc),
-
EACCES => return error.AccessDenied,
- EAGAIN => if (std.event.Loop.instance) |loop| {
- loop.waitUntilFdWritable(sockfd);
- continue;
- } else {
- return error.WouldBlock;
- },
+ EAGAIN => return error.WouldBlock,
EALREADY => return error.FastOpenAlreadyInProgress,
EBADF => unreachable, // always a race condition
ECONNRESET => return error.ConnectionResetByPeer,