ServerBootstrap

public final class ServerBootstrap

A ServerBootstrap is an easy way to bootstrap a ServerSocketChannel when creating network servers.

Example:

    let group = MultiThreadedEventLoopGroup(numberOfThreads: System.coreCount)
    let bootstrap = ServerBootstrap(group: group)
        // Specify backlog and enable SO_REUSEADDR for the server itself
        .serverChannelOption(ChannelOptions.backlog, value: 256)
        .serverChannelOption(ChannelOptions.socket(SocketOptionLevel(SOL_SOCKET), SO_REUSEADDR), value: 1)

        // Set the handlers that are applied to the accepted child `Channel`s.
        .childChannelInitializer { channel in
            // Ensure we don't read faster then we can write by adding the BackPressureHandler into the pipeline.
            channel.pipeline.add(handler: BackPressureHandler()).then { () in
                // make sure to instantiate your `ChannelHandlers` inside of
                // the closure as it will be invoked once per connection.
                channel.pipeline.add(handler: MyChannelHandler())
            }
        }

        // Enable TCP_NODELAY and SO_REUSEADDR for the accepted Channels
        .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: AdaptiveRecvByteBufferAllocator())
    defer {
        try! group.syncShutdownGracefully()
    }
    try! bootstrap.bind(host: host, port: port).wait()
    /* the server will now be accepting connections */

    try! channel.closeFuture.wait() // wait forever as we never close the Channel

The EventLoopFuture returned by bind will fire with a ServerSocketChannel. This is the channel that owns the listening socket. Each time it accepts a new connection it will fire a SocketChannel through the ChannelPipeline via fireChannelRead: as a result, the ServerSocketChannel operates on Channels as inbound messages. Outbound messages are not supported on a ServerSocketChannel which means that each write attempt will fail.

Accepted SocketChannels operate on ByteBuffer as inbound data, and IOData as outbound data.

  • Create a ServerBootstrap for the EventLoopGroup group.

    Declaration

    Swift

    public convenience init(group: EventLoopGroup)

    Parameters

    group

    The EventLoopGroup to use for the ServerSocketChannel.

  • Create a ServerBootstrap.

    Declaration

    Swift

    public init(group: EventLoopGroup, childGroup: EventLoopGroup)

    Parameters

    group

    The EventLoopGroup to use for the bind of the ServerSocketChannel and to accept new SocketChannels with.

    childGroup

    The EventLoopGroup to run the accepted SocketChannels on.

  • Initialize the ServerSocketChannel with initializer. The most common task in initializer is to add ChannelHandlers to the ChannelPipeline.

    The ServerSocketChannel uses the accepted Channels as inbound messages.

    Note

    To set the initializer for the accepted SocketChannels, look at ServerBootstrap.childChannelInitializer.

    Declaration

    Swift

    public func serverChannelInitializer(_ initializer: @escaping (Channel) -> EventLoopFuture<Void>) -> Self

    Parameters

    initializer

    A closure that initializes the provided Channel.

  • Initialize the accepted SocketChannels with initializer. The most common task in initializer is to add ChannelHandlers to the ChannelPipeline.

    Warning

    The initializer will be invoked once for every accepted connection. Therefore it’s usually the right choice to instantiate stateful ChannelHandlers within the closure to make sure they are not accidentally shared across Channels. There are expert use-cases where stateful handler need to be shared across Channels in which case the user is responsible to synchronise the state access appropriately.

    The accepted Channel will operate on ByteBuffer as inbound and IOData as outbound messages.

    Declaration

    Swift

    public func childChannelInitializer(_ initializer: @escaping (Channel) -> EventLoopFuture<Void>) -> Self

    Parameters

    initializer

    A closure that initializes the provided Channel.

  • Specifies a ChannelOption to be applied to the ServerSocketChannel.

    Note

    To specify options for the accepted SocketChannels, look at ServerBootstrap.childChannelOption.

    Declaration

    Swift

    public func serverChannelOption<T>(_ option: T, value: T.OptionType) -> Self where T : ChannelOption

    Parameters

    option

    The option to be applied.

    value

    The value for the option.

  • Specifies a ChannelOption to be applied to the accepted SocketChannels.

    Declaration

    Swift

    public func childChannelOption<T>(_ option: T, value: T.OptionType) -> Self where T : ChannelOption

    Parameters

    option

    The option to be applied.

    value

    The value for the option.

  • Bind the ServerSocketChannel to host and port.

    Declaration

    Swift

    public func bind(host: String, port: Int) -> EventLoopFuture<Channel>

    Parameters

    host

    The host to bind on.

    port

    The port to bind on.

  • Bind the ServerSocketChannel to address.

    Declaration

    Swift

    public func bind(to address: SocketAddress) -> EventLoopFuture<Channel>

    Parameters

    address

    The SocketAddress to bind on.

  • Bind the ServerSocketChannel to a UNIX Domain Socket.

    Declaration

    Swift

    public func bind(unixDomainSocketPath: String) -> EventLoopFuture<Channel>

    Parameters

    unixDomainSocketPath

    The Unix domain socket path to bind to. unixDomainSocketPath must not exist, it will be created by the system.

  • Use the existing bound socket file descriptor.

    Declaration

    Swift

    public func withBoundSocket(descriptor: CInt) -> EventLoopFuture<Channel>

    Parameters

    descriptor

    The Unix file descriptor representing the bound stream socket.