- java.lang.Object
-
- io.netty5.util.DefaultAttributeMap
-
- io.netty5.channel.AbstractChannel<Channel,SocketAddress,SocketAddress>
-
- io.netty5.channel.embedded.EmbeddedChannel
-
- All Implemented Interfaces:
Channel
,ChannelOutboundInvoker
,IoHandle
,AttributeMap
,FuturePromiseFactory
,Comparable<Channel>
public class EmbeddedChannel extends AbstractChannel<Channel,SocketAddress,SocketAddress>
Base class forChannel
implementations that are used in an embedded fashion.
-
-
Nested Class Summary
-
Nested classes/interfaces inherited from class io.netty5.channel.AbstractChannel
AbstractChannel.DefaultAbstractChannelPipeline
-
-
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, 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 Instance Methods Concrete Methods Deprecated Methods Modifier and Type Method Description void
advanceTimeBy(long duration, TimeUnit unit)
Advance the clock of the event loop of this channel by the given duration.void
checkException()
Check if there was anyThrowable
received and if so rethrow it.Future<Void>
close()
Future<Void>
disconnect()
Request to disconnect from the remote peer and notify theFuture
once the operation completes, either because the operation was successful or because of an error.protected void
doBeginRead()
Schedule a read operation.protected void
doBind(SocketAddress localAddress)
Bind theChannel
to theSocketAddress
protected void
doClose()
Close theChannel
protected boolean
doConnect(SocketAddress remoteAddress, SocketAddress localAddress)
Connect to remote peer.protected void
doDisconnect()
Disconnect thisChannel
from its remote peerprotected boolean
doFinishConnect(SocketAddress requestedRemoteAddress)
Finish a connect request.protected void
doShutdown(ChannelShutdownDirection direction)
Shutdown one direction of theChannel
.protected void
doWrite(ChannelOutboundBuffer in)
Flush the content of the given buffer to the remote peer.protected void
ensureOpen()
Ensure theChannel
is open and if not throw an exception.boolean
finish()
Mark thisChannel
as finished.boolean
finishAndReleaseAll()
Mark thisChannel
as finished and release all pending message in the inbound and outbound buffer.EmbeddedChannel
flushInbound()
Flushes the inbound of thisChannel
.EmbeddedChannel
flushOutbound()
Flushes the outbound of thisChannel
.void
freezeTime()
Freeze the clock of this channel's event loop.protected void
handleInboundMessage(Object msg)
Called for each inbound message.protected void
handleOutboundMessage(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()
.Queue<Object>
inboundMessages()
boolean
isActive()
Returntrue
if theChannel
is active and so connected.boolean
isOpen()
Returnstrue
if theChannel
is open and may get active laterboolean
isShutdown(ChannelShutdownDirection direction)
Queue<Object>
lastInboundBuffer()
Deprecated.Queue<Object>
lastOutboundBuffer()
Deprecated.protected SocketAddress
localAddress0()
Returns theSocketAddress
which is bound locally.protected DefaultChannelPipeline
newChannelPipeline()
Returns a newChannelPipeline
instance.Queue<Object>
outboundMessages()
<T> T
readInbound()
Return received data from thisChannel
<T> T
readOutbound()
Read data from the outbound.Future<Void>
register()
Request to register on theEventExecutor
for I/O processing.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 SocketAddress
remoteAddress0()
Return theSocketAddress
which theChannel
is connected to.protected void
runAfterTransportAction()
void
runPendingTasks()
long
runScheduledPendingTasks()
void
unfreezeTime()
Unfreeze an event loop that wasfrozen
.boolean
writeInbound(Object... msgs)
Write messages to the inbound of thisChannel
.Future<Void>
writeOneInbound(Object msg)
Writes one message to the inbound of thisChannel
and does not flush it.Future<Void>
writeOneOutbound(Object msg)
Writes one message to the outbound of thisChannel
and does not flush it.boolean
writeOutbound(Object... msgs)
Write messages to the outbound of thisChannel
.-
Methods inherited from class io.netty5.channel.AbstractChannel
annotateConnectException, assertEventLoop, autoReadCleared, bufferAllocator, cacheAddresses, closeForciblyTransport, closeFuture, closeIfClosed, closeTransport, compareTo, deregisterTransport, ensureOpen, equals, executor, filterOutboundMessage, finishConnect, fireChannelActiveIfNotActiveBefore, getExtendedOption, getMaxMessagesPerWrite, getOption, getWriteSpinCount, handleWriteError, hashCode, id, isAllowHalfClosure, isAutoRead, isConnectPending, isExtendedOptionSupported, isOptionSupported, isRegistered, localAddress, metadata, newSupportedIdentityOptionsSet, outboundBuffer, parent, pipeline, prepareToClose, readIfIsAutoRead, recvBufAllocHandle, remoteAddress, safeSetFailure, safeSetSuccess, setBufferAllocator, setExtendedOption, setOption, shutdownTransport, toString, validate, validateEventLoopGroup, validateFileRegion, writableBytes, writeFlushed
-
Methods inherited from class io.netty5.util.DefaultAttributeMap
attr, hasAttr
-
Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, wait
-
Methods inherited from interface io.netty5.util.AttributeMap
attr, hasAttr
-
Methods inherited from interface io.netty5.channel.Channel
bind, connect, connect, deregister, flush, isWritable, read, sendOutboundEvent, shutdown, write, writeAndFlush
-
Methods inherited from interface io.netty5.channel.ChannelOutboundInvoker
newFailedFuture, newPromise, newSucceededFuture, newSucceededFuture
-
-
-
-
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()
, {@link false} otherwise.handlers
- theChannelHandler
s which will be add in 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()
, {@link false} otherwise.handlers
- theChannelHandler
s which will be add in 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 add in 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()
, {@link false} otherwise.handlers
- theChannelHandler
s which will be add in 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()
, {@link false} otherwise.handlers
- theChannelHandler
s which will be add in 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()
, {@link false} otherwise.handlers
- theChannelHandler
s which will be add in theChannelPipeline
-
-
Method Detail
-
register
public Future<Void> register()
Description copied from interface:ChannelOutboundInvoker
Request to register on theEventExecutor
for I/O processing.Future
once the operation completes, either because the operation was successful or because of an error.This will result in having the
ChannelHandler.register(ChannelHandlerContext)
method called of the nextChannelHandler
contained in theChannelPipeline
of theChannel
.
-
newChannelPipeline
protected final DefaultChannelPipeline newChannelPipeline()
Description copied from class:AbstractChannel
Returns a newChannelPipeline
instance.- Overrides:
newChannelPipeline
in classAbstractChannel<Channel,SocketAddress,SocketAddress>
-
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.
-
lastInboundBuffer
@Deprecated public Queue<Object> lastInboundBuffer()
Deprecated.
-
lastOutboundBuffer
@Deprecated public Queue<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(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 Future<Void> writeOneInbound(Object msg)
Writes one message to the inbound of thisChannel
and does not flush it. This method is conceptually equivalent toChannel.write(Object)
.- See Also:
writeOneOutbound(Object)
-
flushInbound
public EmbeddedChannel flushInbound()
Flushes the inbound of thisChannel
. This method is conceptually equivalent toChannel.flush()
.- See Also:
flushOutbound()
-
writeOutbound
public boolean writeOutbound(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 Future<Void> writeOneOutbound(Object msg)
Writes one message to the outbound of thisChannel
and does not flush it. This method is conceptually equivalent toChannel.write(Object)
.- See Also:
writeOneInbound(Object)
-
flushOutbound
public EmbeddedChannel flushOutbound()
Flushes the outbound of thisChannel
. This method is conceptually equivalent toChannel.flush()
.- See Also:
flushInbound()
-
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
-
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.
-
close
public final Future<Void> close()
Description copied from interface:ChannelOutboundInvoker
Request to close theChannel
and notify theFuture
once the operation completes, either because the operation was successful or because of an error. After it is closed it is not possible to reuse it again.This will result in having the
ChannelHandler.close(ChannelHandlerContext)
method called of the nextChannelHandler
contained in theChannelPipeline
of theChannel
.
-
disconnect
public final Future<Void> disconnect()
Description copied from interface:ChannelOutboundInvoker
Request to disconnect from the remote peer and notify theFuture
once the operation completes, either because the operation was successful or because of an error.This will result in having the
ChannelHandler.disconnect(ChannelHandlerContext)
method called of the nextChannelHandler
contained in theChannelPipeline
of theChannel
.
-
runPendingTasks
public void runPendingTasks()
-
runScheduledPendingTasks
public long runScheduledPendingTasks()
-
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.
-
advanceTimeBy
public void advanceTimeBy(long duration, 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
public void checkException()
Check if there was anyThrowable
received and if so rethrow it.
-
ensureOpen
protected final void ensureOpen()
Ensure theChannel
is open and if not throw an exception.
-
localAddress0
protected SocketAddress localAddress0()
Description copied from class:AbstractChannel
Returns theSocketAddress
which is bound locally.- Specified by:
localAddress0
in classAbstractChannel<Channel,SocketAddress,SocketAddress>
-
remoteAddress0
protected SocketAddress remoteAddress0()
Description copied from class:AbstractChannel
Return theSocketAddress
which theChannel
is connected to.- Specified by:
remoteAddress0
in classAbstractChannel<Channel,SocketAddress,SocketAddress>
-
doBind
protected void doBind(SocketAddress localAddress) throws Exception
Description copied from class:AbstractChannel
Bind theChannel
to theSocketAddress
- Specified by:
doBind
in classAbstractChannel<Channel,SocketAddress,SocketAddress>
- Throws:
Exception
-
doShutdown
protected void doShutdown(ChannelShutdownDirection direction)
Description copied from class:AbstractChannel
Shutdown one direction of theChannel
.- Specified by:
doShutdown
in classAbstractChannel<Channel,SocketAddress,SocketAddress>
- Parameters:
direction
- the direction to shutdown.
-
isShutdown
public boolean isShutdown(ChannelShutdownDirection direction)
Description copied from interface:Channel
-
doDisconnect
protected void doDisconnect() throws Exception
Description copied from class:AbstractChannel
Disconnect thisChannel
from its remote peer- Specified by:
doDisconnect
in classAbstractChannel<Channel,SocketAddress,SocketAddress>
- Throws:
Exception
-
doClose
protected void doClose() throws Exception
Description copied from class:AbstractChannel
Close theChannel
- Specified by:
doClose
in classAbstractChannel<Channel,SocketAddress,SocketAddress>
- Throws:
Exception
-
doBeginRead
protected void doBeginRead() throws Exception
Description copied from class:AbstractChannel
Schedule a read operation.- Specified by:
doBeginRead
in classAbstractChannel<Channel,SocketAddress,SocketAddress>
- Throws:
Exception
-
doWrite
protected void doWrite(ChannelOutboundBuffer in) throws Exception
Description copied from class:AbstractChannel
Flush the content of the given buffer to the remote peer.- Specified by:
doWrite
in classAbstractChannel<Channel,SocketAddress,SocketAddress>
- Throws:
Exception
-
handleOutboundMessage
protected void handleOutboundMessage(Object msg)
Called for each outbound message.- See Also:
doWrite(ChannelOutboundBuffer)
-
handleInboundMessage
protected void handleInboundMessage(Object msg)
Called for each inbound message.
-
runAfterTransportAction
protected void runAfterTransportAction()
- Overrides:
runAfterTransportAction
in classAbstractChannel<Channel,SocketAddress,SocketAddress>
-
doConnect
protected boolean doConnect(SocketAddress remoteAddress, SocketAddress localAddress)
Description copied from class:AbstractChannel
Connect to remote peer.- Specified by:
doConnect
in classAbstractChannel<Channel,SocketAddress,SocketAddress>
- Parameters:
remoteAddress
- the address of the remote peer.localAddress
- the local address of this channel.- Returns:
true
if the connect was completed,false
ifAbstractChannel.finishConnect()
will be called later again to try finishing the connect.
-
doFinishConnect
protected boolean doFinishConnect(SocketAddress requestedRemoteAddress)
Description copied from class:AbstractChannel
Finish a connect request.- Specified by:
doFinishConnect
in classAbstractChannel<Channel,SocketAddress,SocketAddress>
- Parameters:
requestedRemoteAddress
- the remote address of the peer.- Returns:
true
if the connect was completed,false
ifAbstractChannel.finishConnect()
will be called later again to try finishing the connect.
-
-