Minor cleanups of Windows compatability code. (#1663)

Motivation:

Cleanup some noise that came in with some previous Windows compat
patches.

Modifications:

- Move INVALID_SOCKET to NIOBSDSocket, and refer to it by a better name
  there.
- Comment in an empty block to indicate that it's supposed to be empty.

Result:

Bit cleaner code.
This commit is contained in:
Cory Benfield 2020-09-30 19:55:21 +01:00 committed by GitHub
parent b5c1696033
commit c3e2359c55
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 13 additions and 16 deletions

View File

@ -15,6 +15,8 @@
#if os(Windows)
import ucrt
import let WinSDK.INVALID_SOCKET
import let WinSDK.IPPROTO_IP
import let WinSDK.IPPROTO_IPV6
import let WinSDK.IPPROTO_TCP
@ -76,6 +78,12 @@ public enum NIOBSDSocket {
#else
public typealias Handle = CInt
#endif
#if os(Windows)
internal static let invalidHandle: Handle = INVALID_SOCKET
#else
internal static let invalidHandle: Handle = -1
#endif
}
extension NIOBSDSocket {

View File

@ -14,10 +14,6 @@
import NIOConcurrencyHelpers
#if os(Windows)
import let WinSDK.INVALID_SOCKET
#endif
/// The requested UDS path exists and has wrong type (not a socket).
public struct UnixDomainSocketPathWrongType: Error {}
@ -218,7 +214,7 @@ class BaseSocket: BaseSocketProtocol {
private var descriptor: NIOBSDSocket.Handle
public var isOpen: Bool {
#if os(Windows)
return descriptor != WinSDK.INVALID_SOCKET
return descriptor != NIOBSDSocket.invalidHandle
#else
return descriptor >= 0
#endif
@ -343,7 +339,7 @@ class BaseSocket: BaseSocketProtocol {
/// - descriptor: The file descriptor to wrap.
init(socket descriptor: NIOBSDSocket.Handle) throws {
#if os(Windows)
precondition(descriptor != WinSDK.INVALID_SOCKET, "invalid socket")
precondition(descriptor != NIOBSDSocket.invalidHandle, "invalid socket")
#else
precondition(descriptor >= 0, "invalid socket")
#endif
@ -351,11 +347,7 @@ class BaseSocket: BaseSocketProtocol {
do {
try self.ignoreSIGPIPE()
} catch {
#if os(Windows)
self.descriptor = INVALID_SOCKET // We have to unset the fd here, otherwise we'll crash with "leaking open BaseSocket"
#else
self.descriptor = -1 // We have to unset the fd here, otherwise we'll crash with "leaking open BaseSocket"
#endif
self.descriptor = NIOBSDSocket.invalidHandle // We have to unset the fd here, otherwise we'll crash with "leaking open BaseSocket"
throw error
}
}
@ -475,11 +467,7 @@ class BaseSocket: BaseSocketProtocol {
/// - throws: An `IOError` if the operation failed.
final func takeDescriptorOwnership() throws -> NIOBSDSocket.Handle {
return try self.withUnsafeHandle {
#if os(Windows)
self.descriptor = INVALID_SOCKET
#else
self.descriptor = -1
#endif
self.descriptor = NIOBSDSocket.invalidHandle
return $0
}
}

View File

@ -86,6 +86,7 @@ extension BaseSocketProtocol {
fatalError("BUG in NIO. We did not ignore SIGPIPE, this code path should definitely not be reachable.")
}
#elseif os(Windows)
// Deliberately empty: SIGPIPE just ain't a thing on Windows
#else
assert(fd >= 0, "illegal file descriptor \(fd)")
do {