Search in sources :

Example 1 with MessageRegistration

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

the class MessageCodecHandler method encode.

@Override
protected void encode(ChannelHandlerContext ctx, Message message, List<Object> output) throws Exception {
    final Protocol protocol = this.codecContext.getSession().getProtocol();
    final MessageRegistration<Message> registration = (MessageRegistration<Message>) protocol.outbound().findByMessageType(message.getClass()).orElse(null);
    if (registration == null) {
        throw new EncoderException("Message type (" + message.getClass().getName() + ") is not registered!");
    }
    CodecRegistration codecRegistration = registration.getCodecRegistration().orElse(null);
    if (codecRegistration == null) {
        throw new EncoderException("Message type (" + message.getClass().getName() + ") is not registered to allow encoding!");
    }
    /*
        if (message instanceof MessagePlayOutWorldTime ||
                message instanceof MessageInOutKeepAlive) {
        } else {
            System.out.println(message.getClass().getName());
        }
        */
    final ByteBuf opcode = ctx.alloc().buffer();
    // Write the opcode of the message
    writeVarInt(opcode, codecRegistration.getOpcode());
    final Codec codec = codecRegistration.getCodec();
    final ByteBuffer content;
    try {
        content = codec.encode(this.codecContext, message);
    } finally {
        ReferenceCountUtil.release(message);
    }
    // Add the buffer to the output
    output.add(Unpooled.wrappedBuffer(opcode, ((LanternByteBuffer) content).getDelegate()));
}
Also used : EncoderException(io.netty.handler.codec.EncoderException) Codec(org.lanternpowered.server.network.message.codec.Codec) MessageToMessageCodec(io.netty.handler.codec.MessageToMessageCodec) MessageRegistration(org.lanternpowered.server.network.message.MessageRegistration) HandlerMessage(org.lanternpowered.server.network.message.HandlerMessage) NullMessage(org.lanternpowered.server.network.message.NullMessage) Message(org.lanternpowered.server.network.message.Message) BulkMessage(org.lanternpowered.server.network.message.BulkMessage) CodecRegistration(org.lanternpowered.server.network.message.CodecRegistration) Protocol(org.lanternpowered.server.network.protocol.Protocol) ByteBuf(io.netty.buffer.ByteBuf) ByteBuffer(org.lanternpowered.server.network.buffer.ByteBuffer) LanternByteBuffer(org.lanternpowered.server.network.buffer.LanternByteBuffer) LanternByteBuffer(org.lanternpowered.server.network.buffer.LanternByteBuffer)

Example 2 with MessageRegistration

use of org.lanternpowered.server.network.message.MessageRegistration 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)

Example 3 with MessageRegistration

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

the class MessageProcessorHandler method acceptOutboundMessage.

@Override
public boolean acceptOutboundMessage(Object msg) throws Exception {
    final Message message = (Message) msg;
    final Protocol protocol = this.codecContext.getSession().getProtocol();
    final MessageRegistration registration = protocol.outbound().findByMessageType(message.getClass()).orElse(null);
    if (registration == null) {
        throw new EncoderException("Message type (" + message.getClass().getName() + ") is not registered in state " + this.codecContext.getSession().getProtocolState().name() + "!");
    }
    final List<Processor> processors = ((MessageRegistration) protocol.outbound().findByMessageType(message.getClass()).get()).getProcessors();
    // Only process if there are processors found
    if (!processors.isEmpty()) {
        final List<Object> messages = new ArrayList<>();
        for (Processor processor : processors) {
            // The processor should handle the output messages
            processor.process(this.codecContext, message, messages);
        }
        if (message instanceof ReferenceCounted && !messages.contains(message)) {
            ((ReferenceCounted) message).release();
        }
        if (!messages.isEmpty()) {
            this.messages.set(messages);
        }
        return true;
    }
    return false;
}
Also used : EncoderException(io.netty.handler.codec.EncoderException) MessageRegistration(org.lanternpowered.server.network.message.MessageRegistration) Processor(org.lanternpowered.server.network.message.processor.Processor) Message(org.lanternpowered.server.network.message.Message) ArrayList(java.util.ArrayList) Protocol(org.lanternpowered.server.network.protocol.Protocol) ReferenceCounted(io.netty.util.ReferenceCounted)

Aggregations

MessageRegistration (org.lanternpowered.server.network.message.MessageRegistration)3 EncoderException (io.netty.handler.codec.EncoderException)2 BulkMessage (org.lanternpowered.server.network.message.BulkMessage)2 HandlerMessage (org.lanternpowered.server.network.message.HandlerMessage)2 Message (org.lanternpowered.server.network.message.Message)2 Processor (org.lanternpowered.server.network.message.processor.Processor)2 Protocol (org.lanternpowered.server.network.protocol.Protocol)2 ByteBuf (io.netty.buffer.ByteBuf)1 DecoderException (io.netty.handler.codec.DecoderException)1 MessageToMessageCodec (io.netty.handler.codec.MessageToMessageCodec)1 ReferenceCounted (io.netty.util.ReferenceCounted)1 ArrayList (java.util.ArrayList)1 ByteBuffer (org.lanternpowered.server.network.buffer.ByteBuffer)1 LanternByteBuffer (org.lanternpowered.server.network.buffer.LanternByteBuffer)1 CodecRegistration (org.lanternpowered.server.network.message.CodecRegistration)1 NullMessage (org.lanternpowered.server.network.message.NullMessage)1 Codec (org.lanternpowered.server.network.message.codec.Codec)1 Handler (org.lanternpowered.server.network.message.handler.Handler)1