mirror of
https://github.com/apple/swift-nio
synced 2026-11-30 05:28:44 +00:00
* socket: Make destinationPtr param optional in sendmsg(...)
Signed-off-by: Si Beaumont <beaumont@apple.com>
* pdwm: Fixup documentation: scalar writes use sendmsg, not sendto
Signed-off-by: Si Beaumont <beaumont@apple.com>
* pdwm: Make sockaddr pointer param optional in scalarWriteOperation
Signed-off-by: Si Beaumont <beaumont@apple.com>
* pdwm: Add isConnected property to PendingDatagramWritesState
Signed-off-by: Si Beaumont <beaumont@apple.com>
* pdwm: If socket is connected use NULL msg_name in sendmsg(2)
Signed-off-by: Si Beaumont <beaumont@apple.com>
* BaseSocketChannel: Support connect after bind
Signed-off-by: Si Beaumont <beaumont@apple.com>
* DatagramChannel: Implement connectSocket(to:)
Signed-off-by: Si Beaumont <beaumont@apple.com>
* bootstrap: Rename bind0(makeChannel:registerAndBind:) to withNewChannel(makeChannel:bringup:)
Signed-off-by: Si Beaumont <beaumont@apple.com>
* bootstrap: Add set of DatagramBootstrap.connect(...) APIs
Signed-off-by: Si Beaumont <beaumont@apple.com>
* test: Remove DatagramChannelTests.testConnectionFails
Signed-off-by: Si Beaumont <beaumont@apple.com>
* test: Add ConnectedDatagramChannelTests, inheriting from DatagramChannelTests
Signed-off-by: Si Beaumont <beaumont@apple.com>
* NIOUDPEchoClient: Use connected-mode UDP
Signed-off-by: Si Beaumont <beaumont@apple.com>
* soundness: Update copyright notice
Signed-off-by: Si Beaumont <beaumont@apple.com>
* fixup: cleanup bootstrap APIs
Signed-off-by: Si Beaumont <beaumont@apple.com>
* pdwm: Check address of pending write if connected and add test
Signed-off-by: Si Beaumont <beaumont@apple.com>
* Revert "pdwm: Check address of pending write if connected and add test"
This reverts commit a4ee0756d5.
* channel: Fail buffered writes on connect and validate writes when connected
Signed-off-by: Si Beaumont <beaumont@apple.com>
* Run soundness.sh to get linux tests generated
Signed-off-by: Si Beaumont <beaumont@apple.com>
* NIOUDPEchoClient: Connect socket to remote only if --connect is used
Signed-off-by: Si Beaumont <beaumont@apple.com>
* socket: Support ByteBuffer (without AddressedEnvelope) for DatagramChannel
Signed-off-by: Si Beaumont <beaumont@apple.com>
* test: Simplify some test code
Signed-off-by: Si Beaumont <beaumont@apple.com>
* pdwm: Factor out common, private add(_ pendingWrite:)
Signed-off-by: Si Beaumont <beaumont@apple.com>
* channel: Support AddressedEnvelope on connected socket for control messages
Signed-off-by: Si Beaumont <beaumont@apple.com>
* channel: Defer to common unwrapData for error handling
Signed-off-by: Si Beaumont <beaumont@apple.com>
* channel: Throw more specific (new) errors, instead of IOError
Signed-off-by: Si Beaumont <beaumont@apple.com>
* SocketChannelLifecycleManager: Add supportsReconnect boolean property, used in DatagramChannel
Signed-off-by: Si Beaumont <beaumont@apple.com>
114 lines
3.9 KiB
Swift
114 lines
3.9 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
|
|
//
|
|
//===----------------------------------------------------------------------===//
|
|
import NIOCore
|
|
|
|
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 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)
|
|
// 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) || os(Android)
|
|
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 {
|
|
if error.errnoCode == EINVAL {
|
|
// Darwin seems to sometimes do this despite the docs claiming it can't happen
|
|
throw NIOFcntlFailedError()
|
|
}
|
|
try? Posix.close(descriptor: fd) // don't care about failure here
|
|
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
|
|
}
|
|
}
|