[Windows] Make SocketChannel.swift compile (#3348)

- vsock isn't supported on windows.
- creating `in_addr` needs conditional compiling
This commit is contained in:
Fabian Fett authored and GitHub committed 2025-08-19 10:37:12 +00:00
1 parent 3bf9c1bd2c
commit 54b0dc09ca
2 files changed
+21 -2

No files matched your search

+20 -1
View File
@@ -121,7 +121,11 @@ final class SocketChannel: BaseStreamSocketChannel<Socket>, @unchecked Sendable
case _ as ChannelOptions.Types.ConnectTimeoutOption:
return connectTimeout as! Option.Value
case _ as ChannelOptions.Types.LocalVsockContextID:
#if os(Windows)
fallthrough
#else
return try self.socket.getLocalVsockContextID() as! Option.Value
#endif
default:
return try super.getOption0(option)
}
@@ -276,7 +280,11 @@ final class ServerSocketChannel: BaseSocketChannel<ServerSocket>, @unchecked Sen
case _ as ChannelOptions.Types.BacklogOption:
return backlog as! Option.Value
case _ as ChannelOptions.Types.LocalVsockContextID:
#if os(Windows)
fallthrough
#else
return try self.socket.getLocalVsockContextID() as! Option.Value
#endif
default:
return try super.getOption0(option)
}
@@ -311,8 +319,13 @@ final class ServerSocketChannel: BaseSocketChannel<ServerSocket>, @unchecked Sen
switch target {
case .socketAddress(let address):
try socket.bind(to: address)
#if os(Windows)
case .vsockAddress:
fatalError(vsockUnimplemented)
#else
case .vsockAddress(let address):
try socket.bind(to: address)
#endif
}
self.updateCachedAddressesFromSocket(updateRemote: false)
try self.socket.listen(backlog: backlog)
@@ -1185,10 +1198,16 @@ extension DatagramChannel: MulticastChannel {
)
try self.socket.setOption(level: .ip, name: operation.optionName(level: .ip), value: multicastRequest)
case (.v4(let groupAddress), .none):
#if os(Windows)
var addr = in_addr()
addr.S_un.S_addr = INADDR_ANY
#else
let addr = in_addr(s_addr: INADDR_ANY)
#endif
// IPv4 binding without target interface.
let multicastRequest = ip_mreq(
imr_multiaddr: groupAddress.address.sin_addr,
imr_interface: in_addr(s_addr: INADDR_ANY)
imr_interface: addr
)
try self.socket.setOption(level: .ip, name: operation.optionName(level: .ip), value: multicastRequest)
case (.v6(let groupAddress), .some(.v6)):
+1 -1
View File
@@ -24,7 +24,7 @@ import CNIODarwin
#endif
import CNIOLinux
#endif
private let vsockUnimplemented = "VSOCK support is not implemented for this platform"
let vsockUnimplemented = "VSOCK support is not implemented for this platform"
// MARK: - Public API that's available on all platforms.