ChannelHandlerContext
public final class ChannelHandlerContext : ChannelInvoker
Every ChannelHandler
has – when added to a ChannelPipeline
– a corresponding ChannelHandlerContext
which is
the way ChannelHandler
s can interact with other ChannelHandler
s in the pipeline.
Most ChannelHandler
s need to send events through the ChannelPipeline
which they do by calling the respective
method on their ChannelHandlerContext
. In fact all the ChannelHandler
default implementations just forward
the event using the ChannelHandlerContext
.
Many events are instrumental for a ChannelHandler
‘s life-cycle and it is therefore very important to send them
at the right point in time. Often, the right behaviour is to react to an event and then forward it to the next
ChannelHandler
.
-
Undocumented
Declaration
Swift
public let pipeline: ChannelPipeline
-
Undocumented
Declaration
Swift
public var channel: Channel { get }
-
Undocumented
Declaration
Swift
public var handler: ChannelHandler { get }
-
Undocumented
Declaration
Swift
public var remoteAddress: SocketAddress? { get }
-
Undocumented
Declaration
Swift
public var localAddress: SocketAddress? { get }
-
Declaration
Swift
public var eventLoop: EventLoop { get }
-
Undocumented
Declaration
Swift
public let name: String
-
Send a
channelRegistered
event to the next (inbound)ChannelHandler
in theChannelPipeline
.Note
For correct operation it is very important to forward anychannelRegistered
event using this method at the right point in time, that is usually when received.Declaration
Swift
public func fireChannelRegistered()
-
Send a
channelUnregistered
event to the next (inbound)ChannelHandler
in theChannelPipeline
.Note
For correct operation it is very important to forward anychannelUnregistered
event using this method at the right point in time, that is usually when received.Declaration
Swift
public func fireChannelUnregistered()
-
Send a
channelActive
event to the next (inbound)ChannelHandler
in theChannelPipeline
.Note
For correct operation it is very important to forward anychannelActive
event using this method at the right point in time, that is often when received.Declaration
Swift
public func fireChannelActive()
-
Send a
channelInactive
event to the next (inbound)ChannelHandler
in theChannelPipeline
.Note
For correct operation it is very important to forward anychannelInactive
event using this method at the right point in time, that is often when received.Declaration
Swift
public func fireChannelInactive()
-
Send data to the next inbound
ChannelHandler
. The data should be of typeChannelInboundHandler.InboundOut
.Declaration
Swift
public func fireChannelRead(_ data: NIOAny)
-
Signal to the next
ChannelHandler
that a read burst has finished.Declaration
Swift
public func fireChannelReadComplete()
-
Send a
writabilityChanged
event to the next (inbound)ChannelHandler
in theChannelPipeline
.Note
For correct operation it is very important to forward anywritabilityChanged
event using this method at the right point in time, that is usually when received.Declaration
Swift
public func fireChannelWritabilityChanged()
-
Send an error to the next inbound
ChannelHandler
.Declaration
Swift
public func fireErrorCaught(_ error: Error)
-
Send a user event to the next inbound
ChannelHandler
.Declaration
Swift
public func fireUserInboundEventTriggered(_ event: Any)
-
Send a
register
event to the next (outbound)ChannelHandler
in theChannelPipeline
.Note
For correct operation it is very important to forward anyregister
event using this method at the right point in time, that is usually when received.Declaration
Swift
public func register(promise: EventLoopPromise<Void>?)
-
Send a
bind
event to the next outboundChannelHandler
in theChannelPipeline
. When thebind
event reaches theHeadChannelHandler
aServerSocketChannel
will be bound.Declaration
Swift
public func bind(to address: SocketAddress, promise: EventLoopPromise<Void>?)
Parameters
address
The address to bind to.
promise
The promise fulfilled when the socket is bound or failed if it cannot be bound.
-
Send a
connect
event to the next outboundChannelHandler
in theChannelPipeline
. When theconnect
event reaches theHeadChannelHandler
aSocketChannel
will be connected.Declaration
Swift
public func connect(to address: SocketAddress, promise: EventLoopPromise<Void>?)
Parameters
address
The address to connect to.
promise
The promise fulfilled when the socket is connected or failed if it cannot be connected.
-
Send a
write
event to the next outboundChannelHandler
in theChannelPipeline
. When thewrite
event reaches theHeadChannelHandler
the data will be enqueued to be written on the nextflush
event.Declaration
Swift
public func write(_ data: NIOAny, promise: EventLoopPromise<Void>?)
Parameters
data
The data to write, should be of type
ChannelOutboundHandler.OutboundOut
.promise
The promise fulfilled when the data has been written or failed if it cannot be written.
-
Send a
flush
event to the next outboundChannelHandler
in theChannelPipeline
. When theflush
event reaches theHeadChannelHandler
the data previously enqueued will be attempted to be written to the socket.Declaration
Swift
public func flush()
Parameters
promise
The promise fulfilled when the previously written data been flushed or failed if it cannot be flushed.
-
Send a
write
event followed by aflush
event to the next outboundChannelHandler
in theChannelPipeline
. When thewrite
event reaches theHeadChannelHandler
the data will be enqueued to be written when theflush
also reaches theHeadChannelHandler
.Declaration
Swift
public func writeAndFlush(_ data: NIOAny, promise: EventLoopPromise<Void>?)
Parameters
data
The data to write, should be of type
ChannelOutboundHandler.OutboundOut
.promise
The promise fulfilled when the previously written data been written and flushed or if that failed.
-
Send a
read
event to the next outboundChannelHandler
in theChannelPipeline
. When theread
event reaches theHeadChannelHandler
the interest to read data will be signalled to theSelector
. This will subsequently – when data becomes readable – causechannelRead
events containing the data being sent through theChannelPipeline
.Declaration
Swift
public func read()
-
Send a
close
event to the next outboundChannelHandler
in theChannelPipeline
. When theclose
event reaches theHeadChannelHandler
the socket will be closed.Declaration
Swift
public func close(mode: CloseMode = .all, promise: EventLoopPromise<Void>?)
-
Send a user event to the next outbound
ChannelHandler
in theChannelPipeline
.Declaration
Swift
public func triggerUserOutboundEvent(_ event: Any, promise: EventLoopPromise<Void>?)
Parameters
event
The user event to send.
promise
The promise fulfilled when the user event has been sent or failed if it couldn’t be sent.
-
A
RemovalToken
is handed to aRemovableChannelHandler
when itsremoveHandler
function is invoked. ARemovableChannelHandler
is then required to remove itself from theChannelPipeline
. The removal process is finalized by handing theRemovalToken
to theChannelHandlerContext.leavePipeline
function.Declaration
Swift
public struct RemovalToken
-
Synchronously remove the
ChannelHandler
with the givenChannelHandlerContext
.Note
This function must only be used from a
RemovableChannelHandler
to remove itself. Calling this method on any otherChannelHandlerContext
leads to undefined behaviour.Declaration
Swift
public func leavePipeline(removalToken: RemovalToken)
Parameters
removalToken
The removal token received from
RemovableChannelHandler.removeHandler