@@ -126,10 +126,8 @@ public struct Socket: Sendable, Hashable {
126126 switch domain {
127127 case AF_INET:
128128 try setValue ( true , for: . packetInfoIP)
129- #if !canImport(WinSDK)
130129 case AF_INET6:
131130 try setValue ( true , for: . packetInfoIPv6)
132- #endif
133131 default :
134132 return
135133 }
@@ -221,7 +219,7 @@ public struct Socket: Sendable, Hashable {
221219 Socket . connect ( file. rawValue, $0, address. size)
222220 }
223221 guard result >= 0 || errno == EISCONN else {
224- if errno == EINPROGRESS {
222+ if errno == EINPROGRESS || errno == EWOULDBLOCK {
225223 throw SocketError . blocked
226224 } else {
227225 throw SocketError . makeFailed ( " Connect " )
@@ -248,7 +246,7 @@ public struct Socket: Sendable, Hashable {
248246 guard count > 0 else {
249247 if errno == EWOULDBLOCK {
250248 throw SocketError . blocked
251- } else if errno == EBADF || count == 0 {
249+ } else if errnoSignalsDisconnected ( ) || count == 0 {
252250 throw SocketError . disconnected
253251 } else {
254252 throw SocketError . makeFailed ( " Read " )
@@ -275,7 +273,7 @@ public struct Socket: Sendable, Hashable {
275273 guard count > 0 else {
276274 if errno == EWOULDBLOCK {
277275 throw SocketError . blocked
278- } else if errno == EBADF || count == 0 {
276+ } else if errnoSignalsDisconnected ( ) || count == 0 {
279277 throw SocketError . disconnected
280278 } else {
281279 throw SocketError . makeFailed ( " RecvFrom " )
@@ -340,7 +338,7 @@ public struct Socket: Sendable, Hashable {
340338 guard count > 0 else {
341339 if errno == EWOULDBLOCK || errno == EAGAIN {
342340 throw SocketError . blocked
343- } else if errno == EBADF || count == 0 {
341+ } else if errnoSignalsDisconnected ( ) || count == 0 {
344342 throw SocketError . disconnected
345343 } else {
346344 throw SocketError . makeFailed ( " RecvMsg " )
@@ -365,7 +363,7 @@ public struct Socket: Sendable, Hashable {
365363 guard sent > 0 else {
366364 if errno == EWOULDBLOCK {
367365 throw SocketError . blocked
368- } else if errno == EBADF {
366+ } else if errnoSignalsDisconnected ( ) {
369367 throw SocketError . disconnected
370368 } else {
371369 throw SocketError . makeFailed ( " Write " )
@@ -568,10 +566,14 @@ public extension SocketOption where Self == BoolSocketOption {
568566 BoolSocketOption ( level: Socket . ipproto_ip, name: Socket . ip_pktinfo)
569567 }
570568
571- #if !canImport(WinSDK)
572569 static var packetInfoIPv6 : Self {
573570 BoolSocketOption ( level: Socket . ipproto_ipv6, name: Socket . ipv6_recvpktinfo)
574571 }
572+
573+ #if canImport(WinSDK)
574+ static var exclusiveLocalAddressReuse : Self {
575+ BoolSocketOption ( name: ~ SO_REUSEADDR) // SO_EXCLUSIVEADDRUSE macro
576+ }
575577 #endif
576578
577579 #if canImport(Darwin)
@@ -627,6 +629,14 @@ package extension Socket {
627629 }
628630}
629631
632+ fileprivate func errnoSignalsDisconnected( ) -> Bool {
633+ #if canImport(WinSDK)
634+ return errno == WSAENOTSOCK || errno == WSAENOTCONN || errno == WSAECONNRESET
635+ #else
636+ return errno == EBADF
637+ #endif
638+ }
639+
630640#if !canImport(WinSDK)
631641fileprivate extension Socket {
632642 // https://github.com/swiftlang/swift-evolution/blob/main/proposals/0138-unsaferawbufferpointer.md
0 commit comments