-
- 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
ChannelPipelinewhich 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
Futureinstance which will notify you when the requested I/O operation has succeeded, failed, or canceled.Channels are hierarchical
A
Channelcan have a parent depending on how it was created. For instance, aSocketChannel, that was accepted byServerSocketChannel, will return theServerSocketChannelas its parent onparent().The semantics of the hierarchical structure depends on the transport implementation where the
Channelbelongs to. For example, you could write a newChannelimplementation 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
Channelto 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 toChannelHandlerto 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 aChannelwithout down-casting. To update an option map, please callsetOption(ChannelOption, Object).All
Channeltypes 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 givenSocketAddressand notify theFutureonce the operation completes, either because the operation was successful or because of an error.BufferAllocatorbufferAllocator()Return the assignedBufferAllocatorwhich will be used to allocateBuffers.default Future<Void>close()Future<Void>closeFuture()Returns theFuturewhich will be notified when this channel is closed.default Future<Void>connect(SocketAddress remoteAddress)Request to connect to the givenSocketAddressand notify theFutureonce 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 givenSocketAddresswhile bind to the localAddress and notify theFutureonce the operation completes, either because the operation was successful or because of an error.default Future<Void>deregister()Request to deregister from the previous assignedEventExecutorand notify theFutureonce 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 theFutureonce the operation completes, either because the operation was successful or because of an error.EventLoopexecutor()default Channelflush()Request to flush all pending messages via this ChannelOutboundInvoker.<T> TgetOption(ChannelOption<T> option)Return the value of the givenChannelOptionChannelIdid()Returns the globally unique identifier of thisChannel.booleanisActive()Returntrueif theChannelis active and so connected.booleanisOpen()Returnstrueif theChannelis open and may get active laterbooleanisOptionSupported(ChannelOption<?> option)booleanisShutdown(ChannelShutdownDirection direction)default booleanisWritable()Returnstrueif and only if the I/O thread will perform the requested flush operation immediately.SocketAddresslocalAddress()Returns the local address where this channel is bound to.ChannelMetadatametadata()Channelparent()Returns the parent of this channel.ChannelPipelinepipeline()Return the assignedChannelPipeline.default Channelread()Request to Read data from theChannelinto the first inbound buffer, triggers anChannelHandler.channelRead(ChannelHandlerContext, Object)event if data was read, and triggers achannelReadCompleteevent so the handler can decide to continue reading.default Future<Void>register()Request to register on theEventExecutorfor I/O processing.SocketAddressremoteAddress()Returns the remote address where this channel is connected to.default Future<Void>sendOutboundEvent(Object event)Send a custom outbound event via thisChannelOutboundInvokerthrough theChannelPipeline.<T> ChannelsetOption(ChannelOption<T> option, T value)Sets a configuration property with the specified name and value.default Future<Void>shutdown(ChannelShutdownDirection direction)longwritableBytes()Returns how many bytes can be written before theChannelbecomes 'unwritable'.default Future<Void>write(Object msg)Request to write a message via thisChannelHandlerContextthrough 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:
executorin interfaceChannelOutboundInvoker- Returns:
- the executor.
-
parent
Channel parent()
Returns the parent of this channel.- Returns:
- the parent channel.
nullif 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 theChannelOptionis 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 theChannelOptionis not supported.
-
isOptionSupported
boolean isOptionSupported(ChannelOption<?> option)
Returnstrueif the givenChannelOptionis supported by thisChannelimplementation. If this methods returnsfalse, calls tosetOption(ChannelOption, Object)andgetOption(ChannelOption)with theChannelOptionwill throw anUnsupportedOperationException.- Parameters:
option- the option.- Returns:
- true if supported,
falseotherwise.
-
isOpen
boolean isOpen()
Returnstrueif theChannelis open and may get active later
-
isActive
boolean isActive()
Returntrueif theChannelis 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 returnedSocketAddressis supposed to be down-cast into more concrete type such asInetSocketAddressto retrieve the detailed information.- Returns:
- the local address of this channel.
nullif this channel is not bound.
-
remoteAddress
SocketAddress remoteAddress()
Returns the remote address where this channel is connected to. The returnedSocketAddressis supposed to be down-cast into more concrete type such asInetSocketAddressto retrieve the detailed information.- Returns:
- the remote address of this channel.
nullif 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 theFuturewhich will be notified when this channel is closed. This method always returns the same future instance.
-
isWritable
default boolean isWritable()
Returnstrueif and only if the I/O thread will perform the requested flush operation immediately. Any write requests made when this method returnsfalseare 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 theChannelbecomes 'unwritable'. Once aChannelbecomes 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
Channelbecomes unwritable.
-
pipeline
ChannelPipeline pipeline()
Return the assignedChannelPipeline.
-
bufferAllocator
BufferAllocator bufferAllocator()
Return the assignedBufferAllocatorwhich will be used to allocateBuffers.
-
read
default Channel read()
Description copied from interface:ChannelOutboundInvokerRequest to Read data from theChannelinto the first inbound buffer, triggers anChannelHandler.channelRead(ChannelHandlerContext, Object)event if data was read, and triggers achannelReadCompleteevent 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 nextChannelHandlercontained in theChannelPipelineof theChannel.- Specified by:
readin interfaceChannelOutboundInvoker
-
bind
default Future<Void> bind(SocketAddress localAddress)
Description copied from interface:ChannelOutboundInvokerRequest to bind to the givenSocketAddressand notify theFutureonce 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 nextChannelHandlercontained in theChannelPipelineof theChannel.- Specified by:
bindin interfaceChannelOutboundInvoker
-
connect
default Future<Void> connect(SocketAddress remoteAddress)
Description copied from interface:ChannelOutboundInvokerRequest to connect to the givenSocketAddressand notify theFutureonce the operation completes, either because the operation was successful or because of an error.If the connection fails because of a connection timeout, the
Futurewill get failed with aConnectTimeoutException. If it fails because of connection refused aConnectExceptionwill be used.This will result in having the
ChannelHandler.connect(ChannelHandlerContext, SocketAddress, SocketAddress)method called of the nextChannelHandlercontained in theChannelPipelineof theChannel.- Specified by:
connectin interfaceChannelOutboundInvoker
-
connect
default Future<Void> connect(SocketAddress remoteAddress, SocketAddress localAddress)
Description copied from interface:ChannelOutboundInvokerRequest to connect to the givenSocketAddresswhile bind to the localAddress and notify theFutureonce 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 nextChannelHandlercontained in theChannelPipelineof theChannel.- Specified by:
connectin interfaceChannelOutboundInvoker
-
disconnect
default Future<Void> disconnect()
Description copied from interface:ChannelOutboundInvokerRequest to disconnect from the remote peer and notify theFutureonce 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 nextChannelHandlercontained in theChannelPipelineof theChannel.- Specified by:
disconnectin interfaceChannelOutboundInvoker
-
close
default Future<Void> close()
Description copied from interface:ChannelOutboundInvokerRequest to close theChanneland notify theFutureonce 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 nextChannelHandlercontained in theChannelPipelineof theChannel.- Specified by:
closein interfaceChannelOutboundInvoker
-
shutdown
default Future<Void> shutdown(ChannelShutdownDirection direction)
Description copied from interface:ChannelOutboundInvokerRequest shutdown one direction of theChanneland notify theFutureonce 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.OutboundorChannelShutdownDirection.Inboundmight also result in data transferred over the network. Like for example in case of TCP shutting down theChannelShutdownDirection.Outboundwill result in aFINthat 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 nextChannelHandlercontained in theChannelPipelineof theChannel.- Specified by:
shutdownin interfaceChannelOutboundInvoker
-
register
default Future<Void> register()
Description copied from interface:ChannelOutboundInvokerRequest to register on theEventExecutorfor I/O processing.Futureonce 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 nextChannelHandlercontained in theChannelPipelineof theChannel.- Specified by:
registerin interfaceChannelOutboundInvoker
-
deregister
default Future<Void> deregister()
Description copied from interface:ChannelOutboundInvokerRequest to deregister from the previous assignedEventExecutorand notify theFutureonce 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 nextChannelHandlercontained in theChannelPipelineof theChannel.- Specified by:
deregisterin interfaceChannelOutboundInvoker
-
write
default Future<Void> write(Object msg)
Description copied from interface:ChannelOutboundInvokerRequest to write a message via thisChannelHandlerContextthrough 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:
writein interfaceChannelOutboundInvoker
-
writeAndFlush
default Future<Void> writeAndFlush(Object msg)
Description copied from interface:ChannelOutboundInvokerShortcut for callChannelOutboundInvoker.write(Object)andChannelOutboundInvoker.flush().- Specified by:
writeAndFlushin interfaceChannelOutboundInvoker
-
flush
default Channel flush()
Description copied from interface:ChannelOutboundInvokerRequest to flush all pending messages via this ChannelOutboundInvoker.- Specified by:
flushin interfaceChannelOutboundInvoker
-
sendOutboundEvent
default Future<Void> sendOutboundEvent(Object event)
Description copied from interface:ChannelOutboundInvokerSend a custom outbound event via thisChannelOutboundInvokerthrough theChannelPipeline.This will result in having the
ChannelHandler.sendOutboundEvent(ChannelHandlerContext, Object)method called of the nextChannelHandlercontained in theChannelPipelineof theChannel.- Specified by:
sendOutboundEventin interfaceChannelOutboundInvoker
-
-