/src/swift-nio/Sources/NIOPosix/BaseSocketChannel+SocketOptionProvider.swift
Line | Count | Source |
1 | | //===----------------------------------------------------------------------===// |
2 | | // |
3 | | // This source file is part of the SwiftNIO open source project |
4 | | // |
5 | | // Copyright (c) 2017-2021 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 | | #if !os(WASI) |
16 | | |
17 | | import NIOCore |
18 | | |
19 | | extension BaseSocketChannel: SocketOptionProvider { |
20 | | #if !os(Windows) |
21 | | func unsafeSetSocketOption<Value: Sendable>( |
22 | | level: SocketOptionLevel, |
23 | | name: SocketOptionName, |
24 | | value: Value |
25 | 0 | ) -> EventLoopFuture<Void> { |
26 | 0 | unsafeSetSocketOption( |
27 | 0 | level: NIOBSDSocket.OptionLevel(rawValue: CInt(level)), |
28 | 0 | name: NIOBSDSocket.Option(rawValue: CInt(name)), |
29 | 0 | value: value |
30 | 0 | ) |
31 | 0 | } |
32 | | #endif |
33 | | |
34 | | func unsafeSetSocketOption<Value: Sendable>( |
35 | | level: NIOBSDSocket.OptionLevel, |
36 | | name: NIOBSDSocket.Option, |
37 | | value: Value |
38 | 0 | ) -> EventLoopFuture<Void> { |
39 | 0 | if eventLoop.inEventLoop { |
40 | 0 | let promise = eventLoop.makePromise(of: Void.self) |
41 | 0 | executeAndComplete(promise) { |
42 | 0 | try setSocketOption0(level: level, name: name, value: value) |
43 | 0 | } |
44 | 0 | return promise.futureResult |
45 | 0 | } else { |
46 | 0 | return eventLoop.submit { |
47 | 0 | try self.setSocketOption0(level: level, name: name, value: value) |
48 | 0 | } |
49 | 0 | } |
50 | 0 | } |
51 | | |
52 | | #if !os(Windows) |
53 | | func unsafeGetSocketOption<Value: Sendable>( |
54 | | level: SocketOptionLevel, |
55 | | name: SocketOptionName |
56 | 0 | ) -> EventLoopFuture<Value> { |
57 | 0 | unsafeGetSocketOption( |
58 | 0 | level: NIOBSDSocket.OptionLevel(rawValue: CInt(level)), |
59 | 0 | name: NIOBSDSocket.Option(rawValue: CInt(name)) |
60 | 0 | ) |
61 | 0 | } |
62 | | #endif |
63 | | |
64 | | func unsafeGetSocketOption<Value: Sendable>( |
65 | | level: NIOBSDSocket.OptionLevel, |
66 | | name: NIOBSDSocket.Option |
67 | 0 | ) -> EventLoopFuture<Value> { |
68 | 0 | if eventLoop.inEventLoop { |
69 | 0 | let promise = eventLoop.makePromise(of: Value.self) |
70 | 0 | executeAndComplete(promise) { |
71 | 0 | try getSocketOption0(level: level, name: name) |
72 | 0 | } |
73 | 0 | return promise.futureResult |
74 | 0 | } else { |
75 | 0 | return eventLoop.submit { |
76 | 0 | try self.getSocketOption0(level: level, name: name) |
77 | 0 | } |
78 | 0 | } |
79 | 0 | } |
80 | | |
81 | 0 | func setSocketOption0<Value>(level: NIOBSDSocket.OptionLevel, name: NIOBSDSocket.Option, value: Value) throws { |
82 | 0 | try self.socket.setOption(level: level, name: name, value: value) |
83 | 0 | } |
84 | | |
85 | 0 | func getSocketOption0<Value>(level: NIOBSDSocket.OptionLevel, name: NIOBSDSocket.Option) throws -> Value { |
86 | 0 | try self.socket.getOption(level: level, name: name) |
87 | 0 | } |
88 | | } |
89 | | #endif // !os(WASI) |