Structureswift-nio 2.72.0NIOCore
NIOClientTCPBootstrap
NIOClientTCPBootstrap
is a bootstrap that allows you to bootstrap client TCP connections using NIO on BSD Sockets, NIO Transport Services, or other ways.
struct NIOClientTCPBootstrap
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()
See also
protocol NIOClientTCPBootstrapProtocol
NIOClientTCPBootstrapProtocol
is implemented by various underlying transport mechanisms. Typically, this will be the BSD Sockets API implemented byClientBootstrap
.protocol NIOClientTLSProvider
struct NIOInsecureNoTLS<Bootstrap>
Citizens in NIOCore
Type members
init<Bootstrap, TLS>(Bootstrap, tls: TLS
) Initialize a
NIOClientTCPBootstrap
using the underlyingBootstrap
alongside a compatibleTLS
implementation.
Instance members
let underlyingBootstrap: NIOClientTCPBootstrapProtocol
func channelConvenienceOptions(ChannelOptions.TCPConvenienceOptions
) -> 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.func channelInitializer(@escaping (Channel) -> EventLoopFuture<Void>
) -> NIOClientTCPBootstrap Initialize the connected
SocketChannel
withinitializer
. The most common task in initializer is to addChannelHandler
s to theChannelPipeline
.func channelOption<Option>(Option, value: Option.Value
) -> NIOClientTCPBootstrap Specifies a
ChannelOption
to be applied to theSocketChannel
.func connect(host: String, port: Int
) -> EventLoopFuture<Channel> Specify the
host
andport
to connect to for the TCPChannel
that will be established.func connect(to: SocketAddress
) -> EventLoopFuture<Channel> Specify the
address
to connect to for the TCPChannel
that will be established.func connect(unixDomainSocketPath: String
) -> EventLoopFuture<Channel> Specify the
unixDomainSocket
path to connect to for the UDSChannel
that will be established.func connectTimeout(TimeAmount
) -> NIOClientTCPBootstrap func enableTLS(
) -> NIOClientTCPBootstrap