Class RPCPacketListener

java.lang.Object
eu.cloudnetservice.driver.network.rpc.listener.RPCPacketListener
All Implemented Interfaces:
PacketListener

@Singleton public final class RPCPacketListener extends Object implements PacketListener
A network packet listener designed to handle all rpc messages using an underlying handler registry to post method call instructions to it.
Since:
4.0
  • Field Details

  • Constructor Details

    • RPCPacketListener

      @Inject public RPCPacketListener(@NonNull @NonNull RPCHandlerRegistry rpcHandlerRegistry)
      Constructs a new rpc packet listener instance.
      Parameters:
      rpcHandlerRegistry - the registry to use to downstream call instructions to.
      Throws:
      NullPointerException - if the given rpc handler registry is null.
  • Method Details

    • handle

      public void handle(@NonNull @NonNull NetworkChannel channel, @NonNull @NonNull Packet packet) throws Exception
      Handles the incoming packet. A packet handle should be release-safe and transactional to preserve the content of the packet for listeners which are following in the chain. If a packet listener is not doing that, it might lead to unexpected exceptions in other packet listener implementations for the same packet.
      Specified by:
      handle in interface PacketListener
      Parameters:
      channel - the channel from which the original packet came.
      packet - the packet which was received.
      Throws:
      Exception - if any exception occurs during handling of the packet.
    • waitForInvocationCompletion

      private void waitForInvocationCompletion(@Nullable @Nullable CompletableFuture<RPCInvocationResult> invocationTask, @NonNull @NonNull Consumer<RPCInvocationResult> callback)
      Waits for the given invocation task to finish, calling the callback if that is the case. The given callback receives a null value if the given task is null, the task completes exceptionally or with a value of null.
      Parameters:
      invocationTask - the task representing a method invocation process.
      callback - the callback to call when the invocation finished.
      Throws:
      NullPointerException - if the given callback is null.
    • executeRPCChainStep

      private void executeRPCChainStep(int chainDepth, int currentDepth, boolean resultExpected, @NonNull @NonNull DataBuf content, @NonNull @NonNull Packet request, @NonNull @NonNull NetworkChannel channel, @Nullable @Nullable Object previousMethodReturnValue)
      Executes the next RPC chain step of the current RPC chain.
      Parameters:
      chainDepth - the full depth of the RPC chain.
      currentDepth - the current depth the chain execution is at, starting at 1.
      resultExpected - if the RPC invocation expects a result to be sent back.
      content - the data content of the RPC request.
      request - the request packet.
      channel - the network channel from which the request came.
      previousMethodReturnValue - the chain step invocation return value.
      Throws:
      NullPointerException - if one of the required non-null arguments is null.
    • serializeHandlingResult

      @NonNull private @NonNull DataBuf serializeHandlingResult(@Nullable @Nullable RPCInvocationResult invocationResult)
      Serializes the result of the RPC handling process into a data buffer.
      Parameters:
      invocationResult - the handling result to serialize.
      Returns:
      a buffer containing the response content for the handling result.
    • sendResponseData

      private void sendResponseData(@NonNull @NonNull NetworkChannel channel, @NonNull @NonNull Packet request, @NonNull @NonNull DataBuf response)
      Sends a serialized response to given request packet into the given network channel.
      Parameters:
      channel - the channel to which the response should be sent.
      request - the request to which a response is being sent.
      response - the encoded response data to send.
      Throws:
      NullPointerException - if the given channel, request packet or response is null.
    • postRPCRequestToHandler

      Posts the given RPC invocation context to the RPC handler that is registered for the class with the given name. If no handler is registered for the class, this methods returns null instead of an invocation result.
      Parameters:
      targetClassName - the name of class in which the method to call is located.
      context - the invocation context holding the provided execution info from the remote.
      Returns:
      the result of the method invocation, or null if no handler for the given class is registered.
      Throws:
      NullPointerException - if either the given class name or invocation context is null.
    • buildContext

      @NonNull private @NonNull RPCInvocationContext buildContext(@NonNull @NonNull DataBuf content, @Nullable @Nullable Object workingInstance)
      Builds a new context for a rpc method invocation based on the given information and remaining content in the buffer. The given buffer should still contain (in the given order):
      1. the target method name.
      2. the target method descriptor.
      3. the argument information for the invocation, if any.
      Parameters:
      content - the remaining buffer content, containing the data as described above.
      workingInstance - the instance on which the methods should be called, null to use the handler binding.
      Returns:
      a generated invocation context based on the given information.
      Throws:
      NullPointerException - if the given content buffer is null.