NIOAny

    NIOAny is an opaque container for values of any type, similar to Swift’s builtin Any type. Contrary to Any the overhead of NIOAny depends on the the type of the wrapped value. Certain types that are important for the performance of a SwiftNIO application like ByteBuffer, FileRegion and AddressEnvelope<ByteBuffer> can be expected to be wrapped almost without overhead. All others will have similar performance as if they were passed as an Any as NIOAny just like Any will contain them within an existential container.

    NIOAny.swift:45
    struct NIOAny

    The most important use-cases for NIOAny are values travelling through the ChannelPipeline whose type can’t be calculated at compile time. For example:

    • the channelRead of any ChannelInboundHandler

    • the write method of a ChannelOutboundHandler

    The abstraction that delivers a NIOAny to user code must provide a mechanism to unwrap a NIOAny as a certain type known at run-time. Canonical example:

    class SandwichHandler: ChannelInboundHandler {
        typealias InboundIn = Bacon /* we expected to be delivered `Bacon` ... */
        typealias InboundOut = Sandwich /* ... and we will make and deliver a `Sandwich` from that */
    
        func channelRead(context: ChannelHandlerContext, data: NIOAny) {
             /* we receive the `Bacon` as a `NIOAny` as at compile-time the exact configuration of the channel
                pipeline can't be computed. The pipeline can't be computed at compile time as it can change
                dynamically at run-time. Yet, we assert that in any configuration the channel handler before
                `SandwichHandler` does actually send us a stream of `Bacon`.
             */
             let bacon = Self.unwrapInboundIn(data) /* `Bacon` or crash */
             let sandwich = makeSandwich(bacon)
             context.fireChannelRead(Self.wrapInboundOut(sandwich)) /* as promised we deliver a wrapped `Sandwich` */
        }
    }

    See also

    View members

    Hide members

    This section is hidden by default because it contains too many (12) members.

    • protocol Channel

      A Channel is easiest thought of as a network socket. But it can be anything that is capable of I/O operations such as read, write, connect, and bind.

    • protocol MulticastChannel

      A MulticastChannel is a Channel that supports IP multicast operations: that is, a channel that can join multicast groups.

    • protocol ChannelHandler

      Base protocol for handlers that handle I/O events or intercept an I/O operation.

    • protocol ChannelOutboundHandler

      ChannelHandler which handles outbound I/O events or intercept an outbound I/O operation for a Channel.

    • protocol ChannelInboundHandler

      ChannelHandler which handles inbound I/O events for a Channel.

    • typealias ChannelDuplexHandler

      A combination of ChannelInboundHandler and ChannelOutboundHandler.

    • class ChannelHandlerContext

      Every ChannelHandler has – when added to a ChannelPipeline – a corresponding ChannelHandlerContext which is the way ChannelHandlers can interact with other ChannelHandlers in the pipeline.

    • class ChannelPipeline

      A list of ChannelHandlers that handle or intercept inbound events and outbound operations of a Channel. ChannelPipeline implements an advanced form of the Intercepting Filter pattern to give a user full control over how an event is handled and how the ChannelHandlers in a pipeline interact with each other.

    • protocol RemovableChannelHandler

      A RemovableChannelHandler is a ChannelHandler that can be dynamically removed from a ChannelPipeline whilst the Channel is operating normally. A RemovableChannelHandler is required to remove itself from the ChannelPipeline (using ChannelHandlerContext.leavePipeline) as soon as possible.

    • enum ChannelEvent

      An Channel related event that is passed through the ChannelPipeline to notify the user.

    • enum CloseMode

      Specify what kind of close operation is requested.

    • struct ChannelShouldQuiesceEvent

      A Channel user event that is sent when the Channel has been asked to quiesce.

    Citizens in NIOCore

    Conformances

    Type members

    • init<T>(T)

      Wrap a value in a NIOAny. In most cases you should not create a NIOAny directly using this constructor. The abstraction that accepts values of type NIOAny must also provide a mechanism to do the wrapping. An example is a ChannelInboundHandler which provides Self.wrapInboundOut(aValueOfTypeInboundOut).

    Instance members