Search in sources :

Example 16 with EncoderException

use of io.netty.handler.codec.EncoderException in project LanternServer by LanternPowered.

the class CodecPlayOutEntityEquipment method encode.

@Override
public ByteBuffer encode(CodecContext context, MessagePlayOutEntityEquipment message) throws CodecException {
    final ByteBuffer buf = context.byteBufAlloc().buffer();
    buf.writeVarInt(message.getEntityId());
    buf.writeVarInt(message.getSlotIndex());
    final Object item = message.getItem();
    if (item instanceof ItemStack) {
        buf.write(Types.ITEM_STACK, (ItemStack) item);
    } else if (item instanceof RawItemStack || item == null) {
        buf.write(Types.RAW_ITEM_STACK, (RawItemStack) item);
    } else {
        throw new EncoderException("Invalid item type:" + item.getClass().getName());
    }
    return buf;
}
Also used : EncoderException(io.netty.handler.codec.EncoderException) RawItemStack(org.lanternpowered.server.network.objects.RawItemStack) RawItemStack(org.lanternpowered.server.network.objects.RawItemStack) ItemStack(org.spongepowered.api.item.inventory.ItemStack) ByteBuffer(org.lanternpowered.server.network.buffer.ByteBuffer)

Example 17 with EncoderException

use of io.netty.handler.codec.EncoderException in project LanternServer by LanternPowered.

the class CodecPlayInOutCustomPayload method encode0.

@Override
protected MessageResult encode0(CodecContext context, Message message) throws CodecException {
    if (message instanceof MessagePlayInOutBrand) {
        return new MessageResult("MC|Brand", context.byteBufAlloc().buffer().writeString(((MessagePlayInOutBrand) message).getBrand()));
    } else if (message instanceof MessagePlayOutOpenBook) {
        final ByteBuffer buf = context.byteBufAlloc().buffer();
        buf.writeVarInt(((MessagePlayOutOpenBook) message).getHandType() == HandTypes.MAIN_HAND ? 0 : 1);
        return new MessageResult("MC|BOpen", buf);
    } else if (message instanceof MessagePlayOutStopSounds) {
        final MessagePlayOutStopSounds message0 = (MessagePlayOutStopSounds) message;
        final ByteBuffer buf = context.byteBufAlloc().buffer();
        buf.writeString(message0.getCategory() == null ? "" : message0.getCategory().getName());
        buf.writeString(message0.getSound() == null ? "" : message0.getSound());
        return new MessageResult("MC|StopSound", buf);
    }
    throw new EncoderException("Unsupported message type: " + message);
}
Also used : EncoderException(io.netty.handler.codec.EncoderException) MessagePlayOutStopSounds(org.lanternpowered.server.network.vanilla.message.type.play.MessagePlayOutStopSounds) MessagePlayInOutBrand(org.lanternpowered.server.network.vanilla.message.type.play.MessagePlayInOutBrand) MessagePlayOutOpenBook(org.lanternpowered.server.network.vanilla.message.type.play.MessagePlayOutOpenBook) ByteBuffer(org.lanternpowered.server.network.buffer.ByteBuffer)

Example 18 with EncoderException

use of io.netty.handler.codec.EncoderException in project LanternServer by LanternPowered.

the class CodecPlayOutWorldBorder method encode.

@Override
public ByteBuffer encode(CodecContext context, MessagePlayOutWorldBorder message) throws CodecException {
    ByteBuffer buf = context.byteBufAlloc().buffer();
    if (message instanceof MessagePlayOutWorldBorder.Initialize) {
        MessagePlayOutWorldBorder.Initialize message1 = (MessagePlayOutWorldBorder.Initialize) message;
        buf.writeVarInt(3);
        buf.writeDouble(message1.getCenterX());
        buf.writeDouble(message1.getCenterZ());
        buf.writeDouble(message1.getOldDiameter());
        buf.writeDouble(message1.getNewDiameter());
        buf.writeVarLong(message1.getLerpTime());
        buf.writeVarInt(message1.getWorldSize());
        buf.writeVarInt(message1.getWarningTime());
        buf.writeVarInt(message1.getWarningDistance());
    } else if (message instanceof MessagePlayOutWorldBorder.UpdateCenter) {
        MessagePlayOutWorldBorder.UpdateCenter message1 = (MessagePlayOutWorldBorder.UpdateCenter) message;
        buf.writeVarInt(2);
        buf.writeDouble(message1.getX());
        buf.writeDouble(message1.getZ());
    } else if (message instanceof MessagePlayOutWorldBorder.UpdateLerpedDiameter) {
        MessagePlayOutWorldBorder.UpdateLerpedDiameter message1 = (MessagePlayOutWorldBorder.UpdateLerpedDiameter) message;
        buf.writeVarInt(1);
        buf.writeDouble(message1.getOldDiameter());
        buf.writeDouble(message1.getNewDiameter());
        buf.writeVarLong(message1.getLerpTime());
    } else if (message instanceof MessagePlayOutWorldBorder.UpdateDiameter) {
        buf.writeVarInt(0);
        buf.writeDouble(((MessagePlayOutWorldBorder.UpdateDiameter) message).getDiameter());
    } else if (message instanceof MessagePlayOutWorldBorder.UpdateWarningDistance) {
        buf.writeVarInt(5);
        buf.writeVarInt(((MessagePlayOutWorldBorder.UpdateWarningDistance) message).getDistance());
    } else if (message instanceof MessagePlayOutWorldBorder.UpdateWarningTime) {
        buf.writeVarInt(4);
        buf.writeVarInt(((MessagePlayOutWorldBorder.UpdateWarningTime) message).getTime());
    } else {
        throw new EncoderException("Unsupported message type: " + message.getClass().getName());
    }
    return buf;
}
Also used : MessagePlayOutWorldBorder(org.lanternpowered.server.network.vanilla.message.type.play.MessagePlayOutWorldBorder) ByteBuffer(org.lanternpowered.server.network.buffer.ByteBuffer) EncoderException(io.netty.handler.codec.EncoderException)

Example 19 with EncoderException

use of io.netty.handler.codec.EncoderException 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)

Example 20 with EncoderException

use of io.netty.handler.codec.EncoderException in project LanternServer by LanternPowered.

the class CodecPlayInOutCustomPayload method encode0.

@Override
protected MessageResult encode0(CodecContext context, Message message) throws CodecException {
    if (message instanceof MessageForgeHandshakeInOutAck) {
        return new MessageResult("FML|HS", context.byteBufAlloc().buffer(2).writeByte((byte) FML_HANDSHAKE_ACK).writeByte((byte) ((ForgeServerHandshakePhase) ((MessageForgeHandshakeInOutAck) message).getPhase()).ordinal()));
    } else if (message instanceof MessageForgeHandshakeInOutHello) {
        return new MessageResult("FML|HS", context.byteBufAlloc().buffer(2).writeByte((byte) FML_HANDSHAKE_SERVER_HELLO).writeByte((byte) FORGE_PROTOCOL));
    } else if (message instanceof MessageForgeHandshakeInOutModList) {
        Map<String, String> entries = ((MessageForgeHandshakeInOutModList) message).getEntries();
        ByteBuffer buf = context.byteBufAlloc().buffer();
        buf.writeByte((byte) FML_HANDSHAKE_MOD_LIST);
        buf.writeVarInt(entries.size());
        for (Map.Entry<String, String> en : entries.entrySet()) {
            buf.writeString(en.getKey());
            buf.writeString(en.getValue());
        }
        return new MessageResult("FML|HS", buf);
    } else if (message instanceof MessageForgeHandshakeOutReset) {
        return new MessageResult("FML|HS", context.byteBufAlloc().buffer(1).writeByte((byte) FML_HANDSHAKE_RESET));
    }
    throw new EncoderException("Unsupported message type: " + message);
}
Also used : MessageForgeHandshakeInOutHello(org.lanternpowered.server.network.forge.message.type.handshake.MessageForgeHandshakeInOutHello) EncoderException(io.netty.handler.codec.EncoderException) MessageForgeHandshakeOutReset(org.lanternpowered.server.network.forge.message.type.handshake.MessageForgeHandshakeOutReset) MessageForgeHandshakeInOutModList(org.lanternpowered.server.network.forge.message.type.handshake.MessageForgeHandshakeInOutModList) ByteBuffer(org.lanternpowered.server.network.buffer.ByteBuffer) Map(java.util.Map) MessageForgeHandshakeInOutAck(org.lanternpowered.server.network.forge.message.type.handshake.MessageForgeHandshakeInOutAck)

Aggregations

EncoderException (io.netty.handler.codec.EncoderException)26 ByteBuffer (org.lanternpowered.server.network.buffer.ByteBuffer)9 ByteBuf (io.netty.buffer.ByteBuf)6 IOException (java.io.IOException)6 EmbeddedChannel (io.netty.channel.embedded.EmbeddedChannel)3 Test (org.junit.jupiter.api.Test)3 Message (org.lanternpowered.server.network.message.Message)3 CodecException (io.netty.handler.codec.CodecException)2 Deflater (java.util.zip.Deflater)2 Inflater (java.util.zip.Inflater)2 MessageRegistration (org.lanternpowered.server.network.message.MessageRegistration)2 Codec (org.lanternpowered.server.network.message.codec.Codec)2 CodecRegistration (com.flowpowered.network.Codec.CodecRegistration)1 ByteBufInputStream (io.netty.buffer.ByteBufInputStream)1 ChannelHandlerContext (io.netty.channel.ChannelHandlerContext)1 ChannelInboundHandlerAdapter (io.netty.channel.ChannelInboundHandlerAdapter)1 DecoderResult (io.netty.handler.codec.DecoderResult)1 LengthFieldPrepender (io.netty.handler.codec.LengthFieldPrepender)1 MessageToMessageCodec (io.netty.handler.codec.MessageToMessageCodec)1 DefaultHttpContent (io.netty.handler.codec.http.DefaultHttpContent)1