Class DefaultNetworkChannel

java.lang.Object
eu.cloudnetservice.driver.network.DefaultNetworkChannel
All Implemented Interfaces:
NetworkChannel, PacketSender
Direct Known Subclasses:
NettyNetworkChannel

public abstract class DefaultNetworkChannel extends Object implements NetworkChannel
The default abstract implementation of a network channel.
Since:
4.0
  • Field Details

    • CHANNEL_ID_COUNTER

      private static final AtomicLong CHANNEL_ID_COUNTER
    • channelId

      private final long channelId
    • queryPacketManager

      private final QueryPacketManager queryPacketManager
    • packetRegistry

      private final PacketListenerRegistry packetRegistry
    • serverAddress

      private final HostAndPort serverAddress
    • clientAddress

      private final HostAndPort clientAddress
    • clientProvidedChannel

      private final boolean clientProvidedChannel
    • handler

      private final NetworkChannelHandler handler
  • Constructor Details

    • DefaultNetworkChannel

      public DefaultNetworkChannel(@NonNull @NonNull PacketListenerRegistry packetRegistry, @NonNull @NonNull HostAndPort serverAddress, @NonNull @NonNull HostAndPort clientAddress, boolean clientProvidedChannel, @NonNull @NonNull NetworkChannelHandler handler)
      Constructs a new default network channel instance.
      Parameters:
      packetRegistry - the packet registry to use for the channel.
      serverAddress - the associated server address for the connection.
      clientAddress - the associated client address for the connection.
      clientProvidedChannel - true if this channel was opened by a client, false otherwise.
      handler - the handler to post underlying network events to.
      Throws:
      NullPointerException - if one of the given arguments is null.
  • Method Details

    • sendQueryAsync

      @NonNull public @NonNull eu.cloudnetservice.common.concurrent.Task<Packet> sendQueryAsync(@NonNull @NonNull Packet packet)
      Converts and sends the given packet as a query into this channel, returning a future either completed with the response to the query or an exception if no response to the packet was received in time.
      Specified by:
      sendQueryAsync in interface NetworkChannel
      Parameters:
      packet - the packet to send as a query.
      Returns:
      a future completed with the result of the query or an exception in case of a timeout.
    • sendQuery

      public Packet sendQuery(@NonNull @NonNull Packet packet)
      Converts and sends the given packet as a query into this channel, blocking the current thread until a response is available or a timeout of 30 seconds was reached.
      Specified by:
      sendQuery in interface NetworkChannel
      Parameters:
      packet - the packet to send as a query.
      Returns:
      the response to the query or null if no response was received in time.
    • channelId

      public long channelId()
      Get a unique numeric id of this channel. The ids are starting at 1.
      Specified by:
      channelId in interface NetworkChannel
      Returns:
      a unique numeric id of this channel.
    • packetRegistry

      @NonNull public @NonNull PacketListenerRegistry packetRegistry()
      Get the packet listener registry responsible for this channel.
      Specified by:
      packetRegistry in interface NetworkChannel
      Returns:
      the packet listener registry responsible for this channel.
    • queryPacketManager

      @NonNull public @NonNull QueryPacketManager queryPacketManager()
      Get the query packet manager of this channel.
      Specified by:
      queryPacketManager in interface NetworkChannel
      Returns:
      the query packet manager of this channel.
    • serverAddress

      @NonNull public @NonNull HostAndPort serverAddress()
      Get the server host from/to the channel is connected.
      Specified by:
      serverAddress in interface NetworkChannel
      Returns:
      the server address of the channel.
    • clientAddress

      @NonNull public @NonNull HostAndPort clientAddress()
      Get the client host from/to the channel is connected.
      Specified by:
      clientAddress in interface NetworkChannel
      Returns:
      the client address of the channel.
    • clientProvidedChannel

      public boolean clientProvidedChannel()
      Get if this client is opened by a client rather than a server.
      Specified by:
      clientProvidedChannel in interface NetworkChannel
      Returns:
      true if this channel was opened by a client, false otherwise.
    • handler

      Get the handler of this channel listening to all kind of operations on this channel.
      Specified by:
      handler in interface NetworkChannel
      Returns:
      the listener for channel operations.