diff --git a/Sources/NIO/BaseSocketChannel.swift b/Sources/NIO/BaseSocketChannel.swift index 06bdbc175..e838652ab 100644 --- a/Sources/NIO/BaseSocketChannel.swift +++ b/Sources/NIO/BaseSocketChannel.swift @@ -466,17 +466,17 @@ class BaseSocketChannel: SelectableChannel, ChannelCore { } - public final func setOption(option: T, value: T.OptionType) -> EventLoopFuture { + public final func setOption(_ option: Option, value: Option.Value) -> EventLoopFuture { 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(option: T, value: T.OptionType) throws { + func setOption0(_ option: Option, value: Option.Value) throws { self.eventLoop.assertInEventLoop() guard isOpen else { @@ -484,9 +484,8 @@ class BaseSocketChannel: 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: SelectableChannel, ChannelCore { } } - public func getOption(option: T) -> EventLoopFuture where T: ChannelOption { + public func getOption(_ option: Option) -> EventLoopFuture { 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(option: T) throws -> T.OptionType { + func getOption0(_ option: Option) throws -> Option.Value { self.eventLoop.assertInEventLoop() guard isOpen else { @@ -532,17 +531,16 @@ class BaseSocketChannel: 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: 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) diff --git a/Sources/NIO/Bootstrap.swift b/Sources/NIO/Bootstrap.swift index ad1cbc72b..964dc93e7 100644 --- a/Sources/NIO/Bootstrap.swift +++ b/Sources/NIO/Bootstrap.swift @@ -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: Option, value: Option.OptionType) -> Self { + public func serverChannelOption(_ 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: Option, value: Option.OptionType) -> Self { + public func childChannelOption(_ 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: Option, value: Option.OptionType) -> Self { + public func 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: Option, value: Option.OptionType) -> Self { + public func 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))] = [] @inlinable - mutating func put(key: K, - value newValue: K.OptionType) { - func applier(_ t: Channel) -> (Any, Any) -> EventLoopFuture { - return { (x, y) in - return t.setOption(option: x as! K, value: y as! K.OptionType) + mutating func put(key: Option, + value newValue: Option.Value) { + func applier(_ channel: Channel) -> (Any, Any) -> EventLoopFuture { + 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 { diff --git a/Sources/NIO/Channel.swift b/Sources/NIO/Channel.swift index 031127398..43ee27a4f 100644 --- a/Sources/NIO/Channel.swift +++ b/Sources/NIO/Channel.swift @@ -123,10 +123,10 @@ public protocol Channel: class, ChannelOutboundInvoker { var parent: Channel? { get } /// Set `option` to `value` on this `Channel`. - func setOption(option: T, value: T.OptionType) -> EventLoopFuture + func setOption(_ option: Option, value: Option.Value) -> EventLoopFuture /// Get the value of `option` for this `Channel`. - func getOption(option: T) -> EventLoopFuture + func getOption(_ option: Option) -> EventLoopFuture /// Returns if this `Channel` is currently writable. var isWritable: Bool { get } diff --git a/Sources/NIO/ChannelOption.swift b/Sources/NIO/ChannelOption.swift index ee73a5e12..8da153d89 100644 --- a/Sources/NIO/ChannelOption.swift +++ b/Sources/NIO/ChannelOption.swift @@ -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() } diff --git a/Sources/NIO/DeadChannel.swift b/Sources/NIO/DeadChannel.swift index 4f03ed970..468b86a67 100644 --- a/Sources/NIO/DeadChannel.swift +++ b/Sources/NIO/DeadChannel.swift @@ -103,11 +103,11 @@ internal final class DeadChannel: Channel { let parent: Channel? = nil - func setOption(option: T, value: T.OptionType) -> EventLoopFuture where T: ChannelOption { + func setOption(_ option: Option, value: Option.Value) -> EventLoopFuture { return EventLoopFuture(eventLoop: self.pipeline.eventLoop, error: ChannelError.ioOnClosedChannel, file: #file, line: #line) } - func getOption(option: T) -> EventLoopFuture where T: ChannelOption { + func getOption(_ option: Option) -> EventLoopFuture { return eventLoop.makeFailedFuture(ChannelError.ioOnClosedChannel) } diff --git a/Sources/NIO/Embedded.swift b/Sources/NIO/Embedded.swift index 0ecf7eee0..23e2bd86b 100644 --- a/Sources/NIO/Embedded.swift +++ b/Sources/NIO/Embedded.swift @@ -385,14 +385,14 @@ public class EmbeddedChannel: Channel { try! register().wait() } - public func setOption(option: T, value: T.OptionType) -> EventLoopFuture where T: ChannelOption { + public func setOption(_ option: Option, value: Option.Value) -> EventLoopFuture { // No options supported fatalError("no options supported") } - public func getOption(option: T) -> EventLoopFuture where T: ChannelOption { + public func getOption(_ option: Option) -> EventLoopFuture { 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") } diff --git a/Sources/NIO/SocketChannel.swift b/Sources/NIO/SocketChannel.swift index ec8a41a3e..3fde816c9 100644 --- a/Sources/NIO/SocketChannel.swift +++ b/Sources/NIO/SocketChannel.swift @@ -70,7 +70,7 @@ final class SocketChannel: BaseSocketChannel { assert(pendingWrites.isEmpty) } - override func setOption0(option: T, value: T.OptionType) throws { + override func setOption0(_ option: Option, value: Option.Value) throws { self.eventLoop.assertInEventLoop() guard isOpen else { @@ -87,11 +87,11 @@ final class SocketChannel: BaseSocketChannel { 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(option: T) throws -> T.OptionType { + override func getOption0(_ option: Option) throws -> Option.Value { self.eventLoop.assertInEventLoop() guard isOpen else { @@ -100,15 +100,15 @@ final class SocketChannel: BaseSocketChannel { 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 { return .serverSocketChannel(self, interested) } - override func setOption0(option: T, value: T.OptionType) throws { + override func setOption0(_ option: Option, value: Option.Value) throws { self.eventLoop.assertInEventLoop() guard isOpen else { @@ -349,11 +349,11 @@ final class ServerSocketChannel: BaseSocketChannel { case _ as BacklogOption: backlog = value as! Int32 default: - try super.setOption0(option: option, value: value) + try super.setOption0(option, value: value) } } - override func getOption0(option: T) throws -> T.OptionType { + override func getOption0(_ option: Option) throws -> Option.Value { self.eventLoop.assertInEventLoop() guard isOpen else { @@ -362,9 +362,9 @@ final class ServerSocketChannel: BaseSocketChannel { 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 { // MARK: Datagram Channel overrides required by BaseSocketChannel - override func setOption0(option: T, value: T.OptionType) throws { + override func setOption0(_ option: Option, value: Option.Value) throws { self.eventLoop.assertInEventLoop() guard isOpen else { @@ -557,11 +557,11 @@ final class DatagramChannel: BaseSocketChannel { 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(option: T) throws -> T.OptionType { + override func getOption0(_ option: Option) throws -> Option.Value { self.eventLoop.assertInEventLoop() guard isOpen else { @@ -570,11 +570,11 @@ final class DatagramChannel: BaseSocketChannel { 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) } } diff --git a/Sources/_NIO1APIShims/NIO1APIShims.swift b/Sources/_NIO1APIShims/NIO1APIShims.swift index 7c58c70ef..7ae25054c 100644 --- a/Sources/_NIO1APIShims/NIO1APIShims.swift +++ b/Sources/_NIO1APIShims/NIO1APIShims.swift @@ -380,6 +380,21 @@ extension Channel { var _unsafe: ChannelCore { return self._channelCore } + + @available(*, deprecated, renamed: "setOption(_:value:)") + func setOption(option: Option, value: Option.Value) -> EventLoopFuture { + return self.setOption(option, value: value) + } + + @available(*, deprecated, renamed: "getOption(_:)") + func getOption(option: Option) -> EventLoopFuture { + return self.getOption(option) + } +} + +extension ChannelOption { + @available(*, deprecated, renamed: "Value") + public typealias OptionType = Value } @available(*, deprecated, renamed: "HTTPServerProtocolUpgrader") diff --git a/Tests/NIOTests/AcceptBackoffHandlerTest.swift b/Tests/NIOTests/AcceptBackoffHandlerTest.swift index 9095ea8b0..6a42c661d 100644 --- a/Tests/NIOTests/AcceptBackoffHandlerTest.swift +++ b/Tests/NIOTests/AcceptBackoffHandlerTest.swift @@ -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()) diff --git a/Tests/NIOTests/ChannelOptionStorageTest.swift b/Tests/NIOTests/ChannelOptionStorageTest.swift index c101f267f..d993d4bd6 100644 --- a/Tests/NIOTests/ChannelOptionStorageTest.swift +++ b/Tests/NIOTests/ChannelOptionStorageTest.swift @@ -78,12 +78,12 @@ class OptionsCollectingChannel: Channel { var parent: Channel? { fatalError() } - func setOption(option: T, value: T.OptionType) -> EventLoopFuture where T : ChannelOption { + func setOption(_ option: Option, value: Option.Value) -> EventLoopFuture { self.allOptions.append((option, value)) return self.eventLoop.makeSucceededFuture(()) } - func getOption(option: T) -> EventLoopFuture where T : ChannelOption { + func getOption(_ option: Option) -> EventLoopFuture { fatalError() } diff --git a/Tests/NIOTests/ChannelTests.swift b/Tests/NIOTests/ChannelTests.swift index adb6cfb64..c8dd8cae5 100644 --- a/Tests/NIOTests/ChannelTests.swift +++ b/Tests/NIOTests/ChannelTests.swift @@ -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()) diff --git a/Tests/NIOTests/DatagramChannelTests.swift b/Tests/NIOTests/DatagramChannelTests.swift index 30194ebe9..55af6967b 100644 --- a/Tests/NIOTests/DatagramChannelTests.swift +++ b/Tests/NIOTests/DatagramChannelTests.swift @@ -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)) diff --git a/Tests/NIOTests/SelectorTest.swift b/Tests/NIOTests/SelectorTest.swift index a1038f311..71e11d4ae 100644 --- a/Tests/NIOTests/SelectorTest.swift +++ b/Tests/NIOTests/SelectorTest.swift @@ -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") } diff --git a/Tests/NIOTests/SocketChannelTest.swift b/Tests/NIOTests/SocketChannelTest.swift index 8334a5c47..d10709373 100644 --- a/Tests/NIOTests/SocketChannelTest.swift +++ b/Tests/NIOTests/SocketChannelTest.swift @@ -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() diff --git a/Tests/NIOTests/TestUtils.swift b/Tests/NIOTests/TestUtils.swift index d55f7accc..ea70a18f0 100644 --- a/Tests/NIOTests/TestUtils.swift +++ b/Tests/NIOTests/TestUtils.swift @@ -162,20 +162,20 @@ final class NonAcceptingServerSocket: ServerSocket { } } -func assertSetGetOptionOnOpenAndClosed(channel: Channel, option: T, value: T.OptionType) throws { - _ = try channel.setOption(option: option, value: value).wait() - _ = try channel.getOption(option: option).wait() +func assertSetGetOptionOnOpenAndClosed(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(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 diff --git a/docs/public-api-changes-NIO1-to-NIO2.md b/docs/public-api-changes-NIO1-to-NIO2.md index 581a77244..c8037b626 100644 --- a/docs/public-api-changes-NIO1-to-NIO2.md +++ b/docs/public-api-changes-NIO1-to-NIO2.md @@ -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.