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 skeletalChannel
implementation.
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description protected static class
AbstractChannel.DefaultAbstractChannelPipeline
-
Constructor Summary
Constructors Modifier Constructor Description protected
AbstractChannel(P parent, EventLoop eventLoop, ChannelMetadata metadata)
Creates a new instance.protected
AbstractChannel(P parent, EventLoop eventLoop, ChannelMetadata metadata, RecvBufferAllocator defaultRecvBufferAllocator)
Creates a new instance.protected
AbstractChannel(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 Throwable
annotateConnectException(Throwable cause, SocketAddress remoteAddress)
Appends the remote address to the message of the exceptions caused by connection attempt failure.protected void
assertEventLoop()
protected void
autoReadCleared()
BufferAllocator
bufferAllocator()
Return the assignedBufferAllocator
which will be used to allocateBuffer
s.protected void
cacheAddresses(L localAddress, R remoteAddress)
protected void
closeForciblyTransport()
Future<Void>
closeFuture()
Returns theFuture
which will be notified when this channel is closed.protected void
closeIfClosed()
protected void
closeTransport(Promise<Void> promise)
int
compareTo(Channel o)
protected void
deregisterTransport(Promise<Void> promise)
protected abstract void
doBeginRead()
Schedule a read operation.protected abstract void
doBind(SocketAddress localAddress)
Bind theChannel
to theSocketAddress
protected abstract void
doClose()
Close theChannel
protected abstract boolean
doConnect(SocketAddress remoteAddress, SocketAddress localAddress)
Connect to remote peer.protected abstract void
doDisconnect()
Disconnect thisChannel
from its remote peerprotected abstract boolean
doFinishConnect(R requestedRemoteAddress)
Finish a connect request.protected abstract void
doShutdown(ChannelShutdownDirection direction)
Shutdown one direction of theChannel
.protected abstract void
doWrite(ChannelOutboundBuffer in)
Flush the content of the given buffer to the remote peer.protected boolean
ensureOpen(Promise<Void> promise)
boolean
equals(Object o)
Returnstrue
if and only if the specified object is identical with this channel (i.e:this == o
).EventLoop
executor()
protected Object
filterOutboundMessage(Object msg)
Invoked when a new message is added to aChannelOutboundBuffer
of thisAbstractChannel
, so that theChannel
implementation converts the message to another.protected void
finishConnect()
Should be called once the connect request is ready to be completed.protected boolean
fireChannelActiveIfNotActiveBefore()
CallsChannelPipeline.fireChannelActive()
if it was not done yet.protected <T> T
getExtendedOption(ChannelOption<T> option)
Override to add support for moreChannelOption
s.protected int
getMaxMessagesPerWrite()
Get the maximum number of message to write per eventloop run.<T> T
getOption(ChannelOption<T> option)
Return the value of the givenChannelOption
protected int
getWriteSpinCount()
protected void
handleWriteError(Throwable t)
int
hashCode()
Returns the ID of this channel.ChannelId
id()
Returns the globally unique identifier of thisChannel
.protected boolean
isAllowHalfClosure()
protected boolean
isAutoRead()
protected boolean
isConnectPending()
Returns if a connect request was issued before and we are waiting forfinishConnect()
to be called.protected boolean
isExtendedOptionSupported(ChannelOption<?> option)
Override to add support for moreChannelOption
s.boolean
isOptionSupported(ChannelOption<?> option)
boolean
isRegistered()
Return true if registered already.L
localAddress()
Returns the local address where this channel is bound to.protected abstract L
localAddress0()
Returns theSocketAddress
which is bound locally.ChannelMetadata
metadata()
protected ChannelPipeline
newChannelPipeline()
Returns a newChannelPipeline
instance.protected static Set<ChannelOption<?>>
newSupportedIdentityOptionsSet(ChannelOption<?>... options)
protected ChannelOutboundBuffer
outboundBuffer()
Returns theChannelOutboundBuffer
that is used by thisAbstractChannel
.P
parent()
Returns the parent of this channel.ChannelPipeline
pipeline()
Return the assignedChannelPipeline
.protected Future<Executor>
prepareToClose()
Prepares to close theChannel
.protected void
readIfIsAutoRead()
protected RecvBufferAllocator.Handle
recvBufAllocHandle()
R
remoteAddress()
Returns the remote address where this channel is connected to.protected abstract R
remoteAddress0()
Return theSocketAddress
which theChannel
is connected to.protected void
runAfterTransportAction()
protected void
safeSetFailure(Promise<Void> promise, Throwable cause)
Marks the specifiedpromise
as failure.protected void
safeSetSuccess(Promise<Void> promise)
Marks the specifiedpromise
as success.void
setBufferAllocator(BufferAllocator bufferAllocator)
protected <T> void
setExtendedOption(ChannelOption<T> option, T value)
Override to add support for moreChannelOption
s.<T> Channel
setOption(ChannelOption<T> option, T value)
Sets a configuration property with the specified name and value.protected void
shutdownTransport(ChannelShutdownDirection direction, Promise<Void> promise)
String
toString()
Returns theString
representation of this channel.protected <T> void
validate(ChannelOption<T> option, T value)
protected static <T extends EventLoopGroup>
TvalidateEventLoopGroup(T group, String name, Class<? extends Channel> channelType)
protected static void
validateFileRegion(DefaultFileRegion region, long position)
long
writableBytes()
Returns how many bytes can be written before theChannel
becomes 'unwritable'.protected void
writeFlushed()
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.null
if there's no parent.eventLoop
- theEventLoop
which will be used.metadata
- theChannelMetadata
to use.
-
AbstractChannel
protected AbstractChannel(P parent, EventLoop eventLoop, ChannelMetadata metadata, RecvBufferAllocator defaultRecvBufferAllocator)
Creates a new instance.- Parameters:
parent
- the parent of this channel.null
if there's no parent.eventLoop
- theEventLoop
which will be used.metadata
- theChannelMetadata
to use.defaultRecvBufferAllocator
- theRecvBufferAllocator
that 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.null
if there's no parent.eventLoop
- theEventLoop
which will be used.metadata
- theChannelMetadata
to use.defaultRecvBufferAllocator
- theRecvBufferAllocator
that is used by default.id
- theChannelId
which 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:Channel
Returns the globally unique identifier of thisChannel
.
-
metadata
public final ChannelMetadata metadata()
Description copied from interface:Channel
-
newChannelPipeline
protected ChannelPipeline newChannelPipeline()
Returns a newChannelPipeline
instance.
-
bufferAllocator
public BufferAllocator bufferAllocator()
Description copied from interface:Channel
Return the assignedBufferAllocator
which will be used to allocateBuffer
s.- Specified by:
bufferAllocator
in interfaceChannel
-
parent
public final P parent()
Description copied from interface:Channel
Returns the parent of this channel.
-
pipeline
public final ChannelPipeline pipeline()
Description copied from interface:Channel
Return the assignedChannelPipeline
.
-
executor
public final EventLoop executor()
Description copied from interface:Channel
- Specified by:
executor
in interfaceChannel
- Specified by:
executor
in interfaceChannelOutboundInvoker
- Returns:
- the executor.
-
localAddress
public final L localAddress()
Description copied from interface:Channel
Returns the local address where this channel is bound to. The returnedSocketAddress
is supposed to be down-cast into more concrete type such asInetSocketAddress
to retrieve the detailed information.- Specified by:
localAddress
in interfaceChannel
- Returns:
- the local address of this channel.
null
if this channel is not bound.
-
remoteAddress
public final R remoteAddress()
Description copied from interface:Channel
Returns the remote address where this channel is connected to. The returnedSocketAddress
is supposed to be down-cast into more concrete type such asInetSocketAddress
to retrieve the detailed information.- Specified by:
remoteAddress
in interfaceChannel
- Returns:
- the remote address of this channel.
null
if this channel is not connected. If this channel is not connected but it can receive messages from arbitrary remote addresses (e.g.DatagramChannel
, useDefaultAddressedEnvelope.recipient()
to determine the origination of the received message as this method will returnnull
.
-
isRegistered
public final boolean isRegistered()
Description copied from interface:IoHandle
Return true if registered already.- Specified by:
isRegistered
in interfaceIoHandle
- Returns:
true
if registered,false
otherwise
-
closeFuture
public final Future<Void> closeFuture()
Description copied from interface:Channel
Returns theFuture
which will be notified when this channel is closed. This method always returns the same future instance.- Specified by:
closeFuture
in interfaceChannel
-
writableBytes
public final long writableBytes()
Description copied from interface:Channel
Returns how many bytes can be written before theChannel
becomes 'unwritable'. Once aChannel
becomes unwritable, all messages will be queued until the I/O thread is ready to process the queued write requests.- Specified by:
writableBytes
in interfaceChannel
- Returns:
- the number of bytes that can be written before the
Channel
becomes unwritable.
-
hashCode
public final int hashCode()
Returns the ID of this channel.
-
equals
public final boolean equals(Object o)
Returnstrue
if and only if the specified object is identical with this channel (i.e:this == o
).
-
compareTo
public final int compareTo(Channel o)
- Specified by:
compareTo
in interfaceComparable<P extends Channel>
-
toString
public String toString()
Returns theString
representation 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:
true
ifChannelPipeline.fireChannelActive()
was called,false
otherwise.
-
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 specifiedpromise
as success. If thepromise
is done already, log a message.
-
safeSetFailure
protected final void safeSetFailure(Promise<Void> promise, Throwable cause)
Marks the specifiedpromise
as failure. If thepromise
is 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 theChannelOutboundBuffer
that is used by thisAbstractChannel
. This might benull
if no more writes are allowed.- Returns:
- the outbound buffer.
-
localAddress0
protected abstract L localAddress0()
Returns theSocketAddress
which is bound locally.
-
remoteAddress0
protected abstract R remoteAddress0()
Return theSocketAddress
which theChannel
is connected to.
-
doBind
protected abstract void doBind(SocketAddress localAddress) throws Exception
Bind theChannel
to theSocketAddress
- Throws:
Exception
-
doDisconnect
protected abstract void doDisconnect() throws Exception
Disconnect thisChannel
from 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 Exception
Schedule 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:
true
if the connect was completed,false
iffinishConnect()
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:
true
if the connect was completed,false
iffinishConnect()
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:
true
if 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 aChannelOutboundBuffer
of thisAbstractChannel
, so that theChannel
implementation 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:Channel
Return the value of the givenChannelOption
- Specified by:
getOption
in 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 moreChannelOption
s. 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 theChannelOption
is not supported.
-
setOption
public final <T> Channel setOption(ChannelOption<T> option, T value)
Description copied from interface:Channel
Sets a configuration property with the specified name and value.- Specified by:
setOption
in 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 moreChannelOption
s. You need to also call {@link super} after handling the extra options.- Type Parameters:
T
- the value type.- Parameters:
option
- theChannelOption
.- Throws:
UnsupportedOperationException
- if theChannelOption
is not supported.
-
isOptionSupported
public final boolean isOptionSupported(ChannelOption<?> option)
Description copied from interface:Channel
Returnstrue
if the givenChannelOption
is supported by thisChannel
implementation. If this methods returnsfalse
, calls toChannel.setOption(ChannelOption, Object)
andChannel.getOption(ChannelOption)
with theChannelOption
will throw anUnsupportedOperationException
.- Specified by:
isOptionSupported
in interfaceChannel
- Parameters:
option
- the option.- Returns:
- true if supported,
false
otherwise.
-
isExtendedOptionSupported
protected boolean isExtendedOptionSupported(ChannelOption<?> option)
Override to add support for moreChannelOption
s. You need to also call {@link super} after handling the extra options.- Parameters:
option
- theChannelOption
.- Returns:
true
if supported,false
otherwise.
-
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()
-
-