Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions Sources/NIOCore/IO.swift
Original file line number Diff line number Diff line change
Expand Up @@ -62,15 +62,15 @@ public struct IOError: Swift.Error {
.reason(self.failureDescription)
}

private enum Error {
package enum Error {
#if os(Windows)
case windows(DWORD)
case winsock(CInt)
#endif
case errno(CInt)
}

private let error: Error
package let error: Error

/// The `errno` that was set for the operation.
public var errnoCode: CInt {
Expand Down
11 changes: 11 additions & 0 deletions Sources/NIOHTTP1Server/main.swift
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@ import NIOCore
import NIOHTTP1
import NIOPosix

#if os(Windows)
import WinSDK
#endif

extension String {
func chopPrefix(_ prefix: String) -> String? {
if self.unicodeScalars.starts(with: prefix.unicodeScalars) {
Expand Down Expand Up @@ -664,7 +668,14 @@ let channel = try { () -> Channel in
case .unixDomainSocket(let path):
return try socketBootstrap.bind(unixDomainSocketPath: path).wait()
case .stdio:
#if os(Windows)
return try pipeBootstrap.takingOwnershipOfDescriptors(
input: Int32(bitPattern: STD_INPUT_HANDLE),
output: Int32(bitPattern: STD_OUTPUT_HANDLE)
).wait()
#else
return try pipeBootstrap.takingOwnershipOfDescriptors(input: STDIN_FILENO, output: STDOUT_FILENO).wait()
#endif
}
}()

Expand Down
8 changes: 6 additions & 2 deletions Sources/NIOPosix/BSDSocketAPIWindows.swift
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,11 @@ extension NIOBSDSocket {
) throws -> NIOBSDSocket.Handle? {
let socket: NIOBSDSocket.Handle = WinSDK.accept(s, addr, addrlen)
if socket == WinSDK.INVALID_SOCKET {
throw IOError(winsock: WSAGetLastError(), reason: "accept")
let lastError = WSAGetLastError()
if lastError == WSAEWOULDBLOCK {
return nil
}
throw IOError(winsock: lastError, reason: "accept")
}
return socket
}
Expand Down Expand Up @@ -228,7 +232,7 @@ extension NIOBSDSocket {
) throws -> Bool {
if WinSDK.connect(s, name, namelen) == SOCKET_ERROR {
let iResult = WSAGetLastError()
if iResult == WSAEWOULDBLOCK { return true }
if iResult == WSAEWOULDBLOCK { return false }
throw IOError(winsock: WSAGetLastError(), reason: "connect")
}
return true
Expand Down
3 changes: 3 additions & 0 deletions Sources/NIOPosix/BaseSocketChannel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@
import Atomics
import NIOConcurrencyHelpers
import NIOCore
#if os(Windows)
import WinSDK
#endif

private struct SocketChannelLifecycleManager {
// MARK: Types
Expand Down
14 changes: 13 additions & 1 deletion Sources/NIOPosix/SelectorGeneric.swift
Original file line number Diff line number Diff line change
Expand Up @@ -217,8 +217,20 @@ internal class Selector<R: Registration> {
#elseif os(Windows)
@usableFromInline
typealias EventType = WinSDK.pollfd
/// Array of poll file descriptors monitored by WSAPoll. The first entry is always the wakeup socket.
@usableFromInline
var pollFDs = [WinSDK.pollfd]()
var pollFDs = [pollfd]()
/// Tracks indexes of file descriptors pending removal from `pollFDs`. We defer removal until after
/// processing all events in `whenReady0` to avoid invalidating indexes during iteration. Stored as
/// indexes rather than a parallel boolean array for O(1) lookup during cleanup.
@usableFromInline
var deregisteredFDs = Set<Int>()
/// The read end of the wakeup socket pair. This is monitored in WSAPoll to allow waking up the event loop.
@usableFromInline
var wakeupReadSocket: NIOBSDSocket.Handle = NIOBSDSocket.invalidHandle
/// The write end of the wakeup socket pair. Writing to this socket wakes up the event loop.
@usableFromInline
var wakeupWriteSocket: NIOBSDSocket.Handle = NIOBSDSocket.invalidHandle
#else
#error("Unsupported platform, no suitable selector backend (we need kqueue or epoll support)")
#endif
Expand Down
Loading