public interface Channel extends AttributeMap, ChannelOutboundInvoker, Comparable<Channel>
A channel provides a user:
ChannelPipeline
which handles all I/O events and requests
associated with the channel.
All I/O operations in Netty are asynchronous. It means any I/O calls will
return immediately with no guarantee that the requested I/O operation has
been completed at the end of the call. Instead, you will be returned with
a ChannelFuture
instance which will notify you when the requested I/O
operation has succeeded, failed, or canceled.
A Channel
can have a parent depending on
how it was created. For instance, a SocketChannel
, that was accepted
by ServerSocketChannel
, will return the ServerSocketChannel
as its parent on parent()
.
The semantics of the hierarchical structure depends on the transport
implementation where the Channel
belongs to. For example, you could
write a new Channel
implementation that creates the sub-channels that
share one socket connection, as BEEP and
SSH do.
Some transports exposes additional operations that is specific to the
transport. Down-cast the Channel
to sub-type to invoke such
operations. For example, with the old I/O datagram transport, multicast
join / leave operations are provided by DatagramChannel
.
It is important to call ChannelOutboundInvoker.close()
or ChannelOutboundInvoker.close(ChannelPromise)
to release all
resources once you are done with the Channel
. This ensures all resources are
released in a proper way, i.e. filehandles.
Modifier and Type | Interface and Description |
---|---|
static interface |
Channel.Unsafe
Unsafe operations that should never be called from user-code.
|
Modifier and Type | Method and Description |
---|---|
ByteBufAllocator |
alloc()
Return the assigned
ByteBufAllocator which will be used to allocate ByteBuf s. |
long |
bytesBeforeUnwritable()
Get how many bytes can be written until
isWritable() returns false . |
long |
bytesBeforeWritable()
Get how many bytes must be drained from underlying buffers until
isWritable() returns true . |
ChannelFuture |
closeFuture()
Returns the
ChannelFuture which will be notified when this
channel is closed. |
ChannelConfig |
config()
Returns the configuration of this channel.
|
EventLoop |
eventLoop()
|
Channel |
flush()
Request to flush all pending messages via this ChannelOutboundInvoker.
|
ChannelId |
id()
Returns the globally unique identifier of this
Channel . |
boolean |
isActive()
Return
true if the Channel is active and so connected. |
boolean |
isOpen()
Returns
true if the Channel is open and may get active later |
boolean |
isRegistered()
|
boolean |
isWritable()
Returns
true if and only if the I/O thread will perform the
requested write operation immediately. |
SocketAddress |
localAddress()
Returns the local address where this channel is bound to.
|
ChannelMetadata |
metadata()
|
Channel |
parent()
Returns the parent of this channel.
|
ChannelPipeline |
pipeline()
Return the assigned
ChannelPipeline . |
Channel |
read()
Request to Read data from the
Channel into the first inbound buffer, triggers an
ChannelInboundHandler.channelRead(ChannelHandlerContext, Object) event if data was
read, and triggers a
channelReadComplete event so the
handler can decide to continue reading. |
SocketAddress |
remoteAddress()
Returns the remote address where this channel is connected to.
|
Channel.Unsafe |
unsafe()
Returns an internal-use-only object that provides unsafe operations.
|
attr, hasAttr
bind, bind, close, close, connect, connect, connect, connect, deregister, deregister, disconnect, disconnect, newFailedFuture, newProgressivePromise, newPromise, newSucceededFuture, voidPromise, write, write, writeAndFlush, writeAndFlush
compareTo
EventLoop eventLoop()
Channel parent()
null
if this channel does not have a parent channel.ChannelConfig config()
boolean isOpen()
true
if the Channel
is open and may get active laterboolean isRegistered()
boolean isActive()
true
if the Channel
is active and so connected.ChannelMetadata metadata()
SocketAddress localAddress()
SocketAddress
is supposed to be down-cast into more concrete
type such as InetSocketAddress
to retrieve the detailed
information.null
if this channel is not bound.SocketAddress remoteAddress()
SocketAddress
is supposed to be down-cast into more
concrete type such as InetSocketAddress
to retrieve the detailed
information.null
if this channel is not connected.
If this channel is not connected but it can receive messages
from arbitrary remote addresses (e.g. DatagramChannel
,
use DefaultAddressedEnvelope.recipient()
to determine
the origination of the received message as this method will
return null
.ChannelFuture closeFuture()
ChannelFuture
which will be notified when this
channel is closed. This method always returns the same future instance.boolean isWritable()
true
if and only if the I/O thread will perform the
requested write operation immediately. Any write requests made when
this method returns false
are queued until the I/O thread is
ready to process the queued write requests.
WriteBufferWaterMark
can be used to configure on which condition
the write buffer would cause this channel to change writability.long bytesBeforeUnwritable()
isWritable()
returns false
.
This quantity will always be non-negative. If isWritable()
is false
then 0.
WriteBufferWaterMark
can be used to define writability settings.long bytesBeforeWritable()
isWritable()
returns true
.
This quantity will always be non-negative. If isWritable()
is true
then 0.
WriteBufferWaterMark
can be used to define writability settings.Channel.Unsafe unsafe()
ChannelPipeline pipeline()
ChannelPipeline
.ByteBufAllocator alloc()
ByteBufAllocator
which will be used to allocate ByteBuf
s.Channel read()
ChannelOutboundInvoker
Channel
into the first inbound buffer, triggers an
ChannelInboundHandler.channelRead(ChannelHandlerContext, Object)
event if data was
read, and triggers a
channelReadComplete
event so the
handler can decide to continue reading. If there's a pending read operation already, this method does nothing.
This will result in having the
ChannelOutboundHandler.read(ChannelHandlerContext)
method called of the next ChannelOutboundHandler
contained in the ChannelPipeline
of the
Channel
.
read
in interface ChannelOutboundInvoker
Channel flush()
ChannelOutboundInvoker
flush
in interface ChannelOutboundInvoker
Copyright © 2008–2024 The Netty Project. All rights reserved.