Class AbstractChannel

java.lang.Object
io.netty.util.DefaultAttributeMap
io.netty.channel.AbstractChannel
All Implemented Interfaces:
Channel, ChannelOutboundInvoker, AttributeMap, java.lang.Comparable<Channel>
Direct Known Subclasses:
AbstractEpollChannel, AbstractIoUringChannel, AbstractKQueueChannel, AbstractNioChannel, AbstractOioChannel, AbstractServerChannel, EmbeddedChannel, FailedChannel, LocalChannel

public abstract class AbstractChannel extends DefaultAttributeMap implements Channel
A skeletal Channel implementation.
  • Field Details

    • logger

      private static final InternalLogger logger
    • parent

      private final Channel parent
    • id

      private final ChannelId id
    • unsafe

      private final Channel.Unsafe unsafe
    • pipeline

      private final DefaultChannelPipeline pipeline
    • unsafeVoidPromise

      private final VoidChannelPromise unsafeVoidPromise
    • closeFuture

      private final AbstractChannel.CloseFuture closeFuture
    • localAddress

      private volatile java.net.SocketAddress localAddress
    • remoteAddress

      private volatile java.net.SocketAddress remoteAddress
    • eventLoop

      private volatile EventLoop eventLoop
    • registered

      private volatile boolean registered
    • closeInitiated

      private boolean closeInitiated
    • initialCloseCause

      private java.lang.Throwable initialCloseCause
    • strValActive

      private boolean strValActive
      Cache for the string representation of this channel
    • strVal

      private java.lang.String strVal
  • Constructor Details

    • AbstractChannel

      protected AbstractChannel(Channel parent)
      Creates a new instance.
      Parameters:
      parent - the parent of this channel. null if there's no parent.
    • AbstractChannel

      protected AbstractChannel(Channel parent, ChannelId id)
      Creates a new instance.
      Parameters:
      parent - the parent of this channel. null if there's no parent.
  • Method Details

    • maxMessagesPerWrite

      protected final int maxMessagesPerWrite()
    • id

      public final ChannelId id()
      Description copied from interface: Channel
      Returns the globally unique identifier of this Channel.
      Specified by:
      id in interface Channel
    • newId

      protected ChannelId newId()
      Returns a new DefaultChannelId instance. Subclasses may override this method to assign custom ChannelIds to Channels that use the AbstractChannel(Channel) constructor.
    • newChannelPipeline

      protected DefaultChannelPipeline newChannelPipeline()
      Returns a new DefaultChannelPipeline instance.
    • parent

      public Channel parent()
      Description copied from interface: Channel
      Returns the parent of this channel.
      Specified by:
      parent in interface Channel
      Returns:
      the parent channel. null if this channel does not have a parent channel.
    • pipeline

      public ChannelPipeline pipeline()
      Description copied from interface: Channel
      Return the assigned ChannelPipeline.
      Specified by:
      pipeline in interface Channel
    • eventLoop

      public EventLoop eventLoop()
      Description copied from interface: Channel
      Return the EventLoop this Channel was registered to.
      Specified by:
      eventLoop in interface Channel
    • localAddress

      public java.net.SocketAddress localAddress()
      Description copied from interface: Channel
      Returns the local address where this channel is bound to. The returned SocketAddress is supposed to be down-cast into more concrete type such as InetSocketAddress to retrieve the detailed information.
      Specified by:
      localAddress in interface Channel
      Returns:
      the local address of this channel. null if this channel is not bound.
    • invalidateLocalAddress

      @Deprecated protected void invalidateLocalAddress()
      Deprecated.
      no use-case for this.
    • remoteAddress

      public java.net.SocketAddress remoteAddress()
      Description copied from interface: Channel
      Returns the remote address where this channel is connected to. The returned SocketAddress is supposed to be down-cast into more concrete type such as InetSocketAddress to retrieve the detailed information.
      Specified by:
      remoteAddress in interface Channel
      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, use DefaultAddressedEnvelope.recipient() to determine the origination of the received message as this method will return null.
    • invalidateRemoteAddress

      @Deprecated protected void invalidateRemoteAddress()
      Deprecated.
      no use-case for this.
    • isRegistered

      public boolean isRegistered()
      Description copied from interface: Channel
      Returns true if the Channel is registered with an EventLoop.
      Specified by:
      isRegistered in interface Channel
    • closeFuture

      public ChannelFuture closeFuture()
      Description copied from interface: Channel
      Returns the ChannelFuture which will be notified when this channel is closed. This method always returns the same future instance.
      Specified by:
      closeFuture in interface Channel
    • unsafe

      public Channel.Unsafe unsafe()
      Description copied from interface: Channel
      Returns an internal-use-only object that provides unsafe operations.
      Specified by:
      unsafe in interface Channel
    • newUnsafe

      protected abstract AbstractChannel.AbstractUnsafe newUnsafe()
      Create a new AbstractChannel.AbstractUnsafe instance which will be used for the life-time of the Channel
    • hashCode

      public final int hashCode()
      Returns the ID of this channel.
      Overrides:
      hashCode in class java.lang.Object
    • equals

      public final boolean equals(java.lang.Object o)
      Returns true if and only if the specified object is identical with this channel (i.e: this == o).
      Overrides:
      equals in class java.lang.Object
    • compareTo

      public final int compareTo(Channel o)
      Specified by:
      compareTo in interface java.lang.Comparable<Channel>
    • toString

      public java.lang.String toString()
      Returns the String representation of this channel. The returned string contains the ID, local address, and remote address of this channel for easier identification.
      Overrides:
      toString in class java.lang.Object
    • voidPromise

      public final ChannelPromise voidPromise()
      Description copied from interface: ChannelOutboundInvoker
      Return a special ChannelPromise which can be reused for different operations.

      It's only supported to use it for ChannelOutboundInvoker.write(Object, ChannelPromise).

      Be aware that the returned ChannelPromise will not support most operations and should only be used if you want to save an object allocation for every write operation. You will not be able to detect if the operation was complete, only if it failed as the implementation will call ChannelPipeline.fireExceptionCaught(Throwable) in this case.

      Be aware this is an expert feature and should be used with care!
      Specified by:
      voidPromise in interface Channel
      Specified by:
      voidPromise in interface ChannelOutboundInvoker
    • isCompatible

      protected abstract boolean isCompatible(EventLoop loop)
      Return true if the given EventLoop is compatible with this instance.
    • localAddress0

      protected abstract java.net.SocketAddress localAddress0()
      Returns the SocketAddress which is bound locally.
    • remoteAddress0

      protected abstract java.net.SocketAddress remoteAddress0()
      Return the SocketAddress which the Channel is connected to.
    • doRegister

      @Deprecated protected void doRegister() throws java.lang.Exception
      Is called after the Channel is registered with its EventLoop as part of the register process. Subclasses may override this method
      Throws:
      java.lang.Exception
    • doRegister

      protected void doRegister(ChannelPromise promise)
      Is called after the Channel is registered with its EventLoop as part of the register process. Subclasses may override this method
      Parameters:
      promise - ChannelPromise that must be notified once done to continue the registration.
    • doBind

      protected abstract void doBind(java.net.SocketAddress localAddress) throws java.lang.Exception
      Bind the Channel to the SocketAddress
      Throws:
      java.lang.Exception
    • doDisconnect

      protected abstract void doDisconnect() throws java.lang.Exception
      Disconnect this Channel from its remote peer
      Throws:
      java.lang.Exception
    • doClose

      protected abstract void doClose() throws java.lang.Exception
      Close the Channel
      Throws:
      java.lang.Exception
    • doShutdownOutput

      protected void doShutdownOutput() throws java.lang.Exception
      Called when conditions justify shutting down the output portion of the channel. This may happen if a write operation throws an exception.
      Throws:
      java.lang.Exception
    • doDeregister

      protected void doDeregister() throws java.lang.Exception
      Deregister the Channel from its EventLoop. Sub-classes may override this method
      Throws:
      java.lang.Exception
    • doBeginRead

      protected abstract void doBeginRead() throws java.lang.Exception
      Schedule a read operation.
      Throws:
      java.lang.Exception
    • doWrite

      protected abstract void doWrite(ChannelOutboundBuffer in) throws java.lang.Exception
      Flush the content of the given buffer to the remote peer.
      Throws:
      java.lang.Exception
    • filterOutboundMessage

      protected java.lang.Object filterOutboundMessage(java.lang.Object msg) throws java.lang.Exception
      Invoked when a new message is added to a ChannelOutboundBuffer of this AbstractChannel, so that the Channel implementation converts the message to another. (e.g. heap buffer -> direct buffer)
      Throws:
      java.lang.Exception
    • validateFileRegion

      protected void validateFileRegion(DefaultFileRegion region, long position) throws java.io.IOException
      Throws:
      java.io.IOException