diff --git a/Sources/NIO/BaseSocketChannel+SocketOptionProvider.swift b/Sources/NIO/BaseSocketChannel+SocketOptionProvider.swift new file mode 100644 index 000000000..a1241feec --- /dev/null +++ b/Sources/NIO/BaseSocketChannel+SocketOptionProvider.swift @@ -0,0 +1,63 @@ +//===----------------------------------------------------------------------===// +// +// This source file is part of the SwiftNIO open source project +// +// Copyright (c) 2017-2021 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 +// +//===----------------------------------------------------------------------===// + +extension BaseSocketChannel: SocketOptionProvider { + #if !os(Windows) + func unsafeSetSocketOption(level: SocketOptionLevel, name: SocketOptionName, value: Value) -> EventLoopFuture { + return unsafeSetSocketOption(level: NIOBSDSocket.OptionLevel(rawValue: CInt(level)), name: NIOBSDSocket.Option(rawValue: CInt(name)), value: value) + } + #endif + + func unsafeSetSocketOption(level: NIOBSDSocket.OptionLevel, name: NIOBSDSocket.Option, value: Value) -> EventLoopFuture { + if eventLoop.inEventLoop { + let promise = eventLoop.makePromise(of: Void.self) + executeAndComplete(promise) { + try setSocketOption0(level: level, name: name, value: value) + } + return promise.futureResult + } else { + return eventLoop.submit { + try self.setSocketOption0(level: level, name: name, value: value) + } + } + } + + #if !os(Windows) + func unsafeGetSocketOption(level: SocketOptionLevel, name: SocketOptionName) -> EventLoopFuture { + return unsafeGetSocketOption(level: NIOBSDSocket.OptionLevel(rawValue: CInt(level)), name: NIOBSDSocket.Option(rawValue: CInt(name))) + } + #endif + + func unsafeGetSocketOption(level: NIOBSDSocket.OptionLevel, name: NIOBSDSocket.Option) -> EventLoopFuture { + if eventLoop.inEventLoop { + let promise = eventLoop.makePromise(of: Value.self) + executeAndComplete(promise) { + try getSocketOption0(level: level, name: name) + } + return promise.futureResult + } else { + return eventLoop.submit { + try self.getSocketOption0(level: level, name: name) + } + } + } + + func setSocketOption0(level: NIOBSDSocket.OptionLevel, name: NIOBSDSocket.Option, value: Value) throws { + try self.socket.setOption(level: level, name: name, value: value) + } + + func getSocketOption0(level: NIOBSDSocket.OptionLevel, name: NIOBSDSocket.Option) throws -> Value { + return try self.socket.getOption(level: level, name: name) + } +} diff --git a/Sources/NIO/SocketOptionProvider.swift b/Sources/NIOCore/SocketOptionProvider.swift similarity index 86% rename from Sources/NIO/SocketOptionProvider.swift rename to Sources/NIOCore/SocketOptionProvider.swift index f3d16a5df..cabcafd7d 100644 --- a/Sources/NIO/SocketOptionProvider.swift +++ b/Sources/NIOCore/SocketOptionProvider.swift @@ -2,7 +2,7 @@ // // This source file is part of the SwiftNIO open source project // -// Copyright (c) 2017-2018 Apple Inc. and the SwiftNIO project authors +// Copyright (c) 2017-2021 Apple Inc. and the SwiftNIO project authors // Licensed under Apache License v2.0 // // See LICENSE.txt for license information @@ -11,6 +11,11 @@ // SPDX-License-Identifier: Apache-2.0 // //===----------------------------------------------------------------------===// +#if os(macOS) || os(iOS) || os(tvOS) || os(watchOS) +import Darwin +#elseif os(Linux) || os(Android) +import Glibc +#endif /// This protocol defines an object, most commonly a `Channel`, that supports /// setting and getting socket options (via `setsockopt`/`getsockopt` or similar). @@ -276,54 +281,3 @@ extension SocketOptionProvider { } #endif } - - -extension BaseSocketChannel: SocketOptionProvider { - #if !os(Windows) - func unsafeSetSocketOption(level: SocketOptionLevel, name: SocketOptionName, value: Value) -> EventLoopFuture { - return unsafeSetSocketOption(level: NIOBSDSocket.OptionLevel(rawValue: CInt(level)), name: NIOBSDSocket.Option(rawValue: CInt(name)), value: value) - } - #endif - - func unsafeSetSocketOption(level: NIOBSDSocket.OptionLevel, name: NIOBSDSocket.Option, value: Value) -> EventLoopFuture { - if eventLoop.inEventLoop { - let promise = eventLoop.makePromise(of: Void.self) - executeAndComplete(promise) { - try setSocketOption0(level: level, name: name, value: value) - } - return promise.futureResult - } else { - return eventLoop.submit { - try self.setSocketOption0(level: level, name: name, value: value) - } - } - } - - #if !os(Windows) - func unsafeGetSocketOption(level: SocketOptionLevel, name: SocketOptionName) -> EventLoopFuture { - return unsafeGetSocketOption(level: NIOBSDSocket.OptionLevel(rawValue: CInt(level)), name: NIOBSDSocket.Option(rawValue: CInt(name))) - } - #endif - - func unsafeGetSocketOption(level: NIOBSDSocket.OptionLevel, name: NIOBSDSocket.Option) -> EventLoopFuture { - if eventLoop.inEventLoop { - let promise = eventLoop.makePromise(of: Value.self) - executeAndComplete(promise) { - try getSocketOption0(level: level, name: name) - } - return promise.futureResult - } else { - return eventLoop.submit { - try self.getSocketOption0(level: level, name: name) - } - } - } - - func setSocketOption0(level: NIOBSDSocket.OptionLevel, name: NIOBSDSocket.Option, value: Value) throws { - try self.socket.setOption(level: level, name: name, value: value) - } - - func getSocketOption0(level: NIOBSDSocket.OptionLevel, name: NIOBSDSocket.Option) throws -> Value { - return try self.socket.getOption(level: level, name: name) - } -}