Class NettyUtil

java.lang.Object
eu.cloudnetservice.driver.impl.network.netty.NettyUtil

public final class NettyUtil extends Object
Internal util for the default netty based communication between server and clients, http and websocket.
Since:
4.0
  • Field Details

    • PACKET_DISPATCH_THREADS

      private static final int PACKET_DISPATCH_THREADS
    • NETTY_EVENT_LOOP_THREADS

      private static final int NETTY_EVENT_LOOP_THREADS
    • SELECTED_SSL_PROVIDER

      private static final io.netty5.handler.ssl.SslProvider SELECTED_SSL_PROVIDER
    • SELECTED_NETTY_TRANSPORT

      private static final NettyTransport SELECTED_NETTY_TRANSPORT
    • SELECTED_BUFFER_ALLOCATOR

      private static final io.netty5.buffer.BufferAllocator SELECTED_BUFFER_ALLOCATOR
    • NETTY_MEMORY_MANAGER_SYS_PROP_NAME

      private static final String NETTY_MEMORY_MANAGER_SYS_PROP_NAME
      See Also:
  • Constructor Details

    • NettyUtil

      private NettyUtil()
  • Method Details

    • overriddenCountOrDefault

      private static int overriddenCountOrDefault(int overriddenSetting, int defaultValue)
      Returns the given overridden count if given (not zero or negative) or returns the given default value.
      Parameters:
      overriddenSetting - the overridden setting, for example from a user input.
      defaultValue - the default value to use as a fallback, if the overridden value is not valid.
      Returns:
      the given overridden setting if valid, the given default value in all other cases.
    • createPacketDispatcher

      @NonNull public static @NonNull NetworkTaskScheduler createPacketDispatcher(@NonNull @NonNull eu.cloudnetservice.driver.DriverEnvironment driverEnvironment)
      Creates a new executor for all incoming packets. The thread size of the returned dispatcher depends either on a user-provided setting or on the given driver environment.
      Parameters:
      driverEnvironment - the driver environment currently running on.
      Returns:
      a newly created executor for dispatching inbound packets.
      Throws:
      NullPointerException - if the given driver environment is null.
    • createBossEventLoopGroup

      @NonNull public static @NonNull io.netty5.channel.EventLoopGroup createBossEventLoopGroup()
      Creates a new boss event loop group based on the selected netty transport. Boss event loops are used to accept new connections, which only requires a single thread.
      Returns:
      a newly created boss event loop group.
    • createWorkerEventLoopGroup

      @NonNull public static @NonNull io.netty5.channel.EventLoopGroup createWorkerEventLoopGroup(@NonNull @NonNull eu.cloudnetservice.driver.DriverEnvironment driverEnvironment)
      Creates a new worker event loop group based on the selected netty transport. The thread count that is used by the event loop is either set by the user or depends on the given driver environment.
      Parameters:
      driverEnvironment - the driver environment currently running on.
      Returns:
      a newly created worker event loop group.
      Throws:
      NullPointerException - if the given driver environment is null.
    • clientChannelFactory

      @NonNull public static @NonNull io.netty5.channel.ChannelFactory<? extends io.netty5.channel.Channel> clientChannelFactory()
      Get the channel factory for client channels of the selected netty transport.
      Returns:
      the channel factory for client channels of the selected netty transport.
    • serverChannelFactory

      @NonNull public static @NonNull io.netty5.channel.ServerChannelFactory<? extends io.netty5.channel.ServerChannel> serverChannelFactory()
      Get the channel factory for server channels of the selected netty transport.
      Returns:
      the channel factory for server channels of the selected netty transport.
    • writeVarInt

      @NonNull public static @NonNull io.netty5.buffer.Buffer writeVarInt(@NonNull @NonNull io.netty5.buffer.Buffer buffer, int value)
      Writes the given integer value as a var int into the buffer.
      Parameters:
      buffer - the buffer to write to.
      value - the value to write into the buffer.
      Returns:
      the buffer used to call the method, for chaining.
      Throws:
      NullPointerException - if the given byte buf is null.
    • readVarInt

      public static int readVarInt(@NonNull @NonNull io.netty5.buffer.Buffer buffer)
      Reads a var int from the given buffer.
      Parameters:
      buffer - the buffer to read from.
      Returns:
      the var int read from the buffer.
      Throws:
      io.netty5.handler.codec.DecoderException - if the buf current position has no var int.
      NullPointerException - if the given buffer to read from is null.
    • readVarIntOrNull

      @Nullable public static @Nullable Integer readVarIntOrNull(@NonNull @NonNull io.netty5.buffer.Buffer buffer)
      Reads a var int from the given buffer, returns null if there is no Var Int at the current buffer position.
      Parameters:
      buffer - the buffer to read from.
      Returns:
      the var int read from the buffer, or null if no var int is at the given position.
      Throws:
      NullPointerException - if the given buffer to read from is null.
    • varIntBytes

      public static int varIntBytes(int value)
      Gets the number of bytes that writing the given content length as a var int will take in the underlying buffer.
      Parameters:
      value - the number to get the amount of bytes for.
      Returns:
      the number of bytes writing the given number as a var int will take.
    • selectedNettyTransport

      @NonNull public static @NonNull NettyTransport selectedNettyTransport()
      Get the selected netty transport which will be used for client/server channel and event loop group construction.
      Returns:
      the selected netty transport.
    • selectedSslProvider

      @NonNull public static @NonNull io.netty5.handler.ssl.SslProvider selectedSslProvider()
      Get the selected ssl provider which will be used in case ssl configurations are enabled on the client or server.
      Returns:
      the selected ssl provider.
    • selectedBufferAllocator

      @NonNull public static @NonNull io.netty5.buffer.BufferAllocator selectedBufferAllocator()
      Get the selected allocator for buffers that should be used for all buffer allocations.
      Returns:
      the selected allocator for buffers that should be used for all buffer allocations.
    • createEventLoopThreadFactory

      @NonNull private static @NonNull ThreadFactory createEventLoopThreadFactory()
      Creates a new thread factory for the netty event loop group but with daemon threads enabled.
      Returns:
      the newly constructed thread factory for event loops.