mirror of
https://github.com/apple/swift-nio
synced 2026-11-25 04:47:49 +00:00
simplify ChannelOptions (#828)
Motivation: ChannelOptions used a too complicated mechanism for what they are. Modifications: - change the ChannelOptions to a much simpler mechanism - rename the generic parameters from T to Option Result: code easier to read and understand
This commit is contained in:
1 parent
3d3cc6d685
commit
bf3b87cb4e
16 files changed
+137
-158
No files matched your search
@@ -466,17 +466,17 @@ class BaseSocketChannel<T: BaseSocket>: SelectableChannel, ChannelCore {
|
||||
}
|
||||
|
||||
|
||||
public final func setOption<T: ChannelOption>(option: T, value: T.OptionType) -> EventLoopFuture<Void> {
|
||||
public final func setOption<Option: ChannelOption>(_ option: Option, value: Option.Value) -> EventLoopFuture<Void> {
|
||||
if eventLoop.inEventLoop {
|
||||
let promise = eventLoop.makePromise(of: Void.self)
|
||||
executeAndComplete(promise) { try setOption0(option: option, value: value) }
|
||||
executeAndComplete(promise) { try self.setOption0(option, value: value) }
|
||||
return promise.futureResult
|
||||
} else {
|
||||
return eventLoop.submit { try self.setOption0(option: option, value: value) }
|
||||
return eventLoop.submit { try self.setOption0(option, value: value) }
|
||||
}
|
||||
}
|
||||
|
||||
func setOption0<T: ChannelOption>(option: T, value: T.OptionType) throws {
|
||||
func setOption0<Option: ChannelOption>(_ option: Option, value: Option.Value) throws {
|
||||
self.eventLoop.assertInEventLoop()
|
||||
|
||||
guard isOpen else {
|
||||
@@ -484,9 +484,8 @@ class BaseSocketChannel<T: BaseSocket>: SelectableChannel, ChannelCore {
|
||||
}
|
||||
|
||||
switch option {
|
||||
case _ as SocketOption:
|
||||
let (level, name) = option.value as! (SocketOptionLevel, SocketOptionName)
|
||||
try self.setSocketOption0(level: level, name: name, value: value)
|
||||
case let option as SocketOption:
|
||||
try self.setSocketOption0(level: option.level, name: option.name, value: value)
|
||||
case _ as AllocatorOption:
|
||||
bufferAllocator = value as! ByteBufferAllocator
|
||||
case _ as RecvAllocatorOption:
|
||||
@@ -512,19 +511,19 @@ class BaseSocketChannel<T: BaseSocket>: SelectableChannel, ChannelCore {
|
||||
}
|
||||
}
|
||||
|
||||
public func getOption<T>(option: T) -> EventLoopFuture<T.OptionType> where T: ChannelOption {
|
||||
public func getOption<Option: ChannelOption>(_ option: Option) -> EventLoopFuture<Option.Value> {
|
||||
if eventLoop.inEventLoop {
|
||||
do {
|
||||
return eventLoop.makeSucceededFuture(try getOption0(option: option))
|
||||
return self.eventLoop.makeSucceededFuture(try self.getOption0(option))
|
||||
} catch {
|
||||
return eventLoop.makeFailedFuture(error)
|
||||
return self.eventLoop.makeFailedFuture(error)
|
||||
}
|
||||
} else {
|
||||
return eventLoop.submit { try self.getOption0(option: option) }
|
||||
return self.eventLoop.submit { try self.getOption0(option) }
|
||||
}
|
||||
}
|
||||
|
||||
func getOption0<T: ChannelOption>(option: T) throws -> T.OptionType {
|
||||
func getOption0<Option: ChannelOption>(_ option: Option) throws -> Option.Value {
|
||||
self.eventLoop.assertInEventLoop()
|
||||
|
||||
guard isOpen else {
|
||||
@@ -532,17 +531,16 @@ class BaseSocketChannel<T: BaseSocket>: SelectableChannel, ChannelCore {
|
||||
}
|
||||
|
||||
switch option {
|
||||
case _ as SocketOption:
|
||||
let (level, name) = option.value as! (SocketOptionLevel, SocketOptionName)
|
||||
return try self.getSocketOption0(level: level, name: name)
|
||||
case let option as SocketOption:
|
||||
return try self.getSocketOption0(level: option.level, name: option.name)
|
||||
case _ as AllocatorOption:
|
||||
return bufferAllocator as! T.OptionType
|
||||
return bufferAllocator as! Option.Value
|
||||
case _ as RecvAllocatorOption:
|
||||
return recvAllocator as! T.OptionType
|
||||
return recvAllocator as! Option.Value
|
||||
case _ as AutoReadOption:
|
||||
return autoRead as! T.OptionType
|
||||
return autoRead as! Option.Value
|
||||
case _ as MaxMessagesPerReadOption:
|
||||
return maxMessagesPerRead as! T.OptionType
|
||||
return maxMessagesPerRead as! Option.Value
|
||||
default:
|
||||
fatalError("option \(option) not supported")
|
||||
}
|
||||
@@ -972,7 +970,7 @@ class BaseSocketChannel<T: BaseSocket>: SelectableChannel, ChannelCore {
|
||||
|
||||
// getOption0 can only fail if the channel is not active anymore but we assert further up that it is. If
|
||||
// that's not the case this is a precondition failure and we would like to know.
|
||||
if self.lifecycleManager.isActive, try! getOption0(option: ChannelOptions.allowRemoteHalfClosure) {
|
||||
if self.lifecycleManager.isActive, try! getOption0(ChannelOptions.allowRemoteHalfClosure) {
|
||||
// If we want to allow half closure we will just mark the input side of the Channel
|
||||
// as closed.
|
||||
assert(self.lifecycleManager.isActive)
|
||||
|
||||
+10
-10
@@ -122,7 +122,7 @@ public final class ServerBootstrap {
|
||||
/// - option: The option to be applied.
|
||||
/// - value: The value for the option.
|
||||
@inlinable
|
||||
public func serverChannelOption<Option: ChannelOption>(_ option: Option, value: Option.OptionType) -> Self {
|
||||
public func serverChannelOption<Option: ChannelOption>(_ option: Option, value: Option.Value) -> Self {
|
||||
self._serverChannelOptions.put(key: option, value: value)
|
||||
return self
|
||||
}
|
||||
@@ -133,7 +133,7 @@ public final class ServerBootstrap {
|
||||
/// - option: The option to be applied.
|
||||
/// - value: The value for the option.
|
||||
@inlinable
|
||||
public func childChannelOption<Option: ChannelOption>(_ option: Option, value: Option.OptionType) -> Self {
|
||||
public func childChannelOption<Option: ChannelOption>(_ option: Option, value: Option.Value) -> Self {
|
||||
self._childChannelOptions.put(key: option, value: value)
|
||||
return self
|
||||
}
|
||||
@@ -391,7 +391,7 @@ public final class ClientBootstrap {
|
||||
/// - option: The option to be applied.
|
||||
/// - value: The value for the option.
|
||||
@inlinable
|
||||
public func channelOption<Option: ChannelOption>(_ option: Option, value: Option.OptionType) -> Self {
|
||||
public func channelOption<Option: ChannelOption>(_ option: Option, value: Option.Value) -> Self {
|
||||
self._channelOptions.put(key: option, value: value)
|
||||
return self
|
||||
}
|
||||
@@ -589,7 +589,7 @@ public final class DatagramBootstrap {
|
||||
/// - option: The option to be applied.
|
||||
/// - value: The value for the option.
|
||||
@inlinable
|
||||
public func channelOption<Option: ChannelOption>(_ option: Option, value: Option.OptionType) -> Self {
|
||||
public func channelOption<Option: ChannelOption>(_ option: Option, value: Option.Value) -> Self {
|
||||
self._channelOptions.put(key: option, value: value)
|
||||
return self
|
||||
}
|
||||
@@ -686,17 +686,17 @@ public final class DatagramBootstrap {
|
||||
internal var _storage: [(Any, (Any, (Channel) -> (Any, Any) -> EventLoopFuture<Void>))] = []
|
||||
|
||||
@inlinable
|
||||
mutating func put<K: ChannelOption>(key: K,
|
||||
value newValue: K.OptionType) {
|
||||
func applier(_ t: Channel) -> (Any, Any) -> EventLoopFuture<Void> {
|
||||
return { (x, y) in
|
||||
return t.setOption(option: x as! K, value: y as! K.OptionType)
|
||||
mutating func put<Option: ChannelOption>(key: Option,
|
||||
value newValue: Option.Value) {
|
||||
func applier(_ channel: Channel) -> (Any, Any) -> EventLoopFuture<Void> {
|
||||
return { (option, value) in
|
||||
return channel.setOption(option as! Option, value: value as! Option.Value)
|
||||
}
|
||||
}
|
||||
var hasSet = false
|
||||
self._storage = self._storage.map { typeAndValue in
|
||||
let (type, value) = typeAndValue
|
||||
if type is K && type as! K == key {
|
||||
if type is Option && type as! Option == key {
|
||||
hasSet = true
|
||||
return (key, (newValue, applier))
|
||||
} else {
|
||||
|
||||
@@ -123,10 +123,10 @@ public protocol Channel: class, ChannelOutboundInvoker {
|
||||
var parent: Channel? { get }
|
||||
|
||||
/// Set `option` to `value` on this `Channel`.
|
||||
func setOption<T: ChannelOption>(option: T, value: T.OptionType) -> EventLoopFuture<Void>
|
||||
func setOption<Option: ChannelOption>(_ option: Option, value: Option.Value) -> EventLoopFuture<Void>
|
||||
|
||||
/// Get the value of `option` for this `Channel`.
|
||||
func getOption<T: ChannelOption>(option: T) -> EventLoopFuture<T.OptionType>
|
||||
func getOption<Option: ChannelOption>(_ option: Option) -> EventLoopFuture<Option.Value>
|
||||
|
||||
/// Returns if this `Channel` is currently writable.
|
||||
var isWritable: Bool { get }
|
||||
|
||||
@@ -14,26 +14,8 @@
|
||||
|
||||
/// A configuration option that can be set on a `Channel` to configure different behaviour.
|
||||
public protocol ChannelOption: Equatable {
|
||||
associatedtype AssociatedValueType
|
||||
associatedtype OptionType
|
||||
|
||||
/// The value of the `ChannelOption`.
|
||||
var value: AssociatedValueType { get }
|
||||
|
||||
/// The type of the `ChannelOption`
|
||||
var type: OptionType.Type { get }
|
||||
}
|
||||
|
||||
extension ChannelOption {
|
||||
public var type: OptionType.Type {
|
||||
return OptionType.self
|
||||
}
|
||||
}
|
||||
|
||||
extension ChannelOption where AssociatedValueType == () {
|
||||
public var value: () {
|
||||
return ()
|
||||
}
|
||||
/// The type of the `ChannelOption`'s value.
|
||||
associatedtype Value
|
||||
}
|
||||
|
||||
public typealias SocketOptionName = Int32
|
||||
@@ -48,12 +30,11 @@ public typealias SocketOptionName = Int32
|
||||
/// `SocketOption` allows users to specify configuration settings that are directly applied to the underlying socket file descriptor.
|
||||
///
|
||||
/// Valid options are typically found in the various man pages like `man 4 tcp`.
|
||||
public enum SocketOption: ChannelOption {
|
||||
public typealias AssociatedValueType = (SocketOptionLevel, SocketOptionName)
|
||||
public struct SocketOption: ChannelOption, Equatable {
|
||||
public typealias Value = (SocketOptionValue)
|
||||
|
||||
public typealias OptionType = (SocketOptionValue)
|
||||
|
||||
case const(AssociatedValueType)
|
||||
public var level: SocketOptionLevel
|
||||
public var name: SocketOptionName
|
||||
|
||||
/// Create a new `SocketOption`.
|
||||
///
|
||||
@@ -61,74 +42,55 @@ public enum SocketOption: ChannelOption {
|
||||
/// - level: The level for the option as defined in `man setsockopt`, e.g. SO_SOCKET.
|
||||
/// - name: The name of the option as defined in `man setsockopt`, e.g. `SO_REUSEADDR`.
|
||||
public init(level: SocketOptionLevel, name: SocketOptionName) {
|
||||
self = .const((level, name))
|
||||
}
|
||||
|
||||
public var value: (SocketOptionLevel, SocketOptionName) {
|
||||
switch self {
|
||||
case .const(let level, let name):
|
||||
return (level, name)
|
||||
}
|
||||
}
|
||||
|
||||
public static func == (lhs: SocketOption, rhs: SocketOption) -> Bool {
|
||||
switch (lhs, rhs) {
|
||||
case (.const(let lLevel, let lName), .const(let rLevel, let rName)):
|
||||
return lLevel == rLevel && lName == rName
|
||||
}
|
||||
self.level = level
|
||||
self.name = name
|
||||
}
|
||||
}
|
||||
|
||||
/// `AllocatorOption` allows to specify the `ByteBufferAllocator` to use.
|
||||
public enum AllocatorOption: ChannelOption {
|
||||
public typealias AssociatedValueType = ()
|
||||
public typealias OptionType = ByteBufferAllocator
|
||||
public struct AllocatorOption: ChannelOption {
|
||||
public typealias Value = ByteBufferAllocator
|
||||
|
||||
case const
|
||||
public init() {}
|
||||
}
|
||||
|
||||
/// `RecvAllocatorOption` allows users to specify the `RecvByteBufferAllocator` to use.
|
||||
public enum RecvAllocatorOption: ChannelOption {
|
||||
public typealias AssociatedValueType = ()
|
||||
public typealias OptionType = RecvByteBufferAllocator
|
||||
public struct RecvAllocatorOption: ChannelOption {
|
||||
public typealias Value = RecvByteBufferAllocator
|
||||
|
||||
case const
|
||||
public init() {}
|
||||
}
|
||||
|
||||
/// `AutoReadOption` allows users to configure if a `Channel` should automatically call `Channel.read` again once all data was read from the transport or
|
||||
/// if the user is responsible to call `Channel.read` manually.
|
||||
public enum AutoReadOption: ChannelOption {
|
||||
public typealias AssociatedValueType = ()
|
||||
public typealias OptionType = Bool
|
||||
public struct AutoReadOption: ChannelOption {
|
||||
public typealias Value = Bool
|
||||
|
||||
case const
|
||||
public init() {}
|
||||
}
|
||||
|
||||
/// `WriteSpinOption` allows users to configure the number of repetitions of a only partially successful write call before considering the `Channel` not writable.
|
||||
/// Setting this option to `0` means that we only issue one write call and if that call does not write all the bytes,
|
||||
/// we consider the `Channel` not writable.
|
||||
public enum WriteSpinOption: ChannelOption {
|
||||
public typealias AssociatedValueType = ()
|
||||
public typealias OptionType = UInt
|
||||
public struct WriteSpinOption: ChannelOption {
|
||||
public typealias Value = UInt
|
||||
|
||||
case const
|
||||
public init() {}
|
||||
}
|
||||
|
||||
/// `MaxMessagesPerReadOption` allows users to configure the maximum number of read calls to the underlying transport are performed before wait again until
|
||||
/// there is more to read and be notified.
|
||||
public enum MaxMessagesPerReadOption: ChannelOption {
|
||||
public typealias AssociatedValueType = ()
|
||||
public typealias OptionType = UInt
|
||||
public struct MaxMessagesPerReadOption: ChannelOption {
|
||||
public typealias Value = UInt
|
||||
|
||||
case const
|
||||
public init() {}
|
||||
}
|
||||
|
||||
/// `BacklogOption` allows users to configure the `backlog` value as specified in `man 2 listen`. This is only useful for `ServerSocketChannel`s.
|
||||
public enum BacklogOption: ChannelOption {
|
||||
public typealias AssociatedValueType = ()
|
||||
public typealias OptionType = Int32
|
||||
public struct BacklogOption: ChannelOption {
|
||||
public typealias Value = Int32
|
||||
|
||||
case const
|
||||
public init() {}
|
||||
}
|
||||
|
||||
/// The watermark used to detect when `Channel.isWritable` returns `true` or `false`.
|
||||
@@ -165,20 +127,18 @@ public struct WriteBufferWaterMark {
|
||||
/// `Channel.isWritable` will return `false`. Once we were able to write some data out of the outbound buffer and the amount of bytes queued
|
||||
/// falls below `WriteBufferWaterMark.low` the `Channel` will become writable again. Once this happens `Channel.writable` will return
|
||||
/// `true` again. These writability changes are also propagated through the `ChannelPipeline` and so can be intercepted via `ChannelInboundHandler.channelWritabilityChanged`.
|
||||
public enum WriteBufferWaterMarkOption: ChannelOption {
|
||||
public typealias AssociatedValueType = ()
|
||||
public typealias OptionType = WriteBufferWaterMark
|
||||
public struct WriteBufferWaterMarkOption: ChannelOption {
|
||||
public typealias Value = WriteBufferWaterMark
|
||||
|
||||
case const
|
||||
public init() {}
|
||||
}
|
||||
|
||||
/// `ConnectTimeoutOption` allows users to configure the `TimeAmount` after which a connect will fail if it was not established in the meantime. May be
|
||||
/// `nil`, in which case the connection attempt will never time out.
|
||||
public enum ConnectTimeoutOption: ChannelOption {
|
||||
public typealias AssociatedValueType = ()
|
||||
public typealias OptionType = TimeAmount?
|
||||
public struct ConnectTimeoutOption: ChannelOption {
|
||||
public typealias Value = TimeAmount?
|
||||
|
||||
case const
|
||||
public init() {}
|
||||
}
|
||||
|
||||
/// `AllowRemoteHalfClosureOption` allows users to configure whether the `Channel` will close itself when its remote
|
||||
@@ -186,42 +146,43 @@ public enum ConnectTimeoutOption: ChannelOption {
|
||||
/// will be closed automatically if the remote peer shuts down its send stream. If set to true, the `Channel` will
|
||||
/// not be closed: instead, a `ChannelEvent.inboundClosed` user event will be sent on the `ChannelPipeline`,
|
||||
/// and no more data will be received.
|
||||
public enum AllowRemoteHalfClosureOption: ChannelOption {
|
||||
public typealias AssociatedValueType = ()
|
||||
public typealias OptionType = Bool
|
||||
public struct AllowRemoteHalfClosureOption: ChannelOption {
|
||||
public typealias Value = Bool
|
||||
|
||||
case const
|
||||
public init() {}
|
||||
}
|
||||
|
||||
/// Provides `ChannelOption`s to be used with a `Channel`, `Bootstrap` or `ServerBootstrap`.
|
||||
public struct ChannelOptions {
|
||||
/// - seealso: `SocketOption`.
|
||||
public static let socket = { (level: SocketOptionLevel, name: SocketOptionName) -> SocketOption in .const((level, name)) }
|
||||
public static let socket = { (level: SocketOptionLevel, name: SocketOptionName) -> SocketOption in
|
||||
.init(level: level, name: name)
|
||||
}
|
||||
|
||||
/// - seealso: `AllocatorOption`.
|
||||
public static let allocator = AllocatorOption.const
|
||||
public static let allocator = AllocatorOption()
|
||||
|
||||
/// - seealso: `RecvAllocatorOption`.
|
||||
public static let recvAllocator = RecvAllocatorOption.const
|
||||
public static let recvAllocator = RecvAllocatorOption()
|
||||
|
||||
/// - seealso: `AutoReadOption`.
|
||||
public static let autoRead = AutoReadOption.const
|
||||
public static let autoRead = AutoReadOption()
|
||||
|
||||
/// - seealso: `MaxMessagesPerReadOption`.
|
||||
public static let maxMessagesPerRead = MaxMessagesPerReadOption.const
|
||||
public static let maxMessagesPerRead = MaxMessagesPerReadOption()
|
||||
|
||||
/// - seealso: `BacklogOption`.
|
||||
public static let backlog = BacklogOption.const
|
||||
public static let backlog = BacklogOption()
|
||||
|
||||
/// - seealso: `WriteSpinOption`.
|
||||
public static let writeSpin = WriteSpinOption.const
|
||||
public static let writeSpin = WriteSpinOption()
|
||||
|
||||
/// - seealso: `WriteBufferWaterMarkOption`.
|
||||
public static let writeBufferWaterMark = WriteBufferWaterMarkOption.const
|
||||
public static let writeBufferWaterMark = WriteBufferWaterMarkOption()
|
||||
|
||||
/// - seealso: `ConnectTimeoutOption`.
|
||||
public static let connectTimeout = ConnectTimeoutOption.const
|
||||
public static let connectTimeout = ConnectTimeoutOption()
|
||||
|
||||
/// - seealso: `AllowRemoteHalfClosureOption`.
|
||||
public static let allowRemoteHalfClosure = AllowRemoteHalfClosureOption.const
|
||||
public static let allowRemoteHalfClosure = AllowRemoteHalfClosureOption()
|
||||
}
|
||||
@@ -103,11 +103,11 @@ internal final class DeadChannel: Channel {
|
||||
|
||||
let parent: Channel? = nil
|
||||
|
||||
func setOption<T>(option: T, value: T.OptionType) -> EventLoopFuture<Void> where T: ChannelOption {
|
||||
func setOption<Option: ChannelOption>(_ option: Option, value: Option.Value) -> EventLoopFuture<Void> {
|
||||
return EventLoopFuture(eventLoop: self.pipeline.eventLoop, error: ChannelError.ioOnClosedChannel, file: #file, line: #line)
|
||||
}
|
||||
|
||||
func getOption<T>(option: T) -> EventLoopFuture<T.OptionType> where T: ChannelOption {
|
||||
func getOption<Option: ChannelOption>(_ option: Option) -> EventLoopFuture<Option.Value> {
|
||||
return eventLoop.makeFailedFuture(ChannelError.ioOnClosedChannel)
|
||||
}
|
||||
|
||||
|
||||
@@ -385,14 +385,14 @@ public class EmbeddedChannel: Channel {
|
||||
try! register().wait()
|
||||
}
|
||||
|
||||
public func setOption<T>(option: T, value: T.OptionType) -> EventLoopFuture<Void> where T: ChannelOption {
|
||||
public func setOption<Option: ChannelOption>(_ option: Option, value: Option.Value) -> EventLoopFuture<Void> {
|
||||
// No options supported
|
||||
fatalError("no options supported")
|
||||
}
|
||||
|
||||
public func getOption<T>(option: T) -> EventLoopFuture<T.OptionType> where T: ChannelOption {
|
||||
public func getOption<Option: ChannelOption>(_ option: Option) -> EventLoopFuture<Option.Value> {
|
||||
if option is AutoReadOption {
|
||||
return self.eventLoop.makeSucceededFuture(true as! T.OptionType)
|
||||
return self.eventLoop.makeSucceededFuture(true as! Option.Value)
|
||||
}
|
||||
fatalError("option \(option) not supported")
|
||||
}
|
||||
|
||||
@@ -70,7 +70,7 @@ final class SocketChannel: BaseSocketChannel<Socket> {
|
||||
assert(pendingWrites.isEmpty)
|
||||
}
|
||||
|
||||
override func setOption0<T: ChannelOption>(option: T, value: T.OptionType) throws {
|
||||
override func setOption0<Option: ChannelOption>(_ option: Option, value: Option.Value) throws {
|
||||
self.eventLoop.assertInEventLoop()
|
||||
|
||||
guard isOpen else {
|
||||
@@ -87,11 +87,11 @@ final class SocketChannel: BaseSocketChannel<Socket> {
|
||||
case _ as WriteBufferWaterMarkOption:
|
||||
pendingWrites.waterMark = value as! WriteBufferWaterMark
|
||||
default:
|
||||
try super.setOption0(option: option, value: value)
|
||||
try super.setOption0(option, value: value)
|
||||
}
|
||||
}
|
||||
|
||||
override func getOption0<T: ChannelOption>(option: T) throws -> T.OptionType {
|
||||
override func getOption0<Option: ChannelOption>(_ option: Option) throws -> Option.Value {
|
||||
self.eventLoop.assertInEventLoop()
|
||||
|
||||
guard isOpen else {
|
||||
@@ -100,15 +100,15 @@ final class SocketChannel: BaseSocketChannel<Socket> {
|
||||
|
||||
switch option {
|
||||
case _ as ConnectTimeoutOption:
|
||||
return connectTimeout as! T.OptionType
|
||||
return connectTimeout as! Option.Value
|
||||
case _ as AllowRemoteHalfClosureOption:
|
||||
return allowRemoteHalfClosure as! T.OptionType
|
||||
return allowRemoteHalfClosure as! Option.Value
|
||||
case _ as WriteSpinOption:
|
||||
return pendingWrites.writeSpinCount as! T.OptionType
|
||||
return pendingWrites.writeSpinCount as! Option.Value
|
||||
case _ as WriteBufferWaterMarkOption:
|
||||
return pendingWrites.waterMark as! T.OptionType
|
||||
return pendingWrites.waterMark as! Option.Value
|
||||
default:
|
||||
return try super.getOption0(option: option)
|
||||
return try super.getOption0(option)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -338,7 +338,7 @@ final class ServerSocketChannel: BaseSocketChannel<ServerSocket> {
|
||||
return .serverSocketChannel(self, interested)
|
||||
}
|
||||
|
||||
override func setOption0<T: ChannelOption>(option: T, value: T.OptionType) throws {
|
||||
override func setOption0<Option: ChannelOption>(_ option: Option, value: Option.Value) throws {
|
||||
self.eventLoop.assertInEventLoop()
|
||||
|
||||
guard isOpen else {
|
||||
@@ -349,11 +349,11 @@ final class ServerSocketChannel: BaseSocketChannel<ServerSocket> {
|
||||
case _ as BacklogOption:
|
||||
backlog = value as! Int32
|
||||
default:
|
||||
try super.setOption0(option: option, value: value)
|
||||
try super.setOption0(option, value: value)
|
||||
}
|
||||
}
|
||||
|
||||
override func getOption0<T: ChannelOption>(option: T) throws -> T.OptionType {
|
||||
override func getOption0<Option: ChannelOption>(_ option: Option) throws -> Option.Value {
|
||||
self.eventLoop.assertInEventLoop()
|
||||
|
||||
guard isOpen else {
|
||||
@@ -362,9 +362,9 @@ final class ServerSocketChannel: BaseSocketChannel<ServerSocket> {
|
||||
|
||||
switch option {
|
||||
case _ as BacklogOption:
|
||||
return backlog as! T.OptionType
|
||||
return backlog as! Option.Value
|
||||
default:
|
||||
return try super.getOption0(option: option)
|
||||
return try super.getOption0(option)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -544,7 +544,7 @@ final class DatagramChannel: BaseSocketChannel<Socket> {
|
||||
|
||||
// MARK: Datagram Channel overrides required by BaseSocketChannel
|
||||
|
||||
override func setOption0<T: ChannelOption>(option: T, value: T.OptionType) throws {
|
||||
override func setOption0<Option: ChannelOption>(_ option: Option, value: Option.Value) throws {
|
||||
self.eventLoop.assertInEventLoop()
|
||||
|
||||
guard isOpen else {
|
||||
@@ -557,11 +557,11 @@ final class DatagramChannel: BaseSocketChannel<Socket> {
|
||||
case _ as WriteBufferWaterMarkOption:
|
||||
pendingWrites.waterMark = value as! WriteBufferWaterMark
|
||||
default:
|
||||
try super.setOption0(option: option, value: value)
|
||||
try super.setOption0(option, value: value)
|
||||
}
|
||||
}
|
||||
|
||||
override func getOption0<T: ChannelOption>(option: T) throws -> T.OptionType {
|
||||
override func getOption0<Option: ChannelOption>(_ option: Option) throws -> Option.Value {
|
||||
self.eventLoop.assertInEventLoop()
|
||||
|
||||
guard isOpen else {
|
||||
@@ -570,11 +570,11 @@ final class DatagramChannel: BaseSocketChannel<Socket> {
|
||||
|
||||
switch option {
|
||||
case _ as WriteSpinOption:
|
||||
return pendingWrites.writeSpinCount as! T.OptionType
|
||||
return pendingWrites.writeSpinCount as! Option.Value
|
||||
case _ as WriteBufferWaterMarkOption:
|
||||
return pendingWrites.waterMark as! T.OptionType
|
||||
return pendingWrites.waterMark as! Option.Value
|
||||
default:
|
||||
return try super.getOption0(option: option)
|
||||
return try super.getOption0(option)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -380,6 +380,21 @@ extension Channel {
|
||||
var _unsafe: ChannelCore {
|
||||
return self._channelCore
|
||||
}
|
||||
|
||||
@available(*, deprecated, renamed: "setOption(_:value:)")
|
||||
func setOption<Option: ChannelOption>(option: Option, value: Option.Value) -> EventLoopFuture<Void> {
|
||||
return self.setOption(option, value: value)
|
||||
}
|
||||
|
||||
@available(*, deprecated, renamed: "getOption(_:)")
|
||||
func getOption<Option: ChannelOption>(option: Option) -> EventLoopFuture<Option.Value> {
|
||||
return self.getOption(option)
|
||||
}
|
||||
}
|
||||
|
||||
extension ChannelOption {
|
||||
@available(*, deprecated, renamed: "Value")
|
||||
public typealias OptionType = Value
|
||||
}
|
||||
|
||||
@available(*, deprecated, renamed: "HTTPServerProtocolUpgrader")
|
||||
|
||||
@@ -252,7 +252,7 @@ public class AcceptBackoffHandlerTest: XCTestCase {
|
||||
eventLoop: eventLoop,
|
||||
group: group))
|
||||
|
||||
XCTAssertNoThrow(try serverChannel.setOption(option: ChannelOptions.autoRead, value: false).wait())
|
||||
XCTAssertNoThrow(try serverChannel.setOption(ChannelOptions.autoRead, value: false).wait())
|
||||
XCTAssertNoThrow(try serverChannel.pipeline.add(handler: readCountHandler).flatMap { _ in
|
||||
serverChannel.pipeline.add(name: self.acceptHandlerName, handler: AcceptBackoffHandler(backoffProvider: backoffProvider))
|
||||
}.wait())
|
||||
|
||||
@@ -78,12 +78,12 @@ class OptionsCollectingChannel: Channel {
|
||||
|
||||
var parent: Channel? { fatalError() }
|
||||
|
||||
func setOption<T>(option: T, value: T.OptionType) -> EventLoopFuture<Void> where T : ChannelOption {
|
||||
func setOption<Option: ChannelOption>(_ option: Option, value: Option.Value) -> EventLoopFuture<Void> {
|
||||
self.allOptions.append((option, value))
|
||||
return self.eventLoop.makeSucceededFuture(())
|
||||
}
|
||||
|
||||
func getOption<T>(option: T) -> EventLoopFuture<T.OptionType> where T : ChannelOption {
|
||||
func getOption<Option: ChannelOption>(_ option: Option) -> EventLoopFuture<Option.Value> {
|
||||
fatalError()
|
||||
}
|
||||
|
||||
|
||||
@@ -2531,7 +2531,7 @@ public class ChannelTests: XCTestCase {
|
||||
XCTAssertTrue(serverChannel.isActive)
|
||||
// we allow auto-read again to make sure that the socket buffer is drained on write error
|
||||
// (cf. https://github.com/apple/swift-nio/issues/593)
|
||||
ctx.channel.setOption(option: ChannelOptions.autoRead, value: true).flatMap {
|
||||
ctx.channel.setOption(ChannelOptions.autoRead, value: true).flatMap {
|
||||
// let's trigger the write error
|
||||
var buffer = ctx.channel.allocator.buffer(capacity: 16)
|
||||
buffer.writeStaticString("THIS WILL FAIL ANYWAY")
|
||||
@@ -2584,8 +2584,8 @@ public class ChannelTests: XCTestCase {
|
||||
let c = try assertNoThrowWithValue(SocketChannel(socket: WriteAlwaysFailingSocket(),
|
||||
parent: nil,
|
||||
eventLoop: singleThreadedELG.next() as! SelectableEventLoop))
|
||||
XCTAssertNoThrow(try c.setOption(option: ChannelOptions.autoRead, value: false).wait())
|
||||
XCTAssertNoThrow(try c.setOption(option: ChannelOptions.allowRemoteHalfClosure, value: true).wait())
|
||||
XCTAssertNoThrow(try c.setOption(ChannelOptions.autoRead, value: false).wait())
|
||||
XCTAssertNoThrow(try c.setOption(ChannelOptions.allowRemoteHalfClosure, value: true).wait())
|
||||
XCTAssertNoThrow(try c.pipeline.add(handler: MakeChannelInactiveInReadCausedByWriteErrorHandler(serverChannel: serverChannelAvailablePromise.futureResult,
|
||||
allDonePromise: allDonePromise)).wait())
|
||||
XCTAssertNoThrow(try c.register().wait())
|
||||
|
||||
@@ -154,7 +154,7 @@ final class DatagramChannelTests: XCTestCase {
|
||||
}
|
||||
|
||||
func testDatagramChannelHasWatermark() throws {
|
||||
_ = try self.firstChannel.setOption(option: ChannelOptions.writeBufferWaterMark, value: WriteBufferWaterMark(low: 1, high: 1024)).wait()
|
||||
_ = try self.firstChannel.setOption(ChannelOptions.writeBufferWaterMark, value: WriteBufferWaterMark(low: 1, high: 1024)).wait()
|
||||
|
||||
var buffer = self.firstChannel.allocator.buffer(capacity: 256)
|
||||
buffer.writeBytes([UInt8](repeating: 5, count: 256))
|
||||
|
||||
@@ -212,7 +212,7 @@ class SelectorTest: XCTestCase {
|
||||
|
||||
func channelActive(ctx: ChannelHandlerContext) {
|
||||
// collect all the channels
|
||||
ctx.channel.getOption(option: ChannelOptions.allowRemoteHalfClosure).whenSuccess { halfClosureAllowed in
|
||||
ctx.channel.getOption(ChannelOptions.allowRemoteHalfClosure).whenSuccess { halfClosureAllowed in
|
||||
precondition(halfClosureAllowed,
|
||||
"the test configuration is bogus: half-closure is dis-allowed which breaks the setup of this test")
|
||||
}
|
||||
|
||||
@@ -54,12 +54,12 @@ public class SocketChannelTest : XCTestCase {
|
||||
let futureA = channelA.eventLoop.submit {
|
||||
_ = condition.add(1)
|
||||
while condition.load() < 2 { }
|
||||
_ = channelB.setOption(option: ChannelOptions.backlog, value: 1)
|
||||
_ = channelB.setOption(ChannelOptions.backlog, value: 1)
|
||||
}
|
||||
let futureB = channelB.eventLoop.submit {
|
||||
_ = condition.add(1)
|
||||
while condition.load() < 2 { }
|
||||
_ = channelA.setOption(option: ChannelOptions.backlog, value: 1)
|
||||
_ = channelA.setOption(ChannelOptions.backlog, value: 1)
|
||||
}
|
||||
try futureA.wait()
|
||||
try futureB.wait()
|
||||
|
||||
@@ -162,20 +162,20 @@ final class NonAcceptingServerSocket: ServerSocket {
|
||||
}
|
||||
}
|
||||
|
||||
func assertSetGetOptionOnOpenAndClosed<T: ChannelOption>(channel: Channel, option: T, value: T.OptionType) throws {
|
||||
_ = try channel.setOption(option: option, value: value).wait()
|
||||
_ = try channel.getOption(option: option).wait()
|
||||
func assertSetGetOptionOnOpenAndClosed<Option: ChannelOption>(channel: Channel, option: Option, value: Option.Value) throws {
|
||||
_ = try channel.setOption(option, value: value).wait()
|
||||
_ = try channel.getOption(option).wait()
|
||||
try channel.close().wait()
|
||||
try channel.closeFuture.wait()
|
||||
|
||||
do {
|
||||
_ = try channel.setOption(option: option, value: value).wait()
|
||||
_ = try channel.setOption(option, value: value).wait()
|
||||
} catch let err as ChannelError where err == .ioOnClosedChannel {
|
||||
// expected
|
||||
}
|
||||
|
||||
do {
|
||||
_ = try channel.getOption(option: option).wait()
|
||||
_ = try channel.getOption(option).wait()
|
||||
} catch let err as ChannelError where err == .ioOnClosedChannel {
|
||||
// expected
|
||||
}
|
||||
@@ -236,7 +236,7 @@ func assert(_ condition: @autoclosure () -> Bool, within time: TimeAmount, testI
|
||||
|
||||
func getBoolSocketOption<IntType: SignedInteger>(channel: Channel, level: IntType, name: SocketOptionName,
|
||||
file: StaticString = #file, line: UInt = #line) throws -> Bool {
|
||||
return try assertNoThrowWithValue(channel.getOption(option: ChannelOptions.socket(SocketOptionLevel(level),
|
||||
return try assertNoThrowWithValue(channel.getOption(ChannelOptions.socket(SocketOptionLevel(level),
|
||||
name)),
|
||||
file: file,
|
||||
line: line).wait() != 0
|
||||
|
||||
@@ -23,6 +23,11 @@
|
||||
- Made `WebSocketOpcode` a struct. Removed `WebSocketOpcode.unknownControl` and
|
||||
`WebSocketOpcode.unknownNonControl` values: these should be replaced by
|
||||
simply instantiating `WebSocketOpcode` with the value.
|
||||
- `Channel.setOption(option:value:)` has been renamed `Channel.setOption(_:value:)`
|
||||
- `Channel.getOption(option:value:)` has been renamed `Channel.getOption(_:value:)`
|
||||
- `ChannelOption.AssociatedValueType` has been removed
|
||||
- `ChannelOption.OptionType` has been renamed `ChannelOption.Value`
|
||||
- the default `ChannelOption`s have been switched changed from `case FooOption { case const }` to a `struct FooOption { public init() {} }`
|
||||
- `markedElementIndex()`, `markedElement()` and `hasMark()` are now computed variables instead of functions.
|
||||
- `ByteBuffer.set(string:at:)` no longer returns an `Int?`, instead it
|
||||
returns `Int` and has had its return value made discardable.
|
||||
|
||||
Reference in new issue
Block a user