mirror of
https://github.com/apple/swift-nio
synced 2026-11-25 04:47:49 +00:00
* NIO: Add new `BSDSocket` namespace This starts the split of the POSIX/Linux/Darwin/BSD interfaces and the BSD Socket interfaces in order to support Windows. The constants on Windows are not part of the C standard library and need to be explicitly prefixed. Use the import by name to avoid the `#if` conditions on the wrapped versions. This will allow us to cleanly cleave the dependency on the underlying C library. This change adds a `BSDSocket.OptionLevel` and `BSDSocket.Option` types which allow us to get proper enumerations for these values and pipe them throughout the NIO codebase. * Apply suggestions from code review Co-Authored-By: Cory Benfield <lukasa@apple.com>
93 lines
3.1 KiB
Swift
93 lines
3.1 KiB
Swift
//===----------------------------------------------------------------------===//
|
|
//
|
|
// This source file is part of the SwiftNIO open source project
|
|
//
|
|
// Copyright (c) 2017-2018 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
|
|
//
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
protocol BaseSocketProtocol: CustomStringConvertible {
|
|
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 sendto(pointer: UnsafeRawBufferPointer, destinationPtr: UnsafePointer<sockaddr>, destinationSize: socklen_t) throws -> IOResult<Int>
|
|
|
|
func read(pointer: UnsafeMutableRawBufferPointer) throws -> IOResult<Int>
|
|
|
|
func recvfrom(pointer: UnsafeMutableRawBufferPointer, storage: inout sockaddr_storage, storageLen: inout socklen_t) throws -> IOResult<Int>
|
|
|
|
func sendFile(fd: Int32, 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)
|
|
// 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 :( */
|
|
_ = Glibc.signal(SIGPIPE, SIG_IGN)
|
|
return true
|
|
}()
|
|
#endif
|
|
|
|
extension BaseSocketProtocol {
|
|
// used by `BaseSocket` and `PipePair`.
|
|
internal static func ignoreSIGPIPE(descriptor fd: CInt) throws {
|
|
#if os(Linux)
|
|
let haveWeIgnoredSIGPIEThisIsHereToTriggerIgnoringIt = globallyIgnoredSIGPIPE
|
|
guard haveWeIgnoredSIGPIEThisIsHereToTriggerIgnoringIt else {
|
|
fatalError("BUG in NIO. We did not ignore SIGPIPE, this code path should definitely not be reachable.")
|
|
}
|
|
#else
|
|
assert(fd >= 0, "illegal file descriptor \(fd)")
|
|
do {
|
|
try Posix.fcntl(descriptor: fd, command: F_SETNOSIGPIPE, value: 1)
|
|
} catch {
|
|
_ = try? Posix.close(descriptor: fd) // don't care about failure here
|
|
throw error
|
|
}
|
|
#endif
|
|
}
|
|
}
|