-
- All Superinterfaces:
AttributeMap
,ChannelOutboundInvoker
,Comparable<Channel>
,FuturePromiseFactory
,IoHandle
- All Known Subinterfaces:
DatagramChannel
,Http2StreamChannel
,ServerChannel
,ServerSocketChannel
,SocketChannel
,UnixChannel
- All Known Implementing Classes:
AbstractChannel
,AbstractNioByteChannel
,AbstractNioChannel
,AbstractNioMessageChannel
,AbstractServerChannel
,EmbeddedChannel
,EpollDatagramChannel
,EpollServerSocketChannel
,EpollSocketChannel
,KQueueDatagramChannel
,KQueueServerSocketChannel
,KQueueSocketChannel
,LocalChannel
,LocalServerChannel
,NioDatagramChannel
,NioServerSocketChannel
,NioSocketChannel
public interface Channel extends AttributeMap, ChannelOutboundInvoker, Comparable<Channel>, IoHandle
A nexus to a network socket or a component which is capable of I/O operations such as read, write, connect, and bind.A channel provides a user:
- the current state of the channel (e.g. is it open? is it connected?),
- the configuration parameters of the channel (e.g. receive buffer size),
- the I/O operations that the channel supports (e.g. read, write, connect, and bind), and
- the
ChannelPipeline
which handles all I/O events and requests associated with the channel.
All I/O operations are asynchronous.
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
Future
instance which will notify you when the requested I/O operation has succeeded, failed, or canceled.Channels are hierarchical
A
Channel
can have a parent depending on how it was created. For instance, aSocketChannel
, that was accepted byServerSocketChannel
, will return theServerSocketChannel
as its parent onparent()
.The semantics of the hierarchical structure depends on the transport implementation where the
Channel
belongs to. For example, you could write a newChannel
implementation that creates the sub-channels that share one socket connection, as BEEP and SSH do.Downcast to access transport-specific operations
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 byDatagramChannel
.Storing stateful information
AttributeMap.attr(AttributeKey)
allow you to store and access stateful information that is related with a handler and its context. Please refer toChannelHandler
to learn various recommended ways to manage stateful information.Release resources
It is important to call
close()
to release all resources once you are done with theChannel
. This ensures all resources are released in a proper way, i.e. filehandles.Configuration / Option map
An option map property is a dynamic write-only property which allows the configuration of aChannel
without down-casting. To update an option map, please callsetOption(ChannelOption, Object)
.All
Channel
types have the following options:More options are available in the sub-types of
Channel
. For example, you can configure the parameters which are specific to a TCP/IP socket as explained inSocketChannel
.
-
-
Method Summary
All Methods Instance Methods Abstract Methods Default Methods Modifier and Type Method Description default Future<Void>
bind(SocketAddress localAddress)
Request to bind to the givenSocketAddress
and notify theFuture
once the operation completes, either because the operation was successful or because of an error.BufferAllocator
bufferAllocator()
Return the assignedBufferAllocator
which will be used to allocateBuffer
s.default Future<Void>
close()
Future<Void>
closeFuture()
Returns theFuture
which will be notified when this channel is closed.default Future<Void>
connect(SocketAddress remoteAddress)
Request to connect to the givenSocketAddress
and notify theFuture
once the operation completes, either because the operation was successful or because of an error.default Future<Void>
connect(SocketAddress remoteAddress, SocketAddress localAddress)
Request to connect to the givenSocketAddress
while bind to the localAddress and notify theFuture
once the operation completes, either because the operation was successful or because of an error.default Future<Void>
deregister()
Request to deregister from the previous assignedEventExecutor
and notify theFuture
once the operation completes, either because the operation was successful or because of an error.default Future<Void>
disconnect()
Request to disconnect from the remote peer and notify theFuture
once the operation completes, either because the operation was successful or because of an error.EventLoop
executor()
default Channel
flush()
Request to flush all pending messages via this ChannelOutboundInvoker.<T> T
getOption(ChannelOption<T> option)
Return the value of the givenChannelOption
ChannelId
id()
Returns the globally unique identifier of thisChannel
.boolean
isActive()
Returntrue
if theChannel
is active and so connected.boolean
isOpen()
Returnstrue
if theChannel
is open and may get active laterboolean
isOptionSupported(ChannelOption<?> option)
boolean
isShutdown(ChannelShutdownDirection direction)
default boolean
isWritable()
Returnstrue
if and only if the I/O thread will perform the requested flush 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 assignedChannelPipeline
.default Channel
read()
Request to Read data from theChannel
into the first inbound buffer, triggers anChannelHandler.channelRead(ChannelHandlerContext, Object)
event if data was read, and triggers achannelReadComplete
event so the handler can decide to continue reading.default Future<Void>
register()
Request to register on theEventExecutor
for I/O processing.SocketAddress
remoteAddress()
Returns the remote address where this channel is connected to.default Future<Void>
sendOutboundEvent(Object event)
Send a custom outbound event via thisChannelOutboundInvoker
through theChannelPipeline
.<T> Channel
setOption(ChannelOption<T> option, T value)
Sets a configuration property with the specified name and value.default Future<Void>
shutdown(ChannelShutdownDirection direction)
long
writableBytes()
Returns how many bytes can be written before theChannel
becomes 'unwritable'.default Future<Void>
write(Object msg)
Request to write a message via thisChannelHandlerContext
through theChannelPipeline
.default Future<Void>
writeAndFlush(Object msg)
Shortcut for callChannelOutboundInvoker.write(Object)
andChannelOutboundInvoker.flush()
.-
Methods inherited from interface io.netty5.util.AttributeMap
attr, hasAttr
-
Methods inherited from interface io.netty5.channel.ChannelOutboundInvoker
newFailedFuture, newPromise, newSucceededFuture, newSucceededFuture
-
Methods inherited from interface java.lang.Comparable
compareTo
-
Methods inherited from interface io.netty5.channel.IoHandle
isRegistered
-
-
-
-
Method Detail
-
executor
EventLoop executor()
- Specified by:
executor
in interfaceChannelOutboundInvoker
- Returns:
- the executor.
-
parent
Channel parent()
Returns the parent of this channel.- Returns:
- the parent channel.
null
if this channel does not have a parent channel.
-
getOption
<T> T getOption(ChannelOption<T> option)
Return the value of the givenChannelOption
- Type Parameters:
T
- the type of the value.- Parameters:
option
- theChannelOption
.- Returns:
- the value for the
ChannelOption
- Throws:
ChannelException
- thrown on error.UnsupportedOperationException
- if theChannelOption
is not supported.
-
setOption
<T> Channel setOption(ChannelOption<T> option, T value)
Sets a configuration property with the specified name and value.- Type Parameters:
T
- the type of the value.- Parameters:
option
- theChannelOption
.value
- the value for theChannelOption
- Returns:
- itself.
- Throws:
ChannelException
- thrown on error.UnsupportedOperationException
- if theChannelOption
is not supported.
-
isOptionSupported
boolean isOptionSupported(ChannelOption<?> option)
Returnstrue
if the givenChannelOption
is supported by thisChannel
implementation. If this methods returnsfalse
, calls tosetOption(ChannelOption, Object)
andgetOption(ChannelOption)
with theChannelOption
will throw anUnsupportedOperationException
.- Parameters:
option
- the option.- Returns:
- true if supported,
false
otherwise.
-
isOpen
boolean isOpen()
Returnstrue
if theChannel
is open and may get active later
-
isActive
boolean isActive()
Returntrue
if theChannel
is active and so connected.
-
isShutdown
boolean isShutdown(ChannelShutdownDirection direction)
-
metadata
ChannelMetadata metadata()
-
localAddress
SocketAddress localAddress()
Returns the local address where this channel is bound to. The returnedSocketAddress
is supposed to be down-cast into more concrete type such asInetSocketAddress
to retrieve the detailed information.- Returns:
- the local address of this channel.
null
if this channel is not bound.
-
remoteAddress
SocketAddress remoteAddress()
Returns the remote address where this channel is connected to. The returnedSocketAddress
is supposed to be down-cast into more concrete type such asInetSocketAddress
to retrieve the detailed information.- Returns:
- the remote address of this channel.
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
, useDefaultAddressedEnvelope.recipient()
to determine the origination of the received message as this method will returnnull
.
-
closeFuture
Future<Void> closeFuture()
Returns theFuture
which will be notified when this channel is closed. This method always returns the same future instance.
-
isWritable
default boolean isWritable()
Returnstrue
if and only if the I/O thread will perform the requested flush operation immediately. Any write requests made when this method returnsfalse
are queued until the I/O thread is ready to process the queued write requests.
-
writableBytes
long writableBytes()
Returns how many bytes can be written before theChannel
becomes 'unwritable'. Once aChannel
becomes unwritable, all messages will be queued until the I/O thread is ready to process the queued write requests.- Returns:
- the number of bytes that can be written before the
Channel
becomes unwritable.
-
pipeline
ChannelPipeline pipeline()
Return the assignedChannelPipeline
.
-
bufferAllocator
BufferAllocator bufferAllocator()
Return the assignedBufferAllocator
which will be used to allocateBuffer
s.
-
read
default Channel read()
Description copied from interface:ChannelOutboundInvoker
Request to Read data from theChannel
into the first inbound buffer, triggers anChannelHandler.channelRead(ChannelHandlerContext, Object)
event if data was read, and triggers achannelReadComplete
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
ChannelHandler.read(ChannelHandlerContext)
method called of the nextChannelHandler
contained in theChannelPipeline
of theChannel
.- Specified by:
read
in interfaceChannelOutboundInvoker
-
bind
default Future<Void> bind(SocketAddress localAddress)
Description copied from interface:ChannelOutboundInvoker
Request to bind to the givenSocketAddress
and notify theFuture
once the operation completes, either because the operation was successful or because of an error.This will result in having the
ChannelHandler.bind(ChannelHandlerContext, SocketAddress)
method called of the nextChannelHandler
contained in theChannelPipeline
of theChannel
.- Specified by:
bind
in interfaceChannelOutboundInvoker
-
connect
default Future<Void> connect(SocketAddress remoteAddress)
Description copied from interface:ChannelOutboundInvoker
Request to connect to the givenSocketAddress
and notify theFuture
once the operation completes, either because the operation was successful or because of an error.If the connection fails because of a connection timeout, the
Future
will get failed with aConnectTimeoutException
. If it fails because of connection refused aConnectException
will be used.This will result in having the
ChannelHandler.connect(ChannelHandlerContext, SocketAddress, SocketAddress)
method called of the nextChannelHandler
contained in theChannelPipeline
of theChannel
.- Specified by:
connect
in interfaceChannelOutboundInvoker
-
connect
default Future<Void> connect(SocketAddress remoteAddress, SocketAddress localAddress)
Description copied from interface:ChannelOutboundInvoker
Request to connect to the givenSocketAddress
while bind to the localAddress and notify theFuture
once the operation completes, either because the operation was successful or because of an error.This will result in having the
ChannelHandler.connect(ChannelHandlerContext, SocketAddress, SocketAddress)
method called of the nextChannelHandler
contained in theChannelPipeline
of theChannel
.- Specified by:
connect
in interfaceChannelOutboundInvoker
-
disconnect
default Future<Void> disconnect()
Description copied from interface:ChannelOutboundInvoker
Request to disconnect from the remote peer and notify theFuture
once the operation completes, either because the operation was successful or because of an error.This will result in having the
ChannelHandler.disconnect(ChannelHandlerContext)
method called of the nextChannelHandler
contained in theChannelPipeline
of theChannel
.- Specified by:
disconnect
in interfaceChannelOutboundInvoker
-
close
default Future<Void> close()
Description copied from interface:ChannelOutboundInvoker
Request to close theChannel
and notify theFuture
once the operation completes, either because the operation was successful or because of an error. After it is closed it is not possible to reuse it again.This will result in having the
ChannelHandler.close(ChannelHandlerContext)
method called of the nextChannelHandler
contained in theChannelPipeline
of theChannel
.- Specified by:
close
in interfaceChannelOutboundInvoker
-
shutdown
default Future<Void> shutdown(ChannelShutdownDirection direction)
Description copied from interface:ChannelOutboundInvoker
Request shutdown one direction of theChannel
and notify theFuture
once the operation completes, either because the operation was successful or because of an error.When completed, the channel will either not produce any inbound data anymore, or it will not be possible to write data anymore, depending on the given
ChannelShutdownDirection
.Depending on the transport implementation shutting down the
ChannelShutdownDirection.Outbound
orChannelShutdownDirection.Inbound
might also result in data transferred over the network. Like for example in case of TCP shutting down theChannelShutdownDirection.Outbound
will result in aFIN
that is transmitted to the remote peer that will as a result shutdownChannelShutdownDirection.Inbound
.This will result in having the
ChannelHandler.shutdown(ChannelHandlerContext, ChannelShutdownDirection)
. method called of the nextChannelHandler
contained in theChannelPipeline
of theChannel
.- Specified by:
shutdown
in interfaceChannelOutboundInvoker
-
register
default Future<Void> register()
Description copied from interface:ChannelOutboundInvoker
Request to register on theEventExecutor
for I/O processing.Future
once the operation completes, either because the operation was successful or because of an error.This will result in having the
ChannelHandler.register(ChannelHandlerContext)
method called of the nextChannelHandler
contained in theChannelPipeline
of theChannel
.- Specified by:
register
in interfaceChannelOutboundInvoker
-
deregister
default Future<Void> deregister()
Description copied from interface:ChannelOutboundInvoker
Request to deregister from the previous assignedEventExecutor
and notify theFuture
once the operation completes, either because the operation was successful or because of an error.This will result in having the
ChannelHandler.deregister(ChannelHandlerContext)
method called of the nextChannelHandler
contained in theChannelPipeline
of theChannel
.- Specified by:
deregister
in interfaceChannelOutboundInvoker
-
write
default Future<Void> write(Object msg)
Description copied from interface:ChannelOutboundInvoker
Request to write a message via thisChannelHandlerContext
through theChannelPipeline
. This method will not request to actual flush, so be sure to callChannelOutboundInvoker.flush()
once you want to request to flush all pending data to the actual transport.- Specified by:
write
in interfaceChannelOutboundInvoker
-
writeAndFlush
default Future<Void> writeAndFlush(Object msg)
Description copied from interface:ChannelOutboundInvoker
Shortcut for callChannelOutboundInvoker.write(Object)
andChannelOutboundInvoker.flush()
.- Specified by:
writeAndFlush
in interfaceChannelOutboundInvoker
-
flush
default Channel flush()
Description copied from interface:ChannelOutboundInvoker
Request to flush all pending messages via this ChannelOutboundInvoker.- Specified by:
flush
in interfaceChannelOutboundInvoker
-
sendOutboundEvent
default Future<Void> sendOutboundEvent(Object event)
Description copied from interface:ChannelOutboundInvoker
Send a custom outbound event via thisChannelOutboundInvoker
through theChannelPipeline
.This will result in having the
ChannelHandler.sendOutboundEvent(ChannelHandlerContext, Object)
method called of the nextChannelHandler
contained in theChannelPipeline
of theChannel
.- Specified by:
sendOutboundEvent
in interfaceChannelOutboundInvoker
-
-