Class NettyNetworkClient

java.lang.Object
eu.cloudnetservice.driver.network.netty.client.NettyNetworkClient
All Implemented Interfaces:
NetworkClient, NetworkComponent, PacketSender, AutoCloseable

@Singleton public class NettyNetworkClient extends Object implements NetworkClient
The default implementation of a network client, using the netty network client implementations.
Since:
4.0
  • Field Details

    • CONNECTION_TIMEOUT_MILLIS

      private static final int CONNECTION_TIMEOUT_MILLIS
      See Also:
    • WATER_MARK

      private static final io.netty5.channel.WriteBufferWaterMark WATER_MARK
    • sslContext

      protected final io.netty5.handler.ssl.SslContext sslContext
    • eventLoopGroup

      protected final io.netty5.channel.EventLoopGroup eventLoopGroup
    • channels

      protected final Collection<NetworkChannel> channels
    • packetRegistry

      protected final PacketListenerRegistry packetRegistry
    • packetDispatcher

      protected final NetworkTaskScheduler packetDispatcher
    • handlerFactory

      protected final Callable<NetworkChannelHandler> handlerFactory
  • Constructor Details

    • NettyNetworkClient

      public NettyNetworkClient(@NonNull @NonNull ComponentInfo componentInfo, @NonNull @NonNull Callable<NetworkChannelHandler> handlerFactory)
      Constructs a new netty network client instance. Equivalent to new NettyNetworkClient(handlerFactory, null)
      Parameters:
      componentInfo - the component info of the current component the client is created for.
      handlerFactory - the factory for new handlers to be created with.
      Throws:
      NullPointerException - if the given event manager, component info or factory is null.
    • NettyNetworkClient

      public NettyNetworkClient(@NonNull @NonNull ComponentInfo componentInfo, @NonNull @NonNull Callable<NetworkChannelHandler> handlerFactory, @Nullable @Nullable SSLConfiguration sslConfiguration)
      Constructs a new netty network client instance.
      Parameters:
      componentInfo - the component info of the current component the client is created for.
      handlerFactory - the factory for new handler to be created with.
      sslConfiguration - the ssl configuration applying to this client, or null for no ssl configuration.
      Throws:
      NullPointerException - if the given event manager, component info or factory is null.
  • Method Details

    • initializeSslContext

      @Nullable private static @Nullable io.netty5.handler.ssl.SslContext initializeSslContext(@Nullable @Nullable SSLConfiguration sslConfig)
      Builds the ssl context for this client if a ssl configuration was supplied and is active.
      Parameters:
      sslConfig - the supplied ssl configuration to build the ssl context based on, can be null.
      Returns:
      the constructed ssl context based on the given configuration.
      Throws:
      IllegalStateException - if an error occurs during construction of the ssl context.
      IllegalArgumentException - if the trust certificate file does not contain any valid certificates.
    • sslEnabled

      public boolean sslEnabled()
      Get if this component has ssl enabled.
      Specified by:
      sslEnabled in interface NetworkComponent
      Returns:
      true if this component has ssl enabled, false otherwise.
    • connect

      Connects this network client to the network server running at the given host and port.
      Specified by:
      connect in interface NetworkClient
      Parameters:
      hostAndPort - the target host and port to which the client should get connected.
      Returns:
      a task completed successfully, or with the exception thrown during the connection process.
    • close

      public void close()
      Specified by:
      close in interface AutoCloseable
    • channels

      Get an immutable collection of all channels which are associated with this component.
      Specified by:
      channels in interface NetworkComponent
      Returns:
      an immutable collection of all channels which are associated with this component.
    • packetDispatcher

      @NonNull public @NonNull Executor packetDispatcher()
      Get the dispatching executor for received packets in any channel which is bound to this network component.
      Specified by:
      packetDispatcher in interface NetworkComponent
      Returns:
      the dispatching executor for received packets.
    • packetRegistry

      @NonNull public @NonNull PacketListenerRegistry packetRegistry()
      Get the packet listener registry which will be the root registry for all channels initialized by this component.
      Specified by:
      packetRegistry in interface NetworkComponent
      Returns:
      the root packet registry for all associated channels.
    • sendPacket

      public void sendPacket(@NonNull @NonNull Packet packet)
      Sends the given packet to the associated target.
      Specified by:
      sendPacket in interface PacketSender
      Parameters:
      packet - the packet to send.
    • sendPacketSync

      public void sendPacketSync(@NonNull @NonNull Packet packet)
      Sends the given packet to the associated target, waiting for the packet write to the channel to complete before resuming the current thread.
      Specified by:
      sendPacketSync in interface PacketSender
      Parameters:
      packet - the packet to send.
    • closeChannels

      public void closeChannels()
      Closes all open connections associated with this network component.
      Specified by:
      closeChannels in interface NetworkComponent