| author | |
| committer | |
| log | 3600508fe18ecc61ea889a65127a2641a76c2933 |
| tree | dcb1d00874c9abe2b465055dffba9c2cca69e896 |
| parent | d496400cff8b025dea262a9544e1b20482233089 |
Cross-platform versions of msghdr, sendmsg, recvmsg, linger, and iovec
were provided based on findings from glibc, musl, and Microsoft's
documentation.
Implemented initial Reactor interface for epoll (linux) which wraps
around I/O reactor subsystems such as epoll, kqueue, select, etc. across
different platforms. The Reactor interface allows for driving async I/O
in Zig applications.
A test was added for the Reactor interface to drive a TCP
client/listener socket pair.
A greatest-common-subset of possible socket initialization flags (close
socket on exec syscalls, initialize socket to be non-blocking) were
implemented.
A test was added for using sendmsg/recvmsg syscalls across different
platforms for a TCP client/listener socket pair.21 files changed, 548 insertions(+), 451 deletions(-)
lib/std/os.zig+1-1| ... | ... | @@ -4994,7 +4994,7 @@ pub fn sendmsg( |
| 4994 | 4994 | /// The file descriptor of the sending socket. |
| 4995 | 4995 | sockfd: socket_t, |
| 4996 | 4996 | /// Message header and iovecs |
| 4997 | msg: msghdr_const, | |
| 4997 | msg: std.x.os.Socket.Message, | |
| 4998 | 4998 | flags: u32, |
| 4999 | 4999 | ) SendMsgError!usize { |
| 5000 | 5000 | while (true) { |
lib/std/os/bits/dragonfly.zig-9| ... | ... | @@ -768,15 +768,6 @@ pub const dl_phdr_info = extern struct { |
| 768 | 768 | dlpi_phdr: [*]std.elf.Phdr, |
| 769 | 769 | dlpi_phnum: u16, |
| 770 | 770 | }; |
| 771 | pub const msghdr = extern struct { | |
| 772 | msg_name: ?*c_void, | |
| 773 | msg_namelen: socklen_t, | |
| 774 | msg_iov: [*c]iovec, | |
| 775 | msg_iovlen: c_int, | |
| 776 | msg_control: ?*c_void, | |
| 777 | msg_controllen: socklen_t, | |
| 778 | msg_flags: c_int, | |
| 779 | }; | |
| 780 | 771 | pub const cmsghdr = extern struct { |
| 781 | 772 | cmsg_len: socklen_t, |
| 782 | 773 | cmsg_level: c_int, |
lib/std/os/bits/freebsd.zig-46| ... | ... | @@ -82,52 +82,6 @@ pub const Flock = extern struct { |
| 82 | 82 | __unused: [4]u8, |
| 83 | 83 | }; |
| 84 | 84 | |
| 85 | pub const msghdr = extern struct { | |
| 86 | /// optional address | |
| 87 | msg_name: ?*sockaddr, | |
| 88 | ||
| 89 | /// size of address | |
| 90 | msg_namelen: socklen_t, | |
| 91 | ||
| 92 | /// scatter/gather array | |
| 93 | msg_iov: [*]iovec, | |
| 94 | ||
| 95 | /// # elements in msg_iov | |
| 96 | msg_iovlen: i32, | |
| 97 | ||
| 98 | /// ancillary data | |
| 99 | msg_control: ?*c_void, | |
| 100 | ||
| 101 | /// ancillary data buffer len | |
| 102 | msg_controllen: socklen_t, | |
| 103 | ||
| 104 | /// flags on received message | |
| 105 | msg_flags: i32, | |
| 106 | }; | |
| 107 | ||
| 108 | pub const msghdr_const = extern struct { | |
| 109 | /// optional address | |
| 110 | msg_name: ?*const sockaddr, | |
| 111 | ||
| 112 | /// size of address | |
| 113 | msg_namelen: socklen_t, | |
| 114 | ||
| 115 | /// scatter/gather array | |
| 116 | msg_iov: [*]iovec_const, | |
| 117 | ||
| 118 | /// # elements in msg_iov | |
| 119 | msg_iovlen: i32, | |
| 120 | ||
| 121 | /// ancillary data | |
| 122 | msg_control: ?*c_void, | |
| 123 | ||
| 124 | /// ancillary data buffer len | |
| 125 | msg_controllen: socklen_t, | |
| 126 | ||
| 127 | /// flags on received message | |
| 128 | msg_flags: i32, | |
| 129 | }; | |
| 130 | ||
| 131 | 85 | pub const libc_stat = extern struct { |
| 132 | 86 | dev: dev_t, |
| 133 | 87 | ino: ino_t, |
lib/std/os/bits/haiku.zig-46| ... | ... | @@ -73,52 +73,6 @@ pub const Flock = extern struct { |
| 73 | 73 | __unused: [4]u8, |
| 74 | 74 | }; |
| 75 | 75 | |
| 76 | pub const msghdr = extern struct { | |
| 77 | /// optional address | |
| 78 | msg_name: ?*sockaddr, | |
| 79 | ||
| 80 | /// size of address | |
| 81 | msg_namelen: socklen_t, | |
| 82 | ||
| 83 | /// scatter/gather array | |
| 84 | msg_iov: [*]iovec, | |
| 85 | ||
| 86 | /// # elements in msg_iov | |
| 87 | msg_iovlen: i32, | |
| 88 | ||
| 89 | /// ancillary data | |
| 90 | msg_control: ?*c_void, | |
| 91 | ||
| 92 | /// ancillary data buffer len | |
| 93 | msg_controllen: socklen_t, | |
| 94 | ||
| 95 | /// flags on received message | |
| 96 | msg_flags: i32, | |
| 97 | }; | |
| 98 | ||
| 99 | pub const msghdr_const = extern struct { | |
| 100 | /// optional address | |
| 101 | msg_name: ?*const sockaddr, | |
| 102 | ||
| 103 | /// size of address | |
| 104 | msg_namelen: socklen_t, | |
| 105 | ||
| 106 | /// scatter/gather array | |
| 107 | msg_iov: [*]iovec_const, | |
| 108 | ||
| 109 | /// # elements in msg_iov | |
| 110 | msg_iovlen: i32, | |
| 111 | ||
| 112 | /// ancillary data | |
| 113 | msg_control: ?*c_void, | |
| 114 | ||
| 115 | /// ancillary data buffer len | |
| 116 | msg_controllen: socklen_t, | |
| 117 | ||
| 118 | /// flags on received message | |
| 119 | msg_flags: i32, | |
| 120 | }; | |
| 121 | ||
| 122 | 76 | pub const off_t = i64; |
| 123 | 77 | pub const ino_t = u64; |
| 124 | 78 |
lib/std/os/bits/linux.zig+2-2| ... | ... | @@ -1180,12 +1180,12 @@ pub const sockaddr_un = extern struct { |
| 1180 | 1180 | }; |
| 1181 | 1181 | |
| 1182 | 1182 | pub const mmsghdr = extern struct { |
| 1183 | msg_hdr: msghdr, | |
| 1183 | msg_hdr: std.x.os.Socket.Message, | |
| 1184 | 1184 | msg_len: u32, |
| 1185 | 1185 | }; |
| 1186 | 1186 | |
| 1187 | 1187 | pub const mmsghdr_const = extern struct { |
| 1188 | msg_hdr: msghdr_const, | |
| 1188 | msg_hdr: std.x.os.Socket.Message, | |
| 1189 | 1189 | msg_len: u32, |
| 1190 | 1190 | }; |
| 1191 | 1191 |
lib/std/os/bits/linux/arm-eabi.zig-20| ... | ... | @@ -526,26 +526,6 @@ pub const Flock = extern struct { |
| 526 | 526 | __unused: [4]u8, |
| 527 | 527 | }; |
| 528 | 528 | |
| 529 | pub const msghdr = extern struct { | |
| 530 | msg_name: ?*sockaddr, | |
| 531 | msg_namelen: socklen_t, | |
| 532 | msg_iov: [*]iovec, | |
| 533 | msg_iovlen: i32, | |
| 534 | msg_control: ?*c_void, | |
| 535 | msg_controllen: socklen_t, | |
| 536 | msg_flags: i32, | |
| 537 | }; | |
| 538 | ||
| 539 | pub const msghdr_const = extern struct { | |
| 540 | msg_name: ?*const sockaddr, | |
| 541 | msg_namelen: socklen_t, | |
| 542 | msg_iov: [*]iovec_const, | |
| 543 | msg_iovlen: i32, | |
| 544 | msg_control: ?*c_void, | |
| 545 | msg_controllen: socklen_t, | |
| 546 | msg_flags: i32, | |
| 547 | }; | |
| 548 | ||
| 549 | 529 | pub const blksize_t = i32; |
| 550 | 530 | pub const nlink_t = u32; |
| 551 | 531 | pub const time_t = isize; |
lib/std/os/bits/linux/arm64.zig-24| ... | ... | @@ -395,30 +395,6 @@ pub const Flock = extern struct { |
| 395 | 395 | __unused: [4]u8, |
| 396 | 396 | }; |
| 397 | 397 | |
| 398 | pub const msghdr = extern struct { | |
| 399 | msg_name: ?*sockaddr, | |
| 400 | msg_namelen: socklen_t, | |
| 401 | msg_iov: [*]iovec, | |
| 402 | msg_iovlen: i32, | |
| 403 | __pad1: i32 = 0, | |
| 404 | msg_control: ?*c_void, | |
| 405 | msg_controllen: socklen_t, | |
| 406 | __pad2: socklen_t = 0, | |
| 407 | msg_flags: i32, | |
| 408 | }; | |
| 409 | ||
| 410 | pub const msghdr_const = extern struct { | |
| 411 | msg_name: ?*const sockaddr, | |
| 412 | msg_namelen: socklen_t, | |
| 413 | msg_iov: [*]iovec_const, | |
| 414 | msg_iovlen: i32, | |
| 415 | __pad1: i32 = 0, | |
| 416 | msg_control: ?*c_void, | |
| 417 | msg_controllen: socklen_t, | |
| 418 | __pad2: socklen_t = 0, | |
| 419 | msg_flags: i32, | |
| 420 | }; | |
| 421 | ||
| 422 | 398 | pub const blksize_t = i32; |
| 423 | 399 | pub const nlink_t = u32; |
| 424 | 400 | pub const time_t = isize; |
lib/std/os/bits/linux/i386.zig-20| ... | ... | @@ -523,26 +523,6 @@ pub const Flock = extern struct { |
| 523 | 523 | l_pid: pid_t, |
| 524 | 524 | }; |
| 525 | 525 | |
| 526 | pub const msghdr = extern struct { | |
| 527 | msg_name: ?*sockaddr, | |
| 528 | msg_namelen: socklen_t, | |
| 529 | msg_iov: [*]iovec, | |
| 530 | msg_iovlen: i32, | |
| 531 | msg_control: ?*c_void, | |
| 532 | msg_controllen: socklen_t, | |
| 533 | msg_flags: i32, | |
| 534 | }; | |
| 535 | ||
| 536 | pub const msghdr_const = extern struct { | |
| 537 | msg_name: ?*const sockaddr, | |
| 538 | msg_namelen: socklen_t, | |
| 539 | msg_iov: [*]iovec_const, | |
| 540 | msg_iovlen: i32, | |
| 541 | msg_control: ?*c_void, | |
| 542 | msg_controllen: socklen_t, | |
| 543 | msg_flags: i32, | |
| 544 | }; | |
| 545 | ||
| 546 | 526 | pub const blksize_t = i32; |
| 547 | 527 | pub const nlink_t = u32; |
| 548 | 528 | pub const time_t = isize; |
lib/std/os/bits/linux/powerpc.zig-20| ... | ... | @@ -515,26 +515,6 @@ pub const Flock = extern struct { |
| 515 | 515 | l_pid: pid_t, |
| 516 | 516 | }; |
| 517 | 517 | |
| 518 | pub const msghdr = extern struct { | |
| 519 | msg_name: ?*sockaddr, | |
| 520 | msg_namelen: socklen_t, | |
| 521 | msg_iov: [*]iovec, | |
| 522 | msg_iovlen: usize, | |
| 523 | msg_control: ?*c_void, | |
| 524 | msg_controllen: socklen_t, | |
| 525 | msg_flags: i32, | |
| 526 | }; | |
| 527 | ||
| 528 | pub const msghdr_const = extern struct { | |
| 529 | msg_name: ?*const sockaddr, | |
| 530 | msg_namelen: socklen_t, | |
| 531 | msg_iov: [*]iovec_const, | |
| 532 | msg_iovlen: usize, | |
| 533 | msg_control: ?*c_void, | |
| 534 | msg_controllen: socklen_t, | |
| 535 | msg_flags: i32, | |
| 536 | }; | |
| 537 | ||
| 538 | 518 | pub const blksize_t = i32; |
| 539 | 519 | pub const nlink_t = u32; |
| 540 | 520 | pub const time_t = isize; |
lib/std/os/bits/linux/powerpc64.zig-20| ... | ... | @@ -491,26 +491,6 @@ pub const Flock = extern struct { |
| 491 | 491 | __unused: [4]u8, |
| 492 | 492 | }; |
| 493 | 493 | |
| 494 | pub const msghdr = extern struct { | |
| 495 | msg_name: ?*sockaddr, | |
| 496 | msg_namelen: socklen_t, | |
| 497 | msg_iov: [*]iovec, | |
| 498 | msg_iovlen: usize, | |
| 499 | msg_control: ?*c_void, | |
| 500 | msg_controllen: usize, | |
| 501 | msg_flags: i32, | |
| 502 | }; | |
| 503 | ||
| 504 | pub const msghdr_const = extern struct { | |
| 505 | msg_name: ?*const sockaddr, | |
| 506 | msg_namelen: socklen_t, | |
| 507 | msg_iov: [*]iovec_const, | |
| 508 | msg_iovlen: usize, | |
| 509 | msg_control: ?*c_void, | |
| 510 | msg_controllen: usize, | |
| 511 | msg_flags: i32, | |
| 512 | }; | |
| 513 | ||
| 514 | 494 | pub const blksize_t = i64; |
| 515 | 495 | pub const nlink_t = u64; |
| 516 | 496 | pub const time_t = i64; |
lib/std/os/bits/linux/sparc64.zig-20| ... | ... | @@ -465,26 +465,6 @@ pub const Flock = extern struct { |
| 465 | 465 | l_pid: pid_t, |
| 466 | 466 | }; |
| 467 | 467 | |
| 468 | pub const msghdr = extern struct { | |
| 469 | msg_name: ?*sockaddr, | |
| 470 | msg_namelen: socklen_t, | |
| 471 | msg_iov: [*]iovec, | |
| 472 | msg_iovlen: u64, | |
| 473 | msg_control: ?*c_void, | |
| 474 | msg_controllen: u64, | |
| 475 | msg_flags: i32, | |
| 476 | }; | |
| 477 | ||
| 478 | pub const msghdr_const = extern struct { | |
| 479 | msg_name: ?*const sockaddr, | |
| 480 | msg_namelen: socklen_t, | |
| 481 | msg_iov: [*]iovec_const, | |
| 482 | msg_iovlen: u64, | |
| 483 | msg_control: ?*c_void, | |
| 484 | msg_controllen: u64, | |
| 485 | msg_flags: i32, | |
| 486 | }; | |
| 487 | ||
| 488 | 468 | pub const off_t = i64; |
| 489 | 469 | pub const ino_t = u64; |
| 490 | 470 | pub const mode_t = u32; |
lib/std/os/bits/linux/x86_64.zig-24| ... | ... | @@ -489,30 +489,6 @@ pub const Flock = extern struct { |
| 489 | 489 | l_pid: pid_t, |
| 490 | 490 | }; |
| 491 | 491 | |
| 492 | pub const msghdr = extern struct { | |
| 493 | msg_name: ?*sockaddr, | |
| 494 | msg_namelen: socklen_t, | |
| 495 | msg_iov: [*]iovec, | |
| 496 | msg_iovlen: i32, | |
| 497 | __pad1: i32 = 0, | |
| 498 | msg_control: ?*c_void, | |
| 499 | msg_controllen: socklen_t, | |
| 500 | __pad2: socklen_t = 0, | |
| 501 | msg_flags: i32, | |
| 502 | }; | |
| 503 | ||
| 504 | pub const msghdr_const = extern struct { | |
| 505 | msg_name: ?*const sockaddr, | |
| 506 | msg_namelen: socklen_t, | |
| 507 | msg_iov: [*]iovec_const, | |
| 508 | msg_iovlen: i32, | |
| 509 | __pad1: i32 = 0, | |
| 510 | msg_control: ?*c_void, | |
| 511 | msg_controllen: socklen_t, | |
| 512 | __pad2: socklen_t = 0, | |
| 513 | msg_flags: i32, | |
| 514 | }; | |
| 515 | ||
| 516 | 492 | pub const off_t = i64; |
| 517 | 493 | pub const ino_t = u64; |
| 518 | 494 | pub const dev_t = u64; |
lib/std/os/bits/netbsd.zig-46| ... | ... | @@ -108,52 +108,6 @@ pub const EAI = enum(c_int) { |
| 108 | 108 | |
| 109 | 109 | pub const EAI_MAX = 15; |
| 110 | 110 | |
| 111 | pub const msghdr = extern struct { | |
| 112 | /// optional address | |
| 113 | msg_name: ?*sockaddr, | |
| 114 | ||
| 115 | /// size of address | |
| 116 | msg_namelen: socklen_t, | |
| 117 | ||
| 118 | /// scatter/gather array | |
| 119 | msg_iov: [*]iovec, | |
| 120 | ||
| 121 | /// # elements in msg_iov | |
| 122 | msg_iovlen: i32, | |
| 123 | ||
| 124 | /// ancillary data | |
| 125 | msg_control: ?*c_void, | |
| 126 | ||
| 127 | /// ancillary data buffer len | |
| 128 | msg_controllen: socklen_t, | |
| 129 | ||
| 130 | /// flags on received message | |
| 131 | msg_flags: i32, | |
| 132 | }; | |
| 133 | ||
| 134 | pub const msghdr_const = extern struct { | |
| 135 | /// optional address | |
| 136 | msg_name: ?*const sockaddr, | |
| 137 | ||
| 138 | /// size of address | |
| 139 | msg_namelen: socklen_t, | |
| 140 | ||
| 141 | /// scatter/gather array | |
| 142 | msg_iov: [*]iovec_const, | |
| 143 | ||
| 144 | /// # elements in msg_iov | |
| 145 | msg_iovlen: i32, | |
| 146 | ||
| 147 | /// ancillary data | |
| 148 | msg_control: ?*c_void, | |
| 149 | ||
| 150 | /// ancillary data buffer len | |
| 151 | msg_controllen: socklen_t, | |
| 152 | ||
| 153 | /// flags on received message | |
| 154 | msg_flags: i32, | |
| 155 | }; | |
| 156 | ||
| 157 | 111 | pub const libc_stat = extern struct { |
| 158 | 112 | dev: dev_t, |
| 159 | 113 | mode: mode_t, |
lib/std/os/bits/openbsd.zig-46| ... | ... | @@ -124,52 +124,6 @@ pub const EAI = enum(c_int) { |
| 124 | 124 | |
| 125 | 125 | pub const EAI_MAX = 15; |
| 126 | 126 | |
| 127 | pub const msghdr = extern struct { | |
| 128 | /// optional address | |
| 129 | msg_name: ?*sockaddr, | |
| 130 | ||
| 131 | /// size of address | |
| 132 | msg_namelen: socklen_t, | |
| 133 | ||
| 134 | /// scatter/gather array | |
| 135 | msg_iov: [*]iovec, | |
| 136 | ||
| 137 | /// # elements in msg_iov | |
| 138 | msg_iovlen: c_uint, | |
| 139 | ||
| 140 | /// ancillary data | |
| 141 | msg_control: ?*c_void, | |
| 142 | ||
| 143 | /// ancillary data buffer len | |
| 144 | msg_controllen: socklen_t, | |
| 145 | ||
| 146 | /// flags on received message | |
| 147 | msg_flags: c_int, | |
| 148 | }; | |
| 149 | ||
| 150 | pub const msghdr_const = extern struct { | |
| 151 | /// optional address | |
| 152 | msg_name: ?*const sockaddr, | |
| 153 | ||
| 154 | /// size of address | |
| 155 | msg_namelen: socklen_t, | |
| 156 | ||
| 157 | /// scatter/gather array | |
| 158 | msg_iov: [*]iovec_const, | |
| 159 | ||
| 160 | /// # elements in msg_iov | |
| 161 | msg_iovlen: c_uint, | |
| 162 | ||
| 163 | /// ancillary data | |
| 164 | msg_control: ?*c_void, | |
| 165 | ||
| 166 | /// ancillary data buffer len | |
| 167 | msg_controllen: socklen_t, | |
| 168 | ||
| 169 | /// flags on received message | |
| 170 | msg_flags: c_int, | |
| 171 | }; | |
| 172 | ||
| 173 | 127 | pub const libc_stat = extern struct { |
| 174 | 128 | mode: mode_t, |
| 175 | 129 | dev: dev_t, |
lib/std/os/windows/ws2_32.zig+5-25| ... | ... | @@ -3,6 +3,7 @@ |
| 3 | 3 | // This file is part of [zig](https://ziglang.org/), which is MIT licensed. |
| 4 | 4 | // The MIT license requires this copyright notice to be included in all copies |
| 5 | 5 | // and substantial portions of the software. |
| 6 | const std = @import("../../std.zig"); | |
| 6 | 7 | usingnamespace @import("bits.zig"); |
| 7 | 8 | |
| 8 | 9 | pub const SOCKET = *opaque {}; |
| ... | ... | @@ -1093,27 +1094,6 @@ pub const WSABUF = extern struct { |
| 1093 | 1094 | buf: [*]u8, |
| 1094 | 1095 | }; |
| 1095 | 1096 | |
| 1096 | pub const msghdr = WSAMSG; | |
| 1097 | pub const msghdr_const = WSAMSG_const; | |
| 1098 | ||
| 1099 | pub const WSAMSG_const = extern struct { | |
| 1100 | name: *const sockaddr, | |
| 1101 | namelen: INT, | |
| 1102 | lpBuffers: [*]WSABUF, | |
| 1103 | dwBufferCount: DWORD, | |
| 1104 | Control: WSABUF, | |
| 1105 | dwFlags: DWORD, | |
| 1106 | }; | |
| 1107 | ||
| 1108 | pub const WSAMSG = extern struct { | |
| 1109 | name: *sockaddr, | |
| 1110 | namelen: INT, | |
| 1111 | lpBuffers: [*]WSABUF, | |
| 1112 | dwBufferCount: DWORD, | |
| 1113 | Control: WSABUF, | |
| 1114 | dwFlags: DWORD, | |
| 1115 | }; | |
| 1116 | ||
| 1117 | 1097 | pub const WSAPOLLFD = pollfd; |
| 1118 | 1098 | |
| 1119 | 1099 | pub const pollfd = extern struct { |
| ... | ... | @@ -1163,7 +1143,7 @@ pub const LPFN_GETACCEPTEXSOCKADDRS = fn ( |
| 1163 | 1143 | |
| 1164 | 1144 | pub const LPFN_WSASENDMSG = fn ( |
| 1165 | 1145 | s: SOCKET, |
| 1166 | lpMsg: *const WSAMSG_const, | |
| 1146 | lpMsg: *const std.x.os.Socket.Message, | |
| 1167 | 1147 | dwFlags: u32, |
| 1168 | 1148 | lpNumberOfBytesSent: ?*u32, |
| 1169 | 1149 | lpOverlapped: ?*OVERLAPPED, |
| ... | ... | @@ -1172,7 +1152,7 @@ pub const LPFN_WSASENDMSG = fn ( |
| 1172 | 1152 | |
| 1173 | 1153 | pub const LPFN_WSARECVMSG = fn ( |
| 1174 | 1154 | s: SOCKET, |
| 1175 | lpMsg: *WSAMSG, | |
| 1155 | lpMsg: *std.x.os.Socket.Message, | |
| 1176 | 1156 | lpdwNumberOfBytesRecv: ?*u32, |
| 1177 | 1157 | lpOverlapped: ?*OVERLAPPED, |
| 1178 | 1158 | lpCompletionRoutine: ?LPWSAOVERLAPPED_COMPLETION_ROUTINE, |
| ... | ... | @@ -2046,7 +2026,7 @@ pub extern "ws2_32" fn WSASend( |
| 2046 | 2026 | |
| 2047 | 2027 | pub extern "ws2_32" fn WSASendMsg( |
| 2048 | 2028 | s: SOCKET, |
| 2049 | lpMsg: *const WSAMSG_const, | |
| 2029 | lpMsg: *const std.x.os.Socket.Message, | |
| 2050 | 2030 | dwFlags: u32, |
| 2051 | 2031 | lpNumberOfBytesSent: ?*u32, |
| 2052 | 2032 | lpOverlapped: ?*OVERLAPPED, |
| ... | ... | @@ -2055,7 +2035,7 @@ pub extern "ws2_32" fn WSASendMsg( |
| 2055 | 2035 | |
| 2056 | 2036 | pub extern "ws2_32" fn WSARecvMsg( |
| 2057 | 2037 | s: SOCKET, |
| 2058 | lpMsg: *WSAMSG, | |
| 2038 | lpMsg: *std.x.os.Socket.Message, | |
| 2059 | 2039 | lpdwNumberOfBytesRecv: ?*u32, |
| 2060 | 2040 | lpOverlapped: ?*OVERLAPPED, |
| 2061 | 2041 | lpCompletionRoutine: ?LPWSAOVERLAPPED_COMPLETION_ROUTINE, |
lib/std/x.zig+1| ... | ... | @@ -8,6 +8,7 @@ const std = @import("std.zig"); |
| 8 | 8 | |
| 9 | 9 | pub const os = struct { |
| 10 | 10 | pub const Socket = @import("x/os/socket.zig").Socket; |
| 11 | pub usingnamespace @import("x/os/io.zig"); | |
| 11 | 12 | pub usingnamespace @import("x/os/net.zig"); |
| 12 | 13 | }; |
| 13 | 14 |
lib/std/x/net/tcp.zig+58-16| ... | ... | @@ -18,6 +18,7 @@ const testing = std.testing; |
| 18 | 18 | const IPv4 = std.x.os.IPv4; |
| 19 | 19 | const IPv6 = std.x.os.IPv6; |
| 20 | 20 | const Socket = std.x.os.Socket; |
| 21 | const Buffer = std.x.os.Buffer; | |
| 21 | 22 | |
| 22 | 23 | /// A generic TCP socket abstraction. |
| 23 | 24 | const tcp = @This(); |
| ... | ... | @@ -82,12 +83,13 @@ pub const Client = struct { |
| 82 | 83 | }; |
| 83 | 84 | |
| 84 | 85 | /// Opens a new client. |
| 85 | pub fn init(domain: tcp.Domain, flags: u32) !Client { | |
| 86 | pub fn init(domain: tcp.Domain, flags: std.enums.EnumFieldStruct(Socket.InitFlags, bool, false)) !Client { | |
| 86 | 87 | return Client{ |
| 87 | 88 | .socket = try Socket.init( |
| 88 | 89 | @enumToInt(domain), |
| 89 | os.SOCK_STREAM | flags, | |
| 90 | os.SOCK_STREAM, | |
| 90 | 91 | os.IPPROTO_TCP, |
| 92 | flags, | |
| 91 | 93 | ), |
| 92 | 94 | }; |
| 93 | 95 | } |
| ... | ... | @@ -143,14 +145,14 @@ pub const Client = struct { |
| 143 | 145 | /// Writes multiple I/O vectors with a prepended message header to the socket |
| 144 | 146 | /// with a set of flags specified. It returns the number of bytes that are |
| 145 | 147 | /// written to the socket. |
| 146 | pub fn writeVectorized(self: Client, msg: os.msghdr_const, flags: u32) !usize { | |
| 148 | pub fn writeVectorized(self: Client, msg: Socket.Message, flags: u32) !usize { | |
| 147 | 149 | return self.socket.writeVectorized(msg, flags); |
| 148 | 150 | } |
| 149 | 151 | |
| 150 | 152 | /// Read multiple I/O vectors with a prepended message header from the socket |
| 151 | 153 | /// with a set of flags specified. It returns the number of bytes that were |
| 152 | 154 | /// read into the buffer provided. |
| 153 | pub fn readVectorized(self: Client, msg: *os.msghdr, flags: u32) !usize { | |
| 155 | pub fn readVectorized(self: Client, msg: *Socket.Message, flags: u32) !usize { | |
| 154 | 156 | return self.socket.readVectorized(msg, flags); |
| 155 | 157 | } |
| 156 | 158 | |
| ... | ... | @@ -244,12 +246,13 @@ pub const Listener = struct { |
| 244 | 246 | socket: Socket, |
| 245 | 247 | |
| 246 | 248 | /// Opens a new listener. |
| 247 | pub fn init(domain: tcp.Domain, flags: u32) !Listener { | |
| 249 | pub fn init(domain: tcp.Domain, flags: std.enums.EnumFieldStruct(Socket.InitFlags, bool, false)) !Listener { | |
| 248 | 250 | return Listener{ |
| 249 | 251 | .socket = try Socket.init( |
| 250 | 252 | @enumToInt(domain), |
| 251 | os.SOCK_STREAM | flags, | |
| 253 | os.SOCK_STREAM, | |
| 252 | 254 | os.IPPROTO_TCP, |
| 255 | flags, | |
| 253 | 256 | ), |
| 254 | 257 | }; |
| 255 | 258 | } |
| ... | ... | @@ -278,7 +281,7 @@ pub const Listener = struct { |
| 278 | 281 | |
| 279 | 282 | /// Accept a pending incoming connection queued to the kernel backlog |
| 280 | 283 | /// of the listener's socket. |
| 281 | pub fn accept(self: Listener, flags: u32) !tcp.Connection { | |
| 284 | pub fn accept(self: Listener, flags: std.enums.EnumFieldStruct(Socket.InitFlags, bool, false)) !tcp.Connection { | |
| 282 | 285 | return tcp.Connection.from(try self.socket.accept(flags)); |
| 283 | 286 | } |
| 284 | 287 | |
| ... | ... | @@ -324,7 +327,7 @@ pub const Listener = struct { |
| 324 | 327 | test "tcp: create client/listener pair" { |
| 325 | 328 | if (builtin.os.tag == .wasi) return error.SkipZigTest; |
| 326 | 329 | |
| 327 | const listener = try tcp.Listener.init(.ip, os.SOCK_CLOEXEC); | |
| 330 | const listener = try tcp.Listener.init(.ip, .{ .close_on_exec = true }); | |
| 328 | 331 | defer listener.deinit(); |
| 329 | 332 | |
| 330 | 333 | try listener.bind(ip.Address.initIPv4(IPv4.unspecified, 0)); |
| ... | ... | @@ -336,19 +339,19 @@ test "tcp: create client/listener pair" { |
| 336 | 339 | .ipv6 => |*ipv6| ipv6.host = IPv6.localhost, |
| 337 | 340 | } |
| 338 | 341 | |
| 339 | const client = try tcp.Client.init(.ip, os.SOCK_CLOEXEC); | |
| 342 | const client = try tcp.Client.init(.ip, .{ .close_on_exec = true }); | |
| 340 | 343 | defer client.deinit(); |
| 341 | 344 | |
| 342 | 345 | try client.connect(binded_address); |
| 343 | 346 | |
| 344 | const conn = try listener.accept(os.SOCK_CLOEXEC); | |
| 347 | const conn = try listener.accept(.{ .close_on_exec = true }); | |
| 345 | 348 | defer conn.deinit(); |
| 346 | 349 | } |
| 347 | 350 | |
| 348 | test "tcp/client: set read timeout of 1 millisecond on blocking client" { | |
| 351 | test "tcp/client: 1ms read timeout" { | |
| 349 | 352 | if (builtin.os.tag == .wasi) return error.SkipZigTest; |
| 350 | 353 | |
| 351 | const listener = try tcp.Listener.init(.ip, os.SOCK_CLOEXEC); | |
| 354 | const listener = try tcp.Listener.init(.ip, .{ .close_on_exec = true }); | |
| 352 | 355 | defer listener.deinit(); |
| 353 | 356 | |
| 354 | 357 | try listener.bind(ip.Address.initIPv4(IPv4.unspecified, 0)); |
| ... | ... | @@ -360,23 +363,62 @@ test "tcp/client: set read timeout of 1 millisecond on blocking client" { |
| 360 | 363 | .ipv6 => |*ipv6| ipv6.host = IPv6.localhost, |
| 361 | 364 | } |
| 362 | 365 | |
| 363 | const client = try tcp.Client.init(.ip, os.SOCK_CLOEXEC); | |
| 366 | const client = try tcp.Client.init(.ip, .{ .close_on_exec = true }); | |
| 364 | 367 | defer client.deinit(); |
| 365 | 368 | |
| 366 | 369 | try client.connect(binded_address); |
| 367 | 370 | try client.setReadTimeout(1); |
| 368 | 371 | |
| 369 | const conn = try listener.accept(os.SOCK_CLOEXEC); | |
| 372 | const conn = try listener.accept(.{ .close_on_exec = true }); | |
| 370 | 373 | defer conn.deinit(); |
| 371 | 374 | |
| 372 | 375 | var buf: [1]u8 = undefined; |
| 373 | 376 | try testing.expectError(error.WouldBlock, client.reader(0).read(&buf)); |
| 374 | 377 | } |
| 375 | 378 | |
| 379 | test "tcp/client: read and write multiple vectors" { | |
| 380 | if (builtin.os.tag == .wasi) return error.SkipZigTest; | |
| 381 | ||
| 382 | const listener = try tcp.Listener.init(.ip, .{ .close_on_exec = true }); | |
| 383 | defer listener.deinit(); | |
| 384 | ||
| 385 | try listener.bind(ip.Address.initIPv4(IPv4.unspecified, 0)); | |
| 386 | try listener.listen(128); | |
| 387 | ||
| 388 | var binded_address = try listener.getLocalAddress(); | |
| 389 | switch (binded_address) { | |
| 390 | .ipv4 => |*ipv4| ipv4.host = IPv4.localhost, | |
| 391 | .ipv6 => |*ipv6| ipv6.host = IPv6.localhost, | |
| 392 | } | |
| 393 | ||
| 394 | const client = try tcp.Client.init(.ip, .{ .close_on_exec = true }); | |
| 395 | defer client.deinit(); | |
| 396 | ||
| 397 | try client.connect(binded_address); | |
| 398 | ||
| 399 | const conn = try listener.accept(.{ .close_on_exec = true }); | |
| 400 | defer conn.deinit(); | |
| 401 | ||
| 402 | const message = "hello world"; | |
| 403 | _ = try conn.client.writeVectorized(Socket.Message.fromBuffers(&[_]Buffer{ | |
| 404 | Buffer.from(message[0 .. message.len / 2]), | |
| 405 | Buffer.from(message[message.len / 2 ..]), | |
| 406 | }), 0); | |
| 407 | ||
| 408 | var buf: [message.len]u8 = undefined; | |
| 409 | var msg = Socket.Message.fromBuffers(&[_]Buffer{ | |
| 410 | Buffer.from(buf[0 .. message.len / 2]), | |
| 411 | Buffer.from(buf[message.len / 2 ..]), | |
| 412 | }); | |
| 413 | _ = try client.readVectorized(&msg, 0); | |
| 414 | ||
| 415 | try testing.expectEqualStrings(message, &buf); | |
| 416 | } | |
| 417 | ||
| 376 | 418 | test "tcp/listener: bind to unspecified ipv4 address" { |
| 377 | 419 | if (builtin.os.tag == .wasi) return error.SkipZigTest; |
| 378 | 420 | |
| 379 | const listener = try tcp.Listener.init(.ip, os.SOCK_CLOEXEC); | |
| 421 | const listener = try tcp.Listener.init(.ip, .{ .close_on_exec = true }); | |
| 380 | 422 | defer listener.deinit(); |
| 381 | 423 | |
| 382 | 424 | try listener.bind(ip.Address.initIPv4(IPv4.unspecified, 0)); |
| ... | ... | @@ -389,7 +431,7 @@ test "tcp/listener: bind to unspecified ipv4 address" { |
| 389 | 431 | test "tcp/listener: bind to unspecified ipv6 address" { |
| 390 | 432 | if (builtin.os.tag == .wasi) return error.SkipZigTest; |
| 391 | 433 | |
| 392 | const listener = try tcp.Listener.init(.ipv6, os.SOCK_CLOEXEC); | |
| 434 | const listener = try tcp.Listener.init(.ipv6, .{ .close_on_exec = true }); | |
| 393 | 435 | defer listener.deinit(); |
| 394 | 436 | |
| 395 | 437 | try listener.bind(ip.Address.initIPv6(IPv6.unspecified, 0)); |
lib/std/x/os/io.zig created+205| ... | ... | @@ -0,0 +1,205 @@ |
| 1 | const std = @import("../../std.zig"); | |
| 2 | ||
| 3 | const os = std.os; | |
| 4 | const mem = std.mem; | |
| 5 | const builtin = std.builtin; | |
| 6 | const testing = std.testing; | |
| 7 | ||
| 8 | /// POSIX `iovec`, or Windows `WSABUF`. The difference between the two are the ordering | |
| 9 | /// of fields, alongside the length being represented as either a ULONG or a size_t. | |
| 10 | pub const Buffer = if (builtin.os.tag == .windows) | |
| 11 | extern struct { | |
| 12 | len: c_ulong, | |
| 13 | ptr: usize, | |
| 14 | ||
| 15 | pub fn from(slice: []const u8) Buffer { | |
| 16 | return .{ .len = @intCast(c_ulong, slice.len), .ptr = @ptrToInt(slice.ptr) }; | |
| 17 | } | |
| 18 | ||
| 19 | pub fn into(self: Buffer) []const u8 { | |
| 20 | return @intToPtr([*]const u8, self.ptr)[0..self.len]; | |
| 21 | } | |
| 22 | ||
| 23 | pub fn intoMutable(self: Buffer) []u8 { | |
| 24 | return @intToPtr([*]u8, self.ptr)[0..self.len]; | |
| 25 | } | |
| 26 | } | |
| 27 | else | |
| 28 | extern struct { | |
| 29 | ptr: usize, | |
| 30 | len: usize, | |
| 31 | ||
| 32 | pub fn from(slice: []const u8) Buffer { | |
| 33 | return .{ .ptr = @ptrToInt(slice.ptr), .len = slice.len }; | |
| 34 | } | |
| 35 | ||
| 36 | pub fn into(self: Buffer) []const u8 { | |
| 37 | return @intToPtr([*]const u8, self.ptr)[0..self.len]; | |
| 38 | } | |
| 39 | ||
| 40 | pub fn intoMutable(self: Buffer) []u8 { | |
| 41 | return @intToptr([*]u8, self.ptr)[0..self.len]; | |
| 42 | } | |
| 43 | }; | |
| 44 | ||
| 45 | pub const Reactor = struct { | |
| 46 | pub const InitFlags = enum { | |
| 47 | close_on_exec, | |
| 48 | }; | |
| 49 | ||
| 50 | pub const Event = struct { | |
| 51 | data: usize, | |
| 52 | is_error: bool, | |
| 53 | is_hup: bool, | |
| 54 | is_readable: bool, | |
| 55 | is_writable: bool, | |
| 56 | }; | |
| 57 | ||
| 58 | pub const Interest = struct { | |
| 59 | hup: bool = false, | |
| 60 | oneshot: bool = false, | |
| 61 | readable: bool = false, | |
| 62 | writable: bool = false, | |
| 63 | }; | |
| 64 | ||
| 65 | fd: os.fd_t, | |
| 66 | ||
| 67 | pub fn init(flags: std.enums.EnumFieldStruct(Reactor.InitFlags, bool, false)) !Reactor { | |
| 68 | var raw_flags: u32 = 0; | |
| 69 | const set = std.EnumSet(Reactor.InitFlags).init(flags); | |
| 70 | if (set.contains(.close_on_exec)) raw_flags |= os.EPOLL_CLOEXEC; | |
| 71 | return Reactor{ .fd = try os.epoll_create1(raw_flags) }; | |
| 72 | } | |
| 73 | ||
| 74 | pub fn deinit(self: Reactor) void { | |
| 75 | os.close(self.fd); | |
| 76 | } | |
| 77 | ||
| 78 | pub fn update(self: Reactor, fd: os.fd_t, identifier: usize, interest: Reactor.Interest) !void { | |
| 79 | var flags: u32 = 0; | |
| 80 | flags |= if (interest.oneshot) os.EPOLLONESHOT else os.EPOLLET; | |
| 81 | if (interest.hup) flags |= os.EPOLLRDHUP; | |
| 82 | if (interest.readable) flags |= os.EPOLLIN; | |
| 83 | if (interest.writable) flags |= os.EPOLLOUT; | |
| 84 | ||
| 85 | const event = &os.epoll_event{ | |
| 86 | .events = flags, | |
| 87 | .data = .{ .ptr = identifier }, | |
| 88 | }; | |
| 89 | ||
| 90 | os.epoll_ctl(self.fd, os.EPOLL_CTL_MOD, fd, event) catch |err| switch (err) { | |
| 91 | error.FileDescriptorNotRegistered => try os.epoll_ctl(self.fd, os.EPOLL_CTL_ADD, fd, event), | |
| 92 | else => return err, | |
| 93 | }; | |
| 94 | } | |
| 95 | ||
| 96 | pub fn poll(self: Reactor, comptime max_num_events: comptime_int, closure: anytype, timeout_milliseconds: ?u64) !void { | |
| 97 | var events: [max_num_events]os.epoll_event = undefined; | |
| 98 | ||
| 99 | const num_events = os.epoll_wait(self.fd, &events, if (timeout_milliseconds) |ms| @intCast(i32, ms) else -1); | |
| 100 | for (events[0..num_events]) |ev| { | |
| 101 | const is_error = ev.events & os.EPOLLERR != 0; | |
| 102 | const is_hup = ev.events & (os.EPOLLHUP | os.EPOLLRDHUP) != 0; | |
| 103 | const is_readable = ev.events & os.EPOLLIN != 0; | |
| 104 | const is_writable = ev.events & os.EPOLLOUT != 0; | |
| 105 | ||
| 106 | try closure.call(Reactor.Event{ | |
| 107 | .data = ev.data.ptr, | |
| 108 | .is_error = is_error, | |
| 109 | .is_hup = is_hup, | |
| 110 | .is_readable = is_readable, | |
| 111 | .is_writable = is_writable, | |
| 112 | }); | |
| 113 | } | |
| 114 | } | |
| 115 | }; | |
| 116 | ||
| 117 | test "reactor/linux: drive async tcp client/listener pair" { | |
| 118 | if (builtin.os.tag != .linux) return error.SkipZigTest; | |
| 119 | ||
| 120 | const ip = std.x.net.ip; | |
| 121 | const tcp = std.x.net.tcp; | |
| 122 | ||
| 123 | const IPv4 = std.x.os.IPv4; | |
| 124 | const IPv6 = std.x.os.IPv6; | |
| 125 | const Socket = std.x.os.Socket; | |
| 126 | ||
| 127 | const reactor = try Reactor.init(.{ .close_on_exec = true }); | |
| 128 | defer reactor.deinit(); | |
| 129 | ||
| 130 | const listener = try tcp.Listener.init(.ip, .{ | |
| 131 | .close_on_exec = true, | |
| 132 | .nonblocking = true, | |
| 133 | }); | |
| 134 | defer listener.deinit(); | |
| 135 | ||
| 136 | try reactor.update(listener.socket.fd, 0, .{ .readable = true }); | |
| 137 | try reactor.poll(1, struct { | |
| 138 | fn call(event: Reactor.Event) !void { | |
| 139 | try testing.expectEqual(Reactor.Event{ | |
| 140 | .data = 0, | |
| 141 | .is_error = false, | |
| 142 | .is_hup = true, | |
| 143 | .is_readable = false, | |
| 144 | .is_writable = false, | |
| 145 | }, event); | |
| 146 | } | |
| 147 | }, null); | |
| 148 | ||
| 149 | try listener.bind(ip.Address.initIPv4(IPv4.unspecified, 0)); | |
| 150 | try listener.listen(128); | |
| 151 | ||
| 152 | var binded_address = try listener.getLocalAddress(); | |
| 153 | switch (binded_address) { | |
| 154 | .ipv4 => |*ipv4| ipv4.host = IPv4.localhost, | |
| 155 | .ipv6 => |*ipv6| ipv6.host = IPv6.localhost, | |
| 156 | } | |
| 157 | ||
| 158 | const client = try tcp.Client.init(.ip, .{ | |
| 159 | .close_on_exec = true, | |
| 160 | .nonblocking = true, | |
| 161 | }); | |
| 162 | defer client.deinit(); | |
| 163 | ||
| 164 | try reactor.update(client.socket.fd, 1, .{ .readable = true, .writable = true }); | |
| 165 | try reactor.poll(1, struct { | |
| 166 | fn call(event: Reactor.Event) !void { | |
| 167 | try testing.expectEqual(Reactor.Event{ | |
| 168 | .data = 1, | |
| 169 | .is_error = false, | |
| 170 | .is_hup = true, | |
| 171 | .is_readable = false, | |
| 172 | .is_writable = true, | |
| 173 | }, event); | |
| 174 | } | |
| 175 | }, null); | |
| 176 | ||
| 177 | client.connect(binded_address) catch |err| switch (err) { | |
| 178 | error.WouldBlock => {}, | |
| 179 | else => return err, | |
| 180 | }; | |
| 181 | ||
| 182 | try reactor.poll(1, struct { | |
| 183 | fn call(event: Reactor.Event) !void { | |
| 184 | try testing.expectEqual(Reactor.Event{ | |
| 185 | .data = 1, | |
| 186 | .is_error = false, | |
| 187 | .is_hup = false, | |
| 188 | .is_readable = false, | |
| 189 | .is_writable = true, | |
| 190 | }, event); | |
| 191 | } | |
| 192 | }, null); | |
| 193 | ||
| 194 | try reactor.poll(1, struct { | |
| 195 | fn call(event: Reactor.Event) !void { | |
| 196 | try testing.expectEqual(Reactor.Event{ | |
| 197 | .data = 0, | |
| 198 | .is_error = false, | |
| 199 | .is_hup = false, | |
| 200 | .is_readable = true, | |
| 201 | .is_writable = false, | |
| 202 | }, event); | |
| 203 | } | |
| 204 | }, null); | |
| 205 | } |
lib/std/x/os/socket.zig+199| ... | ... | @@ -11,8 +11,13 @@ const os = std.os; |
| 11 | 11 | const fmt = std.fmt; |
| 12 | 12 | const mem = std.mem; |
| 13 | 13 | const time = std.time; |
| 14 | const meta = std.meta; | |
| 14 | 15 | const builtin = std.builtin; |
| 15 | 16 | |
| 17 | const Buffer = std.x.os.Buffer; | |
| 18 | ||
| 19 | const assert = std.debug.assert; | |
| 20 | ||
| 16 | 21 | /// A generic, cross-platform socket abstraction. |
| 17 | 22 | pub const Socket = struct { |
| 18 | 23 | /// A socket-address pair. |
| ... | ... | @@ -29,6 +34,32 @@ pub const Socket = struct { |
| 29 | 34 | /// A generic socket address abstraction. It is safe to directly access and modify |
| 30 | 35 | /// the fields of a `Socket.Address`. |
| 31 | 36 | pub const Address = union(enum) { |
| 37 | pub const Native = struct { | |
| 38 | pub const requires_prepended_length = builtin.os.getVersionRange() == .semver; | |
| 39 | pub const Length = if (requires_prepended_length) u8 else [0]u8; | |
| 40 | ||
| 41 | pub const Family = if (requires_prepended_length) u8 else c_ushort; | |
| 42 | ||
| 43 | /// POSIX `sockaddr_storage`. The expected size and alignment is specified in IETF RFC 2553. | |
| 44 | pub const Storage = extern struct { | |
| 45 | pub const expected_size = 128; | |
| 46 | pub const expected_alignment = 8; | |
| 47 | ||
| 48 | pub const padding_size = expected_size - | |
| 49 | mem.alignForward(@sizeOf(Address.Native.Length), expected_alignment) - | |
| 50 | mem.alignForward(@sizeOf(Address.Native.Family), expected_alignment); | |
| 51 | ||
| 52 | len: Address.Native.Length align(expected_alignment) = undefined, | |
| 53 | family: Address.Native.Family align(expected_alignment) = undefined, | |
| 54 | padding: [padding_size]u8 align(expected_alignment) = undefined, | |
| 55 | ||
| 56 | comptime { | |
| 57 | assert(@sizeOf(Storage) == Storage.expected_size); | |
| 58 | assert(@alignOf(Storage) == Storage.expected_alignment); | |
| 59 | } | |
| 60 | }; | |
| 61 | }; | |
| 62 | ||
| 32 | 63 | ipv4: net.IPv4.Address, |
| 33 | 64 | ipv6: net.IPv6.Address, |
| 34 | 65 | |
| ... | ... | @@ -107,6 +138,174 @@ pub const Socket = struct { |
| 107 | 138 | } |
| 108 | 139 | }; |
| 109 | 140 | |
| 141 | /// POSIX `msghdr`. Denotes a destination address, set of buffers, control data, and flags. Ported | |
| 142 | /// directly from musl. | |
| 143 | pub const Message = if (builtin.os.isAtLeast(.windows, .vista) != null and builtin.os.isAtLeast(.windows, .vista).?) | |
| 144 | extern struct { | |
| 145 | name: usize = @ptrToInt(@as(?[*]u8, null)), | |
| 146 | name_len: c_int = 0, | |
| 147 | ||
| 148 | buffers: usize = undefined, | |
| 149 | buffers_len: c_ulong = undefined, | |
| 150 | ||
| 151 | control: Buffer = .{ | |
| 152 | .ptr = @ptrToInt(@as(?[*]u8, null)), | |
| 153 | .len = 0, | |
| 154 | }, | |
| 155 | flags: c_ulong = 0, | |
| 156 | ||
| 157 | pub usingnamespace MessageMixin(Message); | |
| 158 | } | |
| 159 | else if (builtin.os.tag == .windows) | |
| 160 | extern struct { | |
| 161 | name: usize = @ptrToInt(@as(?[*]u8, null)), | |
| 162 | name_len: c_int = 0, | |
| 163 | ||
| 164 | buffers: usize = undefined, | |
| 165 | buffers_len: u32 = undefined, | |
| 166 | ||
| 167 | control: Buffer = .{ | |
| 168 | .ptr = @ptrToInt(@as(?[*]u8, null)), | |
| 169 | .len = 0, | |
| 170 | }, | |
| 171 | flags: u32 = 0, | |
| 172 | ||
| 173 | pub usingnamespace MessageMixin(Message); | |
| 174 | } | |
| 175 | else if (@sizeOf(usize) > 4 and builtin.endian == .Big) | |
| 176 | extern struct { | |
| 177 | name: usize = @ptrToInt(@as(?[*]u8, null)), | |
| 178 | name_len: c_uint = 0, | |
| 179 | ||
| 180 | buffers: usize = undefined, | |
| 181 | _pad_1: c_int = 0, | |
| 182 | buffers_len: c_int = undefined, | |
| 183 | ||
| 184 | control: usize = @ptrToInt(@as(?[*]u8, null)), | |
| 185 | _pad_2: c_int = 0, | |
| 186 | control_len: c_uint = 0, | |
| 187 | ||
| 188 | flags: c_int = 0, | |
| 189 | ||
| 190 | pub usingnamespace MessageMixin(Message); | |
| 191 | } | |
| 192 | else if (@sizeOf(usize) > 4 and builtin.endian == .Little) | |
| 193 | extern struct { | |
| 194 | name: usize = @ptrToInt(@as(?[*]u8, null)), | |
| 195 | name_len: c_uint = 0, | |
| 196 | ||
| 197 | buffers: usize = undefined, | |
| 198 | buffers_len: c_int = undefined, | |
| 199 | _pad_1: c_int = 0, | |
| 200 | ||
| 201 | control: usize = @ptrToInt(@as(?[*]u8, null)), | |
| 202 | control_len: c_uint = 0, | |
| 203 | _pad_2: c_int = 0, | |
| 204 | ||
| 205 | flags: c_int = 0, | |
| 206 | ||
| 207 | pub usingnamespace MessageMixin(Message); | |
| 208 | } | |
| 209 | else | |
| 210 | extern struct { | |
| 211 | name: usize = @ptrToInt(@as(?[*]u8, null)), | |
| 212 | name_len: c_uint = 0, | |
| 213 | ||
| 214 | buffers: usize = undefined, | |
| 215 | buffers_len: c_int, | |
| 216 | ||
| 217 | control: usize = null, | |
| 218 | control_len: c_uint = 0, | |
| 219 | ||
| 220 | flags: c_int = 0, | |
| 221 | ||
| 222 | pub usingnamespace MessageMixin(Message); | |
| 223 | }; | |
| 224 | ||
| 225 | fn MessageMixin(comptime Self: type) type { | |
| 226 | return struct { | |
| 227 | pub fn fromBuffers(buffers: []const Buffer) Self { | |
| 228 | var self: Self = .{}; | |
| 229 | self.setBuffers(buffers); | |
| 230 | return self; | |
| 231 | } | |
| 232 | ||
| 233 | pub fn setName(self: *Self, name: []const u8) void { | |
| 234 | self.name = @ptrToInt(name.ptr); | |
| 235 | self.name_len = @intCast(meta.fieldInfo(Self, .name_len).field_type, name.len); | |
| 236 | } | |
| 237 | ||
| 238 | pub fn setBuffers(self: *Self, buffers: []const Buffer) void { | |
| 239 | self.buffers = @ptrToInt(buffers.ptr); | |
| 240 | self.buffers_len = @intCast(meta.fieldInfo(Self, .buffers_len).field_type, buffers.len); | |
| 241 | } | |
| 242 | ||
| 243 | pub fn setControl(self: *Self, control: []const u8) void { | |
| 244 | if (builtin.os.tag == .windows) { | |
| 245 | self.control = Buffer.from(control); | |
| 246 | } else { | |
| 247 | self.control = @ptrToInt(control.ptr); | |
| 248 | self.control_len = @intCast(meta.fieldInfo(Self, .control_len).field_type, control.len); | |
| 249 | } | |
| 250 | } | |
| 251 | ||
| 252 | pub fn setFlags(self: *Self, flags: u32) void { | |
| 253 | self.flags = @intCast(meta.fieldInfo(Self, .flags).field_type, flags); | |
| 254 | } | |
| 255 | ||
| 256 | pub fn getName(self: Self) []const u8 { | |
| 257 | return @intToPtr([*]const u8, self.name)[0..@intCast(usize, self.name_len)]; | |
| 258 | } | |
| 259 | ||
| 260 | pub fn getBuffers(self: Self) []const Buffer { | |
| 261 | return @intToPtr([*]const Buffer, self.buffers)[0..@intCast(usize, self.buffers_len)]; | |
| 262 | } | |
| 263 | ||
| 264 | pub fn getControl(self: Self) []const u8 { | |
| 265 | if (builtin.os.tag == .windows) { | |
| 266 | return self.control.into(); | |
| 267 | } else { | |
| 268 | return @intToPtr([*]const u8, self.control)[0..@intCast(usize, self.control_len)]; | |
| 269 | } | |
| 270 | } | |
| 271 | ||
| 272 | pub fn getFlags(self: Self) u32 { | |
| 273 | return @intCast(u32, self.flags); | |
| 274 | } | |
| 275 | }; | |
| 276 | } | |
| 277 | ||
| 278 | /// POSIX `linger`, denoting the linger settings of a socket. | |
| 279 | /// | |
| 280 | /// Microsoft's documentation and glibc denote the fields to be unsigned | |
| 281 | /// short's on Windows, whereas glibc and musl denote the fields to be | |
| 282 | /// int's on every other platform. | |
| 283 | pub const Linger = extern struct { | |
| 284 | pub const Field = switch (builtin.os.tag) { | |
| 285 | .windows => c_ushort, | |
| 286 | else => c_int, | |
| 287 | }; | |
| 288 | ||
| 289 | enabled: Field, | |
| 290 | timeout_seconds: Field, | |
| 291 | ||
| 292 | pub fn init(timeout_seconds: ?u16) Socket.Linger { | |
| 293 | return .{ | |
| 294 | .enabled = @intCast(Socket.Linger.Field, @boolToInt(timeout_seconds != null)), | |
| 295 | .timeout_seconds = if (timeout_seconds) |seconds| @intCast(Socket.Linger.Field, seconds) else 0, | |
| 296 | }; | |
| 297 | } | |
| 298 | }; | |
| 299 | ||
| 300 | /// Possible set of flags to initialize a socket with. | |
| 301 | pub const InitFlags = enum { | |
| 302 | // Initialize a socket to be non-blocking. | |
| 303 | nonblocking, | |
| 304 | ||
| 305 | // Have a socket close itself on exec syscalls. | |
| 306 | close_on_exec, | |
| 307 | }; | |
| 308 | ||
| 110 | 309 | /// The underlying handle of a socket. |
| 111 | 310 | fd: os.socket_t, |
| 112 | 311 |
lib/std/x/os/socket_posix.zig+38-39| ... | ... | @@ -13,8 +13,12 @@ const time = std.time; |
| 13 | 13 | pub fn Mixin(comptime Socket: type) type { |
| 14 | 14 | return struct { |
| 15 | 15 | /// Open a new socket. |
| 16 | pub fn init(domain: u32, socket_type: u32, protocol: u32) !Socket { | |
| 17 | return Socket{ .fd = try os.socket(domain, socket_type, protocol) }; | |
| 16 | pub fn init(domain: u32, socket_type: u32, protocol: u32, flags: std.enums.EnumFieldStruct(Socket.InitFlags, bool, false)) !Socket { | |
| 17 | var raw_flags: u32 = socket_type; | |
| 18 | const set = std.EnumSet(Socket.InitFlags).init(flags); | |
| 19 | if (set.contains(.close_on_exec)) raw_flags |= os.SOCK_CLOEXEC; | |
| 20 | if (set.contains(.nonblocking)) raw_flags |= os.SOCK_NONBLOCK; | |
| 21 | return Socket{ .fd = try os.socket(domain, raw_flags, protocol) }; | |
| 18 | 22 | } |
| 19 | 23 | |
| 20 | 24 | /// Closes the socket. |
| ... | ... | @@ -44,11 +48,16 @@ pub fn Mixin(comptime Socket: type) type { |
| 44 | 48 | |
| 45 | 49 | /// Accept a pending incoming connection queued to the kernel backlog |
| 46 | 50 | /// of the socket. |
| 47 | pub fn accept(self: Socket, flags: u32) !Socket.Connection { | |
| 48 | var address: os.sockaddr_storage = undefined; | |
| 49 | var address_len: u32 = @sizeOf(os.sockaddr_storage); | |
| 51 | pub fn accept(self: Socket, flags: std.enums.EnumFieldStruct(Socket.InitFlags, bool, false)) !Socket.Connection { | |
| 52 | var address: Socket.Address.Native.Storage = undefined; | |
| 53 | var address_len: u32 = @sizeOf(Socket.Address.Native.Storage); | |
| 50 | 54 | |
| 51 | const socket = Socket{ .fd = try os.accept(self.fd, @ptrCast(*os.sockaddr, &address), &address_len, flags) }; | |
| 55 | var raw_flags: u32 = 0; | |
| 56 | const set = std.EnumSet(Socket.InitFlags).init(flags); | |
| 57 | if (set.contains(.close_on_exec)) raw_flags |= os.SOCK_CLOEXEC; | |
| 58 | if (set.contains(.nonblocking)) raw_flags |= os.SOCK_NONBLOCK; | |
| 59 | ||
| 60 | const socket = Socket{ .fd = try os.accept(self.fd, @ptrCast(*os.sockaddr, &address), &address_len, raw_flags) }; | |
| 52 | 61 | const socket_address = Socket.Address.fromNative(@ptrCast(*os.sockaddr, &address)); |
| 53 | 62 | |
| 54 | 63 | return Socket.Connection.from(socket, socket_address); |
| ... | ... | @@ -69,48 +78,45 @@ pub fn Mixin(comptime Socket: type) type { |
| 69 | 78 | /// Writes multiple I/O vectors with a prepended message header to the socket |
| 70 | 79 | /// with a set of flags specified. It returns the number of bytes that are |
| 71 | 80 | /// written to the socket. |
| 72 | pub fn writeVectorized(self: Socket, msg: os.msghdr_const, flags: u32) !usize { | |
| 81 | pub fn writeVectorized(self: Socket, msg: Socket.Message, flags: u32) !usize { | |
| 73 | 82 | return os.sendmsg(self.fd, msg, flags); |
| 74 | 83 | } |
| 75 | 84 | |
| 76 | 85 | /// Read multiple I/O vectors with a prepended message header from the socket |
| 77 | 86 | /// with a set of flags specified. It returns the number of bytes that were |
| 78 | 87 | /// read into the buffer provided. |
| 79 | pub fn readVectorized(self: Socket, msg: *os.msghdr, flags: u32) !usize { | |
| 80 | if (comptime @hasDecl(os.system, "recvmsg")) { | |
| 81 | while (true) { | |
| 82 | const rc = os.system.recvmsg(self.fd, msg, flags); | |
| 83 | return switch (os.errno(rc)) { | |
| 84 | 0 => @intCast(usize, rc), | |
| 85 | os.EBADF => unreachable, // always a race condition | |
| 86 | os.EFAULT => unreachable, | |
| 87 | os.EINVAL => unreachable, | |
| 88 | os.ENOTCONN => unreachable, | |
| 89 | os.ENOTSOCK => unreachable, | |
| 90 | os.EINTR => continue, | |
| 91 | os.EAGAIN => error.WouldBlock, | |
| 92 | os.ENOMEM => error.SystemResources, | |
| 93 | os.ECONNREFUSED => error.ConnectionRefused, | |
| 94 | os.ECONNRESET => error.ConnectionResetByPeer, | |
| 95 | else => |err| os.unexpectedErrno(err), | |
| 96 | }; | |
| 97 | } | |
| 88 | pub fn readVectorized(self: Socket, msg: *Socket.Message, flags: u32) !usize { | |
| 89 | while (true) { | |
| 90 | const rc = os.system.recvmsg(self.fd, msg, flags); | |
| 91 | return switch (os.errno(rc)) { | |
| 92 | 0 => @intCast(usize, rc), | |
| 93 | os.EBADF => unreachable, // always a race condition | |
| 94 | os.EFAULT => unreachable, | |
| 95 | os.EINVAL => unreachable, | |
| 96 | os.ENOTCONN => unreachable, | |
| 97 | os.ENOTSOCK => unreachable, | |
| 98 | os.EINTR => continue, | |
| 99 | os.EAGAIN => error.WouldBlock, | |
| 100 | os.ENOMEM => error.SystemResources, | |
| 101 | os.ECONNREFUSED => error.ConnectionRefused, | |
| 102 | os.ECONNRESET => error.ConnectionResetByPeer, | |
| 103 | else => |err| os.unexpectedErrno(err), | |
| 104 | }; | |
| 98 | 105 | } |
| 99 | return error.NotSupported; | |
| 100 | 106 | } |
| 101 | 107 | |
| 102 | 108 | /// Query the address that the socket is locally bounded to. |
| 103 | 109 | pub fn getLocalAddress(self: Socket) !Socket.Address { |
| 104 | var address: os.sockaddr_storage = undefined; | |
| 105 | var address_len: u32 = @sizeOf(os.sockaddr_storage); | |
| 110 | var address: Socket.Address.Native.Storage = undefined; | |
| 111 | var address_len: u32 = @sizeOf(Socket.Address.Native.Storage); | |
| 106 | 112 | try os.getsockname(self.fd, @ptrCast(*os.sockaddr, &address), &address_len); |
| 107 | 113 | return Socket.Address.fromNative(@ptrCast(*os.sockaddr, &address)); |
| 108 | 114 | } |
| 109 | 115 | |
| 110 | 116 | /// Query the address that the socket is connected to. |
| 111 | 117 | pub fn getRemoteAddress(self: Socket) !Socket.Address { |
| 112 | var address: os.sockaddr_storage = undefined; | |
| 113 | var address_len: u32 = @sizeOf(os.sockaddr_storage); | |
| 118 | var address: Socket.Address.Native.Storage = undefined; | |
| 119 | var address_len: u32 = @sizeOf(Socket.Address.Native.Storage); | |
| 114 | 120 | try os.getpeername(self.fd, @ptrCast(*os.sockaddr, &address), &address_len); |
| 115 | 121 | return Socket.Address.fromNative(@ptrCast(*os.sockaddr, &address)); |
| 116 | 122 | } |
| ... | ... | @@ -165,14 +171,7 @@ pub fn Mixin(comptime Socket: type) type { |
| 165 | 171 | /// seconds. |
| 166 | 172 | pub fn setLinger(self: Socket, timeout_seconds: ?u16) !void { |
| 167 | 173 | if (comptime @hasDecl(os, "SO_LINGER")) { |
| 168 | const settings = extern struct { | |
| 169 | l_onoff: c_int, | |
| 170 | l_linger: c_int, | |
| 171 | }{ | |
| 172 | .l_onoff = @intCast(c_int, @boolToInt(timeout_seconds != null)), | |
| 173 | .l_linger = if (timeout_seconds) |seconds| @intCast(c_int, seconds) else 0, | |
| 174 | }; | |
| 175 | ||
| 174 | const settings = Socket.Linger.init(timeout_seconds); | |
| 176 | 175 | return self.setOption(os.SOL_SOCKET, os.SO_LINGER, mem.asBytes(&settings)); |
| 177 | 176 | } |
| 178 | 177 |
lib/std/x/os/socket_windows.zig+39-27| ... | ... | @@ -16,27 +16,24 @@ const ws2_32 = windows.ws2_32; |
| 16 | 16 | pub fn Mixin(comptime Socket: type) type { |
| 17 | 17 | return struct { |
| 18 | 18 | /// Open a new socket. |
| 19 | pub fn init(domain: u32, socket_type: u32, protocol: u32) !Socket { | |
| 20 | var filtered_socket_type = socket_type & ~@as(u32, os.SOCK_CLOEXEC); | |
| 21 | ||
| 22 | var filtered_flags: u32 = ws2_32.WSA_FLAG_OVERLAPPED; | |
| 23 | if (socket_type & os.SOCK_CLOEXEC != 0) { | |
| 24 | filtered_flags |= ws2_32.WSA_FLAG_NO_HANDLE_INHERIT; | |
| 25 | } | |
| 19 | pub fn init(domain: u32, socket_type: u32, protocol: u32, flags: std.enums.EnumFieldStruct(Socket.InitFlags, bool, false)) !Socket { | |
| 20 | var raw_flags: u32 = 0; | |
| 21 | const set = std.EnumSet(Socket.InitFlags).init(flags); | |
| 22 | if (set.contains(.close_on_exec)) raw_flags |= ws2_32.WSA_FLAG_NO_HANDLE_INHERIT; | |
| 26 | 23 | |
| 27 | 24 | const fd = ws2_32.WSASocketW( |
| 28 | 25 | @intCast(i32, domain), |
| 29 | @intCast(i32, filtered_socket_type), | |
| 26 | @intCast(i32, socket_type), | |
| 30 | 27 | @intCast(i32, protocol), |
| 31 | 28 | null, |
| 32 | 29 | 0, |
| 33 | filtered_flags, | |
| 30 | raw_flags, | |
| 34 | 31 | ); |
| 35 | 32 | if (fd == ws2_32.INVALID_SOCKET) { |
| 36 | 33 | return switch (ws2_32.WSAGetLastError()) { |
| 37 | 34 | .WSANOTINITIALISED => { |
| 38 | 35 | _ = try windows.WSAStartup(2, 2); |
| 39 | return Socket.init(domain, socket_type, protocol); | |
| 36 | return Socket.init(domain, socket_type, protocol, flags); | |
| 40 | 37 | }, |
| 41 | 38 | .WSAEAFNOSUPPORT => error.AddressFamilyNotSupported, |
| 42 | 39 | .WSAEMFILE => error.ProcessFdQuotaExceeded, |
| ... | ... | @@ -46,6 +43,14 @@ pub fn Mixin(comptime Socket: type) type { |
| 46 | 43 | }; |
| 47 | 44 | } |
| 48 | 45 | |
| 46 | if (set.contains(.nonblocking)) { | |
| 47 | var enabled: c_ulong = 1; | |
| 48 | const rc = ws2_32.ioctlsocket(fd, ws2_32.FIONBIO, &enabled); | |
| 49 | if (rc == ws2_32.SOCKET_ERROR) { | |
| 50 | return windows.unexpectedWSAError(ws2_32.WSAGetLastError()); | |
| 51 | } | |
| 52 | } | |
| 53 | ||
| 49 | 54 | return Socket{ .fd = fd }; |
| 50 | 55 | } |
| 51 | 56 | |
| ... | ... | @@ -138,12 +143,12 @@ pub fn Mixin(comptime Socket: type) type { |
| 138 | 143 | |
| 139 | 144 | /// Accept a pending incoming connection queued to the kernel backlog |
| 140 | 145 | /// of the socket. |
| 141 | pub fn accept(self: Socket, flags: u32) !Socket.Connection { | |
| 142 | var address: ws2_32.sockaddr_storage = undefined; | |
| 143 | var address_len: c_int = @sizeOf(ws2_32.sockaddr_storage); | |
| 146 | pub fn accept(self: Socket, flags: std.enums.EnumFieldStruct(Socket.InitFlags, bool, false)) !Socket.Connection { | |
| 147 | var address: Socket.Address.Native.Storage = undefined; | |
| 148 | var address_len: c_int = @sizeOf(Socket.Address.Native.Storage); | |
| 144 | 149 | |
| 145 | const rc = ws2_32.accept(self.fd, @ptrCast(*ws2_32.sockaddr, &address), &address_len); | |
| 146 | if (rc == ws2_32.INVALID_SOCKET) { | |
| 150 | const fd = ws2_32.accept(self.fd, @ptrCast(*ws2_32.sockaddr, &address), &address_len); | |
| 151 | if (fd == ws2_32.INVALID_SOCKET) { | |
| 147 | 152 | return switch (ws2_32.WSAGetLastError()) { |
| 148 | 153 | .WSANOTINITIALISED => unreachable, |
| 149 | 154 | .WSAECONNRESET => error.ConnectionResetByPeer, |
| ... | ... | @@ -158,9 +163,20 @@ pub fn Mixin(comptime Socket: type) type { |
| 158 | 163 | }; |
| 159 | 164 | } |
| 160 | 165 | |
| 161 | const socket = Socket.from(rc); | |
| 166 | const socket = Socket.from(fd); | |
| 167 | errdefer socket.deinit(); | |
| 168 | ||
| 162 | 169 | const socket_address = Socket.Address.fromNative(@ptrCast(*ws2_32.sockaddr, &address)); |
| 163 | 170 | |
| 171 | const set = std.EnumSet(Socket.InitFlags).init(flags); | |
| 172 | if (set.contains(.nonblocking)) { | |
| 173 | var enabled: c_ulong = 1; | |
| 174 | const rc = ws2_32.ioctlsocket(fd, ws2_32.FIONBIO, &enabled); | |
| 175 | if (rc == ws2_32.SOCKET_ERROR) { | |
| 176 | return windows.unexpectedWSAError(ws2_32.WSAGetLastError()); | |
| 177 | } | |
| 178 | } | |
| 179 | ||
| 164 | 180 | return Socket.Connection.from(socket, socket_address); |
| 165 | 181 | } |
| 166 | 182 | |
| ... | ... | @@ -238,7 +254,7 @@ pub fn Mixin(comptime Socket: type) type { |
| 238 | 254 | /// Writes multiple I/O vectors with a prepended message header to the socket |
| 239 | 255 | /// with a set of flags specified. It returns the number of bytes that are |
| 240 | 256 | /// written to the socket. |
| 241 | pub fn writeVectorized(self: Socket, msg: ws2_32.msghdr_const, flags: u32) !usize { | |
| 257 | pub fn writeVectorized(self: Socket, msg: Socket.Message, flags: u32) !usize { | |
| 242 | 258 | const call = try windows.loadWinsockExtensionFunction(ws2_32.LPFN_WSASENDMSG, self.fd, ws2_32.WSAID_WSASENDMSG); |
| 243 | 259 | |
| 244 | 260 | var num_bytes: u32 = undefined; |
| ... | ... | @@ -275,7 +291,7 @@ pub fn Mixin(comptime Socket: type) type { |
| 275 | 291 | /// Read multiple I/O vectors with a prepended message header from the socket |
| 276 | 292 | /// with a set of flags specified. It returns the number of bytes that were |
| 277 | 293 | /// read into the buffer provided. |
| 278 | pub fn readVectorized(self: Socket, msg: *ws2_32.msghdr, flags: u32) !usize { | |
| 294 | pub fn readVectorized(self: Socket, msg: *Socket.Message, flags: u32) !usize { | |
| 279 | 295 | const call = try windows.loadWinsockExtensionFunction(ws2_32.LPFN_WSARECVMSG, self.fd, ws2_32.WSAID_WSARECVMSG); |
| 280 | 296 | |
| 281 | 297 | var num_bytes: u32 = undefined; |
| ... | ... | @@ -311,8 +327,8 @@ pub fn Mixin(comptime Socket: type) type { |
| 311 | 327 | |
| 312 | 328 | /// Query the address that the socket is locally bounded to. |
| 313 | 329 | pub fn getLocalAddress(self: Socket) !Socket.Address { |
| 314 | var address: ws2_32.sockaddr_storage = undefined; | |
| 315 | var address_len: c_int = @sizeOf(ws2_32.sockaddr_storage); | |
| 330 | var address: Socket.Address.Native.Storage = undefined; | |
| 331 | var address_len: c_int = @sizeOf(Socket.Address.Native.Storage); | |
| 316 | 332 | |
| 317 | 333 | const rc = ws2_32.getsockname(self.fd, @ptrCast(*ws2_32.sockaddr, &address), &address_len); |
| 318 | 334 | if (rc == ws2_32.SOCKET_ERROR) { |
| ... | ... | @@ -331,8 +347,8 @@ pub fn Mixin(comptime Socket: type) type { |
| 331 | 347 | |
| 332 | 348 | /// Query the address that the socket is connected to. |
| 333 | 349 | pub fn getRemoteAddress(self: Socket) !Socket.Address { |
| 334 | var address: ws2_32.sockaddr_storage = undefined; | |
| 335 | var address_len: c_int = @sizeOf(ws2_32.sockaddr_storage); | |
| 350 | var address: Socket.Address.Native.Storage = undefined; | |
| 351 | var address_len: c_int = @sizeOf(Socket.Address.Native.Storage); | |
| 336 | 352 | |
| 337 | 353 | const rc = ws2_32.getpeername(self.fd, @ptrCast(*ws2_32.sockaddr, &address), &address_len); |
| 338 | 354 | if (rc == ws2_32.SOCKET_ERROR) { |
| ... | ... | @@ -384,11 +400,7 @@ pub fn Mixin(comptime Socket: type) type { |
| 384 | 400 | /// if the host does not support the option for a socket to linger around up until a timeout specified in |
| 385 | 401 | /// seconds. |
| 386 | 402 | pub fn setLinger(self: Socket, timeout_seconds: ?u16) !void { |
| 387 | const settings = ws2_32.linger{ | |
| 388 | .l_onoff = @as(u16, @boolToInt(timeout_seconds != null)), | |
| 389 | .l_linger = if (timeout_seconds) |seconds| seconds else 0, | |
| 390 | }; | |
| 391 | ||
| 403 | const settings = Socket.Linger.init(timeout_seconds); | |
| 392 | 404 | return self.setOption(ws2_32.SOL_SOCKET, ws2_32.SO_LINGER, mem.asBytes(&settings)); |
| 393 | 405 | } |
| 394 | 406 |