Search in sources :

Example 1 with Handler

use of org.lanternpowered.server.network.message.handler.Handler in project LanternServer by LanternPowered.

the class MessageCodecHandler method processMessage.

private void processMessage(Message message, List<Object> output, Protocol protocol, ProtocolState state, CodecContext context) {
    if (message == NullMessage.INSTANCE) {
        return;
    }
    if (message instanceof BulkMessage) {
        ((BulkMessage) message).getMessages().forEach(message1 -> processMessage(message1, output, protocol, state, context));
        return;
    }
    final MessageRegistration messageRegistration = (MessageRegistration) protocol.inbound().findByMessageType(message.getClass()).orElseThrow(() -> new DecoderException("The returned message type is not attached to the used protocol state (" + state.toString() + ")!"));
    final List<Processor> processors = messageRegistration.getProcessors();
    // Only process if there are processors found
    if (!processors.isEmpty()) {
        for (Processor processor : processors) {
            // The processor should handle the output messages
            processor.process(context, message, output);
        }
    } else {
        final Optional<Handler> optHandler = messageRegistration.getHandler();
        if (optHandler.isPresent()) {
            // Add the message to the output
            output.add(new HandlerMessage(message, optHandler.get()));
        } else {
            output.add(message);
        }
    }
}
Also used : DecoderException(io.netty.handler.codec.DecoderException) BulkMessage(org.lanternpowered.server.network.message.BulkMessage) MessageRegistration(org.lanternpowered.server.network.message.MessageRegistration) Processor(org.lanternpowered.server.network.message.processor.Processor) Handler(org.lanternpowered.server.network.message.handler.Handler) HandlerMessage(org.lanternpowered.server.network.message.HandlerMessage)

Aggregations

DecoderException (io.netty.handler.codec.DecoderException)1 BulkMessage (org.lanternpowered.server.network.message.BulkMessage)1 HandlerMessage (org.lanternpowered.server.network.message.HandlerMessage)1 MessageRegistration (org.lanternpowered.server.network.message.MessageRegistration)1 Handler (org.lanternpowered.server.network.message.handler.Handler)1 Processor (org.lanternpowered.server.network.message.processor.Processor)1