Namespace ChannelOptions. (#61)

Motivation:

In NIO 2.9.0 we moved all first-party `ChannelOption` types into their
own namespace. That was a good idea, but NIOTS uses them too and so it
now encounters build warnings. Additionally, NIOTS defines its own
`ChannelOption`s, and so should namespace those as well.

Modifications:

- Updated the code to use the namespaced `ChannelOption` types.
- Namespaced our own.

Result:

More namespacing.
This commit is contained in:
Cory Benfield 2019-10-23 17:26:25 -07:00 committed by Johannes Weiss
parent 4add2df501
commit e30bf63ea1
6 changed files with 79 additions and 62 deletions

View File

@ -23,7 +23,7 @@ let package = Package(
.executable(name: "NIOTSHTTPServer", targets: ["NIOTSHTTPServer"]),
],
dependencies: [
.package(url: "https://github.com/apple/swift-nio.git", from: "2.0.0"),
.package(url: "https://github.com/apple/swift-nio.git", from: "2.9.0"),
],
targets: [
.target(name: "NIOTransportServices",

View File

@ -16,42 +16,59 @@
#if canImport(Network)
import NIO
/// `NIOTSWaitForActivityOption` controls whether the `Channel` should wait for connection changes
/// during the connection process if the connection attempt fails. If Network.framework believes that
/// a connection may succeed in future, it may transition into the `.waiting` state. By default, this option
/// is set to `true` and NIO allows this state transition, though it does count time in that state against
/// the timeout. If this option is set to `false`, transitioning into this state will be treated the same as
/// transitioning into the `failed` state, causing immediate connection failure.
///
/// This option is only valid with `NIOTSConnectionBootstrap`.
@available(OSX 10.14, iOS 12.0, tvOS 12.0, watchOS 6.0, *)
public struct NIOTSWaitForActivityOption: ChannelOption, Equatable {
public typealias Value = Bool
public init() {}
}
/// `NIOTSEnablePeerToPeerOption` controls whether the `Channel` will advertise services using peer-to-peer
/// connectivity. Setting this to true is the equivalent of setting `NWParameters.enablePeerToPeer` to
/// `true`. By default this option is set to `false`.
///
/// This option must be set on the bootstrap: setting it after the channel is initialized will have no effect.
@available(OSX 10.14, iOS 12.0, tvOS 12.0, watchOS 6.0, *)
public struct NIOTSEnablePeerToPeerOption: ChannelOption, Equatable {
public typealias Value = Bool
public init() {}
}
/// Options that can be set explicitly and only on bootstraps provided by `NIOTransportServices`.
@available(OSX 10.14, iOS 12.0, tvOS 12.0, watchOS 6.0, *)
public struct NIOTSChannelOptions {
/// - seealso: `NIOTSWaitForActivityOption`.
public static let waitForActivity = NIOTSWaitForActivityOption()
public static let waitForActivity = NIOTSChannelOptions.Types.NIOTSWaitForActivityOption()
public static let enablePeerToPeer = NIOTSEnablePeerToPeerOption()
public static let enablePeerToPeer = NIOTSChannelOptions.Types.NIOTSEnablePeerToPeerOption()
}
@available(OSX 10.14, iOS 12.0, tvOS 12.0, watchOS 6.0, *)
extension NIOTSChannelOptions {
public enum Types {
/// `NIOTSWaitForActivityOption` controls whether the `Channel` should wait for connection changes
/// during the connection process if the connection attempt fails. If Network.framework believes that
/// a connection may succeed in future, it may transition into the `.waiting` state. By default, this option
/// is set to `true` and NIO allows this state transition, though it does count time in that state against
/// the timeout. If this option is set to `false`, transitioning into this state will be treated the same as
/// transitioning into the `failed` state, causing immediate connection failure.
///
/// This option is only valid with `NIOTSConnectionBootstrap`.
@available(OSX 10.14, iOS 12.0, tvOS 12.0, watchOS 6.0, *)
public struct NIOTSWaitForActivityOption: ChannelOption, Equatable {
public typealias Value = Bool
public init() {}
}
/// `NIOTSEnablePeerToPeerOption` controls whether the `Channel` will advertise services using peer-to-peer
/// connectivity. Setting this to true is the equivalent of setting `NWParameters.enablePeerToPeer` to
/// `true`. By default this option is set to `false`.
///
/// This option must be set on the bootstrap: setting it after the channel is initialized will have no effect.
@available(OSX 10.14, iOS 12.0, tvOS 12.0, watchOS 6.0, *)
public struct NIOTSEnablePeerToPeerOption: ChannelOption, Equatable {
public typealias Value = Bool
public init() {}
}
}
}
/// See: `NIOTSChannelOptions.Types.NIOTSWaitForActivityOption`.
@available(*, deprecated, renamed: "NIOTSChannelOptions.Types.NIOTSWaitForActivityOption")
@available(OSX 10.14, iOS 12.0, tvOS 12.0, watchOS 6.0, *)
public typealias NIOTSWaitForActivityOption = NIOTSChannelOptions.Types.NIOTSWaitForActivityOption
/// See: `NIOTSChannelOptions.Types.NIOTSEnablePeerToPeerOption`
@available(*, deprecated, renamed: "NIOTSChannelOptions.Types.NIOTSEnablePeerToPeerOption")
@available(OSX 10.14, iOS 12.0, tvOS 12.0, watchOS 6.0, *)
public typealias NIOTSEnablePeerToPeerOption = NIOTSChannelOptions.Types.NIOTSEnablePeerToPeerOption
#endif

View File

@ -87,7 +87,7 @@ private struct BackpressureManager {
private var outstandingBytes: Int = 0
/// The watermarks currently configured by the user.
private(set) var waterMarks: WriteBufferWaterMark = WriteBufferWaterMark(low: 32 * 1024, high: 64 * 1024)
private(set) var waterMarks = ChannelOptions.Types.WriteBufferWaterMark(low: 32 * 1024, high: 64 * 1024)
/// Adds `newBytes` to the queue of outstanding bytes, and returns whether this
/// has caused a writability change.
@ -127,7 +127,7 @@ private struct BackpressureManager {
/// - parameters:
/// - waterMarks: The new waterMarks to use.
/// - returns: Whether the state changed.
mutating func writabilityChanges(whenUpdatingWaterMarks waterMarks: WriteBufferWaterMark) -> Bool {
mutating func writabilityChanges(whenUpdatingWaterMarks waterMarks: ChannelOptions.Types.WriteBufferWaterMark) -> Bool {
let writable = self.writable.load()
self.waterMarks = waterMarks
@ -300,13 +300,13 @@ extension NIOTSConnectionChannel: Channel {
}
switch option {
case _ as AutoReadOption:
case _ as ChannelOptions.Types.AutoReadOption:
self.options.autoRead = value as! Bool
self.readIfNeeded0()
case _ as AllowRemoteHalfClosureOption:
case _ as ChannelOptions.Types.AllowRemoteHalfClosureOption:
self.options.supportRemoteHalfClosure = value as! Bool
case _ as SocketOption:
let optionValue = option as! SocketOption
case _ as ChannelOptions.Types.SocketOption:
let optionValue = option as! ChannelOptions.Types.SocketOption
// SO_REUSEADDR and SO_REUSEPORT are handled here.
switch (optionValue.level, optionValue.name) {
@ -317,11 +317,11 @@ extension NIOTSConnectionChannel: Channel {
default:
try self.tcpOptions.applyChannelOption(option: optionValue, value: value as! SocketOptionValue)
}
case _ as WriteBufferWaterMarkOption:
if self.backpressureManager.writabilityChanges(whenUpdatingWaterMarks: value as! WriteBufferWaterMark) {
case _ as ChannelOptions.Types.WriteBufferWaterMarkOption:
if self.backpressureManager.writabilityChanges(whenUpdatingWaterMarks: value as! ChannelOptions.Types.WriteBufferWaterMark) {
self.pipeline.fireChannelWritabilityChanged()
}
case _ as NIOTSWaitForActivityOption:
case _ as NIOTSChannelOptions.Types.NIOTSWaitForActivityOption:
let newValue = value as! Bool
self.options.waitForActivity = newValue
@ -329,8 +329,8 @@ extension NIOTSConnectionChannel: Channel {
// We're in waiting now, so we should drop the connection.
self.close0(error: err, mode: .all, promise: nil)
}
case is NIOTSEnablePeerToPeerOption:
self.enablePeerToPeer = value as! NIOTSEnablePeerToPeerOption.Value
case is NIOTSChannelOptions.Types.NIOTSEnablePeerToPeerOption:
self.enablePeerToPeer = value as! NIOTSChannelOptions.Types.NIOTSEnablePeerToPeerOption.Value
default:
fatalError("option \(type(of: option)).\(option) not supported")
}
@ -354,12 +354,12 @@ extension NIOTSConnectionChannel: Channel {
}
switch option {
case _ as AutoReadOption:
case _ as ChannelOptions.Types.AutoReadOption:
return self.options.autoRead as! Option.Value
case _ as AllowRemoteHalfClosureOption:
case _ as ChannelOptions.Types.AllowRemoteHalfClosureOption:
return self.options.supportRemoteHalfClosure as! Option.Value
case _ as SocketOption:
let optionValue = option as! SocketOption
case _ as ChannelOptions.Types.SocketOption:
let optionValue = option as! ChannelOptions.Types.SocketOption
// SO_REUSEADDR and SO_REUSEPORT are handled here.
switch (optionValue.level, optionValue.name) {
@ -370,11 +370,11 @@ extension NIOTSConnectionChannel: Channel {
default:
return try self.tcpOptions.valueFor(socketOption: optionValue) as! Option.Value
}
case _ as WriteBufferWaterMarkOption:
case _ as ChannelOptions.Types.WriteBufferWaterMarkOption:
return self.backpressureManager.waterMarks as! Option.Value
case _ as NIOTSWaitForActivityOption:
case _ as NIOTSChannelOptions.Types.NIOTSWaitForActivityOption:
return self.options.waitForActivity as! Option.Value
case is NIOTSEnablePeerToPeerOption:
case is NIOTSChannelOptions.Types.NIOTSEnablePeerToPeerOption:
return self.enablePeerToPeer as! Option.Value
default:
fatalError("option \(type(of: option)).\(option) not supported")

View File

@ -36,7 +36,7 @@ public enum NIOTSErrors {
/// `UnsupportedSocketOption` is thrown when an attempt is made to configure a socket option that
/// is not supported by Network.framework.
public struct UnsupportedSocketOption: NIOTSError {
public let optionValue: SocketOption
public let optionValue: ChannelOptions.Types.SocketOption
public static func ==(lhs: UnsupportedSocketOption, rhs: UnsupportedSocketOption) -> Bool {
return lhs.optionValue == rhs.optionValue

View File

@ -178,12 +178,12 @@ extension NIOTSListenerChannel: Channel {
// TODO: Many more channel options, both from NIO and Network.framework.
switch option {
case is AutoReadOption:
case is ChannelOptions.Types.AutoReadOption:
// AutoRead is currently mandatory for TS listeners.
if value as! AutoReadOption.Value == false {
if value as! ChannelOptions.Types.AutoReadOption.Value == false {
throw ChannelError.operationUnsupported
}
case let optionValue as SocketOption:
case let optionValue as ChannelOptions.Types.SocketOption:
// SO_REUSEADDR and SO_REUSEPORT are handled here.
switch (optionValue.level, optionValue.name) {
case (SOL_SOCKET, SO_REUSEADDR):
@ -193,8 +193,8 @@ extension NIOTSListenerChannel: Channel {
default:
try self.tcpOptions.applyChannelOption(option: optionValue, value: value as! SocketOptionValue)
}
case is NIOTSEnablePeerToPeerOption:
self.enablePeerToPeer = value as! NIOTSEnablePeerToPeerOption.Value
case is NIOTSChannelOptions.Types.NIOTSEnablePeerToPeerOption:
self.enablePeerToPeer = value as! NIOTSChannelOptions.Types.NIOTSEnablePeerToPeerOption.Value
default:
fatalError("option \(option) not supported")
}
@ -218,9 +218,9 @@ extension NIOTSListenerChannel: Channel {
}
switch option {
case is AutoReadOption:
case is ChannelOptions.Types.AutoReadOption:
return autoRead as! Option.Value
case let optionValue as SocketOption:
case let optionValue as ChannelOptions.Types.SocketOption:
// SO_REUSEADDR and SO_REUSEPORT are handled here.
switch (optionValue.level, optionValue.name) {
case (SOL_SOCKET, SO_REUSEADDR):
@ -230,7 +230,7 @@ extension NIOTSListenerChannel: Channel {
default:
return try self.tcpOptions.valueFor(socketOption: optionValue) as! Option.Value
}
case is NIOTSEnablePeerToPeerOption:
case is NIOTSChannelOptions.Types.NIOTSEnablePeerToPeerOption:
return self.enablePeerToPeer as! Option.Value
default:
fatalError("option \(option) not supported")

View File

@ -22,7 +22,7 @@ import Network
@available(OSX 10.14, iOS 12.0, tvOS 12.0, watchOS 6.0, *)
internal extension NWProtocolTCP.Options {
/// Apply a given channel `SocketOption` to this protocol options state.
func applyChannelOption(option: SocketOption, value: SocketOptionValue) throws {
func applyChannelOption(option: ChannelOptions.Types.SocketOption, value: SocketOptionValue) throws {
switch (option.level, option.name) {
case (IPPROTO_TCP, TCP_NODELAY):
self.noDelay = value != 0
@ -54,7 +54,7 @@ internal extension NWProtocolTCP.Options {
}
/// Obtain the given `SocketOption` value for this protocol options state.
func valueFor(socketOption option: SocketOption) throws -> SocketOptionValue {
func valueFor(socketOption option: ChannelOptions.Types.SocketOption) throws -> SocketOptionValue {
switch (option.level, option.name) {
case (IPPROTO_TCP, TCP_NODELAY):
return self.noDelay ? 1 : 0