mirror of
https://github.com/apple/swift-nio
synced 2026-11-24 04:37:54 +00:00
## Motivation NIO channels abstract over an underlying transport mechanisms (sockets, pipes, etc.), which users typically need not interact with. However, there are scenarios where users need direct access to the underlying transport for low-level operations that work outside NIOs abstraction. One example is performing out out-of-band operations on the underlying file descriptor for a socket-based channel. This PR adds a structured way to access the underlying transport of a channel, for channels that choose to implement it. ## Modifications - Add a new `public protocol NIOTransportAccessibleChannelCore<Transport>` which provides a scoped `withUnsafeTransport(_:)`, used for channel implementations to opt-in. - Add conformance to `NIOTransportAccessibleChannel<NIOBSDSocket.Handle>` for `BaseSocketChannel`, to make this API available for all socket-based channels, including channels returned from the socket-based bootstrap public APIs. - Add public API `ChannelPipeline.SynchronousOperations.withUnsafeTransportIfAvailable(_:)` Note that not all channels need to or should their transport, which is why this was added as an additional protocol that refines `ChannelCore`, vs. extending `Channel` or `ChannelCore` with a default implementation. The protocol uses a primary associated type allowing channels to provide typed access to the transport. E.g. this could be used in NIO Transport Services to expose the underlying `NWConnection`, if desired. The method itself is spelled with "unsafe", uses scoped access, and has clear documentation that users must not violated any of NIOs assumptions about the state of the underlying transport. It's very much not intended for every day use. It being on `ChannelCore` should defer users of the low-level API, since `Channel._channelCore` is marked as for NIO internal use, and the public `withUnsafeTransportIfAvailable(_:)` will take care of the runtime checks for channels that have opted into this API. ## Result - New opt-in API for channel implementations to expose their underlying transport - All socket-based channels from NIOPosix now expose the underlying socket file descriptor - New API `ChannelPipeline.SynchronousOperations.withUnsafeTransportIfAvailable(_:)` for users --------- Co-authored-by: Agam Dua <agam_dua@apple.com>
46 lines
1.7 KiB
Swift
46 lines
1.7 KiB
Swift
//===----------------------------------------------------------------------===//
|
|
//
|
|
// This source file is part of the SwiftNIO open source project
|
|
//
|
|
// Copyright (c) 2026 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
|
|
|
|
extension BaseSocketChannel where SocketType: BaseSocket {
|
|
/// The underlying transport type backing this channel.
|
|
typealias Transport = NIOBSDSocket.Handle
|
|
|
|
/// Provides scoped access to the socket file handle.
|
|
func withUnsafeTransport<Result>(_ body: (_ transport: NIOBSDSocket.Handle) throws -> Result) throws -> Result {
|
|
try self.socket.withUnsafeHandle(body)
|
|
}
|
|
}
|
|
|
|
extension BaseSocketChannel where SocketType: PipePair {
|
|
/// The underlying transport type backing this channel.
|
|
typealias Transport = NIOBSDSocket.PipeHandle
|
|
|
|
/// Provides scoped access to the pipe file descriptors.
|
|
func withUnsafeTransport<Result>(_ body: (_ transport: NIOBSDSocket.PipeHandle) throws -> Result) throws -> Result {
|
|
try body(
|
|
NIOBSDSocket.PipeHandle(
|
|
input: self.socket.input?.fileDescriptor ?? NIOBSDSocket.invalidHandle,
|
|
output: self.socket.output?.fileDescriptor ?? NIOBSDSocket.invalidHandle
|
|
)
|
|
)
|
|
}
|
|
}
|
|
|
|
extension SocketChannel: NIOTransportAccessibleChannelCore {}
|
|
extension ServerSocketChannel: NIOTransportAccessibleChannelCore {}
|
|
extension DatagramChannel: NIOTransportAccessibleChannelCore {}
|
|
extension PipeChannel: NIOTransportAccessibleChannelCore {}
|