Search in sources :

Example 1 with LanternByteBuffer

use of org.lanternpowered.server.network.buffer.LanternByteBuffer 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 LanternByteBuffer

use of org.lanternpowered.server.network.buffer.LanternByteBuffer in project LanternServer by LanternPowered.

the class MessageCodecHandler method decode.

@Override
public void decode(ChannelHandlerContext ctx, ByteBuf input, List<Object> output) throws Exception {
    if (input.readableBytes() == 0) {
        return;
    }
    final int opcode = readVarInt(input);
    final ProtocolState state = this.codecContext.getSession().getProtocolState();
    final Protocol protocol = state.getProtocol();
    final CodecRegistration registration = protocol.inbound().find(opcode).orElse(null);
    if (registration == null) {
        if (warnedMissingOpcodes.add(opcode)) {
            Lantern.getLogger().warn("Failed to find a message registration with opcode 0x{} in state {}!", Integer.toHexString(opcode), state);
        }
        return;
    }
    // Copy the remaining content of the buffer to a new buffer used by the
    // message decoding
    final ByteBuffer content = this.codecContext.byteBufAlloc().buffer(input.readableBytes());
    input.readBytes(((LanternByteBuffer) content).getDelegate(), input.readableBytes());
    // Read the content of the message
    final Message message;
    try {
        message = registration.getCodec().decode(this.codecContext, content);
        if (content.available() > 0) {
            Lantern.getLogger().warn("Trailing bytes {}b after decoding with message codec {} with opcode 0x{} in state {}!\n{}", content.available(), registration.getCodec().getClass().getName(), Integer.toHexString(opcode), state, message);
        }
    } finally {
        content.release();
    }
    /*
        if (message instanceof MessageInOutKeepAlive ||
                message instanceof MessagePlayInPlayerMovement) {
        } else {
            System.out.println(message.getClass().getName());
        }
        */
    processMessage(message, output, protocol, state, this.codecContext);
}
Also used : 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) ProtocolState(org.lanternpowered.server.network.protocol.ProtocolState) Protocol(org.lanternpowered.server.network.protocol.Protocol) ByteBuffer(org.lanternpowered.server.network.buffer.ByteBuffer) LanternByteBuffer(org.lanternpowered.server.network.buffer.LanternByteBuffer)

Aggregations

ByteBuffer (org.lanternpowered.server.network.buffer.ByteBuffer)2 LanternByteBuffer (org.lanternpowered.server.network.buffer.LanternByteBuffer)2 BulkMessage (org.lanternpowered.server.network.message.BulkMessage)2 CodecRegistration (org.lanternpowered.server.network.message.CodecRegistration)2 HandlerMessage (org.lanternpowered.server.network.message.HandlerMessage)2 Message (org.lanternpowered.server.network.message.Message)2 NullMessage (org.lanternpowered.server.network.message.NullMessage)2 Protocol (org.lanternpowered.server.network.protocol.Protocol)2 ByteBuf (io.netty.buffer.ByteBuf)1 EncoderException (io.netty.handler.codec.EncoderException)1 MessageToMessageCodec (io.netty.handler.codec.MessageToMessageCodec)1 MessageRegistration (org.lanternpowered.server.network.message.MessageRegistration)1 Codec (org.lanternpowered.server.network.message.codec.Codec)1 ProtocolState (org.lanternpowered.server.network.protocol.ProtocolState)1