Class EmbeddedChannel
- java.lang.Object
-
- io.netty.util.DefaultAttributeMap
-
- io.netty.channel.AbstractChannel
-
- io.netty.channel.embedded.EmbeddedChannel
-
- All Implemented Interfaces:
Channel
,ChannelOutboundInvoker
,AttributeMap
,java.lang.Comparable<Channel>
public class EmbeddedChannel extends AbstractChannel
Base class forChannel
implementations that are used in an embedded fashion.
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description private class
EmbeddedChannel.EmbeddedChannelPipeline
private class
EmbeddedChannel.EmbeddedUnsafe
private static class
EmbeddedChannel.State
-
Nested classes/interfaces inherited from class io.netty.channel.AbstractChannel
AbstractChannel.AbstractUnsafe
-
Nested classes/interfaces inherited from interface io.netty.channel.Channel
Channel.Unsafe
-
-
Field Summary
Fields Modifier and Type Field Description private boolean
cancelRemainingScheduledTasks
private ChannelConfig
config
private static ChannelHandler[]
EMPTY_HANDLERS
private int
executingStackCnt
private java.util.Queue<java.lang.Object>
inboundMessages
private java.lang.Throwable
lastException
private static java.net.SocketAddress
LOCAL_ADDRESS
private static InternalLogger
logger
private EmbeddedEventLoop
loop
private ChannelMetadata
metadata
private static ChannelMetadata
METADATA_DISCONNECT
private static ChannelMetadata
METADATA_NO_DISCONNECT
private java.util.Queue<java.lang.Object>
outboundMessages
private ChannelFutureListener
recordExceptionListener
private static java.net.SocketAddress
REMOTE_ADDRESS
private EmbeddedChannel.State
state
-
Constructor Summary
Constructors Constructor Description EmbeddedChannel()
Create a new instance with anEmbeddedChannelId
and an empty pipeline.EmbeddedChannel(boolean register, boolean hasDisconnect, ChannelHandler... handlers)
Create a new instance with the pipeline initialized with the specified handlers.EmbeddedChannel(boolean hasDisconnect, ChannelHandler... handlers)
Create a new instance with the pipeline initialized with the specified handlers.EmbeddedChannel(ChannelHandler... handlers)
Create a new instance with the pipeline initialized with the specified handlers.EmbeddedChannel(ChannelId channelId)
Create a new instance with the specified ID and an empty pipeline.EmbeddedChannel(ChannelId channelId, boolean register, boolean hasDisconnect, ChannelHandler... handlers)
Create a new instance with the channel ID set to the given ID and the pipeline initialized with the specified handlers.EmbeddedChannel(ChannelId channelId, boolean hasDisconnect, ChannelConfig config, ChannelHandler... handlers)
Create a new instance with the channel ID set to the given ID and the pipeline initialized with the specified handlers.EmbeddedChannel(ChannelId channelId, boolean hasDisconnect, ChannelHandler... handlers)
Create a new instance with the channel ID set to the given ID and the pipeline initialized with the specified handlers.EmbeddedChannel(ChannelId channelId, ChannelHandler... handlers)
Create a new instance with the channel ID set to the given ID and the pipeline initialized with the specified handlers.EmbeddedChannel(Channel parent, ChannelId channelId, boolean register, boolean hasDisconnect, ChannelHandler... handlers)
Create a new instance with the channel ID set to the given ID and the pipeline initialized with the specified handlers.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Deprecated Methods Modifier and Type Method Description void
advanceTimeBy(long duration, java.util.concurrent.TimeUnit unit)
Advance the clock of the event loop of this channel by the given duration.ChannelFuture
bind(java.net.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.ChannelFuture
bind(java.net.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.void
checkException()
Check if there was anyThrowable
received and if so rethrow it.private ChannelFuture
checkException(ChannelPromise promise)
Checks for the presence of anException
.private boolean
checkOpen(boolean recordException)
ChannelFuture
close()
Request to close theChannel
and notify theChannelFuture
once the operation completes, either because the operation was successful or because of an error.ChannelFuture
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.ChannelConfig
config()
Returns the configuration of this channel.ChannelFuture
connect(java.net.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.ChannelFuture
connect(java.net.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.ChannelFuture
connect(java.net.SocketAddress remoteAddress, java.net.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.ChannelFuture
connect(java.net.SocketAddress remoteAddress, java.net.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.ChannelFuture
deregister()
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.ChannelFuture
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.ChannelFuture
disconnect()
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.ChannelFuture
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 void
doBeginRead()
Schedule a read operation.protected void
doBind(java.net.SocketAddress localAddress)
Bind theChannel
to theSocketAddress
protected void
doClose()
Close theChannel
protected void
doDisconnect()
Disconnect thisChannel
from its remote peerprotected void
doRegister()
protected void
doWrite(ChannelOutboundBuffer in)
Flush the content of the given buffer to the remote peer.private EmbeddedEventLoop
embeddedEventLoop()
protected void
ensureOpen()
Ensure theChannel
is open and if not throw an exception.boolean
finish()
Mark thisChannel
as finished.private boolean
finish(boolean releaseAll)
Mark thisChannel
as finished.boolean
finishAndReleaseAll()
Mark thisChannel
as finished and release all pending message in the inbound and outbound buffer.Channel
flush()
Request to flush all pending messages via this ChannelOutboundInvoker.EmbeddedChannel
flushInbound()
Flushes the inbound of thisChannel
.private ChannelFuture
flushInbound(boolean recordException, ChannelPromise promise)
EmbeddedChannel
flushOutbound()
Flushes the outbound of thisChannel
.private void
flushOutbound0()
void
freezeTime()
Freeze the clock of this channel's event loop.protected void
handleInboundMessage(java.lang.Object msg)
Called for each inbound message.protected void
handleOutboundMessage(java.lang.Object msg)
Called for each outbound message.boolean
hasPendingTasks()
Check whether this channel has any pending tasks that would be executed by a call torunPendingTasks()
.java.util.Queue<java.lang.Object>
inboundMessages()
boolean
isActive()
Returntrue
if theChannel
is active and so connected.protected boolean
isCompatible(EventLoop loop)
Returntrue
if the givenEventLoop
is compatible with this instance.private static boolean
isNotEmpty(java.util.Queue<java.lang.Object> queue)
boolean
isOpen()
Returnstrue
if theChannel
is open and may get active laterjava.util.Queue<java.lang.Object>
lastInboundBuffer()
Deprecated.java.util.Queue<java.lang.Object>
lastOutboundBuffer()
Deprecated.protected java.net.SocketAddress
localAddress0()
Returns theSocketAddress
which is bound locally.private void
maybeRunPendingTasks()
ChannelMetadata
metadata()
private static ChannelMetadata
metadata(boolean hasDisconnect)
protected DefaultChannelPipeline
newChannelPipeline()
Returns a newDefaultChannelPipeline
instance.protected AbstractChannel.AbstractUnsafe
newUnsafe()
Create a newAbstractChannel.AbstractUnsafe
instance which will be used for the life-time of theChannel
java.util.Queue<java.lang.Object>
outboundMessages()
private static java.lang.Object
poll(java.util.Queue<java.lang.Object> queue)
Channel
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.<T> T
readInbound()
Return received data from thisChannel
<T> T
readOutbound()
Read data from the outbound.private void
recordException(ChannelFuture future)
private void
recordException(java.lang.Throwable cause)
void
register()
Register thisChannel
on itsEventLoop
.private static boolean
releaseAll(java.util.Queue<java.lang.Object> queue)
boolean
releaseInbound()
Release all buffered inbound messages and returntrue
if any were in the inbound buffer,false
otherwise.boolean
releaseOutbound()
Release all buffered outbound messages and returntrue
if any were in the outbound buffer,false
otherwise.protected java.net.SocketAddress
remoteAddress0()
Return theSocketAddress
which theChannel
is connected to.void
runPendingTasks()
long
runScheduledPendingTasks()
private void
setup(boolean register, ChannelHandler... handlers)
void
unfreezeTime()
Unfreeze an event loop that wasfrozen
.Channel.Unsafe
unsafe()
Returns an internal-use-only object that provides unsafe operations.ChannelFuture
write(java.lang.Object msg)
Request to write a message via thisChannelHandlerContext
through theChannelPipeline
.ChannelFuture
write(java.lang.Object msg, ChannelPromise promise)
Request to write a message via thisChannelHandlerContext
through theChannelPipeline
.ChannelFuture
writeAndFlush(java.lang.Object msg)
Shortcut for callChannelOutboundInvoker.write(Object)
andChannelOutboundInvoker.flush()
.ChannelFuture
writeAndFlush(java.lang.Object msg, ChannelPromise promise)
Shortcut for callChannelOutboundInvoker.write(Object, ChannelPromise)
andChannelOutboundInvoker.flush()
.boolean
writeInbound(java.lang.Object... msgs)
Write messages to the inbound of thisChannel
.ChannelFuture
writeOneInbound(java.lang.Object msg)
Writes one message to the inbound of thisChannel
and does not flush it.ChannelFuture
writeOneInbound(java.lang.Object msg, ChannelPromise promise)
Writes one message to the inbound of thisChannel
and does not flush it.ChannelFuture
writeOneOutbound(java.lang.Object msg)
Writes one message to the outbound of thisChannel
and does not flush it.ChannelFuture
writeOneOutbound(java.lang.Object msg, ChannelPromise promise)
Writes one message to the outbound of thisChannel
and does not flush it.boolean
writeOutbound(java.lang.Object... msgs)
Write messages to the outbound of thisChannel
.-
Methods inherited from class io.netty.channel.AbstractChannel
alloc, bytesBeforeUnwritable, bytesBeforeWritable, closeFuture, compareTo, doDeregister, doShutdownOutput, equals, eventLoop, filterOutboundMessage, hashCode, id, invalidateLocalAddress, invalidateRemoteAddress, isRegistered, isWritable, localAddress, maxMessagesPerWrite, newFailedFuture, newId, newProgressivePromise, newPromise, newSucceededFuture, parent, pipeline, remoteAddress, toString, validateFileRegion, voidPromise
-
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 Detail
-
LOCAL_ADDRESS
private static final java.net.SocketAddress LOCAL_ADDRESS
-
REMOTE_ADDRESS
private static final java.net.SocketAddress REMOTE_ADDRESS
-
EMPTY_HANDLERS
private static final ChannelHandler[] EMPTY_HANDLERS
-
logger
private static final InternalLogger logger
-
METADATA_NO_DISCONNECT
private static final ChannelMetadata METADATA_NO_DISCONNECT
-
METADATA_DISCONNECT
private static final ChannelMetadata METADATA_DISCONNECT
-
loop
private final EmbeddedEventLoop loop
-
recordExceptionListener
private final ChannelFutureListener recordExceptionListener
-
metadata
private final ChannelMetadata metadata
-
config
private final ChannelConfig config
-
inboundMessages
private java.util.Queue<java.lang.Object> inboundMessages
-
outboundMessages
private java.util.Queue<java.lang.Object> outboundMessages
-
lastException
private java.lang.Throwable lastException
-
state
private EmbeddedChannel.State state
-
executingStackCnt
private int executingStackCnt
-
cancelRemainingScheduledTasks
private boolean cancelRemainingScheduledTasks
-
-
Constructor Detail
-
EmbeddedChannel
public EmbeddedChannel()
Create a new instance with anEmbeddedChannelId
and an empty pipeline.
-
EmbeddedChannel
public EmbeddedChannel(ChannelId channelId)
Create a new instance with the specified ID and an empty pipeline.- Parameters:
channelId
- theChannelId
that will be used to identify this channel
-
EmbeddedChannel
public EmbeddedChannel(ChannelHandler... handlers)
Create a new instance with the pipeline initialized with the specified handlers.- Parameters:
handlers
- theChannelHandler
s which will be add in theChannelPipeline
-
EmbeddedChannel
public EmbeddedChannel(boolean hasDisconnect, ChannelHandler... handlers)
Create a new instance with the pipeline initialized with the specified handlers.- Parameters:
hasDisconnect
-false
if thisChannel
will delegatedisconnect()
toclose()
,true
otherwise.handlers
- theChannelHandler
s which will be added to theChannelPipeline
-
EmbeddedChannel
public EmbeddedChannel(boolean register, boolean hasDisconnect, ChannelHandler... handlers)
Create a new instance with the pipeline initialized with the specified handlers.- Parameters:
register
-true
if thisChannel
is registered to theEventLoop
in the constructor. Iffalse
the user will need to callregister()
.hasDisconnect
-false
if thisChannel
will delegatedisconnect()
toclose()
,true
otherwise.handlers
- theChannelHandler
s which will be added to theChannelPipeline
-
EmbeddedChannel
public EmbeddedChannel(ChannelId channelId, ChannelHandler... handlers)
Create a new instance with the channel ID set to the given ID and the pipeline initialized with the specified handlers.- Parameters:
channelId
- theChannelId
that will be used to identify this channelhandlers
- theChannelHandler
s which will be added to theChannelPipeline
-
EmbeddedChannel
public EmbeddedChannel(ChannelId channelId, boolean hasDisconnect, ChannelHandler... handlers)
Create a new instance with the channel ID set to the given ID and the pipeline initialized with the specified handlers.- Parameters:
channelId
- theChannelId
that will be used to identify this channelhasDisconnect
-false
if thisChannel
will delegatedisconnect()
toclose()
,true
otherwise.handlers
- theChannelHandler
s which will be added to theChannelPipeline
-
EmbeddedChannel
public EmbeddedChannel(ChannelId channelId, boolean register, boolean hasDisconnect, ChannelHandler... handlers)
Create a new instance with the channel ID set to the given ID and the pipeline initialized with the specified handlers.- Parameters:
channelId
- theChannelId
that will be used to identify this channelregister
-true
if thisChannel
is registered to theEventLoop
in the constructor. Iffalse
the user will need to callregister()
.hasDisconnect
-false
if thisChannel
will delegatedisconnect()
toclose()
,true
otherwise.handlers
- theChannelHandler
s which will be added to theChannelPipeline
-
EmbeddedChannel
public EmbeddedChannel(Channel parent, ChannelId channelId, boolean register, boolean hasDisconnect, ChannelHandler... handlers)
Create a new instance with the channel ID set to the given ID and the pipeline initialized with the specified handlers.- Parameters:
parent
- the parentChannel
of thisEmbeddedChannel
.channelId
- theChannelId
that will be used to identify this channelregister
-true
if thisChannel
is registered to theEventLoop
in the constructor. Iffalse
the user will need to callregister()
.hasDisconnect
-false
if thisChannel
will delegatedisconnect()
toclose()
,true
otherwise.handlers
- theChannelHandler
s which will be added to theChannelPipeline
-
EmbeddedChannel
public EmbeddedChannel(ChannelId channelId, boolean hasDisconnect, ChannelConfig config, ChannelHandler... handlers)
Create a new instance with the channel ID set to the given ID and the pipeline initialized with the specified handlers.- Parameters:
channelId
- theChannelId
that will be used to identify this channelhasDisconnect
-false
if thisChannel
will delegatedisconnect()
toclose()
,true
otherwise.config
- theChannelConfig
which will be returned byconfig()
.handlers
- theChannelHandler
s which will be added to theChannelPipeline
-
-
Method Detail
-
metadata
private static ChannelMetadata metadata(boolean hasDisconnect)
-
setup
private void setup(boolean register, ChannelHandler... handlers)
-
register
public void register() throws java.lang.Exception
Register thisChannel
on itsEventLoop
.- Throws:
java.lang.Exception
-
newChannelPipeline
protected final DefaultChannelPipeline newChannelPipeline()
Description copied from class:AbstractChannel
Returns a newDefaultChannelPipeline
instance.- Overrides:
newChannelPipeline
in classAbstractChannel
-
metadata
public ChannelMetadata metadata()
Description copied from interface:Channel
-
config
public ChannelConfig config()
Description copied from interface:Channel
Returns the configuration of this channel.
-
isOpen
public boolean isOpen()
Description copied from interface:Channel
Returnstrue
if theChannel
is open and may get active later
-
isActive
public boolean isActive()
Description copied from interface:Channel
Returntrue
if theChannel
is active and so connected.
-
inboundMessages
public java.util.Queue<java.lang.Object> inboundMessages()
-
lastInboundBuffer
@Deprecated public java.util.Queue<java.lang.Object> lastInboundBuffer()
Deprecated.
-
outboundMessages
public java.util.Queue<java.lang.Object> outboundMessages()
-
lastOutboundBuffer
@Deprecated public java.util.Queue<java.lang.Object> lastOutboundBuffer()
Deprecated.
-
readInbound
public <T> T readInbound()
Return received data from thisChannel
-
readOutbound
public <T> T readOutbound()
Read data from the outbound. This may returnnull
if nothing is readable.
-
writeInbound
public boolean writeInbound(java.lang.Object... msgs)
Write messages to the inbound of thisChannel
.- Parameters:
msgs
- the messages to be written- Returns:
true
if the write operation did add something to the inbound buffer
-
writeOneInbound
public ChannelFuture writeOneInbound(java.lang.Object msg)
Writes one message to the inbound of thisChannel
and does not flush it. This method is conceptually equivalent towrite(Object)
.- See Also:
writeOneOutbound(Object)
-
writeOneInbound
public ChannelFuture writeOneInbound(java.lang.Object msg, ChannelPromise promise)
Writes one message to the inbound of thisChannel
and does not flush it. This method is conceptually equivalent towrite(Object, ChannelPromise)
.- See Also:
writeOneOutbound(Object, ChannelPromise)
-
flushInbound
public EmbeddedChannel flushInbound()
- See Also:
flushOutbound()
-
flushInbound
private ChannelFuture flushInbound(boolean recordException, ChannelPromise promise)
-
writeOutbound
public boolean writeOutbound(java.lang.Object... msgs)
Write messages to the outbound of thisChannel
.- Parameters:
msgs
- the messages to be written- Returns:
- bufferReadable returns
true
if the write operation did add something to the outbound buffer
-
writeOneOutbound
public ChannelFuture writeOneOutbound(java.lang.Object msg)
Writes one message to the outbound of thisChannel
and does not flush it. This method is conceptually equivalent towrite(Object)
.- See Also:
writeOneInbound(Object)
-
writeOneOutbound
public ChannelFuture writeOneOutbound(java.lang.Object msg, ChannelPromise promise)
Writes one message to the outbound of thisChannel
and does not flush it. This method is conceptually equivalent towrite(Object, ChannelPromise)
.- See Also:
writeOneInbound(Object, ChannelPromise)
-
flushOutbound
public EmbeddedChannel flushOutbound()
- See Also:
flushInbound()
-
flushOutbound0
private void flushOutbound0()
-
finish
public boolean finish()
Mark thisChannel
as finished. Any further try to write data to it will fail.- Returns:
- bufferReadable returns
true
if any of the used buffers has something left to read
-
finishAndReleaseAll
public boolean finishAndReleaseAll()
Mark thisChannel
as finished and release all pending message in the inbound and outbound buffer. Any further try to write data to it will fail.- Returns:
- bufferReadable returns
true
if any of the used buffers has something left to read
-
finish
private boolean finish(boolean releaseAll)
Mark thisChannel
as finished. Any further try to write data to it will fail.- Parameters:
releaseAll
- iftrue
all pending message in the inbound and outbound buffer are released.- Returns:
- bufferReadable returns
true
if any of the used buffers has something left to read
-
releaseInbound
public boolean releaseInbound()
Release all buffered inbound messages and returntrue
if any were in the inbound buffer,false
otherwise.
-
releaseOutbound
public boolean releaseOutbound()
Release all buffered outbound messages and returntrue
if any were in the outbound buffer,false
otherwise.
-
releaseAll
private static boolean releaseAll(java.util.Queue<java.lang.Object> queue)
-
close
public final ChannelFuture 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
- Overrides:
close
in classAbstractChannel
-
disconnect
public final ChannelFuture 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
- Overrides:
disconnect
in classAbstractChannel
-
close
public final ChannelFuture close(ChannelPromise promise)
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
- Overrides:
close
in classAbstractChannel
-
disconnect
public final ChannelFuture disconnect(ChannelPromise promise)
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
- Overrides:
disconnect
in classAbstractChannel
-
bind
public ChannelFuture bind(java.net.SocketAddress localAddress)
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
- Overrides:
bind
in classAbstractChannel
-
connect
public ChannelFuture connect(java.net.SocketAddress remoteAddress)
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
- Overrides:
connect
in classAbstractChannel
-
connect
public ChannelFuture connect(java.net.SocketAddress remoteAddress, java.net.SocketAddress localAddress)
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
- Overrides:
connect
in classAbstractChannel
-
deregister
public ChannelFuture 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
- Overrides:
deregister
in classAbstractChannel
-
flush
public Channel 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
- Overrides:
flush
in classAbstractChannel
-
bind
public ChannelFuture bind(java.net.SocketAddress localAddress, ChannelPromise promise)
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
- Overrides:
bind
in classAbstractChannel
-
connect
public ChannelFuture connect(java.net.SocketAddress remoteAddress, ChannelPromise promise)
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
- Overrides:
connect
in classAbstractChannel
-
connect
public ChannelFuture connect(java.net.SocketAddress remoteAddress, java.net.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
- Overrides:
connect
in classAbstractChannel
-
deregister
public ChannelFuture deregister(ChannelPromise promise)
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
- Overrides:
deregister
in classAbstractChannel
-
read
public Channel 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
- Overrides:
read
in classAbstractChannel
-
write
public ChannelFuture write(java.lang.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
- Overrides:
write
in classAbstractChannel
-
write
public ChannelFuture write(java.lang.Object msg, ChannelPromise promise)
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
- Overrides:
write
in classAbstractChannel
-
writeAndFlush
public ChannelFuture writeAndFlush(java.lang.Object msg)
Description copied from interface:ChannelOutboundInvoker
Shortcut for callChannelOutboundInvoker.write(Object)
andChannelOutboundInvoker.flush()
.- Specified by:
writeAndFlush
in interfaceChannelOutboundInvoker
- Overrides:
writeAndFlush
in classAbstractChannel
-
writeAndFlush
public ChannelFuture writeAndFlush(java.lang.Object msg, ChannelPromise promise)
Description copied from interface:ChannelOutboundInvoker
Shortcut for callChannelOutboundInvoker.write(Object, ChannelPromise)
andChannelOutboundInvoker.flush()
.- Specified by:
writeAndFlush
in interfaceChannelOutboundInvoker
- Overrides:
writeAndFlush
in classAbstractChannel
-
isNotEmpty
private static boolean isNotEmpty(java.util.Queue<java.lang.Object> queue)
-
poll
private static java.lang.Object poll(java.util.Queue<java.lang.Object> queue)
-
maybeRunPendingTasks
private void maybeRunPendingTasks()
-
runPendingTasks
public void runPendingTasks()
-
hasPendingTasks
public boolean hasPendingTasks()
Check whether this channel has any pending tasks that would be executed by a call torunPendingTasks()
. This includes normal tasks, and scheduled tasks where the deadline has expired. If this method returnsfalse
, a call torunPendingTasks()
would do nothing.- Returns:
true
if there are any pending tasks,false
otherwise.
-
runScheduledPendingTasks
public long runScheduledPendingTasks()
-
recordException
private void recordException(ChannelFuture future)
-
recordException
private void recordException(java.lang.Throwable cause)
-
advanceTimeBy
public void advanceTimeBy(long duration, java.util.concurrent.TimeUnit unit)
Advance the clock of the event loop of this channel by the given duration. Any scheduled tasks will execute sooner by the given time (butrunScheduledPendingTasks()
still needs to be called).
-
freezeTime
public void freezeTime()
Freeze the clock of this channel's event loop. Any scheduled tasks that are not already due will not run on futurerunScheduledPendingTasks()
calls. While the event loop is frozen, it is still possible toadvance time
manually so that scheduled tasks execute.
-
unfreezeTime
public void unfreezeTime()
Unfreeze an event loop that wasfrozen
. Time will continue at the point wherefreezeTime()
stopped it: if a task was scheduled ten minutes in the future andfreezeTime()
was called, it will run ten minutes after this method is called again (assuming noadvanceTimeBy(long, TimeUnit)
calls, and assuming pending scheduled tasks are run at that time usingrunScheduledPendingTasks()
).
-
checkException
private ChannelFuture checkException(ChannelPromise promise)
Checks for the presence of anException
.
-
checkException
public void checkException()
Check if there was anyThrowable
received and if so rethrow it.
-
checkOpen
private boolean checkOpen(boolean recordException)
-
embeddedEventLoop
private EmbeddedEventLoop embeddedEventLoop()
-
ensureOpen
protected final void ensureOpen()
Ensure theChannel
is open and if not throw an exception.
-
isCompatible
protected boolean isCompatible(EventLoop loop)
Description copied from class:AbstractChannel
Returntrue
if the givenEventLoop
is compatible with this instance.- Specified by:
isCompatible
in classAbstractChannel
-
localAddress0
protected java.net.SocketAddress localAddress0()
Description copied from class:AbstractChannel
Returns theSocketAddress
which is bound locally.- Specified by:
localAddress0
in classAbstractChannel
-
remoteAddress0
protected java.net.SocketAddress remoteAddress0()
Description copied from class:AbstractChannel
Return theSocketAddress
which theChannel
is connected to.- Specified by:
remoteAddress0
in classAbstractChannel
-
doRegister
protected void doRegister() throws java.lang.Exception
Description copied from class:AbstractChannel
Is called after theChannel
is registered with itsEventLoop
as part of the register process. Sub-classes may override this method- Overrides:
doRegister
in classAbstractChannel
- Throws:
java.lang.Exception
-
doBind
protected void doBind(java.net.SocketAddress localAddress) throws java.lang.Exception
Description copied from class:AbstractChannel
Bind theChannel
to theSocketAddress
- Specified by:
doBind
in classAbstractChannel
- Throws:
java.lang.Exception
-
doDisconnect
protected void doDisconnect() throws java.lang.Exception
Description copied from class:AbstractChannel
Disconnect thisChannel
from its remote peer- Specified by:
doDisconnect
in classAbstractChannel
- Throws:
java.lang.Exception
-
doClose
protected void doClose() throws java.lang.Exception
Description copied from class:AbstractChannel
Close theChannel
- Specified by:
doClose
in classAbstractChannel
- Throws:
java.lang.Exception
-
doBeginRead
protected void doBeginRead() throws java.lang.Exception
Description copied from class:AbstractChannel
Schedule a read operation.- Specified by:
doBeginRead
in classAbstractChannel
- Throws:
java.lang.Exception
-
newUnsafe
protected AbstractChannel.AbstractUnsafe newUnsafe()
Description copied from class:AbstractChannel
Create a newAbstractChannel.AbstractUnsafe
instance which will be used for the life-time of theChannel
- Specified by:
newUnsafe
in classAbstractChannel
-
unsafe
public Channel.Unsafe unsafe()
Description copied from interface:Channel
Returns an internal-use-only object that provides unsafe operations.- Specified by:
unsafe
in interfaceChannel
- Overrides:
unsafe
in classAbstractChannel
-
doWrite
protected void doWrite(ChannelOutboundBuffer in) throws java.lang.Exception
Description copied from class:AbstractChannel
Flush the content of the given buffer to the remote peer.- Specified by:
doWrite
in classAbstractChannel
- Throws:
java.lang.Exception
-
handleOutboundMessage
protected void handleOutboundMessage(java.lang.Object msg)
Called for each outbound message.- See Also:
doWrite(ChannelOutboundBuffer)
-
handleInboundMessage
protected void handleInboundMessage(java.lang.Object msg)
Called for each inbound message.
-
-