implement poll for windows with WSAPoll (only available on vista and higher)

This commit is contained in:
Bas van den Berg 2020-09-10 20:20:27 +02:00
parent f5b9e445aa
commit 127fa80090
4 changed files with 68 additions and 7 deletions

View file

@ -5208,6 +5208,9 @@ pub fn copy_file_range(fd_in: fd_t, off_in: u64, fd_out: fd_t, off_out: u64, len
} }
pub const PollError = error{ pub const PollError = error{
/// The network subsystem has failed.
NetworkSubsystemFailed,
/// The kernel had no space to allocate file descriptor tables. /// The kernel had no space to allocate file descriptor tables.
SystemResources, SystemResources,
} || UnexpectedError; } || UnexpectedError;
@ -5215,6 +5218,19 @@ pub const PollError = error{
pub fn poll(fds: []pollfd, timeout: i32) PollError!usize { pub fn poll(fds: []pollfd, timeout: i32) PollError!usize {
while (true) { while (true) {
const rc = system.poll(fds.ptr, fds.len, timeout); const rc = system.poll(fds.ptr, fds.len, timeout);
if (builtin.os.tag == .windows) {
if (rc == windows.ws2_32.SOCKET_ERROR) {
switch (windows.ws2_32.WSAGetLastError()) {
.WSANOTINITIALISED => unreachable,
.WSAENETDOWN => return error.NetworkSubsystemFailed,
.WSAENOBUFS => return error.SystemResources,
// TODO: handle more errors
else => |err| return windows.unexpectedWSAError(err),
}
} else {
return @intCast(usize, rc);
}
} else {
switch (errno(rc)) { switch (errno(rc)) {
0 => return @intCast(usize, rc), 0 => return @intCast(usize, rc),
EFAULT => unreachable, EFAULT => unreachable,
@ -5224,6 +5240,8 @@ pub fn poll(fds: []pollfd, timeout: i32) PollError!usize {
else => |err| return unexpectedErrno(err), else => |err| return unexpectedErrno(err),
} }
} }
unreachable;
}
} }
pub const RecvFromError = error{ pub const RecvFromError = error{

View file

@ -243,6 +243,19 @@ pub const IPPROTO_UDP = ws2_32.IPPROTO_UDP;
pub const IPPROTO_ICMPV6 = ws2_32.IPPROTO_ICMPV6; pub const IPPROTO_ICMPV6 = ws2_32.IPPROTO_ICMPV6;
pub const IPPROTO_RM = ws2_32.IPPROTO_RM; pub const IPPROTO_RM = ws2_32.IPPROTO_RM;
pub const pollfd = ws2_32.pollfd;
pub const POLLRDNORM = ws2_32.POLLRDNORM;
pub const POLLRDBAND = ws2_32.POLLRDBAND;
pub const POLLIN = ws2_32.POLLIN;
pub const POLLPRI = ws2_32.POLLPRI;
pub const POLLWRNORM = ws2_32.POLLWRNORM;
pub const POLLOUT = ws2_32.POLLOUT;
pub const POLLWRBAND = ws2_32.POLLWRBAND;
pub const POLLERR = ws2_32.POLLERR;
pub const POLLHUP = ws2_32.POLLHUP;
pub const POLLNVAL = ws2_32.POLLNVAL;
pub const O_RDONLY = 0o0; pub const O_RDONLY = 0o0;
pub const O_WRONLY = 0o1; pub const O_WRONLY = 0o1;
pub const O_RDWR = 0o2; pub const O_RDWR = 0o2;

View file

@ -1201,6 +1201,10 @@ pub fn recvfrom(s: ws2_32.SOCKET, buf: [*]u8, len: usize, flags: u32, from: ?*ws
} }
} }
pub fn poll(fds: [*]ws2_32.pollfd, n: usize, timeout: i32) i32 {
return ws2_32.WSAPoll(fds, @intCast(u32, n), timeout);
}
pub fn WSAIoctl( pub fn WSAIoctl(
s: ws2_32.SOCKET, s: ws2_32.SOCKET,
dwIoControlCode: DWORD, dwIoControlCode: DWORD,

View file

@ -234,6 +234,27 @@ pub const WSAMSG = extern struct {
dwFlags: DWORD, dwFlags: DWORD,
}; };
pub const pollfd = extern struct {
fd: SOCKET,
events: SHORT,
revents: SHORT,
};
// Event flag definitions for WSAPoll().
pub const POLLRDNORM = 0x0100;
pub const POLLRDBAND = 0x0200;
pub const POLLIN = (POLLRDNORM | POLLRDBAND);
pub const POLLPRI = 0x0400;
pub const POLLWRNORM = 0x0010;
pub const POLLOUT = (POLLWRNORM);
pub const POLLWRBAND = 0x0020;
pub const POLLERR = 0x0001;
pub const POLLHUP = 0x0002;
pub const POLLNVAL = 0x0004;
// https://docs.microsoft.com/en-au/windows/win32/winsock/windows-sockets-error-codes-2 // https://docs.microsoft.com/en-au/windows/win32/winsock/windows-sockets-error-codes-2
pub const WinsockError = extern enum(u16) { pub const WinsockError = extern enum(u16) {
/// Specified event object handle is invalid. /// Specified event object handle is invalid.
@ -790,6 +811,11 @@ pub extern "ws2_32" fn WSASendTo(
lpOverlapped: ?*WSAOVERLAPPED, lpOverlapped: ?*WSAOVERLAPPED,
lpCompletionRoutine: ?WSAOVERLAPPED_COMPLETION_ROUTINE, lpCompletionRoutine: ?WSAOVERLAPPED_COMPLETION_ROUTINE,
) callconv(.Stdcall) c_int; ) callconv(.Stdcall) c_int;
pub extern "ws2_32" fn WSAPoll(
fdArray: [*]pollfd,
fds: c_ulong,
timeout: c_int,
) callconv(.Stdcall) c_int;
pub extern "ws2_32" fn getaddrinfo( pub extern "ws2_32" fn getaddrinfo(
pNodeName: [*:0]const u8, pNodeName: [*:0]const u8,
pServiceName: [*:0]const u8, pServiceName: [*:0]const u8,