Skip to content

Commit a7e064d

Browse files
authored
Split and move SocketOptionProvider (#1930)
Motivation: SocketOptionProvider is a general interface for setting socket options that are "wider" than a single platform Int. There is nothing inherent in NIO to require the definition of this protocol to be there, so we can move it to NIOCore. Modifications: - Move the definition of SocketOptionProvider to NIOCore. Result: SocketOptionProvider is generally available.
1 parent cdf27b6 commit a7e064d

File tree

2 files changed

+69
-52
lines changed

2 files changed

+69
-52
lines changed
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
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+
extension BaseSocketChannel: SocketOptionProvider {
16+
#if !os(Windows)
17+
func unsafeSetSocketOption<Value>(level: SocketOptionLevel, name: SocketOptionName, value: Value) -> EventLoopFuture<Void> {
18+
return unsafeSetSocketOption(level: NIOBSDSocket.OptionLevel(rawValue: CInt(level)), name: NIOBSDSocket.Option(rawValue: CInt(name)), value: value)
19+
}
20+
#endif
21+
22+
func unsafeSetSocketOption<Value>(level: NIOBSDSocket.OptionLevel, name: NIOBSDSocket.Option, value: Value) -> EventLoopFuture<Void> {
23+
if eventLoop.inEventLoop {
24+
let promise = eventLoop.makePromise(of: Void.self)
25+
executeAndComplete(promise) {
26+
try setSocketOption0(level: level, name: name, value: value)
27+
}
28+
return promise.futureResult
29+
} else {
30+
return eventLoop.submit {
31+
try self.setSocketOption0(level: level, name: name, value: value)
32+
}
33+
}
34+
}
35+
36+
#if !os(Windows)
37+
func unsafeGetSocketOption<Value>(level: SocketOptionLevel, name: SocketOptionName) -> EventLoopFuture<Value> {
38+
return unsafeGetSocketOption(level: NIOBSDSocket.OptionLevel(rawValue: CInt(level)), name: NIOBSDSocket.Option(rawValue: CInt(name)))
39+
}
40+
#endif
41+
42+
func unsafeGetSocketOption<Value>(level: NIOBSDSocket.OptionLevel, name: NIOBSDSocket.Option) -> EventLoopFuture<Value> {
43+
if eventLoop.inEventLoop {
44+
let promise = eventLoop.makePromise(of: Value.self)
45+
executeAndComplete(promise) {
46+
try getSocketOption0(level: level, name: name)
47+
}
48+
return promise.futureResult
49+
} else {
50+
return eventLoop.submit {
51+
try self.getSocketOption0(level: level, name: name)
52+
}
53+
}
54+
}
55+
56+
func setSocketOption0<Value>(level: NIOBSDSocket.OptionLevel, name: NIOBSDSocket.Option, value: Value) throws {
57+
try self.socket.setOption(level: level, name: name, value: value)
58+
}
59+
60+
func getSocketOption0<Value>(level: NIOBSDSocket.OptionLevel, name: NIOBSDSocket.Option) throws -> Value {
61+
return try self.socket.getOption(level: level, name: name)
62+
}
63+
}

Sources/NIO/SocketOptionProvider.swift renamed to Sources/NIOCore/SocketOptionProvider.swift

Lines changed: 6 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
//
33
// This source file is part of the SwiftNIO open source project
44
//
5-
// Copyright (c) 2017-2018 Apple Inc. and the SwiftNIO project authors
5+
// Copyright (c) 2017-2021 Apple Inc. and the SwiftNIO project authors
66
// Licensed under Apache License v2.0
77
//
88
// See LICENSE.txt for license information
@@ -11,6 +11,11 @@
1111
// SPDX-License-Identifier: Apache-2.0
1212
//
1313
//===----------------------------------------------------------------------===//
14+
#if os(macOS) || os(iOS) || os(tvOS) || os(watchOS)
15+
import Darwin
16+
#elseif os(Linux) || os(Android)
17+
import Glibc
18+
#endif
1419

1520
/// This protocol defines an object, most commonly a `Channel`, that supports
1621
/// setting and getting socket options (via `setsockopt`/`getsockopt` or similar).
@@ -276,54 +281,3 @@ extension SocketOptionProvider {
276281
}
277282
#endif
278283
}
279-
280-
281-
extension BaseSocketChannel: SocketOptionProvider {
282-
#if !os(Windows)
283-
func unsafeSetSocketOption<Value>(level: SocketOptionLevel, name: SocketOptionName, value: Value) -> EventLoopFuture<Void> {
284-
return unsafeSetSocketOption(level: NIOBSDSocket.OptionLevel(rawValue: CInt(level)), name: NIOBSDSocket.Option(rawValue: CInt(name)), value: value)
285-
}
286-
#endif
287-
288-
func unsafeSetSocketOption<Value>(level: NIOBSDSocket.OptionLevel, name: NIOBSDSocket.Option, value: Value) -> EventLoopFuture<Void> {
289-
if eventLoop.inEventLoop {
290-
let promise = eventLoop.makePromise(of: Void.self)
291-
executeAndComplete(promise) {
292-
try setSocketOption0(level: level, name: name, value: value)
293-
}
294-
return promise.futureResult
295-
} else {
296-
return eventLoop.submit {
297-
try self.setSocketOption0(level: level, name: name, value: value)
298-
}
299-
}
300-
}
301-
302-
#if !os(Windows)
303-
func unsafeGetSocketOption<Value>(level: SocketOptionLevel, name: SocketOptionName) -> EventLoopFuture<Value> {
304-
return unsafeGetSocketOption(level: NIOBSDSocket.OptionLevel(rawValue: CInt(level)), name: NIOBSDSocket.Option(rawValue: CInt(name)))
305-
}
306-
#endif
307-
308-
func unsafeGetSocketOption<Value>(level: NIOBSDSocket.OptionLevel, name: NIOBSDSocket.Option) -> EventLoopFuture<Value> {
309-
if eventLoop.inEventLoop {
310-
let promise = eventLoop.makePromise(of: Value.self)
311-
executeAndComplete(promise) {
312-
try getSocketOption0(level: level, name: name)
313-
}
314-
return promise.futureResult
315-
} else {
316-
return eventLoop.submit {
317-
try self.getSocketOption0(level: level, name: name)
318-
}
319-
}
320-
}
321-
322-
func setSocketOption0<Value>(level: NIOBSDSocket.OptionLevel, name: NIOBSDSocket.Option, value: Value) throws {
323-
try self.socket.setOption(level: level, name: name, value: value)
324-
}
325-
326-
func getSocketOption0<Value>(level: NIOBSDSocket.OptionLevel, name: NIOBSDSocket.Option) throws -> Value {
327-
return try self.socket.getOption(level: level, name: name)
328-
}
329-
}

0 commit comments

Comments
 (0)