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