- 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 forChannelimplementations 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 anEmbeddedChannelIdand 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 voidadvanceTimeBy(long duration, TimeUnit unit)Advance the clock of the event loop of this channel by the given duration.voidcheckException()Check if there was anyThrowablereceived and if so rethrow it.Future<Void>close()Future<Void>disconnect()Request to disconnect from the remote peer and notify theFutureonce the operation completes, either because the operation was successful or because of an error.protected voiddoBeginRead()Schedule a read operation.protected voiddoBind(SocketAddress localAddress)Bind theChannelto theSocketAddressprotected voiddoClose()Close theChannelprotected booleandoConnect(SocketAddress remoteAddress, SocketAddress localAddress)Connect to remote peer.protected voiddoDisconnect()Disconnect thisChannelfrom its remote peerprotected booleandoFinishConnect(SocketAddress requestedRemoteAddress)Finish a connect request.protected voiddoShutdown(ChannelShutdownDirection direction)Shutdown one direction of theChannel.protected voiddoWrite(ChannelOutboundBuffer in)Flush the content of the given buffer to the remote peer.protected voidensureOpen()Ensure theChannelis open and if not throw an exception.booleanfinish()Mark thisChannelas finished.booleanfinishAndReleaseAll()Mark thisChannelas finished and release all pending message in the inbound and outbound buffer.EmbeddedChannelflushInbound()Flushes the inbound of thisChannel.EmbeddedChannelflushOutbound()Flushes the outbound of thisChannel.voidfreezeTime()Freeze the clock of this channel's event loop.protected voidhandleInboundMessage(Object msg)Called for each inbound message.protected voidhandleOutboundMessage(Object msg)Called for each outbound message.booleanhasPendingTasks()Check whether this channel has any pending tasks that would be executed by a call torunPendingTasks().Queue<Object>inboundMessages()booleanisActive()Returntrueif theChannelis active and so connected.booleanisOpen()Returnstrueif theChannelis open and may get active laterbooleanisShutdown(ChannelShutdownDirection direction)Queue<Object>lastInboundBuffer()Deprecated.Queue<Object>lastOutboundBuffer()Deprecated.protected SocketAddresslocalAddress0()Returns theSocketAddresswhich is bound locally.protected DefaultChannelPipelinenewChannelPipeline()Returns a newChannelPipelineinstance.Queue<Object>outboundMessages()<T> TreadInbound()Return received data from thisChannel<T> TreadOutbound()Read data from the outbound.Future<Void>register()Request to register on theEventExecutorfor I/O processing.booleanreleaseInbound()Release all buffered inbound messages and returntrueif any were in the inbound buffer,falseotherwise.booleanreleaseOutbound()Release all buffered outbound messages and returntrueif any were in the outbound buffer,falseotherwise.protected SocketAddressremoteAddress0()Return theSocketAddresswhich theChannelis connected to.protected voidrunAfterTransportAction()voidrunPendingTasks()longrunScheduledPendingTasks()voidunfreezeTime()Unfreeze an event loop that wasfrozen.booleanwriteInbound(Object... msgs)Write messages to the inbound of thisChannel.Future<Void>writeOneInbound(Object msg)Writes one message to the inbound of thisChanneland does not flush it.Future<Void>writeOneOutbound(Object msg)Writes one message to the outbound of thisChanneland does not flush it.booleanwriteOutbound(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 anEmbeddedChannelIdand an empty pipeline.
-
EmbeddedChannel
public EmbeddedChannel(ChannelId channelId)
Create a new instance with the specified ID and an empty pipeline.- Parameters:
channelId- theChannelIdthat 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- theChannelHandlers 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-falseif thisChannelwill delegatedisconnect()toclose(), {@link false} otherwise.handlers- theChannelHandlers 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-trueif thisChannelis registered to theEventLoopin the constructor. Iffalsethe user will need to callregister().hasDisconnect-falseif thisChannelwill delegatedisconnect()toclose(), {@link false} otherwise.handlers- theChannelHandlers 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- theChannelIdthat will be used to identify this channelhandlers- theChannelHandlers 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- theChannelIdthat will be used to identify this channelhasDisconnect-falseif thisChannelwill delegatedisconnect()toclose(), {@link false} otherwise.handlers- theChannelHandlers 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- theChannelIdthat will be used to identify this channelregister-trueif thisChannelis registered to theEventLoopin the constructor. Iffalsethe user will need to callregister().hasDisconnect-falseif thisChannelwill delegatedisconnect()toclose(), {@link false} otherwise.handlers- theChannelHandlers 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 parentChannelof thisEmbeddedChannel.channelId- theChannelIdthat will be used to identify this channelregister-trueif thisChannelis registered to theEventLoopin the constructor. Iffalsethe user will need to callregister().hasDisconnect-falseif thisChannelwill delegatedisconnect()toclose(), {@link false} otherwise.handlers- theChannelHandlers which will be add in theChannelPipeline
-
-
Method Detail
-
register
public Future<Void> register()
Description copied from interface:ChannelOutboundInvokerRequest to register on theEventExecutorfor I/O processing.Futureonce the operation completes, either because the operation was successful or because of an error.This will result in having the
ChannelHandler.register(ChannelHandlerContext)method called of the nextChannelHandlercontained in theChannelPipelineof theChannel.
-
newChannelPipeline
protected final DefaultChannelPipeline newChannelPipeline()
Description copied from class:AbstractChannelReturns a newChannelPipelineinstance.- Overrides:
newChannelPipelinein classAbstractChannel<Channel,SocketAddress,SocketAddress>
-
isOpen
public boolean isOpen()
Description copied from interface:ChannelReturnstrueif theChannelis open and may get active later
-
isActive
public boolean isActive()
Description copied from interface:ChannelReturntrueif theChannelis 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 returnnullif nothing is readable.
-
writeInbound
public boolean writeInbound(Object... msgs)
Write messages to the inbound of thisChannel.- Parameters:
msgs- the messages to be written- Returns:
trueif the write operation did add something to the inbound buffer
-
writeOneInbound
public Future<Void> writeOneInbound(Object msg)
Writes one message to the inbound of thisChanneland 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
trueif the write operation did add something to the outbound buffer
-
writeOneOutbound
public Future<Void> writeOneOutbound(Object msg)
Writes one message to the outbound of thisChanneland 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 thisChannelas finished. Any further try to write data to it will fail.- Returns:
- bufferReadable returns
trueif any of the used buffers has something left to read
-
finishAndReleaseAll
public boolean finishAndReleaseAll()
Mark thisChannelas 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
trueif any of the used buffers has something left to read
-
releaseInbound
public boolean releaseInbound()
Release all buffered inbound messages and returntrueif any were in the inbound buffer,falseotherwise.
-
releaseOutbound
public boolean releaseOutbound()
Release all buffered outbound messages and returntrueif any were in the outbound buffer,falseotherwise.
-
close
public final Future<Void> close()
Description copied from interface:ChannelOutboundInvokerRequest to close theChanneland notify theFutureonce the operation completes, either because the operation was successful or because of an error. After it is closed it is not possible to reuse it again.This will result in having the
ChannelHandler.close(ChannelHandlerContext)method called of the nextChannelHandlercontained in theChannelPipelineof theChannel.
-
disconnect
public final Future<Void> disconnect()
Description copied from interface:ChannelOutboundInvokerRequest to disconnect from the remote peer and notify theFutureonce the operation completes, either because the operation was successful or because of an error.This will result in having the
ChannelHandler.disconnect(ChannelHandlerContext)method called of the nextChannelHandlercontained in theChannelPipelineof theChannel.
-
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:
trueif there are any pending tasks,falseotherwise.
-
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 timemanually 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 anyThrowablereceived and if so rethrow it.
-
ensureOpen
protected final void ensureOpen()
Ensure theChannelis open and if not throw an exception.
-
localAddress0
protected SocketAddress localAddress0()
Description copied from class:AbstractChannelReturns theSocketAddresswhich is bound locally.- Specified by:
localAddress0in classAbstractChannel<Channel,SocketAddress,SocketAddress>
-
remoteAddress0
protected SocketAddress remoteAddress0()
Description copied from class:AbstractChannelReturn theSocketAddresswhich theChannelis connected to.- Specified by:
remoteAddress0in classAbstractChannel<Channel,SocketAddress,SocketAddress>
-
doBind
protected void doBind(SocketAddress localAddress) throws Exception
Description copied from class:AbstractChannelBind theChannelto theSocketAddress- Specified by:
doBindin classAbstractChannel<Channel,SocketAddress,SocketAddress>- Throws:
Exception
-
doShutdown
protected void doShutdown(ChannelShutdownDirection direction)
Description copied from class:AbstractChannelShutdown one direction of theChannel.- Specified by:
doShutdownin 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 ExceptionDescription copied from class:AbstractChannelDisconnect thisChannelfrom its remote peer- Specified by:
doDisconnectin classAbstractChannel<Channel,SocketAddress,SocketAddress>- Throws:
Exception
-
doClose
protected void doClose() throws ExceptionDescription copied from class:AbstractChannelClose theChannel- Specified by:
doClosein classAbstractChannel<Channel,SocketAddress,SocketAddress>- Throws:
Exception
-
doBeginRead
protected void doBeginRead() throws ExceptionDescription copied from class:AbstractChannelSchedule a read operation.- Specified by:
doBeginReadin classAbstractChannel<Channel,SocketAddress,SocketAddress>- Throws:
Exception
-
doWrite
protected void doWrite(ChannelOutboundBuffer in) throws Exception
Description copied from class:AbstractChannelFlush the content of the given buffer to the remote peer.- Specified by:
doWritein 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:
runAfterTransportActionin classAbstractChannel<Channel,SocketAddress,SocketAddress>
-
doConnect
protected boolean doConnect(SocketAddress remoteAddress, SocketAddress localAddress)
Description copied from class:AbstractChannelConnect to remote peer.- Specified by:
doConnectin classAbstractChannel<Channel,SocketAddress,SocketAddress>- Parameters:
remoteAddress- the address of the remote peer.localAddress- the local address of this channel.- Returns:
trueif the connect was completed,falseifAbstractChannel.finishConnect()will be called later again to try finishing the connect.
-
doFinishConnect
protected boolean doFinishConnect(SocketAddress requestedRemoteAddress)
Description copied from class:AbstractChannelFinish a connect request.- Specified by:
doFinishConnectin classAbstractChannel<Channel,SocketAddress,SocketAddress>- Parameters:
requestedRemoteAddress- the remote address of the peer.- Returns:
trueif the connect was completed,falseifAbstractChannel.finishConnect()will be called later again to try finishing the connect.
-
-