/src/swift-nio/Sources/NIOPosix/BaseSocketChannel+AccessibleTransport.swift
Line | Count | Source |
1 | | //===----------------------------------------------------------------------===// |
2 | | // |
3 | | // This source file is part of the SwiftNIO open source project |
4 | | // |
5 | | // Copyright (c) 2026 Apple Inc. and the SwiftNIO project authors |
6 | | // Licensed under Apache License v2.0 |
7 | | // |
8 | | // See LICENSE.txt for license information |
9 | | // See CONTRIBUTORS.txt for the list of SwiftNIO project authors |
10 | | // |
11 | | // SPDX-License-Identifier: Apache-2.0 |
12 | | // |
13 | | //===----------------------------------------------------------------------===// |
14 | | |
15 | | import NIOCore |
16 | | |
17 | | extension BaseSocketChannel where SocketType: BaseSocket { |
18 | | /// The underlying transport type backing this channel. |
19 | | typealias Transport = NIOBSDSocket.Handle |
20 | | |
21 | | /// Provides scoped access to the socket file handle. |
22 | 0 | func withUnsafeTransport<Result>(_ body: (_ transport: NIOBSDSocket.Handle) throws -> Result) throws -> Result { |
23 | 0 | try self.socket.withUnsafeHandle(body) |
24 | 0 | } |
25 | | } |
26 | | |
27 | | extension BaseSocketChannel where SocketType: PipePair { |
28 | | /// The underlying transport type backing this channel. |
29 | | typealias Transport = NIOBSDSocket.PipeHandle |
30 | | |
31 | | /// Provides scoped access to the pipe file descriptors. |
32 | 0 | func withUnsafeTransport<Result>(_ body: (_ transport: NIOBSDSocket.PipeHandle) throws -> Result) throws -> Result { |
33 | 0 | try body( |
34 | 0 | NIOBSDSocket.PipeHandle( |
35 | 0 | input: self.socket.input?.fileDescriptor ?? NIOBSDSocket.invalidHandle, |
36 | 0 | output: self.socket.output?.fileDescriptor ?? NIOBSDSocket.invalidHandle |
37 | 0 | ) |
38 | 0 | ) |
39 | 0 | } |
40 | | } |
41 | | |
42 | | extension SocketChannel: NIOTransportAccessibleChannelCore {} |
43 | | extension ServerSocketChannel: NIOTransportAccessibleChannelCore {} |
44 | | extension DatagramChannel: NIOTransportAccessibleChannelCore {} |
45 | | extension PipeChannel: NIOTransportAccessibleChannelCore {} |