mirror of
https://github.com/apple/swift-nio
synced 2026-11-24 04:37:54 +00:00
### Motivation:
Packages that consume NIOPosix should be able to compile to WASI
platforms without special configurations. This change elides the
NIOPosix source from WASI platforms to simplify configuration.
Without this change:
```swift
dependencies: [
// Without this PR, downstream packages must maintain exhaustive platform list to exclude `.wasi`:
.product(
name: "NIOPosix",
package: "swift-nio",
condition: .when(platforms: [
.macOS,
.macCatalyst,
.iOS,
.tvOS,
.watchOS,
.visionOS,
.driverKit,
.linux,
.windows,
.android,
.openbsd,
// .wasi // <-- Need to exclude this, because there is no exclusion list api for SPM conditionals
])
),
]
```
With this change:
```swift
dependencies: [
// Without this PR, downstream packages can consume NIOPosix simply:
.product(name: "NIOPosix", package: "swift-nio"),
]
```
### Modifications:
- Add compiler directives (`#if !os(WASI)`) to all source files in
NIOPosix
### Result:
Downstream packages can compile to wasm without manually excluding the
NIOPosix dependency.
### Testing performed
- Verified `swift build --swift-sdk wasm32-unknown-wasip1 --target
NIOPosix` compiles, which demonstrates proper elision of source files in
NIOPosix that aren't wasm-ready.
- Confirmed GitHub [checks
pass](https://github.com/PassiveLogic/swift-nio/actions/runs/21151341738).
134 lines
4.2 KiB
Swift
134 lines
4.2 KiB
Swift
//===----------------------------------------------------------------------===//
|
|
//
|
|
// This source file is part of the SwiftNIO open source project
|
|
//
|
|
// Copyright (c) 2017-2021 Apple Inc. and the SwiftNIO project authors
|
|
// Licensed under Apache License v2.0
|
|
//
|
|
// See LICENSE.txt for license information
|
|
// See CONTRIBUTORS.txt for the list of SwiftNIO project authors
|
|
//
|
|
// SPDX-License-Identifier: Apache-2.0
|
|
//
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
#if !os(WASI)
|
|
|
|
import NIOCore
|
|
|
|
#if canImport(WinSDK)
|
|
import struct WinSDK.socklen_t
|
|
#endif
|
|
|
|
protocol BaseSocketProtocol: CustomStringConvertible, _NIOPosixSendableMetatype {
|
|
associatedtype SelectableType: Selectable
|
|
|
|
var isOpen: Bool { get }
|
|
|
|
func close() throws
|
|
|
|
func bind(to address: SocketAddress) throws
|
|
|
|
func localAddress() throws -> SocketAddress
|
|
|
|
func remoteAddress() throws -> SocketAddress
|
|
|
|
func setOption<T>(level: NIOBSDSocket.OptionLevel, name: NIOBSDSocket.Option, value: T) throws
|
|
|
|
func getOption<T>(level: NIOBSDSocket.OptionLevel, name: NIOBSDSocket.Option) throws -> T
|
|
}
|
|
|
|
protocol ServerSocketProtocol: BaseSocketProtocol {
|
|
func listen(backlog: Int32) throws
|
|
|
|
func accept(setNonBlocking: Bool) throws -> Socket?
|
|
}
|
|
|
|
protocol SocketProtocol: BaseSocketProtocol {
|
|
func connect(to address: SocketAddress) throws -> Bool
|
|
|
|
func finishConnect() throws
|
|
|
|
func write(pointer: UnsafeRawBufferPointer) throws -> IOResult<Int>
|
|
|
|
func writev(iovecs: UnsafeBufferPointer<IOVector>) throws -> IOResult<Int>
|
|
|
|
func read(pointer: UnsafeMutableRawBufferPointer) throws -> IOResult<Int>
|
|
|
|
func recvmsg(
|
|
pointer: UnsafeMutableRawBufferPointer,
|
|
storage: inout sockaddr_storage,
|
|
storageLen: inout socklen_t,
|
|
controlBytes: inout UnsafeReceivedControlBytes
|
|
) throws -> IOResult<Int>
|
|
|
|
func sendmsg(
|
|
pointer: UnsafeRawBufferPointer,
|
|
destinationPtr: UnsafePointer<sockaddr>?,
|
|
destinationSize: socklen_t,
|
|
controlBytes: UnsafeMutableRawBufferPointer
|
|
) throws -> IOResult<Int>
|
|
|
|
func sendFile(fd: CInt, offset: Int, count: Int) throws -> IOResult<Int>
|
|
|
|
func recvmmsg(msgs: UnsafeMutableBufferPointer<MMsgHdr>) throws -> IOResult<Int>
|
|
|
|
func sendmmsg(msgs: UnsafeMutableBufferPointer<MMsgHdr>) throws -> IOResult<Int>
|
|
|
|
func shutdown(how: Shutdown) throws
|
|
|
|
func ignoreSIGPIPE() throws
|
|
}
|
|
|
|
#if os(Linux) || os(Android) || os(OpenBSD)
|
|
// This is a lazily initialised global variable that when read for the first time, will ignore SIGPIPE.
|
|
private let globallyIgnoredSIGPIPE: Bool = {
|
|
// no F_SETNOSIGPIPE on Linux :(
|
|
#if canImport(Glibc)
|
|
_ = Glibc.signal(SIGPIPE, SIG_IGN)
|
|
#elseif canImport(Musl)
|
|
_ = Musl.signal(SIGPIPE, SIG_IGN)
|
|
#elseif canImport(Android)
|
|
_ = Android.signal(SIGPIPE, SIG_IGN)
|
|
#else
|
|
#error("Don't know which stdlib to use")
|
|
#endif
|
|
return true
|
|
}()
|
|
#endif
|
|
|
|
extension BaseSocketProtocol {
|
|
// used by `BaseSocket` and `PipePair`.
|
|
internal static func ignoreSIGPIPE(descriptor fd: CInt) throws {
|
|
#if os(Linux) || os(Android) || os(OpenBSD)
|
|
let haveWeIgnoredSIGPIEThisIsHereToTriggerIgnoringIt = globallyIgnoredSIGPIPE
|
|
guard haveWeIgnoredSIGPIEThisIsHereToTriggerIgnoringIt else {
|
|
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 {
|
|
try Posix.fcntl(descriptor: fd, command: F_SETNOSIGPIPE, value: 1)
|
|
} catch let error as IOError {
|
|
try? Posix.close(descriptor: fd) // don't care about failure here
|
|
if error.errnoCode == EINVAL {
|
|
// Darwin seems to sometimes do this despite the docs claiming it can't happen
|
|
throw NIOFcntlFailedError()
|
|
}
|
|
throw error
|
|
}
|
|
#endif
|
|
}
|
|
|
|
internal static func ignoreSIGPIPE(socket handle: NIOBSDSocket.Handle) throws {
|
|
#if os(Windows)
|
|
// Deliberately empty: SIGPIPE just ain't a thing on Windows
|
|
#else
|
|
try ignoreSIGPIPE(descriptor: handle)
|
|
#endif
|
|
}
|
|
}
|
|
#endif // !os(WASI)
|