Module io.netty5.transport
Package io.netty5.channel
Class AbstractChannel<P extends Channel,L extends SocketAddress,R extends SocketAddress>
- java.lang.Object
-
- io.netty5.util.DefaultAttributeMap
-
- io.netty5.channel.AbstractChannel<P,L,R>
-
- All Implemented Interfaces:
Channel,ChannelOutboundInvoker,IoHandle,AttributeMap,FuturePromiseFactory,Comparable<Channel>
- Direct Known Subclasses:
AbstractNioChannel,AbstractServerChannel,EmbeddedChannel,EpollDatagramChannel,EpollServerSocketChannel,EpollSocketChannel,KQueueDatagramChannel,KQueueServerSocketChannel,KQueueSocketChannel,LocalChannel
public abstract class AbstractChannel<P extends Channel,L extends SocketAddress,R extends SocketAddress> extends DefaultAttributeMap implements Channel
A skeletalChannelimplementation.
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description protected static classAbstractChannel.DefaultAbstractChannelPipeline
-
Constructor Summary
Constructors Modifier Constructor Description protectedAbstractChannel(P parent, EventLoop eventLoop, ChannelMetadata metadata)Creates a new instance.protectedAbstractChannel(P parent, EventLoop eventLoop, ChannelMetadata metadata, RecvBufferAllocator defaultRecvBufferAllocator)Creates a new instance.protectedAbstractChannel(P parent, EventLoop eventLoop, ChannelMetadata metadata, RecvBufferAllocator defaultRecvBufferAllocator, ChannelId id)Creates a new instance.
-
Method Summary
All Methods Static Methods Instance Methods Abstract Methods Concrete Methods Modifier and Type Method Description protected static ThrowableannotateConnectException(Throwable cause, SocketAddress remoteAddress)Appends the remote address to the message of the exceptions caused by connection attempt failure.protected voidassertEventLoop()protected voidautoReadCleared()BufferAllocatorbufferAllocator()Return the assignedBufferAllocatorwhich will be used to allocateBuffers.protected voidcacheAddresses(L localAddress, R remoteAddress)protected voidcloseForciblyTransport()Future<Void>closeFuture()Returns theFuturewhich will be notified when this channel is closed.protected voidcloseIfClosed()protected voidcloseTransport(Promise<Void> promise)intcompareTo(Channel o)protected voidderegisterTransport(Promise<Void> promise)protected abstract voiddoBeginRead()Schedule a read operation.protected abstract voiddoBind(SocketAddress localAddress)Bind theChannelto theSocketAddressprotected abstract voiddoClose()Close theChannelprotected abstract booleandoConnect(SocketAddress remoteAddress, SocketAddress localAddress)Connect to remote peer.protected abstract voiddoDisconnect()Disconnect thisChannelfrom its remote peerprotected abstract booleandoFinishConnect(R requestedRemoteAddress)Finish a connect request.protected abstract voiddoShutdown(ChannelShutdownDirection direction)Shutdown one direction of theChannel.protected abstract voiddoWrite(ChannelOutboundBuffer in)Flush the content of the given buffer to the remote peer.protected booleanensureOpen(Promise<Void> promise)booleanequals(Object o)Returnstrueif and only if the specified object is identical with this channel (i.e:this == o).EventLoopexecutor()protected ObjectfilterOutboundMessage(Object msg)Invoked when a new message is added to aChannelOutboundBufferof thisAbstractChannel, so that theChannelimplementation converts the message to another.protected voidfinishConnect()Should be called once the connect request is ready to be completed.protected booleanfireChannelActiveIfNotActiveBefore()CallsChannelPipeline.fireChannelActive()if it was not done yet.protected <T> TgetExtendedOption(ChannelOption<T> option)Override to add support for moreChannelOptions.protected intgetMaxMessagesPerWrite()Get the maximum number of message to write per eventloop run.<T> TgetOption(ChannelOption<T> option)Return the value of the givenChannelOptionprotected intgetWriteSpinCount()protected voidhandleWriteError(Throwable t)inthashCode()Returns the ID of this channel.ChannelIdid()Returns the globally unique identifier of thisChannel.protected booleanisAllowHalfClosure()protected booleanisAutoRead()protected booleanisConnectPending()Returns if a connect request was issued before and we are waiting forfinishConnect()to be called.protected booleanisExtendedOptionSupported(ChannelOption<?> option)Override to add support for moreChannelOptions.booleanisOptionSupported(ChannelOption<?> option)booleanisRegistered()Return true if registered already.LlocalAddress()Returns the local address where this channel is bound to.protected abstract LlocalAddress0()Returns theSocketAddresswhich is bound locally.ChannelMetadatametadata()protected ChannelPipelinenewChannelPipeline()Returns a newChannelPipelineinstance.protected static Set<ChannelOption<?>>newSupportedIdentityOptionsSet(ChannelOption<?>... options)protected ChannelOutboundBufferoutboundBuffer()Returns theChannelOutboundBufferthat is used by thisAbstractChannel.Pparent()Returns the parent of this channel.ChannelPipelinepipeline()Return the assignedChannelPipeline.protected Future<Executor>prepareToClose()Prepares to close theChannel.protected voidreadIfIsAutoRead()protected RecvBufferAllocator.HandlerecvBufAllocHandle()RremoteAddress()Returns the remote address where this channel is connected to.protected abstract RremoteAddress0()Return theSocketAddresswhich theChannelis connected to.protected voidrunAfterTransportAction()protected voidsafeSetFailure(Promise<Void> promise, Throwable cause)Marks the specifiedpromiseas failure.protected voidsafeSetSuccess(Promise<Void> promise)Marks the specifiedpromiseas success.voidsetBufferAllocator(BufferAllocator bufferAllocator)protected <T> voidsetExtendedOption(ChannelOption<T> option, T value)Override to add support for moreChannelOptions.<T> ChannelsetOption(ChannelOption<T> option, T value)Sets a configuration property with the specified name and value.protected voidshutdownTransport(ChannelShutdownDirection direction, Promise<Void> promise)StringtoString()Returns theStringrepresentation of this channel.protected <T> voidvalidate(ChannelOption<T> option, T value)protected static <T extends EventLoopGroup>
TvalidateEventLoopGroup(T group, String name, Class<? extends Channel> channelType)protected static voidvalidateFileRegion(DefaultFileRegion region, long position)longwritableBytes()Returns how many bytes can be written before theChannelbecomes 'unwritable'.protected voidwriteFlushed()Write previous flushed messages.-
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, close, connect, connect, deregister, disconnect, flush, isActive, isOpen, isShutdown, isWritable, read, register, sendOutboundEvent, shutdown, write, writeAndFlush
-
Methods inherited from interface io.netty5.channel.ChannelOutboundInvoker
newFailedFuture, newPromise, newSucceededFuture, newSucceededFuture
-
-
-
-
Constructor Detail
-
AbstractChannel
protected AbstractChannel(P parent, EventLoop eventLoop, ChannelMetadata metadata)
Creates a new instance.- Parameters:
parent- the parent of this channel.nullif there's no parent.eventLoop- theEventLoopwhich will be used.metadata- theChannelMetadatato use.
-
AbstractChannel
protected AbstractChannel(P parent, EventLoop eventLoop, ChannelMetadata metadata, RecvBufferAllocator defaultRecvBufferAllocator)
Creates a new instance.- Parameters:
parent- the parent of this channel.nullif there's no parent.eventLoop- theEventLoopwhich will be used.metadata- theChannelMetadatato use.defaultRecvBufferAllocator- theRecvBufferAllocatorthat is used by default.
-
AbstractChannel
protected AbstractChannel(P parent, EventLoop eventLoop, ChannelMetadata metadata, RecvBufferAllocator defaultRecvBufferAllocator, ChannelId id)
Creates a new instance.- Parameters:
parent- the parent of this channel.nullif there's no parent.eventLoop- theEventLoopwhich will be used.metadata- theChannelMetadatato use.defaultRecvBufferAllocator- theRecvBufferAllocatorthat is used by default.id- theChannelIdwhich will be used.
-
-
Method Detail
-
validateEventLoopGroup
protected static <T extends EventLoopGroup> T validateEventLoopGroup(T group, String name, Class<? extends Channel> channelType)
-
id
public final ChannelId id()
Description copied from interface:ChannelReturns the globally unique identifier of thisChannel.
-
metadata
public final ChannelMetadata metadata()
Description copied from interface:Channel
-
newChannelPipeline
protected ChannelPipeline newChannelPipeline()
Returns a newChannelPipelineinstance.
-
bufferAllocator
public BufferAllocator bufferAllocator()
Description copied from interface:ChannelReturn the assignedBufferAllocatorwhich will be used to allocateBuffers.- Specified by:
bufferAllocatorin interfaceChannel
-
parent
public final P parent()
Description copied from interface:ChannelReturns the parent of this channel.
-
pipeline
public final ChannelPipeline pipeline()
Description copied from interface:ChannelReturn the assignedChannelPipeline.
-
executor
public final EventLoop executor()
Description copied from interface:Channel- Specified by:
executorin interfaceChannel- Specified by:
executorin interfaceChannelOutboundInvoker- Returns:
- the executor.
-
localAddress
public final L localAddress()
Description copied from interface:ChannelReturns the local address where this channel is bound to. The returnedSocketAddressis supposed to be down-cast into more concrete type such asInetSocketAddressto retrieve the detailed information.- Specified by:
localAddressin interfaceChannel- Returns:
- the local address of this channel.
nullif this channel is not bound.
-
remoteAddress
public final R remoteAddress()
Description copied from interface:ChannelReturns the remote address where this channel is connected to. The returnedSocketAddressis supposed to be down-cast into more concrete type such asInetSocketAddressto retrieve the detailed information.- Specified by:
remoteAddressin interfaceChannel- Returns:
- the remote address of this channel.
nullif this channel is not connected. If this channel is not connected but it can receive messages from arbitrary remote addresses (e.g.DatagramChannel, useDefaultAddressedEnvelope.recipient()to determine the origination of the received message as this method will returnnull.
-
isRegistered
public final boolean isRegistered()
Description copied from interface:IoHandleReturn true if registered already.- Specified by:
isRegisteredin interfaceIoHandle- Returns:
trueif registered,falseotherwise
-
closeFuture
public final Future<Void> closeFuture()
Description copied from interface:ChannelReturns theFuturewhich will be notified when this channel is closed. This method always returns the same future instance.- Specified by:
closeFuturein interfaceChannel
-
writableBytes
public final long writableBytes()
Description copied from interface:ChannelReturns how many bytes can be written before theChannelbecomes 'unwritable'. Once aChannelbecomes unwritable, all messages will be queued until the I/O thread is ready to process the queued write requests.- Specified by:
writableBytesin interfaceChannel- Returns:
- the number of bytes that can be written before the
Channelbecomes unwritable.
-
hashCode
public final int hashCode()
Returns the ID of this channel.
-
equals
public final boolean equals(Object o)
Returnstrueif and only if the specified object is identical with this channel (i.e:this == o).
-
compareTo
public final int compareTo(Channel o)
- Specified by:
compareToin interfaceComparable<P extends Channel>
-
toString
public String toString()
Returns theStringrepresentation of this channel. The returned string contains the ID, local address, and remote address of this channel for easier identification.
-
readIfIsAutoRead
protected final void readIfIsAutoRead()
-
assertEventLoop
protected final void assertEventLoop()
-
recvBufAllocHandle
protected RecvBufferAllocator.Handle recvBufAllocHandle()
-
fireChannelActiveIfNotActiveBefore
protected final boolean fireChannelActiveIfNotActiveBefore()
CallsChannelPipeline.fireChannelActive()if it was not done yet.- Returns:
trueifChannelPipeline.fireChannelActive()was called,falseotherwise.
-
closeForciblyTransport
protected final void closeForciblyTransport()
-
shutdownTransport
protected final void shutdownTransport(ChannelShutdownDirection direction, Promise<Void> promise)
-
writeFlushed
protected void writeFlushed()
Write previous flushed messages.
-
handleWriteError
protected final void handleWriteError(Throwable t)
-
safeSetSuccess
protected final void safeSetSuccess(Promise<Void> promise)
Marks the specifiedpromiseas success. If thepromiseis done already, log a message.
-
safeSetFailure
protected final void safeSetFailure(Promise<Void> promise, Throwable cause)
Marks the specifiedpromiseas failure. If thepromiseis done already, log a message.
-
closeIfClosed
protected final void closeIfClosed()
-
annotateConnectException
protected static Throwable annotateConnectException(Throwable cause, SocketAddress remoteAddress)
Appends the remote address to the message of the exceptions caused by connection attempt failure.
-
outboundBuffer
protected final ChannelOutboundBuffer outboundBuffer()
Returns theChannelOutboundBufferthat is used by thisAbstractChannel. This might benullif no more writes are allowed.- Returns:
- the outbound buffer.
-
localAddress0
protected abstract L localAddress0()
Returns theSocketAddresswhich is bound locally.
-
remoteAddress0
protected abstract R remoteAddress0()
Return theSocketAddresswhich theChannelis connected to.
-
doBind
protected abstract void doBind(SocketAddress localAddress) throws Exception
Bind theChannelto theSocketAddress- Throws:
Exception
-
doDisconnect
protected abstract void doDisconnect() throws ExceptionDisconnect thisChannelfrom its remote peer- Throws:
Exception
-
doShutdown
protected abstract void doShutdown(ChannelShutdownDirection direction) throws Exception
Shutdown one direction of theChannel.- Parameters:
direction- the direction to shutdown.- Throws:
Exception- thrown on error.
-
doBeginRead
protected abstract void doBeginRead() throws ExceptionSchedule a read operation.- Throws:
Exception
-
doWrite
protected abstract void doWrite(ChannelOutboundBuffer in) throws Exception
Flush the content of the given buffer to the remote peer.- Throws:
Exception
-
doConnect
protected abstract boolean doConnect(SocketAddress remoteAddress, SocketAddress localAddress) throws Exception
Connect to remote peer.- Parameters:
remoteAddress- the address of the remote peer.localAddress- the local address of this channel.- Returns:
trueif the connect was completed,falseiffinishConnect()will be called later again to try finishing the connect.- Throws:
Exception- thrown on error.
-
doFinishConnect
protected abstract boolean doFinishConnect(R requestedRemoteAddress) throws Exception
Finish a connect request.- Parameters:
requestedRemoteAddress- the remote address of the peer.- Returns:
trueif the connect was completed,falseiffinishConnect()will be called later again to try finishing the connect.- Throws:
Exception- thrown on error.
-
isConnectPending
protected final boolean isConnectPending()
Returns if a connect request was issued before and we are waiting forfinishConnect()to be called.- Returns:
trueif there is an outstanding connect request.
-
finishConnect
protected final void finishConnect()
Should be called once the connect request is ready to be completed.
-
filterOutboundMessage
protected Object filterOutboundMessage(Object msg) throws Exception
Invoked when a new message is added to aChannelOutboundBufferof thisAbstractChannel, so that theChannelimplementation converts the message to another. (e.g. heap buffer -> direct buffer)- Throws:
Exception
-
validateFileRegion
protected static void validateFileRegion(DefaultFileRegion region, long position) throws IOException
- Throws:
IOException
-
getOption
public final <T> T getOption(ChannelOption<T> option)
Description copied from interface:ChannelReturn the value of the givenChannelOption- Specified by:
getOptionin interfaceChannel- Type Parameters:
T- the type of the value.- Parameters:
option- theChannelOption.- Returns:
- the value for the
ChannelOption
-
getExtendedOption
protected <T> T getExtendedOption(ChannelOption<T> option)
Override to add support for moreChannelOptions. You need to also call {@link super} after handling the extra options.- Type Parameters:
T- the value type.- Parameters:
option- theChannelOption.- Returns:
- the value for the option
- Throws:
UnsupportedOperationException- if theChannelOptionis not supported.
-
setOption
public final <T> Channel setOption(ChannelOption<T> option, T value)
Description copied from interface:ChannelSets a configuration property with the specified name and value.- Specified by:
setOptionin interfaceChannel- Type Parameters:
T- the type of the value.- Parameters:
option- theChannelOption.value- the value for theChannelOption- Returns:
- itself.
-
setExtendedOption
protected <T> void setExtendedOption(ChannelOption<T> option, T value)
Override to add support for moreChannelOptions. You need to also call {@link super} after handling the extra options.- Type Parameters:
T- the value type.- Parameters:
option- theChannelOption.- Throws:
UnsupportedOperationException- if theChannelOptionis not supported.
-
isOptionSupported
public final boolean isOptionSupported(ChannelOption<?> option)
Description copied from interface:ChannelReturnstrueif the givenChannelOptionis supported by thisChannelimplementation. If this methods returnsfalse, calls toChannel.setOption(ChannelOption, Object)andChannel.getOption(ChannelOption)with theChannelOptionwill throw anUnsupportedOperationException.- Specified by:
isOptionSupportedin interfaceChannel- Parameters:
option- the option.- Returns:
- true if supported,
falseotherwise.
-
isExtendedOptionSupported
protected boolean isExtendedOptionSupported(ChannelOption<?> option)
Override to add support for moreChannelOptions. You need to also call {@link super} after handling the extra options.- Parameters:
option- theChannelOption.- Returns:
trueif supported,falseotherwise.
-
newSupportedIdentityOptionsSet
protected static Set<ChannelOption<?>> newSupportedIdentityOptionsSet(ChannelOption<?>... options)
-
validate
protected <T> void validate(ChannelOption<T> option, T value)
-
getMaxMessagesPerWrite
protected final int getMaxMessagesPerWrite()
Get the maximum number of message to write per eventloop run. Once this limit is reached we will continue to process other events before trying to write the remaining messages.
-
getWriteSpinCount
protected final int getWriteSpinCount()
-
setBufferAllocator
public void setBufferAllocator(BufferAllocator bufferAllocator)
-
isAutoRead
protected final boolean isAutoRead()
-
autoReadCleared
protected void autoReadCleared()
-
isAllowHalfClosure
protected final boolean isAllowHalfClosure()
-
runAfterTransportAction
protected void runAfterTransportAction()
-
-