authorgravatar for kappaloris@gmail.comLoris Cro <kappaloris@gmail.com> 2020-06-20 00:35:08+02:00
committergravatar for kappaloris@gmail.comLoris Cro <kappaloris@gmail.com> 2020-09-24 22:06:41+02:00
log419aea54cb30b394191778fcc70effaf5181bf33
treeac929fd736e0f970c78be45402a2bae012b02af9
parent7fec5b3def36bc73e9e48a777bcca838c4b86770

sendto

Signed-off-by: Loris Cro <kappaloris@gmail.com>

3 files changed, 32 insertions(+), 9 deletions(-)

lib/std/event/loop.zig+21
......@@ -1088,6 +1088,27 @@ pub const Loop = struct {
10881088 }
10891089 }
10901090
1091 pub fn sendto(
1092 self: *Loop,
1093 /// The file descriptor of the sending socket.
1094 sockfd: os.fd_t,
1095 /// Message to send.
1096 buf: []const u8,
1097 flags: u32,
1098 dest_addr: ?*const os.sockaddr,
1099 addrlen: os.socklen_t,
1100 ) os.SendError!usize {
1101 while (true) {
1102 return os.sendto(sockfd, buf, flags, dest_addr, addrlen) catch |err| switch (err) {
1103 error.WouldBlock => {
1104 self.waitUntilFdWritable(sockfd);
1105 continue;
1106 },
1107 else => return err,
1108 };
1109 }
1110 }
1111
10911112 /// Performs an async `os.faccessatZ` using a separate thread.
10921113 /// `fd` must block and not return EAGAIN.
10931114 pub fn faccessatZ(
lib/std/net.zig+10-2
......@@ -1435,7 +1435,11 @@ fn resMSendRc(
14351435 if (answers[i].len == 0) {
14361436 var j: usize = 0;
14371437 while (j < ns.len) : (j += 1) {
1438 _ = os.sendto(fd, queries[i], os.MSG_NOSIGNAL, &ns[j].any, sl) catch undefined;
1438 if (std.io.is_async) {
1439 _ = std.event.Loop.instance.?.sendto(fd, queries[i], os.MSG_NOSIGNAL, &ns[j].any, sl) catch undefined;
1440 } else {
1441 _ = os.sendto(fd, queries[i], os.MSG_NOSIGNAL, &ns[j].any, sl) catch undefined;
1442 }
14391443 }
14401444 }
14411445 }
......@@ -1476,7 +1480,11 @@ fn resMSendRc(
14761480 0, 3 => {},
14771481 2 => if (servfail_retry != 0) {
14781482 servfail_retry -= 1;
1479 _ = os.sendto(fd, queries[i], os.MSG_NOSIGNAL, &ns[j].any, sl) catch undefined;
1483 if (std.io.is_async) {
1484 _ = std.event.Loop.instance.?.sendto(fd, queries[i], os.MSG_NOSIGNAL, &ns[j].any, sl) catch undefined;
1485 } else {
1486 _ = os.sendto(fd, queries[i], os.MSG_NOSIGNAL, &ns[j].any, sl) catch undefined;
1487 }
14801488 },
14811489 else => continue,
14821490 }
lib/std/os.zig+1-7
......@@ -4571,14 +4571,8 @@ pub fn sendto(
45714571 const rc = system.sendto(sockfd, buf.ptr, buf.len, flags, dest_addr, addrlen);
45724572 switch (errno(rc)) {
45734573 0 => return @intCast(usize, rc),
4574
45754574 EACCES => return error.AccessDenied,
4576 EAGAIN => if (std.event.Loop.instance) |loop| {
4577 loop.waitUntilFdWritable(sockfd);
4578 continue;
4579 } else {
4580 return error.WouldBlock;
4581 },
4575 EAGAIN => return error.WouldBlock,
45824576 EALREADY => return error.FastOpenAlreadyInProgress,
45834577 EBADF => unreachable, // always a race condition
45844578 ECONNRESET => return error.ConnectionResetByPeer,