diff --git a/Sources/NIO/Bootstrap.swift b/Sources/NIO/Bootstrap.swift index 0120260e6..25cd2f368 100644 --- a/Sources/NIO/Bootstrap.swift +++ b/Sources/NIO/Bootstrap.swift @@ -12,85 +12,132 @@ // //===----------------------------------------------------------------------===// +/// A `ServerBootstrap` is an easy way to bootstrap a `ServerChannel` when creating network servers. public final class ServerBootstrap { private let group: EventLoopGroup private let childGroup: EventLoopGroup - private var handler: ChannelHandler? - private var childHandler: ChannelHandler? - private var options = ChannelOptionStorage() - private var childOptions = ChannelOptionStorage() + private var serverChannelInit: ((Channel) -> EventLoopFuture<()>)? + private var childChannelInit: ((Channel) -> EventLoopFuture<()>)? + private var serverChannelOptions = ChannelOptionStorage() + private var childChannelOptions = ChannelOptionStorage() + /// Create a `ServerBootstrap` for the `EventLoopGroup` `group`. + /// + /// - parameters: + /// - group: The `EventLoopGroup` to use for the `ServerChannel`. public convenience init(group: EventLoopGroup) { self.init(group: group, childGroup: group) } - + + /// Create a `ServerBootstrap`. + /// + /// - parameters: + /// - group: The `EventLoopGroup` to use for the `bind` of the `ServerSocketChannel`. + /// - childGroup: The `EventLoopGroup` to run the accepted `SocketChannel`s on. public init(group: EventLoopGroup, childGroup: EventLoopGroup) { self.group = group self.childGroup = childGroup } - public func handler(handler: ChannelHandler) -> Self { - self.handler = handler + /// Initialize the `ServerSocketChannel` with `initializer`. The most common task in initializer is to add + /// `ChannelHandler`s to the `ChannelPipeline`. + /// + /// - note: To set the initializer for the accepted `SocketChannel`s, look at `ServerBootstrap.childChannelInitializer`. + /// + /// - parameters: + /// - initializer: A closure that initializes the provided `Channel`. + public func serverChannelInitializer(_ initializer: @escaping (Channel) -> EventLoopFuture<()>) -> Self { + self.serverChannelInit = initializer return self } - public func handler(childHandler: ChannelHandler) -> Self { - self.childHandler = childHandler + /// Initialize the accepted `SocketChannel`s with `initializer`. The most common task in initializer is to add + /// `ChannelHandler`s to the `ChannelPipeline`. + /// + /// - parameters: + /// - initializer: A closure that initializes the provided `Channel`. + public func childChannelInitializer(_ initializer: @escaping (Channel) -> EventLoopFuture<()>) -> Self { + self.childChannelInit = initializer return self } - public func option(option: T, value: T.OptionType) -> Self { - options.put(key: option, value: value) + /// Specifies a `ChannelOption` to be applied to the `ServerSocketChannel`. + /// + /// - note: To specify options for the accepted `SocketChannel`s, look at `ServerBootstrap.childChannelOption`. + /// + /// - parameters: + /// - option: The option to be applied. + /// - value: The value for the option. + public func serverChannelOption(_ option: T, value: T.OptionType) -> Self { + serverChannelOptions.put(key: option, value: value) return self } - public func option(childOption: T, childValue: T.OptionType) -> Self { - childOptions.put(key: childOption, value: childValue) + /// Specifies a `ChannelOption` to be applied to the accepted `SocketChannel`s. + /// + /// - parameters: + /// - option: The option to be applied. + /// - value: The value for the option. + public func childChannelOption(_ option: T, value: T.OptionType) -> Self { + childChannelOptions.put(key: option, value: value) return self } + /// Bind the `ServerSocketChannel` to `host` and `port`. + /// + /// - parameters: + /// - host: The host to bind on. + /// - port: The port to bind on. public func bind(to host: String, on port: Int32) -> EventLoopFuture { let evGroup = group do { let address = try SocketAddress.newAddressResolving(host: host, port: port) - return bind0(evGroup: evGroup, to: address) + return bind0(eventLoopGroup: evGroup, to: address) } catch let err { return evGroup.next().newFailedFuture(error: err) } } + /// Bind the `ServerSocketChannel` to `address`. + /// + /// - parameters: + /// - address: The `SocketAddress` to bind on. public func bind(to address: SocketAddress) -> EventLoopFuture { - return bind0(evGroup: group, to: address) + return bind0(eventLoopGroup: group, to: address) } - + + /// Bind the `ServerSocketChannel` to a UNIX Domain Socket. + /// + /// - parameters: + /// - path: The path of the UNIX Domain Socket to bind on. `path` must not exist, it will be created by the system. public func bind(unixDomainSocket path: String) -> EventLoopFuture { let evGroup = group do { let address = try SocketAddress.unixDomainSocketAddress(path: path) - return bind0(evGroup: evGroup, to: address) + return bind0(eventLoopGroup: evGroup, to: address) } catch let err { return evGroup.next().newFailedFuture(error: err) } } - private func bind0(evGroup: EventLoopGroup, to address: SocketAddress) -> EventLoopFuture { - let chEvGroup = childGroup - let opts = options - let eventLoop = evGroup.next() - let h = handler - let chHandler = childHandler - let chOptions = childOptions + private func bind0(eventLoopGroup: EventLoopGroup, to address: SocketAddress) -> EventLoopFuture { + let childEventLoopGroup = self.childGroup + let serverChannelOptions = self.serverChannelOptions + let eventLoop = eventLoopGroup.next() + let serverChannelInit = self.serverChannelInit + let childChannelInit = self.childChannelInit + let childChannelOptions = self.childChannelOptions let promise: EventLoopPromise = eventLoop.newPromise() do { let serverChannel = try ServerSocketChannel(eventLoop: eventLoop as! SelectableEventLoop, - group: chEvGroup, + group: childEventLoopGroup, protocolFamily: address.protocolFamily) func finishServerSetup() { do { - try opts.applyAll(channel: serverChannel) + try serverChannelOptions.applyAll(channel: serverChannel) let f = serverChannel.register().then(callback: { (_) -> EventLoopFuture<()> in serverChannel.bind(to: address) }) f.whenComplete(callback: { v in switch v { @@ -106,7 +153,8 @@ public final class ServerBootstrap { } func addAcceptHandlerAndFinishServerSetup() { - let f = serverChannel.pipeline.add(handler: AcceptHandler(childHandler: chHandler, childOptions: chOptions)) + let f = serverChannel.pipeline.add(handler: AcceptHandler(childChannelInitializer: childChannelInit, + childChannelOptions: childChannelOptions)) f.whenComplete(callback: { v in switch v { case .failure(let err): @@ -117,9 +165,8 @@ public final class ServerBootstrap { }) } - if let serverHandler = h { - let future = serverChannel.pipeline.add(handler: serverHandler) - future.whenComplete(callback: { v in + if let serverChannelInit = serverChannelInit { + serverChannelInit(serverChannel).whenComplete(callback: { v in switch v { case .failure(let err): promise.fail(error: err) @@ -140,22 +187,21 @@ public final class ServerBootstrap { private class AcceptHandler : ChannelInboundHandler { public typealias InboundIn = SocketChannel - private let childHandler: ChannelHandler? - private let childOptions: ChannelOptionStorage + private let childChannelInit: ((Channel) -> EventLoopFuture<()>)? + private let childChannelOptions: ChannelOptionStorage - init(childHandler: ChannelHandler?, childOptions: ChannelOptionStorage) { - self.childHandler = childHandler - self.childOptions = childOptions + init(childChannelInitializer: ((Channel) -> EventLoopFuture<()>)?, childChannelOptions: ChannelOptionStorage) { + self.childChannelInit = childChannelInitializer + self.childChannelOptions = childChannelOptions } func channelRead(ctx: ChannelHandlerContext, data: NIOAny) { let accepted = self.unwrapInboundIn(data) do { - try self.childOptions.applyAll(channel: accepted) + try self.childChannelOptions.applyAll(channel: accepted) - if let handler = childHandler { - let f = accepted.pipeline.add(handler: handler) - f.whenComplete(callback: { v in + if let childChannelInit = childChannelInit { + childChannelInit(accepted).whenComplete(callback: { v in switch v { case .failure(let err): self.closeAndFire(ctx: ctx, accepted: accepted, err: err) @@ -190,51 +236,53 @@ public final class ServerBootstrap { } } +/// A `ClientBootstrap` is an easy way to bootstrap a `SocketChannel` when creating network clients. public final class ClientBootstrap { private let group: EventLoopGroup - private var handler: ChannelHandler? - private var options = ChannelOptionStorage() + private var channelInitializer: ((Channel) -> EventLoopFuture<()>)? + private var channelOptions = ChannelOptionStorage() + /// Create a `ClientBootstrap` on the `EventLoopGroup` `group`. + /// + /// - parameters: + /// - group: The `EventLoopGroup` to use. public init(group: EventLoopGroup) { self.group = group } - public func handler(handler: ChannelHandler) -> Self { - self.handler = handler + /// Initialize the connected `SocketChannel` with `initializer`. The most common task in initializer is to add + /// `ChannelHandler`s to the `ChannelPipeline`. + /// + /// - parameters: + /// - handler: A closure that initializes the provided `Channel`. + public func channelInitializer(_ handler: @escaping (Channel) -> EventLoopFuture<()>) -> Self { + self.channelInitializer = handler return self } + /// Specifies a `ChannelOption` to be applied to the `SocketChannel`. + /// + /// - parameters: + /// - option: The option to be applied. + /// - value: The value for the option. public func option(option: T, value: T.OptionType) -> Self { - options.put(key: option, value: value) + channelOptions.put(key: option, value: value) return self } - - public func bind(protocolFamily: Int32, to host: String, on port: Int32) -> EventLoopFuture { - let evGroup = group - do { - let address = try SocketAddress.newAddressResolving(host: host, port: port) - return execute(evGroup: evGroup, protocolFamily: protocolFamily) { channel in - return channel.bind(to: address) - } - } catch let err { - return evGroup.next().newFailedFuture(error: err) - } - } - - public func bind(to address: SocketAddress) -> EventLoopFuture { - return execute(evGroup: group, protocolFamily: address.protocolFamily) { channel in - return channel.bind(to: address) - } - } - + /// Specify the `host` and `port` to connect to for the TCP `Channel` that will be established. + /// + /// - parameters: + /// - host: The host to connect to. + /// - port: The port to connect to. + /// - returns: An `EventLoopFuture` to deliver the `Channel` when connected. public func connect(to host: String, on port: Int32) -> EventLoopFuture { let evGroup = group do { let address = try SocketAddress.newAddressResolving(host: host, port: port) - return execute(evGroup: group, protocolFamily: address.protocolFamily) { channel in + return execute(eventLoopGroup: group, protocolFamily: address.protocolFamily) { channel in return channel.connect(to: address) } } catch let err { @@ -242,12 +290,22 @@ public final class ClientBootstrap { } } + /// Specify the `address` to connect to for the TCP `Channel` that will be established. + /// + /// - parameters: + /// - address: The address to connect to. + /// - returns: An `EventLoopFuture` to deliver the `Channel` when connected. public func connect(to address: SocketAddress) -> EventLoopFuture { - return execute(evGroup: group, protocolFamily: address.protocolFamily) { channel in + return execute(eventLoopGroup: group, protocolFamily: address.protocolFamily) { channel in return channel.connect(to: address) } } + /// Specify the `unixDomainSocket` path to connect to for the UDS `Channel` that will be established. + /// + /// - parameters: + /// - address: The address to connect to. + /// - returns: An `EventLoopFuture` to deliver the `Channel` when connected. public func connect(to unixDomainSocket: String) -> EventLoopFuture { do { let address = try SocketAddress.unixDomainSocketAddress(path: unixDomainSocket) @@ -257,12 +315,12 @@ public final class ClientBootstrap { } } - private func execute(evGroup: EventLoopGroup, + private func execute(eventLoopGroup: EventLoopGroup, protocolFamily: Int32, - fn: @escaping (Channel) -> EventLoopFuture) -> EventLoopFuture { - let eventLoop = evGroup.next() - let h = handler - let opts = options + _ body: @escaping (Channel) -> EventLoopFuture) -> EventLoopFuture { + let eventLoop = eventLoopGroup.next() + let channelInitializer = self.channelInitializer + let channelOptions = self.channelOptions let promise: EventLoopPromise = eventLoop.newPromise() do { @@ -270,9 +328,9 @@ public final class ClientBootstrap { func finishClientSetup() { do { - try opts.applyAll(channel: channel) + try channelOptions.applyAll(channel: channel) let f = channel.register().then { (_) -> EventLoopFuture in - fn(channel) + body(channel) } f.whenComplete(callback: { v in switch v { @@ -287,9 +345,8 @@ public final class ClientBootstrap { } } - if let clientHandler = h { - let future = channel.pipeline.add(handler: clientHandler) - future.whenComplete(callback: { v in + if let channelInitializer = channelInitializer { + channelInitializer(channel).whenComplete(callback: { v in switch v { case .failure(let err): promise.fail(error: err) diff --git a/Sources/NIO/ChannelHandlers.swift b/Sources/NIO/ChannelHandlers.swift index 1e2c5a4d8..04618544d 100644 --- a/Sources/NIO/ChannelHandlers.swift +++ b/Sources/NIO/ChannelHandlers.swift @@ -77,34 +77,6 @@ public class BackPressureHandler: ChannelInboundHandler, _ChannelOutboundHandler } } -public class ChannelInitializer: _ChannelInboundHandler { - private let initChannel: (Channel) -> (EventLoopFuture) - - public init(initChannel: @escaping (Channel) -> (EventLoopFuture)) { - self.initChannel = initChannel - } - - public func channelRegistered(ctx: ChannelHandlerContext) throws { - defer { - let _ = ctx.pipeline?.remove(handler: self) - } - - if let ch = ctx.channel { - let f = initChannel(ch) - f.whenComplete(callback: { v in - switch v { - case .failure(let err): - ctx.fireErrorCaught(error: err) - case .success(_): - ctx.fireChannelRegistered() - } - }) - } else { - ctx.fireChannelRegistered() - } - } -} - /// Triggers an IdleStateEvent when a Channel has not performed read, write, or both operation for a while. public class IdleStateHandler : ChannelInboundHandler, ChannelOutboundHandler { public typealias InboundIn = NIOAny diff --git a/Sources/NIO/SocketAddresses.swift b/Sources/NIO/SocketAddresses.swift index 5a94786d6..db3c344db 100644 --- a/Sources/NIO/SocketAddresses.swift +++ b/Sources/NIO/SocketAddresses.swift @@ -100,8 +100,7 @@ public enum SocketAddress: CustomStringConvertible { throw SocketAddressError.unixDomainSocketPathTooLong } - var pathBytes = Array(path.utf8) - pathBytes.append(0) + let pathBytes = path.utf8 + [0] var addr = sockaddr_un() addr.sun_family = sa_family_t(AF_UNIX) diff --git a/Sources/NIOEchoServer/main.swift b/Sources/NIOEchoServer/main.swift index d2d10c527..fe7da1501 100644 --- a/Sources/NIOEchoServer/main.swift +++ b/Sources/NIOEchoServer/main.swift @@ -47,22 +47,22 @@ private final class EchoHandler: ChannelInboundHandler { let group = MultiThreadedEventLoopGroup(numThreads: 1) let bootstrap = ServerBootstrap(group: group) // Specify backlog and enable SO_REUSEADDR for the server itself - .option(option: ChannelOptions.Backlog, value: 256) - .option(option: ChannelOptions.Socket(SocketOptionLevel(SOL_SOCKET), SO_REUSEADDR), value: 1) + .serverChannelOption(ChannelOptions.Backlog, value: 256) + .serverChannelOption(ChannelOptions.Socket(SocketOptionLevel(SOL_SOCKET), SO_REUSEADDR), value: 1) // Set the handlers that are appled to the accepted Channels - .handler(childHandler: ChannelInitializer(initChannel: { channel in - // Ensure we not read faster then we can write by adding the BackPressureHandler into the pipeline. + .childChannelInitializer { channel in + // Ensure we don't read faster then we can write by adding the BackPressureHandler into the pipeline. return channel.pipeline.add(handler: BackPressureHandler()).then(callback: { v in return channel.pipeline.add(handler: EchoHandler()) }) - })) + } // Enable TCP_NODELAY and SO_REUSEADDR for the accepted Channels - .option(childOption: ChannelOptions.Socket(IPPROTO_TCP, TCP_NODELAY), childValue: 1) - .option(childOption: ChannelOptions.Socket(SocketOptionLevel(SOL_SOCKET), SO_REUSEADDR), childValue: 1) - .option(childOption: ChannelOptions.MaxMessagesPerRead, childValue: 16) - .option(childOption: ChannelOptions.RecvAllocator, childValue: FixedSizeRecvByteBufferAllocator(capacity: 8192)) + .childChannelOption(ChannelOptions.Socket(IPPROTO_TCP, TCP_NODELAY), value: 1) + .childChannelOption(ChannelOptions.Socket(SocketOptionLevel(SOL_SOCKET), SO_REUSEADDR), value: 1) + .childChannelOption(ChannelOptions.MaxMessagesPerRead, value: 16) + .childChannelOption(ChannelOptions.RecvAllocator, value: FixedSizeRecvByteBufferAllocator(capacity: 8192)) defer { try! group.syncShutdownGracefully() } diff --git a/Sources/NIOHTTP1Server/main.swift b/Sources/NIOHTTP1Server/main.swift index 4555a45ad..910c529cd 100644 --- a/Sources/NIOHTTP1Server/main.swift +++ b/Sources/NIOHTTP1Server/main.swift @@ -326,20 +326,20 @@ default: let group = MultiThreadedEventLoopGroup(numThreads: 1) let bootstrap = ServerBootstrap(group: group) // Specify backlog and enable SO_REUSEADDR for the server itself - .option(option: ChannelOptions.Backlog, value: 256) - .option(option: ChannelOptions.Socket(SocketOptionLevel(SOL_SOCKET), SO_REUSEADDR), value: 1) + .serverChannelOption(ChannelOptions.Backlog, value: 256) + .serverChannelOption(ChannelOptions.Socket(SocketOptionLevel(SOL_SOCKET), SO_REUSEADDR), value: 1) // Set the handlers that are applied to the accepted Channels - .handler(childHandler: ChannelInitializer(initChannel: { channel in + .childChannelInitializer { channel in return channel.pipeline.addHTTPServerHandlers().then { return channel.pipeline.add(handler: HTTPHandler(htdocsPath: htdocs)) } - })) + } // Enable TCP_NODELAY and SO_REUSEADDR for the accepted Channels - .option(childOption: ChannelOptions.Socket(IPPROTO_TCP, TCP_NODELAY), childValue: 1) - .option(childOption: ChannelOptions.Socket(SocketOptionLevel(SOL_SOCKET), SO_REUSEADDR), childValue: 1) - .option(childOption: ChannelOptions.MaxMessagesPerRead, childValue: 1) + .childChannelOption(ChannelOptions.Socket(IPPROTO_TCP, TCP_NODELAY), value: 1) + .childChannelOption(ChannelOptions.Socket(SocketOptionLevel(SOL_SOCKET), SO_REUSEADDR), value: 1) + .childChannelOption(ChannelOptions.MaxMessagesPerRead, value: 1) defer { try! group.syncShutdownGracefully() diff --git a/Sources/NIOTLSServer/main.swift b/Sources/NIOTLSServer/main.swift index 41f23eb0a..b905e6cf8 100644 --- a/Sources/NIOTLSServer/main.swift +++ b/Sources/NIOTLSServer/main.swift @@ -35,20 +35,20 @@ let sslContext = try! SSLContext(configuration: TLSConfiguration.forServer(certi let group = MultiThreadedEventLoopGroup(numThreads: 1) let bootstrap = ServerBootstrap(group: group) // Specify backlog and enable SO_REUSEADDR for the server itself - .option(option: ChannelOptions.Backlog, value: 256) - .option(option: ChannelOptions.Socket(SocketOptionLevel(SOL_SOCKET), SO_REUSEADDR), value: 1) + .serverChannelOption(ChannelOptions.Backlog, value: 256) + .serverChannelOption(ChannelOptions.Socket(SocketOptionLevel(SOL_SOCKET), SO_REUSEADDR), value: 1) // Set the handlers that are applied to the accepted channels. - .handler(childHandler: ChannelInitializer(initChannel: { channel in + .childChannelInitializer { channel in return channel.pipeline.add(handler: try! OpenSSLServerHandler(context: sslContext)).then(callback: { v2 in return channel.pipeline.add(handler: EchoHandler()) }) - })) + } // Enable TCP_NODELAY and SO_REUSEADDR for the accepted Channels - .option(childOption: ChannelOptions.Socket(IPPROTO_TCP, TCP_NODELAY), childValue: 1) - .option(childOption: ChannelOptions.Socket(SocketOptionLevel(SOL_SOCKET), SO_REUSEADDR), childValue: 1) - .option(childOption: ChannelOptions.MaxMessagesPerRead, childValue: 1) + .childChannelOption(ChannelOptions.Socket(IPPROTO_TCP, TCP_NODELAY), value: 1) + .childChannelOption(ChannelOptions.Socket(SocketOptionLevel(SOL_SOCKET), SO_REUSEADDR), value: 1) + .childChannelOption(ChannelOptions.MaxMessagesPerRead, value: 1) defer { try! group.syncShutdownGracefully() diff --git a/Tests/NIOHTTP1Tests/HTTPServerClientTest.swift b/Tests/NIOHTTP1Tests/HTTPServerClientTest.swift index a85edae66..70b07c0fe 100644 --- a/Tests/NIOHTTP1Tests/HTTPServerClientTest.swift +++ b/Tests/NIOHTTP1Tests/HTTPServerClientTest.swift @@ -336,26 +336,26 @@ class HTTPServerClientTest : XCTestCase { let numBytes = 16 * 1024 let httpHandler = SimpleHTTPServer(mode) let serverChannel = try ServerBootstrap(group: group) - .option(option: ChannelOptions.Socket(SocketOptionLevel(SOL_SOCKET), SO_REUSEADDR), value: 1) + .serverChannelOption(ChannelOptions.Socket(SocketOptionLevel(SOL_SOCKET), SO_REUSEADDR), value: 1) // Set the handlers that are appled to the accepted Channels - .handler(childHandler: ChannelInitializer(initChannel: { channel in - // Ensure we not read faster then we can write by adding the BackPressureHandler into the pipeline. + .childChannelInitializer { channel in + // Ensure we don't read faster then we can write by adding the BackPressureHandler into the pipeline. channel.pipeline.addHTTPServerHandlers().then { channel.pipeline.add(handler: httpHandler) } - })).bind(to: "127.0.0.1", on: 0).wait() + }.bind(to: "127.0.0.1", on: 0).wait() defer { _ = serverChannel.close() } let clientChannel = try ClientBootstrap(group: group) - .handler(handler: ChannelInitializer(initChannel: { channel in + .channelInitializer { channel in channel.pipeline.addHTTPClientHandlers().then { channel.pipeline.add(handler: accumulation) } - })) + } .connect(to: serverChannel.localAddress!) .wait() @@ -394,26 +394,26 @@ class HTTPServerClientTest : XCTestCase { let numBytes = 16 * 1024 let httpHandler = SimpleHTTPServer(mode) let serverChannel = try ServerBootstrap(group: group) - .option(option: ChannelOptions.Socket(SocketOptionLevel(SOL_SOCKET), SO_REUSEADDR), value: 1) + .serverChannelOption(ChannelOptions.Socket(SocketOptionLevel(SOL_SOCKET), SO_REUSEADDR), value: 1) // Set the handlers that are appled to the accepted Channels - .handler(childHandler: ChannelInitializer(initChannel: { channel in - // Ensure we not read faster then we can write by adding the BackPressureHandler into the pipeline. + .childChannelInitializer { channel in + // Ensure we don't read faster then we can write by adding the BackPressureHandler into the pipeline. channel.pipeline.addHTTPServerHandlers().then { channel.pipeline.add(handler: httpHandler) } - })).bind(to: "127.0.0.1", on: 0).wait() + }.bind(to: "127.0.0.1", on: 0).wait() defer { _ = serverChannel.close() } let clientChannel = try ClientBootstrap(group: group) - .handler(handler: ChannelInitializer(initChannel: { channel in + .channelInitializer { channel in channel.pipeline.addHTTPClientHandlers().then { channel.pipeline.add(handler: accumulation) } - })) + } .connect(to: serverChannel.localAddress!) .wait() @@ -455,23 +455,23 @@ class HTTPServerClientTest : XCTestCase { let numBytes = 16 * 1024 let httpHandler = SimpleHTTPServer(mode) let serverChannel = try ServerBootstrap(group: group) - .option(option: ChannelOptions.Socket(SocketOptionLevel(SOL_SOCKET), SO_REUSEADDR), value: 1) - .handler(childHandler: ChannelInitializer(initChannel: { channel in + .serverChannelOption(ChannelOptions.Socket(SocketOptionLevel(SOL_SOCKET), SO_REUSEADDR), value: 1) + .childChannelInitializer { channel in channel.pipeline.addHTTPServerHandlers().then { channel.pipeline.add(handler: httpHandler) } - })).bind(to: "127.0.0.1", on: 0).wait() + }.bind(to: "127.0.0.1", on: 0).wait() defer { _ = serverChannel.close() } let clientChannel = try ClientBootstrap(group: group) - .handler(handler: ChannelInitializer(initChannel: { channel in + .channelInitializer { channel in channel.pipeline.addHTTPClientHandlers().then { channel.pipeline.add(handler: accumulation) } - })) + } .connect(to: serverChannel.localAddress!) .wait() @@ -512,22 +512,22 @@ class HTTPServerClientTest : XCTestCase { let numBytes = 16 * 1024 let httpHandler = SimpleHTTPServer(mode) let serverChannel = try ServerBootstrap(group: group) - .option(option: ChannelOptions.Socket(SocketOptionLevel(SOL_SOCKET), SO_REUSEADDR), value: 1) + .serverChannelOption(ChannelOptions.Socket(SocketOptionLevel(SOL_SOCKET), SO_REUSEADDR), value: 1) // Set the handlers that are appled to the accepted Channels - .handler(childHandler: ChannelInitializer(initChannel: { channel in - // Ensure we not read faster then we can write by adding the BackPressureHandler into the pipeline. + .childChannelInitializer { channel in + // Ensure we don't read faster then we can write by adding the BackPressureHandler into the pipeline. channel.pipeline.addHTTPServerHandlers().then { channel.pipeline.add(handler: httpHandler) } - })).bind(to: "127.0.0.1", on: 0).wait() + }.bind(to: "127.0.0.1", on: 0).wait() defer { _ = serverChannel.close() } let clientChannel = try ClientBootstrap(group: group) - .handler(handler: accumulation) + .channelInitializer({ $0.pipeline.add(handler: accumulation) }) .connect(to: serverChannel.localAddress!) .wait() @@ -557,22 +557,22 @@ class HTTPServerClientTest : XCTestCase { let numBytes = 16 * 1024 let httpHandler = SimpleHTTPServer(.byteBuffer) let serverChannel = try ServerBootstrap(group: group) - .option(option: ChannelOptions.Socket(SocketOptionLevel(SOL_SOCKET), SO_REUSEADDR), value: 1) - .handler(childHandler: ChannelInitializer(initChannel: { channel in + .serverChannelOption(ChannelOptions.Socket(SocketOptionLevel(SOL_SOCKET), SO_REUSEADDR), value: 1) + .childChannelInitializer { channel in channel.pipeline.addHTTPServerHandlers().then { channel.pipeline.add(handler: httpHandler) } - })).bind(to: "127.0.0.1", on: 0).wait() + }.bind(to: "127.0.0.1", on: 0).wait() defer { _ = serverChannel.close() } let clientChannel = try ClientBootstrap(group: group) - .handler(handler: ChannelInitializer(initChannel: { channel in + .channelInitializer { channel in channel.pipeline.addHTTPClientHandlers().then { channel.pipeline.add(handler: accumulation) } - })) + } .connect(to: serverChannel.localAddress!) .wait() @@ -602,22 +602,22 @@ class HTTPServerClientTest : XCTestCase { let numBytes = 16 * 1024 let httpHandler = SimpleHTTPServer(.byteBuffer) let serverChannel = try ServerBootstrap(group: group) - .option(option: ChannelOptions.Socket(SocketOptionLevel(SOL_SOCKET), SO_REUSEADDR), value: 1) - .handler(childHandler: ChannelInitializer(initChannel: { channel in + .serverChannelOption(ChannelOptions.Socket(SocketOptionLevel(SOL_SOCKET), SO_REUSEADDR), value: 1) + .childChannelInitializer { channel in channel.pipeline.addHTTPServerHandlers().then { channel.pipeline.add(handler: httpHandler) } - })).bind(to: "127.0.0.1", on: 0).wait() + }.bind(to: "127.0.0.1", on: 0).wait() defer { _ = serverChannel.close() } let clientChannel = try ClientBootstrap(group: group) - .handler(handler: ChannelInitializer(initChannel: { channel in + .channelInitializer { channel in channel.pipeline.addHTTPClientHandlers().then { channel.pipeline.add(handler: accumulation) } - })) + } .connect(to: serverChannel.localAddress!) .wait() diff --git a/Tests/NIOHTTP1Tests/HTTPUpgradeTests.swift b/Tests/NIOHTTP1Tests/HTTPUpgradeTests.swift index ea30b6d10..956e5b3f5 100644 --- a/Tests/NIOHTTP1Tests/HTTPUpgradeTests.swift +++ b/Tests/NIOHTTP1Tests/HTTPUpgradeTests.swift @@ -29,13 +29,13 @@ private extension ChannelPipeline { private func serverHTTPChannel(group: EventLoopGroup, handlers: [ChannelHandler]) -> Channel { return try! ServerBootstrap(group: group) - .option(option: ChannelOptions.Socket(SocketOptionLevel(SOL_SOCKET), SO_REUSEADDR), value: 1) - .handler(childHandler: ChannelInitializer(initChannel: { channel in + .serverChannelOption(ChannelOptions.Socket(SocketOptionLevel(SOL_SOCKET), SO_REUSEADDR), value: 1) + .childChannelInitializer { channel in channel.pipeline.addHTTPServerHandlers().then { let futureResults = handlers.map { channel.pipeline.add(handler: $0) } return EventLoopFuture.andAll(futureResults, eventLoop: channel.eventLoop) } - })).bind(to: "127.0.0.1", on: 0).wait() + }.bind(to: "127.0.0.1", on: 0).wait() } private func connectedClientChannel(group: EventLoopGroup, serverAddress: SocketAddress) -> Channel { diff --git a/Tests/NIOOpenSSLTests/OpenSSLIntegrationTest.swift b/Tests/NIOOpenSSLTests/OpenSSLIntegrationTest.swift index 09df8c53f..2683345ab 100644 --- a/Tests/NIOOpenSSLTests/OpenSSLIntegrationTest.swift +++ b/Tests/NIOOpenSSLTests/OpenSSLIntegrationTest.swift @@ -215,8 +215,8 @@ internal func serverTLSChannel(withContext: NIOOpenSSL.SSLContext, andHandlers: internal func serverTLSChannel(withContext: NIOOpenSSL.SSLContext, preHandlers: [ChannelHandler], postHandlers: [ChannelHandler], onGroup: EventLoopGroup) throws -> Channel { return try ServerBootstrap(group: onGroup) - .option(option: ChannelOptions.Socket(SocketOptionLevel(SOL_SOCKET), SO_REUSEADDR), value: 1) - .handler(childHandler: ChannelInitializer(initChannel: { channel in + .serverChannelOption(ChannelOptions.Socket(SocketOptionLevel(SOL_SOCKET), SO_REUSEADDR), value: 1) + .childChannelInitializer { channel in let results = preHandlers.map { channel.pipeline.add(handler: $0) } return EventLoopFuture.andAll(results, eventLoop: results.first?.eventLoop ?? onGroup.next()).then { return channel.pipeline.add(handler: try! OpenSSLServerHandler(context: withContext)).then(callback: { v2 in @@ -224,7 +224,7 @@ internal func serverTLSChannel(withContext: NIOOpenSSL.SSLContext, preHandlers: return EventLoopFuture.andAll(results, eventLoop: results.first?.eventLoop ?? onGroup.next()) }) } - })).bind(to: "127.0.0.1", on: 0).wait() + }.bind(to: "127.0.0.1", on: 0).wait() } internal func clientTLSChannel(withContext: NIOOpenSSL.SSLContext, @@ -234,7 +234,7 @@ internal func clientTLSChannel(withContext: NIOOpenSSL.SSLContext, connectingTo: SocketAddress, serverHostname: String? = nil) throws -> Channel { return try ClientBootstrap(group: onGroup) - .handler(handler: ChannelInitializer(initChannel: { channel in + .channelInitializer { channel in let results = preHandlers.map { channel.pipeline.add(handler: $0) } return EventLoopFuture.andAll(results, eventLoop: results.first?.eventLoop ?? onGroup.next()).then(callback: { v2 in return channel.pipeline.add(handler: try! OpenSSLClientHandler(context: withContext, serverHostname: serverHostname)).then(callback: { v2 in @@ -242,7 +242,7 @@ internal func clientTLSChannel(withContext: NIOOpenSSL.SSLContext, return EventLoopFuture.andAll(results, eventLoop: results.first?.eventLoop ?? onGroup.next()) }) }) - })).connect(to: connectingTo).wait() + }.connect(to: connectingTo).wait() } class OpenSSLIntegrationTest: XCTestCase { @@ -601,7 +601,7 @@ class OpenSSLIntegrationTest: XCTestCase { let readPromise: EventLoopPromise = group.next().newPromise() let promiseOnReadHandler = PromiseOnReadHandler(promise: readPromise) let clientChannel = try ClientBootstrap(group: group) - .handler(handler: promiseOnReadHandler) + .channelInitializer({ $0.pipeline.add(handler: promiseOnReadHandler) }) .connect(to: serverChannel.localAddress!).wait() defer { _ = try! clientChannel.close().wait() diff --git a/Tests/NIOOpenSSLTests/SSLCertificateTest.swift b/Tests/NIOOpenSSLTests/SSLCertificateTest.swift index 6ac67767e..28ab6915e 100644 --- a/Tests/NIOOpenSSLTests/SSLCertificateTest.swift +++ b/Tests/NIOOpenSSLTests/SSLCertificateTest.swift @@ -105,8 +105,7 @@ i5PCcPYi39q101UIxV/WokS0mqHx/XuTYTwhWYd/C49OnM8MLZOUJd8w0VvS0ItY private func makeTemporaryFile() -> String { let template = "/tmp/niotestXXXXXXX" - var templateBytes = Array(template.utf8) - templateBytes.append(0) + var templateBytes = template.utf8 + [0] let templateBytesCount = templateBytes.count let fd = templateBytes.withUnsafeMutableBufferPointer { ptr in ptr.baseAddress!.withMemoryRebound(to: Int8.self, capacity: templateBytesCount) { (ptr: UnsafeMutablePointer) in diff --git a/Tests/NIOTests/ChannelPipelineTest.swift b/Tests/NIOTests/ChannelPipelineTest.swift index ec5ae20b4..40d54799d 100644 --- a/Tests/NIOTests/ChannelPipelineTest.swift +++ b/Tests/NIOTests/ChannelPipelineTest.swift @@ -58,15 +58,15 @@ class ChannelPipelineTest: XCTestCase { var buf = channel.allocator.buffer(capacity: 1024) buf.write(string: "hello") - _ = try channel.pipeline.add(handler: TestChannelOutboundHandler({ data in + _ = try channel.pipeline.add(handler: TestChannelOutboundHandler { data in XCTAssertEqual(1, data) return buf - })).wait() + }).wait() - _ = try channel.pipeline.add(handler: TestChannelOutboundHandler({ data in + _ = try channel.pipeline.add(handler: TestChannelOutboundHandler { data in XCTAssertEqual("msg", data) return 1 - })).wait() + }).wait() _ = channel.write(data: NIOAny("msg")) @@ -87,9 +87,9 @@ class ChannelPipelineTest: XCTestCase { let sa = SocketAddress(IPv4Address: ipv4SocketAddress, host: "foobar.com") _ = try channel.pipeline.add(handler: NoBindAllowed()).wait() - _ = try channel.pipeline.add(handler: TestChannelOutboundHandler({ data in + _ = try channel.pipeline.add(handler: TestChannelOutboundHandler { data in return data - })).wait() + }).wait() _ = try channel.connect(to: sa).wait() _ = try channel.close().wait() diff --git a/Tests/NIOTests/ChannelTests.swift b/Tests/NIOTests/ChannelTests.swift index 95c3bca87..473f3efc0 100644 --- a/Tests/NIOTests/ChannelTests.swift +++ b/Tests/NIOTests/ChannelTests.swift @@ -76,15 +76,15 @@ public class ChannelTests: XCTestCase { var serverAcceptedChannel: Channel? = nil let serverLifecycleHandler = ChannelLifecycleHandler() let serverChannel = try ServerBootstrap(group: group) - .option(option: ChannelOptions.Socket(SocketOptionLevel(SOL_SOCKET), SO_REUSEADDR), value: 1) - .handler(childHandler: ChannelInitializer(initChannel: { channel in + .serverChannelOption(ChannelOptions.Socket(SocketOptionLevel(SOL_SOCKET), SO_REUSEADDR), value: 1) + .childChannelInitializer { channel in serverAcceptedChannel = channel return channel.pipeline.add(handler: serverLifecycleHandler) - })).bind(to: "127.0.0.1", on: 0).wait() + }.bind(to: "127.0.0.1", on: 0).wait() let clientLifecycleHandler = ChannelLifecycleHandler() let clientChannel = try ClientBootstrap(group: group) - .handler(handler: clientLifecycleHandler) + .channelInitializer({ (channel: Channel) in channel.pipeline.add(handler: clientLifecycleHandler) }) .connect(to: serverChannel.localAddress!).wait() var buffer = clientChannel.allocator.buffer(capacity: 1) @@ -110,7 +110,7 @@ public class ChannelTests: XCTestCase { } let serverChannel = try ServerBootstrap(group: group) - .option(option: ChannelOptions.Socket(SocketOptionLevel(SOL_SOCKET), SO_REUSEADDR), value: 1) + .serverChannelOption(ChannelOptions.Socket(SocketOptionLevel(SOL_SOCKET), SO_REUSEADDR), value: 1) .bind(to: "127.0.0.1", on: 0).wait() let clientChannel = try ClientBootstrap(group: group) @@ -136,7 +136,7 @@ public class ChannelTests: XCTestCase { } let serverChannel = try ServerBootstrap(group: group) - .option(option: ChannelOptions.Socket(SocketOptionLevel(SOL_SOCKET), SO_REUSEADDR), value: 1) + .serverChannelOption(ChannelOptions.Socket(SocketOptionLevel(SOL_SOCKET), SO_REUSEADDR), value: 1) .bind(to: "127.0.0.1", on: 0).wait() let clientChannel = try ClientBootstrap(group: group) @@ -172,11 +172,11 @@ public class ChannelTests: XCTestCase { let childChannelPromise: EventLoopPromise = group.next().newPromise() let serverChannel = try ServerBootstrap(group: group) - .option(option: ChannelOptions.Socket(SocketOptionLevel(SOL_SOCKET), SO_REUSEADDR), value: 1) - .handler(childHandler: ChannelInitializer(initChannel: { channel in + .serverChannelOption(ChannelOptions.Socket(SocketOptionLevel(SOL_SOCKET), SO_REUSEADDR), value: 1) + .childChannelInitializer { channel in childChannelPromise.succeed(result: channel) return channel.eventLoop.newSucceedFuture(result: ()) - })).bind(to: "127.0.0.1", on: 0).wait() + }.bind(to: "127.0.0.1", on: 0).wait() let clientChannel = try ClientBootstrap(group: group) .connect(to: serverChannel.localAddress!).wait() diff --git a/Tests/NIOTests/EchoServerClientTest.swift b/Tests/NIOTests/EchoServerClientTest.swift index 04ca4b8c6..650d3e9ea 100644 --- a/Tests/NIOTests/EchoServerClientTest.swift +++ b/Tests/NIOTests/EchoServerClientTest.swift @@ -21,8 +21,7 @@ class EchoServerClientTest : XCTestCase { func buildTempDir() -> String { let template = "/tmp/.NIOTests-UDS-container-dir_XXXXXX" - var templateBytes = Array(template.utf8) - templateBytes.append(0) + var templateBytes = template.utf8 + [0] let templateBytesCount = templateBytes.count templateBytes.withUnsafeMutableBufferPointer { ptr in ptr.baseAddress!.withMemoryRebound(to: Int8.self, capacity: templateBytesCount) { (ptr: UnsafeMutablePointer) in @@ -44,13 +43,13 @@ class EchoServerClientTest : XCTestCase { let numBytes = 16 * 1024 let countingHandler = ByteCountingHandler(numBytes: numBytes, promise: group.next().newPromise()) let serverChannel = try ServerBootstrap(group: group) - .option(option: ChannelOptions.Socket(SocketOptionLevel(SOL_SOCKET), SO_REUSEADDR), value: 1) + .serverChannelOption(ChannelOptions.Socket(SocketOptionLevel(SOL_SOCKET), SO_REUSEADDR), value: 1) // Set the handlers that are appled to the accepted Channels - .handler(childHandler: ChannelInitializer(initChannel: { channel in - // Ensure we not read faster then we can write by adding the BackPressureHandler into the pipeline. + .childChannelInitializer { channel in + // Ensure we don't read faster then we can write by adding the BackPressureHandler into the pipeline. return channel.pipeline.add(handler: countingHandler) - })).bind(to: "127.0.0.1", on: 0).wait() + }.bind(to: "127.0.0.1", on: 0).wait() defer { _ = serverChannel.close() @@ -80,10 +79,10 @@ class EchoServerClientTest : XCTestCase { } let serverChannel = try ServerBootstrap(group: group) - .option(option: ChannelOptions.Socket(SocketOptionLevel(SOL_SOCKET), SO_REUSEADDR), value: 1) - .handler(childHandler: ChannelInitializer(initChannel: { channel in + .serverChannelOption(ChannelOptions.Socket(SocketOptionLevel(SOL_SOCKET), SO_REUSEADDR), value: 1) + .childChannelInitializer { channel in return channel.pipeline.add(handler: WriteALotHandler()) - })).bind(to: "127.0.0.1", on: 0).wait() + }.bind(to: "127.0.0.1", on: 0).wait() defer { _ = try! serverChannel.close().wait() @@ -91,11 +90,11 @@ class EchoServerClientTest : XCTestCase { let promise: EventLoopPromise = group.next().newPromise() let clientChannel = try ClientBootstrap(group: group) - .handler(handler: ChannelInitializer(initChannel: { channel in + .channelInitializer { channel in return channel.pipeline.add(handler: WriteOnConnectHandler(toWrite: "X")).then { v2 in return channel.pipeline.add(handler: ByteCountingHandler(numBytes: 10000, promise: promise)) } - })) + } .connect(to: serverChannel.localAddress!).wait() defer { _ = clientChannel.close() @@ -120,13 +119,13 @@ class EchoServerClientTest : XCTestCase { let numBytes = 16 * 1024 let countingHandler = ByteCountingHandler(numBytes: numBytes, promise: group.next().newPromise()) let serverChannel = try ServerBootstrap(group: group) - .option(option: ChannelOptions.Socket(SocketOptionLevel(SOL_SOCKET), SO_REUSEADDR), value: 1) + .serverChannelOption(ChannelOptions.Socket(SocketOptionLevel(SOL_SOCKET), SO_REUSEADDR), value: 1) // Set the handlers that are appled to the accepted Channels - .handler(childHandler: ChannelInitializer(initChannel: { channel in - // Ensure we not read faster then we can write by adding the BackPressureHandler into the pipeline. + .childChannelInitializer { channel in + // Ensure we don't read faster then we can write by adding the BackPressureHandler into the pipeline. return channel.pipeline.add(handler: countingHandler) - })).bind(unixDomainSocket: udsTempDir + "/server.sock").wait() + }.bind(unixDomainSocket: udsTempDir + "/server.sock").wait() defer { _ = serverChannel.close() @@ -163,13 +162,13 @@ class EchoServerClientTest : XCTestCase { let numBytes = 16 * 1024 let countingHandler = ByteCountingHandler(numBytes: numBytes, promise: group.next().newPromise()) let serverChannel = try ServerBootstrap(group: group) - .option(option: ChannelOptions.Socket(SocketOptionLevel(SOL_SOCKET), SO_REUSEADDR), value: 1) + .serverChannelOption(ChannelOptions.Socket(SocketOptionLevel(SOL_SOCKET), SO_REUSEADDR), value: 1) // Set the handlers that are appled to the accepted Channels - .handler(childHandler: ChannelInitializer(initChannel: { channel in - // Ensure we not read faster then we can write by adding the BackPressureHandler into the pipeline. + .childChannelInitializer { channel in + // Ensure we don't read faster then we can write by adding the BackPressureHandler into the pipeline. return channel.pipeline.add(handler: countingHandler) - })).bind(unixDomainSocket: udsTempDir + "/server.sock").wait() + }.bind(unixDomainSocket: udsTempDir + "/server.sock").wait() defer { _ = serverChannel.close() @@ -200,7 +199,7 @@ class EchoServerClientTest : XCTestCase { let handler = ChannelActiveHandler() let serverChannel = try ServerBootstrap(group: group) - .option(option: ChannelOptions.Socket(SocketOptionLevel(SOL_SOCKET), SO_REUSEADDR), value: 1) + .serverChannelOption(ChannelOptions.Socket(SocketOptionLevel(SOL_SOCKET), SO_REUSEADDR), value: 1) .bind(to: "127.0.0.1", on: 0).wait() defer { @@ -208,7 +207,7 @@ class EchoServerClientTest : XCTestCase { } let clientChannel = try ClientBootstrap(group: group) - .handler(handler: handler) + .channelInitializer({ $0.pipeline.add(handler: handler) }) .connect(to: serverChannel.localAddress!).wait() defer { @@ -225,10 +224,10 @@ class EchoServerClientTest : XCTestCase { } let serverChannel = try ServerBootstrap(group: group) - .option(option: ChannelOptions.Socket(SocketOptionLevel(SOL_SOCKET), SO_REUSEADDR), value: 1) - .handler(childHandler: ChannelInitializer(initChannel: { channel in + .serverChannelOption(ChannelOptions.Socket(SocketOptionLevel(SOL_SOCKET), SO_REUSEADDR), value: 1) + .childChannelInitializer { channel in return channel.pipeline.add(handler: EchoServer()) - })).bind(to: "127.0.0.1", on: 0).wait() + }.bind(to: "127.0.0.1", on: 0).wait() defer { _ = serverChannel.close() @@ -237,7 +236,7 @@ class EchoServerClientTest : XCTestCase { let numBytes = 16 * 1024 let countingHandler = ByteCountingHandler(numBytes: numBytes, promise: group.next().newPromise()) let clientChannel = try ClientBootstrap(group: group) - .handler(handler: countingHandler) + .channelInitializer({ $0.pipeline.add(handler: countingHandler) }) .connect(to: serverChannel.localAddress!).wait() defer { @@ -460,13 +459,13 @@ class EchoServerClientTest : XCTestCase { channelInactivePromise: inactivePromise) let serverChannel = try ServerBootstrap(group: group) - .option(option: ChannelOptions.Socket(SocketOptionLevel(SOL_SOCKET), SO_REUSEADDR), value: 1) + .serverChannelOption(ChannelOptions.Socket(SocketOptionLevel(SOL_SOCKET), SO_REUSEADDR), value: 1) // Set the handlers that are appled to the accepted Channels - .handler(childHandler: ChannelInitializer(initChannel: { channel in - // Ensure we not read faster then we can write by adding the BackPressureHandler into the pipeline. + .childChannelInitializer { channel in + // Ensure we don't read faster then we can write by adding the BackPressureHandler into the pipeline. return channel.pipeline.add(handler: handler) - })).bind(to: "127.0.0.1", on: 0).wait() + }.bind(to: "127.0.0.1", on: 0).wait() defer { _ = try! serverChannel.close().wait() @@ -494,11 +493,11 @@ class EchoServerClientTest : XCTestCase { let bytesReceivedPromise: EventLoopPromise = group.next().newPromise() let byteCountingHandler = ByteCountingHandler(numBytes: writingBytes.utf8.count, promise: bytesReceivedPromise) let serverChannel = try ServerBootstrap(group: group) - .option(option: ChannelOptions.Socket(SocketOptionLevel(SOL_SOCKET), SO_REUSEADDR), value: 1) - .handler(childHandler: ChannelInitializer(initChannel: { channel in - // Ensure we not read faster then we can write by adding the BackPressureHandler into the pipeline. + .serverChannelOption(ChannelOptions.Socket(SocketOptionLevel(SOL_SOCKET), SO_REUSEADDR), value: 1) + .childChannelInitializer { channel in + // Ensure we don't read faster then we can write by adding the BackPressureHandler into the pipeline. return channel.pipeline.add(handler: byteCountingHandler) - })).bind(to: "127.0.0.1", on: 0).wait() + }.bind(to: "127.0.0.1", on: 0).wait() defer { _ = try! serverChannel.close().wait() @@ -539,10 +538,10 @@ class EchoServerClientTest : XCTestCase { } let serverChannel = try ServerBootstrap(group: group) - .option(option: ChannelOptions.Socket(SocketOptionLevel(SOL_SOCKET), SO_REUSEADDR), value: 1) - .handler(childHandler: ChannelInitializer(initChannel: { channel in + .serverChannelOption(ChannelOptions.Socket(SocketOptionLevel(SOL_SOCKET), SO_REUSEADDR), value: 1) + .childChannelInitializer { channel in return channel.pipeline.add(handler: EchoServer()) - })).bind(to: "127.0.0.1", on: 0).wait() + }.bind(to: "127.0.0.1", on: 0).wait() defer { _ = try! serverChannel.close().wait() @@ -551,11 +550,11 @@ class EchoServerClientTest : XCTestCase { let stringToWrite = "hello" let promise: EventLoopPromise = group.next().newPromise() let clientChannel = try ClientBootstrap(group: group) - .handler(handler: ChannelInitializer(initChannel: { channel in + .channelInitializer { channel in return channel.pipeline.add(handler: WriteOnConnectHandler(toWrite: stringToWrite)).then { v2 in return channel.pipeline.add(handler: ByteCountingHandler(numBytes: stringToWrite.utf8.count, promise: promise)) } - })) + } .connect(to: serverChannel.localAddress!).wait() defer { _ = clientChannel.close() @@ -573,10 +572,10 @@ class EchoServerClientTest : XCTestCase { let stringToWrite = "hello" let serverChannel = try ServerBootstrap(group: group) - .option(option: ChannelOptions.Socket(SocketOptionLevel(SOL_SOCKET), SO_REUSEADDR), value: 1) - .handler(childHandler: ChannelInitializer(initChannel: { channel in + .serverChannelOption(ChannelOptions.Socket(SocketOptionLevel(SOL_SOCKET), SO_REUSEADDR), value: 1) + .childChannelInitializer { channel in return channel.pipeline.add(handler: WriteOnConnectHandler(toWrite: stringToWrite)) - })).bind(to: "127.0.0.1", on: 0).wait() + }.bind(to: "127.0.0.1", on: 0).wait() defer { _ = try! serverChannel.close().wait() @@ -584,9 +583,9 @@ class EchoServerClientTest : XCTestCase { let promise: EventLoopPromise = group.next().newPromise() let clientChannel = try ClientBootstrap(group: group) - .handler(handler: ChannelInitializer(initChannel: { channel in + .channelInitializer { channel in return channel.pipeline.add(handler: ByteCountingHandler(numBytes: stringToWrite.utf8.count, promise: promise)) - })) + } .connect(to: serverChannel.localAddress!).wait() defer { @@ -603,13 +602,12 @@ class EchoServerClientTest : XCTestCase { dpGroup.enter() let serverChannel = try ServerBootstrap(group: group) - .option(option: ChannelOptions.Socket(SocketOptionLevel(SOL_SOCKET), SO_REUSEADDR), value: 1) - .handler(childHandler: ChannelInitializer(initChannel: { channel in + .serverChannelOption(ChannelOptions.Socket(SocketOptionLevel(SOL_SOCKET), SO_REUSEADDR), value: 1) + .childChannelInitializer { channel in return channel.pipeline.add(handler: EchoAndEchoAgainAfterSomeTimeServer(time: .seconds(1), secondWriteDoneHandler: { dpGroup.leave() - })) - })).bind(to: "127.0.0.1", on: 0).wait() + }.bind(to: "127.0.0.1", on: 0).wait() defer { _ = serverChannel.close() @@ -618,7 +616,7 @@ class EchoServerClientTest : XCTestCase { let str = "hi there" let countingHandler = ByteCountingHandler(numBytes: str.utf8.count, promise: group.next().newPromise()) let clientChannel = try ClientBootstrap(group: group) - .handler(handler: countingHandler) + .channelInitializer({ $0.pipeline.add(handler: countingHandler) }) .connect(to: serverChannel.localAddress!).wait() var buffer = clientChannel.allocator.buffer(capacity: str.utf8.count) diff --git a/Tests/NIOTests/EventLoopTest.swift b/Tests/NIOTests/EventLoopTest.swift index 7df9aa765..326beee57 100644 --- a/Tests/NIOTests/EventLoopTest.swift +++ b/Tests/NIOTests/EventLoopTest.swift @@ -43,7 +43,7 @@ public class EventLoopTest : XCTestCase { // First, we create a server and client channel, but don't connect them. let serverChannel = try ServerBootstrap(group: eventLoopGroup) - .option(option: ChannelOptions.Socket(SocketOptionLevel(SOL_SOCKET), SO_REUSEADDR), value: 1) + .serverChannelOption(ChannelOptions.Socket(SocketOptionLevel(SOL_SOCKET), SO_REUSEADDR), value: 1) .bind(to: "127.0.0.1", on: 0).wait() let clientBootstrap = ClientBootstrap(group: eventLoopGroup) @@ -100,7 +100,7 @@ public class EventLoopTest : XCTestCase { // Create a server channel. let serverChannel = try ServerBootstrap(group: group) - .option(option: ChannelOptions.Socket(SocketOptionLevel(SOL_SOCKET), SO_REUSEADDR), value: 1) + .serverChannelOption(ChannelOptions.Socket(SocketOptionLevel(SOL_SOCKET), SO_REUSEADDR), value: 1) .bind(to: "127.0.0.1", on: 0).wait() // We now want to connect to it. To try to slow this stuff down, we're going to use a multiple of the number diff --git a/Tests/NIOTests/FileRegionTest.swift b/Tests/NIOTests/FileRegionTest.swift index 83b68c2ad..971654485 100644 --- a/Tests/NIOTests/FileRegionTest.swift +++ b/Tests/NIOTests/FileRegionTest.swift @@ -49,8 +49,8 @@ class FileRegionTest : XCTestCase { let countingHandler = ByteCountingHandler(numBytes: bytes.count, promise: group.next().newPromise()) let serverChannel = try ServerBootstrap(group: group) - .option(option: ChannelOptions.Socket(SocketOptionLevel(SOL_SOCKET), SO_REUSEADDR), value: 1) - .handler(childHandler: countingHandler).bind(to: "127.0.0.1", on: 0).wait() + .serverChannelOption(ChannelOptions.Socket(SocketOptionLevel(SOL_SOCKET), SO_REUSEADDR), value: 1) + .childChannelInitializer({ $0.pipeline.add(handler: countingHandler) }).bind(to: "127.0.0.1", on: 0).wait() defer { _ = serverChannel.close() @@ -84,8 +84,8 @@ class FileRegionTest : XCTestCase { let countingHandler = ByteCountingHandler(numBytes: 0, promise: group.next().newPromise()) let serverChannel = try ServerBootstrap(group: group) - .option(option: ChannelOptions.Socket(SocketOptionLevel(SOL_SOCKET), SO_REUSEADDR), value: 1) - .handler(childHandler: countingHandler).bind(to: "127.0.0.1", on: 0).wait() + .serverChannelOption(ChannelOptions.Socket(SocketOptionLevel(SOL_SOCKET), SO_REUSEADDR), value: 1) + .childChannelInitializer({ $0.pipeline.add(handler: countingHandler) }).bind(to: "127.0.0.1", on: 0).wait() defer { _ = serverChannel.close() @@ -162,8 +162,8 @@ class FileRegionTest : XCTestCase { let countingHandler = ByteCountingHandler(numBytes: bytes.count, promise: group.next().newPromise()) let serverChannel = try ServerBootstrap(group: group) - .option(option: ChannelOptions.Socket(SocketOptionLevel(SOL_SOCKET), SO_REUSEADDR), value: 1) - .handler(childHandler: countingHandler).bind(to: "127.0.0.1", on: 0).wait() + .serverChannelOption(ChannelOptions.Socket(SocketOptionLevel(SOL_SOCKET), SO_REUSEADDR), value: 1) + .childChannelInitializer({ $0.pipeline.add(handler: countingHandler) }).bind(to: "127.0.0.1", on: 0).wait() defer { _ = serverChannel.close() diff --git a/Tests/NIOTests/IdleStateHandlerTest.swift b/Tests/NIOTests/IdleStateHandlerTest.swift index 4314290c1..0cb338072 100644 --- a/Tests/NIOTests/IdleStateHandlerTest.swift +++ b/Tests/NIOTests/IdleStateHandlerTest.swift @@ -76,12 +76,12 @@ class IdleStateHandlerTest : XCTestCase { } let serverChannel = try ServerBootstrap(group: group) - .option(option: ChannelOptions.Socket(SocketOptionLevel(SOL_SOCKET), SO_REUSEADDR), value: 1) - .handler(childHandler: ChannelInitializer(initChannel: { channel in + .serverChannelOption(ChannelOptions.Socket(SocketOptionLevel(SOL_SOCKET), SO_REUSEADDR), value: 1) + .childChannelInitializer { channel in channel.pipeline.add(handler: handler).then(callback: { f in channel.pipeline.add(handler: TestWriteHandler(writeToChannel, assertEventFn)) }) - })).bind(to: "127.0.0.1", on: 0).wait() + }.bind(to: "127.0.0.1", on: 0).wait() defer { _ = serverChannel.close()