Channel

public protocol Channel : AnyObject, ChannelOutboundInvoker

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.

Note

All operations on Channel are thread-safe.

In SwiftNIO, all I/O operations are asynchronous and hence all operations on Channel are asynchronous too. This means that all I/O operations will return immediately, usually before the work has been completed. The EventLoopPromises passed to or returned by the operations are used to retrieve the result of an operation after it has completed.

A Channel owns its ChannelPipeline which handles all I/O events and requests associated with the Channel.

  • The Channel‘s ByteBuffer allocator. This is the only supported way of allocating ByteBuffers to be used with this Channel.

    Declaration

    Swift

    var allocator: ByteBufferAllocator { get }
  • The closeFuture will fire when the Channel has been closed.

    Declaration

    Swift

    var closeFuture: EventLoopFuture<Void> { get }
  • The ChannelPipeline which handles all I/O events and requests associated with this Channel.

    Declaration

    Swift

    var pipeline: ChannelPipeline { get }
  • The local SocketAddress.

    Declaration

    Swift

    var localAddress: SocketAddress? { get }
  • The remote peer’s SocketAddress.

    Declaration

    Swift

    var remoteAddress: SocketAddress? { get }
  • Channels are hierarchical and might have a parent Channel. Channel hierarchies are in use for certain protocols such as HTTP/2.

    Declaration

    Swift

    var parent: Channel? { get }
  • Set option to value on this Channel.

    Declaration

    Swift

    func setOption<T>(option: T, value: T.OptionType) -> EventLoopFuture<Void> where T : ChannelOption
  • Get the value of option for this Channel.

    Declaration

    Swift

    func getOption<T>(option: T) -> EventLoopFuture<T.OptionType> where T : ChannelOption
  • Returns if this Channel is currently writable.

    Declaration

    Swift

    var isWritable: Bool { get }
  • Returns if this Channel is currently active. Active is defined as the period of time after the channelActive and before channelInactive has fired. The main use for this is to know if channelActive or channelInactive can be expected next when handlerAdded was received.

    Declaration

    Swift

    var isActive: Bool { get }
  • Reach out to the ChannelCore.

    Warning

    Unsafe, this is for use in NIO’s core only.

    Declaration

    Swift

    var _unsafe: ChannelCore { get }
  • write(_:) Extension method

    Write data into the Channel, automatically wrapping with NIOAny.

    Seealso

    ChannelOutboundInvoker.write.

    Declaration

    Swift

    func write<T>(_ any: T) -> EventLoopFuture<Void>
  • write(_:promise:) Extension method

    Write data into the Channel, automatically wrapping with NIOAny.

    Seealso

    ChannelOutboundInvoker.write.

    Declaration

    Swift

    func write<T>(_ any: T, promise: EventLoopPromise<Void>?)
  • writeAndFlush(_:) Extension method

    Write and flush data into the Channel, automatically wrapping with NIOAny.

    Seealso

    ChannelOutboundInvoker.writeAndFlush.

    Declaration

    Swift

    func writeAndFlush<T>(_ any: T) -> EventLoopFuture<Void>
  • writeAndFlush(_:promise:) Extension method

    Write and flush data into the Channel, automatically wrapping with NIOAny.

    Seealso

    ChannelOutboundInvoker.writeAndFlush.

    Declaration

    Swift

    func writeAndFlush<T>(_ any: T, promise: EventLoopPromise<Void>?)