public abstract class AbstractChannel extends DefaultAttributeMap implements Channel
Channel
implementation.Modifier and Type | Class and Description |
---|---|
protected class |
AbstractChannel.AbstractUnsafe
Unsafe implementation which sub-classes must extend and use. |
Channel.Unsafe
Modifier | Constructor and Description |
---|---|
protected |
AbstractChannel(Channel parent)
Creates a new instance.
|
protected |
AbstractChannel(Channel parent,
ChannelId id)
Creates a new instance.
|
Modifier and Type | Method and Description |
---|---|
ByteBufAllocator |
alloc()
Return the assigned
ByteBufAllocator which will be used to allocate ByteBuf s. |
ChannelFuture |
bind(SocketAddress localAddress)
Request to bind to the given
SocketAddress and notify the ChannelFuture once the operation
completes, either because the operation was successful or because of an error. |
ChannelFuture |
bind(SocketAddress localAddress,
ChannelPromise promise)
Request to bind to the given
SocketAddress and notify the ChannelFuture once the operation
completes, either because the operation was successful or because of an error. |
ChannelFuture |
close()
Request to close this
Channel and notify the ChannelFuture once the operation completes,
either because the operation was successful or because of
an error. |
ChannelFuture |
close(ChannelPromise promise)
Request to close this
Channel and notify the ChannelFuture once the operation completes,
either because the operation was successful or because of
an error. |
ChannelFuture |
closeFuture()
Returns the
ChannelFuture which will be notified when this
channel is closed. |
int |
compareTo(Channel o) |
ChannelFuture |
connect(SocketAddress remoteAddress)
Request to connect to the given
SocketAddress and notify the ChannelFuture once the operation
completes, either because the operation was successful or because of an error. |
ChannelFuture |
connect(SocketAddress remoteAddress,
ChannelPromise promise)
Request to connect to the given
SocketAddress and notify the ChannelFuture once the operation
completes, either because the operation was successful or because of an error. |
ChannelFuture |
connect(SocketAddress remoteAddress,
SocketAddress localAddress)
Request to connect to the given
SocketAddress while bind to the localAddress and notify the
ChannelFuture once the operation completes, either because the operation was successful or because of
an error. |
ChannelFuture |
connect(SocketAddress remoteAddress,
SocketAddress localAddress,
ChannelPromise promise)
Request to connect to the given
SocketAddress while bind to the localAddress and notify the
ChannelFuture once the operation completes, either because the operation was successful or because of
an error. |
ChannelFuture |
deregister()
Request to deregister this
Channel from its assigned EventLoop and notify the
ChannelFuture once the operation completes, either because the operation was successful or because of
an error. |
ChannelFuture |
deregister(ChannelPromise promise)
Request to deregister this
Channel from its assigned EventLoop and notify the
ChannelPromise once the operation completes, either because the operation was successful or because of
an error. |
ChannelFuture |
disconnect()
Request to disconnect from the remote peer and notify the
ChannelFuture once the operation completes,
either because the operation was successful or because of an error. |
ChannelFuture |
disconnect(ChannelPromise promise)
Request to disconnect from the remote peer and notify the
ChannelFuture once the operation completes,
either because the operation was successful or because of an error. |
protected abstract void |
doBeginRead()
Schedule a read operation.
|
protected abstract void |
doBind(SocketAddress localAddress)
Bind the
Channel to the SocketAddress |
protected abstract void |
doClose()
Close the
Channel |
protected void |
doDeregister()
|
protected abstract void |
doDisconnect()
Disconnect this
Channel from its remote peer |
protected void |
doRegister()
|
protected abstract void |
doWrite(ChannelOutboundBuffer in)
Flush the content of the given buffer to the remote peer.
|
boolean |
equals(Object o)
Returns
true if and only if the specified object is identical
with this channel (i.e: this == o ). |
EventLoop |
eventLoop()
|
protected Object |
filterOutboundMessage(Object msg)
Invoked when a new message is added to a
ChannelOutboundBuffer of this AbstractChannel , so that
the Channel implementation converts the message to another. |
Channel |
flush()
Request to flush all pending messages.
|
int |
hashCode()
Returns the ID of this channel.
|
ChannelId |
id()
Returns the globally unique identifier of this
Channel . |
protected void |
invalidateLocalAddress() |
protected void |
invalidateRemoteAddress()
Reset the stored remoteAddress
|
protected abstract boolean |
isCompatible(EventLoop loop)
Return
true if the given EventLoop is compatible with this instance. |
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.
|
protected abstract SocketAddress |
localAddress0()
Returns the
SocketAddress which is bound locally. |
ChannelFuture |
newFailedFuture(Throwable cause)
Create a new
ChannelFuture which is marked as failed already. |
ChannelProgressivePromise |
newProgressivePromise()
Return an new
ChannelProgressivePromise |
ChannelPromise |
newPromise()
Return a new
ChannelPromise . |
ChannelFuture |
newSucceededFuture()
Create a new
ChannelFuture which is marked as succeeded already. |
protected abstract AbstractChannel.AbstractUnsafe |
newUnsafe()
Create a new
AbstractChannel.AbstractUnsafe instance which will be used for the life-time of the Channel |
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
ChannelHandler.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.
|
protected abstract SocketAddress |
remoteAddress0()
Return the
SocketAddress which the Channel is connected to. |
String |
toString()
Returns the
String representation of this channel. |
Channel.Unsafe |
unsafe()
Returns an internal-use-only object that provides unsafe operations.
|
ChannelPromise |
voidPromise()
Return a special ChannelPromise which can be reused for different operations.
|
ChannelFuture |
write(Object msg)
Request to write a message via this
Channel through the ChannelPipeline . |
ChannelFuture |
write(Object msg,
ChannelPromise promise)
Request to write a message via this
Channel through the ChannelPipeline . |
ChannelFuture |
writeAndFlush(Object msg)
Shortcut for call
Channel.write(Object) and Channel.flush() . |
ChannelFuture |
writeAndFlush(Object msg,
ChannelPromise promise)
Shortcut for call
Channel.write(Object, ChannelPromise) and Channel.flush() . |
attr, hasAttr
clone, finalize, getClass, notify, notifyAll, wait, wait, wait
attr, hasAttr
protected AbstractChannel(Channel parent)
parent
- the parent of this channel. null
if there's no parent.public final ChannelId id()
Channel
Channel
.public boolean isWritable()
Channel
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.isWritable
in interface Channel
public Channel parent()
Channel
public ChannelPipeline pipeline()
Channel
ChannelPipeline
public ByteBufAllocator alloc()
Channel
ByteBufAllocator
which will be used to allocate ByteBuf
s.public SocketAddress localAddress()
Channel
SocketAddress
is supposed to be down-cast into more concrete
type such as InetSocketAddress
to retrieve the detailed
information.localAddress
in interface Channel
null
if this channel is not bound.protected void invalidateLocalAddress()
public SocketAddress remoteAddress()
Channel
SocketAddress
is supposed to be down-cast into more
concrete type such as InetSocketAddress
to retrieve the detailed
information.remoteAddress
in interface 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
,
use DefaultAddressedEnvelope.recipient()
to determine
the origination of the received message as this method will
return null
.protected void invalidateRemoteAddress()
public boolean isRegistered()
Channel
isRegistered
in interface Channel
public ChannelFuture bind(SocketAddress localAddress)
Channel
SocketAddress
and notify the ChannelFuture
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, ChannelPromise)
method
called of the next ChannelHandler
contained in the ChannelPipeline
of the
Channel
.
public ChannelFuture connect(SocketAddress remoteAddress)
Channel
SocketAddress
and notify the ChannelFuture
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
a ConnectTimeoutException
. If it fails because of connection refused a ConnectException
will be used.
This will result in having the
ChannelHandler.connect(ChannelHandlerContext, SocketAddress, SocketAddress, ChannelPromise)
method called of the next ChannelHandler
contained in the ChannelPipeline
of the
Channel
.
public ChannelFuture connect(SocketAddress remoteAddress, SocketAddress localAddress)
Channel
SocketAddress
while bind to the localAddress and notify the
ChannelFuture
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, ChannelPromise)
method called of the next ChannelHandler
contained in the ChannelPipeline
of the
Channel
.
public ChannelFuture disconnect()
Channel
ChannelFuture
once the operation completes,
either because the operation was successful or because of an error.
This will result in having the
ChannelHandler.disconnect(ChannelHandlerContext, ChannelPromise)
method called of the next ChannelHandler
contained in the ChannelPipeline
of the
Channel
.
disconnect
in interface Channel
public ChannelFuture close()
Channel
Channel
and notify the ChannelFuture
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, ChannelPromise)
method called of the next ChannelHandler
contained in the ChannelPipeline
of the
Channel
.
public ChannelFuture deregister()
Channel
Channel
from its assigned EventLoop
and notify the
ChannelFuture
once the operation completes, either because the operation was successful or because of
an error.
This will result in having the
ChannelHandler.deregister(ChannelHandlerContext, ChannelPromise)
method called of the next ChannelHandler
contained in the ChannelPipeline
of the
Channel
.
After this method completes (not the ChannelFuture
!) one can not submit new tasks to the
Channel
's EventLoop
until the Channel
is again registered with an EventLoop
.
Any attempt to do so will result in a RejectedExecutionException
being thrown.
Any tasks that were submitted before the call to Channel.deregister()
will finish before the
ChannelFuture
completes. Furthermore, periodic and delayed tasks will not be executed until the
Channel
is registered with an EventLoop
again. Theses are tasks submitted
to the EventLoop
via one of the methods declared by ScheduledExecutorService
.
Please note that all of the above only applies to tasks created from within the deregistered Channel
's
ChannelHandler
s.
It's only safe to EventLoopGroup.register(Channel) the Channel
with another (or the same)
EventLoop
after the ChannelFuture
has completed.
deregister
in interface Channel
public Channel flush()
Channel
public ChannelFuture bind(SocketAddress localAddress, ChannelPromise promise)
Channel
SocketAddress
and notify the ChannelFuture
once the operation
completes, either because the operation was successful or because of an error.
The given ChannelPromise
will be notified.
This will result in having the
ChannelHandler.bind(ChannelHandlerContext, SocketAddress, ChannelPromise)
method
called of the next ChannelHandler
contained in the ChannelPipeline
of the
Channel
.
public ChannelFuture connect(SocketAddress remoteAddress, ChannelPromise promise)
Channel
SocketAddress
and notify the ChannelFuture
once the operation
completes, either because the operation was successful or because of an error.
The given ChannelFuture
will be notified.
If the connection fails because of a connection timeout, the ChannelFuture
will get failed with
a ConnectTimeoutException
. If it fails because of connection refused a ConnectException
will be used.
This will result in having the
ChannelHandler.connect(ChannelHandlerContext, SocketAddress, SocketAddress, ChannelPromise)
method called of the next ChannelHandler
contained in the ChannelPipeline
of the
Channel
.
public ChannelFuture connect(SocketAddress remoteAddress, SocketAddress localAddress, ChannelPromise promise)
Channel
SocketAddress
while bind to the localAddress and notify the
ChannelFuture
once the operation completes, either because the operation was successful or because of
an error.
The given ChannelPromise
will be notified and also returned.
This will result in having the
ChannelHandler.connect(ChannelHandlerContext, SocketAddress, SocketAddress, ChannelPromise)
method called of the next ChannelHandler
contained in the ChannelPipeline
of the
Channel
.
public ChannelFuture disconnect(ChannelPromise promise)
Channel
ChannelFuture
once the operation completes,
either because the operation was successful or because of an error.
The given ChannelPromise
will be notified.
This will result in having the
ChannelHandler.disconnect(ChannelHandlerContext, ChannelPromise)
method called of the next ChannelHandler
contained in the ChannelPipeline
of the
Channel
.
disconnect
in interface Channel
public ChannelFuture close(ChannelPromise promise)
Channel
Channel
and notify the ChannelFuture
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 given ChannelPromise
will be notified.
This will result in having the
ChannelHandler.close(ChannelHandlerContext, ChannelPromise)
method called of the next ChannelHandler
contained in the ChannelPipeline
of the
Channel
.
public ChannelFuture deregister(ChannelPromise promise)
Channel
Channel
from its assigned EventLoop
and notify the
ChannelPromise
once the operation completes, either because the operation was successful or because of
an error.
This will result in having the
ChannelHandler.deregister(ChannelHandlerContext, ChannelPromise)
method called of the next ChannelHandler
contained in the ChannelPipeline
of the
Channel
.
After this method completes (not the ChannelPromise
!) one can not submit new tasks to the
Channel
's EventLoop
until the Channel
is again registered with an EventLoop
.
Any attempt to do so will result in a RejectedExecutionException
being thrown.
Any tasks that were submitted before the call to Channel.deregister()
will finish before the
ChannelPromise
completes. Furthermore, periodic and delayed tasks will not be executed until the
Channel
is registered with an EventLoop
again. Theses are tasks submitted
to the EventLoop
via one of the methods declared by ScheduledExecutorService
.
Please note that all of the above only applies to tasks created from within the deregistered Channel
's
ChannelHandler
s.
It's only safe to EventLoopGroup.register(Channel) the Channel
with another (or the same)
EventLoop
after the ChannelPromise
has completed.
deregister
in interface Channel
public Channel read()
Channel
Channel
into the first inbound buffer, triggers an
ChannelHandler.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
ChannelHandler.read(ChannelHandlerContext)
method called of the next ChannelHandler
contained in the ChannelPipeline
of the
Channel
.
public ChannelFuture write(Object msg)
Channel
Channel
through the ChannelPipeline
.
This method will not request to actual flush, so be sure to call Channel.flush()
once you want to request to flush all pending data to the actual transport.public ChannelFuture write(Object msg, ChannelPromise promise)
Channel
Channel
through the ChannelPipeline
.
This method will not request to actual flush, so be sure to call Channel.flush()
once you want to request to flush all pending data to the actual transport.public ChannelFuture writeAndFlush(Object msg)
Channel
Channel.write(Object)
and Channel.flush()
.writeAndFlush
in interface Channel
public ChannelFuture writeAndFlush(Object msg, ChannelPromise promise)
Channel
Channel.write(Object, ChannelPromise)
and Channel.flush()
.writeAndFlush
in interface Channel
public ChannelPromise newPromise()
Channel
ChannelPromise
.newPromise
in interface Channel
public ChannelProgressivePromise newProgressivePromise()
Channel
ChannelProgressivePromise
newProgressivePromise
in interface Channel
public ChannelFuture newSucceededFuture()
Channel
ChannelFuture
which is marked as succeeded already. So Future.isSuccess()
will return true
. All FutureListener
added to it will be notified directly. Also
every call of blocking methods will just return without blocking.newSucceededFuture
in interface Channel
public ChannelFuture newFailedFuture(Throwable cause)
Channel
ChannelFuture
which is marked as failed already. So Future.isSuccess()
will return false
. All FutureListener
added to it will be notified directly. Also
every call of blocking methods will just return without blocking.newFailedFuture
in interface Channel
public ChannelFuture closeFuture()
Channel
ChannelFuture
which will be notified when this
channel is closed. This method always returns the same future instance.closeFuture
in interface Channel
public Channel.Unsafe unsafe()
Channel
protected abstract AbstractChannel.AbstractUnsafe newUnsafe()
AbstractChannel.AbstractUnsafe
instance which will be used for the life-time of the Channel
public final int hashCode()
public final boolean equals(Object o)
true
if and only if the specified object is identical
with this channel (i.e: this == o
).public final int compareTo(Channel o)
compareTo
in interface Comparable<Channel>
public String toString()
String
representation of this channel. The returned
string contains the hashCode() ID}, local address,
and remote address of this channel for
easier identification.public final ChannelPromise voidPromise()
Channel
It's only supported to use
it for Channel.write(Object, ChannelPromise)
.
Be aware that the returned 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 call
ChannelPipeline.fireExceptionCaught(Throwable)
in this case.
voidPromise
in interface Channel
protected abstract boolean isCompatible(EventLoop loop)
true
if the given EventLoop
is compatible with this instance.protected abstract SocketAddress localAddress0()
SocketAddress
which is bound locally.protected abstract SocketAddress remoteAddress0()
SocketAddress
which the Channel
is connected to.protected void doRegister() throws Exception
Channel
is registered with its EventLoop
as part of the register process.
Sub-classes may override this methodException
protected abstract void doBind(SocketAddress localAddress) throws Exception
Channel
to the SocketAddress
Exception
protected abstract void doDisconnect() throws Exception
Channel
from its remote peerException
protected abstract void doBeginRead() throws Exception
Exception
protected abstract void doWrite(ChannelOutboundBuffer in) throws Exception
Exception
protected Object filterOutboundMessage(Object msg) throws Exception
ChannelOutboundBuffer
of this AbstractChannel
, so that
the Channel
implementation converts the message to another. (e.g. heap buffer -> direct buffer)Exception
Copyright © 2008–2015 The Netty Project. All rights reserved.