Class AbstractChannel
- All Implemented Interfaces:
Channel
,ChannelOutboundInvoker
,AttributeMap
,Comparable<Channel>
- Direct Known Subclasses:
AbstractEpollChannel
,AbstractKQueueChannel
,AbstractNioChannel
,AbstractOioChannel
,AbstractServerChannel
,EmbeddedChannel
,FailedChannel
,LocalChannel
Channel
implementation.-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionprotected class
Channel.Unsafe
implementation which sub-classes must extend and use.private static final class
private static final class
private static final class
(package private) static final class
Nested classes/interfaces inherited from interface io.netty.channel.Channel
Channel.Unsafe
-
Field Summary
FieldsModifier and TypeFieldDescriptionprivate final AbstractChannel.CloseFuture
private boolean
private EventLoop
private final ChannelId
private Throwable
private SocketAddress
private static final InternalLogger
private final Channel
private final DefaultChannelPipeline
private boolean
private SocketAddress
private String
private boolean
Cache for the string representation of this channelprivate final Channel.Unsafe
private final VoidChannelPromise
-
Constructor Summary
ConstructorsModifierConstructorDescriptionprotected
AbstractChannel
(Channel parent) Creates a new instance.protected
AbstractChannel
(Channel parent, ChannelId id) Creates a new instance. -
Method Summary
Modifier and TypeMethodDescriptionalloc()
Return the assignedByteBufAllocator
which will be used to allocateByteBuf
s.bind
(SocketAddress localAddress) Request to bind to the givenSocketAddress
and notify theChannelFuture
once the operation completes, either because the operation was successful or because of an error.bind
(SocketAddress localAddress, ChannelPromise promise) Request to bind to the givenSocketAddress
and notify theChannelFuture
once the operation completes, either because the operation was successful or because of an error.long
Get how many bytes can be written untilChannel.isWritable()
returnsfalse
.long
Get how many bytes must be drained from underlying buffers untilChannel.isWritable()
returnstrue
.close()
Request to close theChannel
and notify theChannelFuture
once the operation completes, either because the operation was successful or because of an error.close
(ChannelPromise promise) Request to close theChannel
and notify theChannelFuture
once the operation completes, either because the operation was successful or because of an error.Returns theChannelFuture
which will be notified when this channel is closed.final int
connect
(SocketAddress remoteAddress) Request to connect to the givenSocketAddress
and notify theChannelFuture
once the operation completes, either because the operation was successful or because of an error.connect
(SocketAddress remoteAddress, ChannelPromise promise) Request to connect to the givenSocketAddress
and notify theChannelFuture
once the operation completes, either because the operation was successful or because of an error.connect
(SocketAddress remoteAddress, SocketAddress localAddress) Request to connect to the givenSocketAddress
while bind to the localAddress and notify theChannelFuture
once the operation completes, either because the operation was successful or because of an error.connect
(SocketAddress remoteAddress, SocketAddress localAddress, ChannelPromise promise) Request to connect to the givenSocketAddress
while bind to the localAddress and notify theChannelFuture
once the operation completes, either because the operation was successful or because of an error.Request to deregister from the previous assignedEventExecutor
and notify theChannelFuture
once the operation completes, either because the operation was successful or because of an error.deregister
(ChannelPromise promise) Request to deregister from the previous assignedEventExecutor
and notify theChannelFuture
once the operation completes, either because the operation was successful or because of an error.Request to disconnect from the remote peer and notify theChannelFuture
once the operation completes, either because the operation was successful or because of an error.disconnect
(ChannelPromise promise) Request to disconnect from the remote peer and notify theChannelFuture
once the operation completes, either because the operation was successful or because of an error.protected abstract void
Schedule a read operation.protected abstract void
doBind
(SocketAddress localAddress) Bind theChannel
to theSocketAddress
protected abstract void
doClose()
Close theChannel
protected void
protected abstract void
Disconnect thisChannel
from its remote peerprotected void
protected void
Called when conditions justify shutting down the output portion of the channel.protected abstract void
Flush the content of the given buffer to the remote peer.final boolean
Returnstrue
if and only if the specified object is identical with this channel (i.e:this == o
).protected Object
Invoked when a new message is added to aChannelOutboundBuffer
of thisAbstractChannel
, so that theChannel
implementation converts the message to another.flush()
Request to flush all pending messages via this ChannelOutboundInvoker.final int
hashCode()
Returns the ID of this channel.final ChannelId
id()
Returns the globally unique identifier of thisChannel
.protected void
Deprecated.no use-case for this.protected void
Deprecated.no use-case for this.protected abstract boolean
isCompatible
(EventLoop loop) Returntrue
if the givenEventLoop
is compatible with this instance.boolean
boolean
Returnstrue
if and only if the I/O thread will perform the requested write operation immediately.Returns the local address where this channel is bound to.protected abstract SocketAddress
Returns theSocketAddress
which is bound locally.protected final int
protected DefaultChannelPipeline
Returns a newDefaultChannelPipeline
instance.newFailedFuture
(Throwable cause) Create a newChannelFuture
which is marked as failed already.protected ChannelId
newId()
Returns a newDefaultChannelId
instance.Return an newChannelProgressivePromise
Return a newChannelPromise
.Create a newChannelFuture
which is marked as succeeded already.protected abstract AbstractChannel.AbstractUnsafe
Create a newAbstractChannel.AbstractUnsafe
instance which will be used for the life-time of theChannel
parent()
Returns the parent of this channel.pipeline()
Return the assignedChannelPipeline
.read()
Request to Read data from theChannel
into the first inbound buffer, triggers anChannelInboundHandler.channelRead(ChannelHandlerContext, Object)
event if data was read, and triggers achannelReadComplete
event so the handler can decide to continue reading.Returns the remote address where this channel is connected to.protected abstract SocketAddress
Return theSocketAddress
which theChannel
is connected to.toString()
Returns theString
representation of this channel.unsafe()
Returns an internal-use-only object that provides unsafe operations.protected void
validateFileRegion
(DefaultFileRegion region, long position) final ChannelPromise
Return a special ChannelPromise which can be reused for different operations.Request to write a message via thisChannelHandlerContext
through theChannelPipeline
.write
(Object msg, ChannelPromise promise) Request to write a message via thisChannelHandlerContext
through theChannelPipeline
.writeAndFlush
(Object msg) Shortcut for callChannelOutboundInvoker.write(Object)
andChannelOutboundInvoker.flush()
.writeAndFlush
(Object msg, ChannelPromise promise) Shortcut for callChannelOutboundInvoker.write(Object, ChannelPromise)
andChannelOutboundInvoker.flush()
.Methods inherited from class io.netty.util.DefaultAttributeMap
attr, hasAttr
Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, wait
Methods inherited from interface io.netty.util.AttributeMap
attr, hasAttr
-
Field Details
-
logger
-
parent
-
id
-
unsafe
-
pipeline
-
unsafeVoidPromise
-
closeFuture
-
localAddress
-
remoteAddress
-
eventLoop
-
registered
private volatile boolean registered -
closeInitiated
private boolean closeInitiated -
initialCloseCause
-
strValActive
private boolean strValActiveCache for the string representation of this channel -
strVal
-
-
Constructor Details
-
AbstractChannel
Creates a new instance.- Parameters:
parent
- the parent of this channel.null
if there's no parent.
-
AbstractChannel
Creates a new instance.- Parameters:
parent
- the parent of this channel.null
if there's no parent.
-
-
Method Details
-
maxMessagesPerWrite
protected final int maxMessagesPerWrite() -
id
Description copied from interface:Channel
Returns the globally unique identifier of thisChannel
. -
newId
Returns a newDefaultChannelId
instance. Subclasses may override this method to assign customChannelId
s toChannel
s that use theAbstractChannel(Channel)
constructor. -
newChannelPipeline
Returns a newDefaultChannelPipeline
instance. -
isWritable
public boolean isWritable()Description copied from interface:Channel
Returnstrue
if and only if the I/O thread will perform the requested write 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.WriteBufferWaterMark
can be used to configure on which condition the write buffer would cause this channel to change writability.- Specified by:
isWritable
in interfaceChannel
-
bytesBeforeUnwritable
public long bytesBeforeUnwritable()Description copied from interface:Channel
Get how many bytes can be written untilChannel.isWritable()
returnsfalse
. This quantity will always be non-negative. IfChannel.isWritable()
isfalse
then 0.WriteBufferWaterMark
can be used to define writability settings.- Specified by:
bytesBeforeUnwritable
in interfaceChannel
-
bytesBeforeWritable
public long bytesBeforeWritable()Description copied from interface:Channel
Get how many bytes must be drained from underlying buffers untilChannel.isWritable()
returnstrue
. This quantity will always be non-negative. IfChannel.isWritable()
istrue
then 0.WriteBufferWaterMark
can be used to define writability settings.- Specified by:
bytesBeforeWritable
in interfaceChannel
-
parent
Description copied from interface:Channel
Returns the parent of this channel. -
pipeline
Description copied from interface:Channel
Return the assignedChannelPipeline
. -
alloc
Description copied from interface:Channel
Return the assignedByteBufAllocator
which will be used to allocateByteBuf
s. -
eventLoop
Description copied from interface:Channel
-
localAddress
Description copied from interface:Channel
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.- Specified by:
localAddress
in interfaceChannel
- Returns:
- the local address of this channel.
null
if this channel is not bound.
-
invalidateLocalAddress
Deprecated.no use-case for this. -
remoteAddress
Description copied from interface:Channel
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.- Specified by:
remoteAddress
in interfaceChannel
- 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
.
-
invalidateRemoteAddress
Deprecated.no use-case for this. -
isRegistered
public boolean isRegistered()Description copied from interface:Channel
- Specified by:
isRegistered
in interfaceChannel
-
bind
Description copied from interface:ChannelOutboundInvoker
Request to bind to the givenSocketAddress
and notify theChannelFuture
once the operation completes, either because the operation was successful or because of an error.This will result in having the
ChannelOutboundHandler.bind(ChannelHandlerContext, SocketAddress, ChannelPromise)
method called of the nextChannelOutboundHandler
contained in theChannelPipeline
of theChannel
.- Specified by:
bind
in interfaceChannelOutboundInvoker
-
connect
Description copied from interface:ChannelOutboundInvoker
Request to connect to the givenSocketAddress
and notify theChannelFuture
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
ChannelFuture
will get failed with aConnectTimeoutException
. If it fails because of connection refused aConnectException
will be used.This will result in having the
ChannelOutboundHandler.connect(ChannelHandlerContext, SocketAddress, SocketAddress, ChannelPromise)
method called of the nextChannelOutboundHandler
contained in theChannelPipeline
of theChannel
.- Specified by:
connect
in interfaceChannelOutboundInvoker
-
connect
Description copied from interface:ChannelOutboundInvoker
Request to connect to the givenSocketAddress
while bind to the localAddress and notify theChannelFuture
once the operation completes, either because the operation was successful or because of an error.This will result in having the
ChannelOutboundHandler.connect(ChannelHandlerContext, SocketAddress, SocketAddress, ChannelPromise)
method called of the nextChannelOutboundHandler
contained in theChannelPipeline
of theChannel
.- Specified by:
connect
in interfaceChannelOutboundInvoker
-
disconnect
Description copied from interface:ChannelOutboundInvoker
Request to disconnect from the remote peer and notify theChannelFuture
once the operation completes, either because the operation was successful or because of an error.This will result in having the
ChannelOutboundHandler.disconnect(ChannelHandlerContext, ChannelPromise)
method called of the nextChannelOutboundHandler
contained in theChannelPipeline
of theChannel
.- Specified by:
disconnect
in interfaceChannelOutboundInvoker
-
close
Description copied from interface:ChannelOutboundInvoker
Request to close theChannel
and notify theChannelFuture
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
ChannelOutboundHandler.close(ChannelHandlerContext, ChannelPromise)
method called of the nextChannelOutboundHandler
contained in theChannelPipeline
of theChannel
.- Specified by:
close
in interfaceChannelOutboundInvoker
-
deregister
Description copied from interface:ChannelOutboundInvoker
Request to deregister from the previous assignedEventExecutor
and notify theChannelFuture
once the operation completes, either because the operation was successful or because of an error.This will result in having the
ChannelOutboundHandler.deregister(ChannelHandlerContext, ChannelPromise)
method called of the nextChannelOutboundHandler
contained in theChannelPipeline
of theChannel
.- Specified by:
deregister
in interfaceChannelOutboundInvoker
-
flush
Description copied from interface:ChannelOutboundInvoker
Request to flush all pending messages via this ChannelOutboundInvoker.- Specified by:
flush
in interfaceChannel
- Specified by:
flush
in interfaceChannelOutboundInvoker
-
bind
Description copied from interface:ChannelOutboundInvoker
Request to bind to the givenSocketAddress
and notify theChannelFuture
once the operation completes, either because the operation was successful or because of an error. The givenChannelPromise
will be notified.This will result in having the
ChannelOutboundHandler.bind(ChannelHandlerContext, SocketAddress, ChannelPromise)
method called of the nextChannelOutboundHandler
contained in theChannelPipeline
of theChannel
.- Specified by:
bind
in interfaceChannelOutboundInvoker
-
connect
Description copied from interface:ChannelOutboundInvoker
Request to connect to the givenSocketAddress
and notify theChannelFuture
once the operation completes, either because the operation was successful or because of an error. The givenChannelFuture
will be notified.If the connection fails because of a connection timeout, the
ChannelFuture
will get failed with aConnectTimeoutException
. If it fails because of connection refused aConnectException
will be used.This will result in having the
ChannelOutboundHandler.connect(ChannelHandlerContext, SocketAddress, SocketAddress, ChannelPromise)
method called of the nextChannelOutboundHandler
contained in theChannelPipeline
of theChannel
.- Specified by:
connect
in interfaceChannelOutboundInvoker
-
connect
public ChannelFuture connect(SocketAddress remoteAddress, SocketAddress localAddress, ChannelPromise promise) Description copied from interface:ChannelOutboundInvoker
Request to connect to the givenSocketAddress
while bind to the localAddress and notify theChannelFuture
once the operation completes, either because the operation was successful or because of an error. The givenChannelPromise
will be notified and also returned.This will result in having the
ChannelOutboundHandler.connect(ChannelHandlerContext, SocketAddress, SocketAddress, ChannelPromise)
method called of the nextChannelOutboundHandler
contained in theChannelPipeline
of theChannel
.- Specified by:
connect
in interfaceChannelOutboundInvoker
-
disconnect
Description copied from interface:ChannelOutboundInvoker
Request to disconnect from the remote peer and notify theChannelFuture
once the operation completes, either because the operation was successful or because of an error. The givenChannelPromise
will be notified.This will result in having the
ChannelOutboundHandler.disconnect(ChannelHandlerContext, ChannelPromise)
method called of the nextChannelOutboundHandler
contained in theChannelPipeline
of theChannel
.- Specified by:
disconnect
in interfaceChannelOutboundInvoker
-
close
Description copied from interface:ChannelOutboundInvoker
Request to close theChannel
and notify theChannelFuture
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. The givenChannelPromise
will be notified.This will result in having the
ChannelOutboundHandler.close(ChannelHandlerContext, ChannelPromise)
method called of the nextChannelOutboundHandler
contained in theChannelPipeline
of theChannel
.- Specified by:
close
in interfaceChannelOutboundInvoker
-
deregister
Description copied from interface:ChannelOutboundInvoker
Request to deregister from the previous assignedEventExecutor
and notify theChannelFuture
once the operation completes, either because the operation was successful or because of an error. The givenChannelPromise
will be notified.This will result in having the
ChannelOutboundHandler.deregister(ChannelHandlerContext, ChannelPromise)
method called of the nextChannelOutboundHandler
contained in theChannelPipeline
of theChannel
.- Specified by:
deregister
in interfaceChannelOutboundInvoker
-
read
Description copied from interface:ChannelOutboundInvoker
Request to Read data from theChannel
into the first inbound buffer, triggers anChannelInboundHandler.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
ChannelOutboundHandler.read(ChannelHandlerContext)
method called of the nextChannelOutboundHandler
contained in theChannelPipeline
of theChannel
.- Specified by:
read
in interfaceChannel
- Specified by:
read
in interfaceChannelOutboundInvoker
-
write
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
-
write
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
Description copied from interface:ChannelOutboundInvoker
Shortcut for callChannelOutboundInvoker.write(Object)
andChannelOutboundInvoker.flush()
.- Specified by:
writeAndFlush
in interfaceChannelOutboundInvoker
-
writeAndFlush
Description copied from interface:ChannelOutboundInvoker
Shortcut for callChannelOutboundInvoker.write(Object, ChannelPromise)
andChannelOutboundInvoker.flush()
.- Specified by:
writeAndFlush
in interfaceChannelOutboundInvoker
-
newPromise
Description copied from interface:ChannelOutboundInvoker
Return a newChannelPromise
.- Specified by:
newPromise
in interfaceChannelOutboundInvoker
-
newProgressivePromise
Description copied from interface:ChannelOutboundInvoker
Return an newChannelProgressivePromise
- Specified by:
newProgressivePromise
in interfaceChannelOutboundInvoker
-
newSucceededFuture
Description copied from interface:ChannelOutboundInvoker
Create a newChannelFuture
which is marked as succeeded already. SoFuture.isSuccess()
will returntrue
. AllFutureListener
added to it will be notified directly. Also every call of blocking methods will just return without blocking.- Specified by:
newSucceededFuture
in interfaceChannelOutboundInvoker
-
newFailedFuture
Description copied from interface:ChannelOutboundInvoker
Create a newChannelFuture
which is marked as failed already. SoFuture.isSuccess()
will returnfalse
. AllFutureListener
added to it will be notified directly. Also every call of blocking methods will just return without blocking.- Specified by:
newFailedFuture
in interfaceChannelOutboundInvoker
-
closeFuture
Description copied from interface:Channel
Returns theChannelFuture
which will be notified when this channel is closed. This method always returns the same future instance.- Specified by:
closeFuture
in interfaceChannel
-
unsafe
Description copied from interface:Channel
Returns an internal-use-only object that provides unsafe operations. -
newUnsafe
Create a newAbstractChannel.AbstractUnsafe
instance which will be used for the life-time of theChannel
-
hashCode
public final int hashCode()Returns the ID of this channel. -
equals
Returnstrue
if and only if the specified object is identical with this channel (i.e:this == o
). -
compareTo
- Specified by:
compareTo
in interfaceComparable<Channel>
-
toString
Returns theString
representation of this channel. The returned string contains the ID, local address, and remote address of this channel for easier identification. -
voidPromise
Description copied from interface:ChannelOutboundInvoker
Return a special ChannelPromise which can be reused for different operations.It's only supported to use it for
ChannelOutboundInvoker.write(Object, ChannelPromise)
.Be aware that the returned
Be aware this is an expert feature and should be used with care!ChannelPromise
will not support most operations and should only be used if you want to save an object allocation for every write operation. You will not be able to detect if the operation was complete, only if it failed as the implementation will callChannelPipeline.fireExceptionCaught(Throwable)
in this case.- Specified by:
voidPromise
in interfaceChannelOutboundInvoker
-
isCompatible
Returntrue
if the givenEventLoop
is compatible with this instance. -
localAddress0
Returns theSocketAddress
which is bound locally. -
remoteAddress0
Return theSocketAddress
which theChannel
is connected to. -
doRegister
Is called after theChannel
is registered with itsEventLoop
as part of the register process. Sub-classes may override this method- Throws:
Exception
-
doBind
Bind theChannel
to theSocketAddress
- Throws:
Exception
-
doDisconnect
Disconnect thisChannel
from its remote peer- Throws:
Exception
-
doClose
Close theChannel
- Throws:
Exception
-
doShutdownOutput
Called when conditions justify shutting down the output portion of the channel. This may happen if a write operation throws an exception.- Throws:
Exception
-
doDeregister
- Throws:
Exception
-
doBeginRead
Schedule a read operation.- Throws:
Exception
-
doWrite
Flush the content of the given buffer to the remote peer.- Throws:
Exception
-
filterOutboundMessage
Invoked when a new message is added to aChannelOutboundBuffer
of thisAbstractChannel
, so that theChannel
implementation converts the message to another. (e.g. heap buffer -> direct buffer)- Throws:
Exception
-
validateFileRegion
- Throws:
IOException
-