Class RPCPacketListener

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

@Singleton public 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.
      NullPointerException - if either the given channel or packet is null.
    • handle

      Posts the next rpc instruction in the given context into the handler for the given class which potentially contains the target method and serializes the result into a data buffer. Null is returned when no handler for the given class is present.
      Parameters:
      clazz - the class in which the method to call is located.
      context - the context of the method invocation passed to the handler for the method invocation.
      Returns:
      the serialized result of the method invocation, or null if no handler for the given class is registered.
      Throws:
      NullPointerException - if either the given class or invocation context is null.
      CannotDecideException - if none or multiple methods are matching the method to call in the given class.
    • serializeResult

      Serializes the given handling result into a newly allocated buffer using the given data buf factory. This method returns null if the caller of this handler did not expect an invocation result.
      Parameters:
      result - the result to serialize.
      dataBufFactory - the factory to use for buffer allocation.
      objectMapper - the mapper to use for object serialization.
      context - the invocation context of the invocation the result is getting serialized of.
      Returns:
      the serialized invocation result or null if the calling method did not expect a result.
      Throws:
      NullPointerException - if one of the given parameters is null.
    • handleRaw

      Posts the next rpc instruction in the given context into the handler for the given class which potentially contains the target method. Null is returned when no handler for the given class is present.
      Parameters:
      clazz - the class in which the method to call is located.
      context - the context of the method invocation passed to the handler for the method invocation.
      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 or invocation context is null.
      CannotDecideException - if none or multiple methods are matching the method to call in the given class.
    • buildContext

      @NonNull protected @NonNull RPCInvocationContext buildContext(@NonNull @NonNull NetworkChannel channel, @NonNull @NonNull DataBuf content, @Nullable @Nullable Object on, boolean strictInstanceUsage)
      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. a boolean indicating if the rpc call expects a result
      3. the number of arguments of the target method
      Parameters:
      channel - the network channel on which the rpc request was received.
      content - the remaining buffer content, containing the data as described above.
      on - the object to call the method on, when using a rpc chain.
      strictInstanceUsage - if using the instance provided to the context is required.
      Returns:
      a generated invocation context based on the given information.
      Throws:
      NullPointerException - if either the given channel or content buffer is null.