NIOClientTCPBootstrap
public struct NIOClientTCPBootstrap
NIOClientTCPBootstrap
is a bootstrap that allows you to bootstrap client TCP connections using NIO on BSD Sockets,
NIO Transport Services, or other ways.
Usually, to bootstrap a connection with SwiftNIO, you have to match the right EventLoopGroup
, the right bootstrap,
and the right TLS implementation. Typical choices involve:
MultiThreadedEventLoopGroup
,ClientBootstrap
, andNIOSSLClientHandler
(fromswift-nio-ssl
) for NIO on BSD sockets.NIOTSEventLoopGroup
,NIOTSConnectionBootstrap
, and the Network.framework TLS implementation (all fromswift-nio-transport-services
.
Bootstrapping connections that way works but is quite tedious for packages that support multiple ways of
bootstrapping. The idea behind NIOClientTCPBootstrap
is to do all configuration in one place (when you initialize
a NIOClientTCPBootstrap
) and then have a common API that works for all use-cases.
Example:
// This function combines the right pieces and returns you a "universal client bootstrap"
// (`NIOClientTCPBootstrap`). This allows you to bootstrap connections (with or without TLS) using either the
// NIO on sockets (`NIO`) or NIO on Network.framework (`NIOTransportServices`) stacks.
// The remainder of the code should be platform-independent.
func makeUniversalBootstrap(serverHostname: String) throws -> (NIOClientTCPBootstrap, EventLoopGroup) {
func useNIOOnSockets() throws -> (NIOClientTCPBootstrap, EventLoopGroup) {
let group = MultiThreadedEventLoopGroup(numberOfThreads: 1)
let sslContext = try NIOSSLContext(configuration: TLSConfiguration.forClient())
let bootstrap = try NIOClientTCPBootstrap(ClientBootstrap(group: group),
tls: NIOSSLClientTLSProvider(context: sslContext,
serverHostname: serverHostname))
return (bootstrap, group)
}
#if canImport(Network)
if #available(macOS 10.14, iOS 12, tvOS 12, watchOS 3, *) {
// We run on a new-enough Darwin so we can use Network.framework
let group = NIOTSEventLoopGroup()
let bootstrap = NIOClientTCPBootstrap(NIOTSConnectionBootstrap(group: group),
tls: NIOTSClientTLSProvider())
return (bootstrap, group)
} else {
// We're on Darwin but not new enough for Network.framework, so we fall back on NIO on BSD sockets.
return try useNIOOnSockets()
}
#else
// We are on a non-Darwin platform, so we'll use BSD sockets.
return try useNIOOnSockets()
#endif
}
let (bootstrap, group) = try makeUniversalBootstrap(serverHostname: "example.com")
let connection = try bootstrap
.channelInitializer { channel in
channel.pipeline.addHandler(PrintEverythingHandler { _ in })
}
.enableTLS()
.connect(host: "example.com", port: 443)
.wait()
-
Undocumented
Declaration
Swift
public let underlyingBootstrap: NIOClientTCPBootstrapProtocol
-
Initialize a
NIOClientTCPBootstrap
using the underlyingBootstrap
alongside a compatibleTLS
implementation.Note
If you do not require
TLS
, you can useNIOInsecureNoTLS
which supports only plain-text connections. We highly recommend to always use TLS.Declaration
Swift
public init<Bootstrap: NIOClientTCPBootstrapProtocol, TLS: NIOClientTLSProvider>(_ bootstrap: Bootstrap, tls: TLS) where TLS.Bootstrap == Bootstrap
Parameters
bootstrap
The underlying bootstrap to use.
tls
The TLS implementation to use, needs to be compatible with
Bootstrap
. -
Initialize the connected
SocketChannel
withinitializer
. The most common task in initializer is to addChannelHandler
s to theChannelPipeline
.The connected
Channel
will operate onByteBuffer
as inbound andIOData
as outbound messages.Warning
The
handler
closure may be invoked multiple times so it’s usually the right choice to instantiateChannelHandler
s withinhandler
. The reasonhandler
may be invoked multiple times is that to successfully set up a connection multiple connections might be setup in the process. Assuming a hostname that resolves to both IPv4 and IPv6 addresses, NIO will follow Happy Eyeballs and race both an IPv4 and an IPv6 connection. It is possible that both connections get fully established before the IPv4 connection will be closed again because the IPv6 connection ‘won the race’. Therefore thechannelInitializer
might be called multiple times and it’s important not to share statefulChannelHandler
s in more than oneChannel
.Declaration
Swift
public func channelInitializer(_ handler: @escaping (Channel) -> EventLoopFuture<Void>) -> NIOClientTCPBootstrap
Parameters
handler
A closure that initializes the provided
Channel
. -
Specifies a
ChannelOption
to be applied to theSocketChannel
.Declaration
Swift
public func channelOption<Option>(_ option: Option, value: Option.Value) -> NIOClientTCPBootstrap where Option : ChannelOption
Parameters
option
The option to be applied.
value
The value for the option.
-
Declaration
Swift
public func connectTimeout(_ timeout: TimeAmount) -> NIOClientTCPBootstrap
Parameters
timeout
The timeout that will apply to the connection attempt.
-
Specify the
host
andport
to connect to for the TCPChannel
that will be established.Declaration
Swift
public func connect(host: String, port: Int) -> EventLoopFuture<Channel>
Parameters
host
The host to connect to.
port
The port to connect to.
Return Value
An
EventLoopFuture<Channel>
to deliver theChannel
when connected. -
Specify the
address
to connect to for the TCPChannel
that will be established.Declaration
Swift
public func connect(to address: SocketAddress) -> EventLoopFuture<Channel>
Parameters
address
The address to connect to.
Return Value
An
EventLoopFuture<Channel>
to deliver theChannel
when connected. -
Specify the
unixDomainSocket
path to connect to for the UDSChannel
that will be established.Declaration
Swift
public func connect(unixDomainSocketPath: String) -> EventLoopFuture<Channel>
Parameters
unixDomainSocketPath
The Unix domain socket path to connect to.
Return Value
An
EventLoopFuture<Channel>
to deliver theChannel
when connected. -
Undocumented
Declaration
Swift
@discardableResult public func enableTLS() -> NIOClientTCPBootstrap
-
Specifies some
TCPConvenienceOption
s to be applied to the channel. These are preferred over regular channel options as they are easier to use and restrict options to those which a normal user would consider.Declaration
Swift
public func channelConvenienceOptions(_ options: ChannelOptions.TCPConvenienceOptions) -> NIOClientTCPBootstrap
Parameters
options
Set of convenience options to apply.
Return Value
The updated bootstrap (
self
being mutated)