Class NettyUtil

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

@Internal 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

    • VAR_INT_BYTE_LENGTHS

      private static final int[] VAR_INT_BYTE_LENGTHS
    • NO_NATIVE_TRANSPORT

      private static final boolean NO_NATIVE_TRANSPORT
    • CURR_NETTY_TRANSPORT

      private static final NettyTransport CURR_NETTY_TRANSPORT
    • DEFAULT_REJECT_HANDLER

      private static final RejectedExecutionHandler DEFAULT_REJECT_HANDLER
  • Constructor Details

    • NettyUtil

      private NettyUtil()
  • Method Details

    • newPacketDispatcher

      @NonNull public static @NonNull Executor newPacketDispatcher(@NonNull @NonNull DriverEnvironment driverEnvironment)
      Get a so-called "packet dispatcher" which represents an executor handling the received packets sent to a network component. The executor is optimized for the currently running-in environment - this represents the default settings applied to the dispatcher. If running in a node env the packet dispatcher uses the amount of processors multiplied by 2 for the maximum thread amount, in a wrapper env this is fixed to 8. All threads in the dispatcher can idle for 30 seconds before they are terminated forcefully. One thread will always idle in the handler to speed up just-in-time handling of packets. Given tasks are queued in the order they are given into the dispatcher and if the dispatcher has no capacity to run the task, the caller will automatically call the task instead.
      Parameters:
      driverEnvironment - the driver environment to get the executor for.
      Returns:
      a new packet dispatcher instance.
      Throws:
      NullPointerException - if the given environment is null.
      See Also:
    • newEventLoopGroup

      @NonNull public static @NonNull io.netty5.channel.EventLoopGroup newEventLoopGroup(int threads)
      Creates a new nio or epoll event loop group based on their availability.
      Parameters:
      threads - the number of threads to use for the event loop.
      Returns:
      a new nio or epoll event loop group.
    • clientChannelFactory

      @NonNull public static @NonNull io.netty5.channel.ChannelFactory<? extends io.netty5.channel.Channel> clientChannelFactory()
      Creates a new channel factory for network clients based on the epoll availability.
      Returns:
      a new channel factory for network clients based on the epoll availability.
    • serverChannelFactory

      @NonNull public static @NonNull io.netty5.channel.ServerChannelFactory<? extends io.netty5.channel.ServerChannel> serverChannelFactory()
      Creates a new channel factory for network servers based on the epoll availability.
      Returns:
      a new channel factory for network servers based on the epoll availability.
    • 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 contentLength)
      Gets the number of bytes that writing the given content length as a var int will take in the underlying buffer.
      Parameters:
      contentLength - the number to get the amount of bytes for.
      Returns:
      the number of bytes writing the given number as a var int will take.
    • awaitFuture

      @NonNull public static <T> @NonNull io.netty5.util.concurrent.Future<T> awaitFuture(@NonNull @NonNull io.netty5.util.concurrent.Future<T> future)
      Waits for the given future to complete, either returning the same future instance as given (but completed) or rethrowing all exceptions that occurred during completion. This method throws an IllegalThreadStateException if the current thread was interrupted during the future computation.
      Type Parameters:
      T - the type of data returned by the future.
      Parameters:
      future - the future to wait for.
      Returns:
      the same future as given to the method, but completed.
      Throws:
      NullPointerException - if the given future is null.
      CancellationException - if the computation was cancelled
      CompletionException - if the computation threw an exception.
      IllegalThreadStateException - if the current thread was interrupted during computation.
    • threadAmount

      public static @org.jetbrains.annotations.Range(from=2L, to=2147483647L) int threadAmount(@NonNull @NonNull DriverEnvironment environment)
      Get the thread amount used by the packet dispatcher to dispatch incoming packets. This method returns always 4 when running in as a wrapper and the amount of processors cores multiplied by 2 when running either embedded or as a node.
      Parameters:
      environment - the environment to get the thread count for.
      Returns:
      the thread amount used by the packet dispatcher to dispatch incoming packets.
      Throws:
      NullPointerException - if the given environment is null.
    • 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.