Class RPCPacketListener
java.lang.Object
eu.cloudnetservice.driver.network.rpc.listener.RPCPacketListener
- All Implemented Interfaces:
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 Summary
Fields -
Constructor Summary
ConstructorsConstructorDescriptionRPCPacketListener(@NonNull RPCHandlerRegistry rpcHandlerRegistry) Constructs a new rpc packet listener instance. -
Method Summary
Modifier and TypeMethodDescriptionprotected @NonNull RPCInvocationContextbuildContext(@NonNull NetworkChannel channel, @NonNull DataBuf content, @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.voidhandle(@NonNull NetworkChannel channel, @NonNull Packet packet) Handles the incoming packet.handle(@NonNull String clazz, @NonNull RPCInvocationContext context) 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.protected RPCHandler.HandlingResulthandleRaw(@NonNull String clazz, @NonNull RPCInvocationContext context) Posts the next rpc instruction in the given context into the handler for the given class which potentially contains the target method.serializeResult(RPCHandler.HandlingResult result, @NonNull DataBufFactory dataBufFactory, @NonNull ObjectMapper objectMapper, @NonNull RPCInvocationContext context) Serializes the given handling result into a newly allocated buffer using the given data buf factory.
-
Field Details
-
rpcHandlerRegistry
-
-
Constructor Details
-
RPCPacketListener
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:
handlein interfacePacketListener- 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
@Nullable protected @Nullable DataBuf handle(@NonNull @NonNull String clazz, @NonNull @NonNull RPCInvocationContext context) 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
@Nullable protected @Nullable DataBuf serializeResult(@NonNull RPCHandler.HandlingResult result, @NonNull @NonNull DataBufFactory dataBufFactory, @NonNull @NonNull ObjectMapper objectMapper, @NonNull @NonNull RPCInvocationContext context) 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
@Nullable protected RPCHandler.HandlingResult handleRaw(@NonNull @NonNull String clazz, @NonNull @NonNull RPCInvocationContext context) 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):- the target method name
- a boolean indicating if the rpc call expects a result
- 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.
-