Search in sources :

Example 11 with EncoderException

use of io.netty.handler.codec.EncoderException in project Glowstone by GlowstoneMC.

the class CodecsHandler method encode.

@Override
protected void encode(ChannelHandlerContext ctx, Message msg, List<Object> out) throws Exception {
    // find codec
    Class<? extends Message> clazz = msg.getClass();
    CodecRegistration reg = protocol.getCodecRegistration(clazz);
    if (reg == null) {
        throw new EncoderException("Unknown message type: " + clazz + ".");
    }
    // write header
    ByteBuf headerBuf = ctx.alloc().buffer(8);
    ByteBufUtils.writeVarInt(headerBuf, reg.getOpcode());
    // write body
    ByteBuf messageBuf = ctx.alloc().buffer();
    messageBuf = reg.getCodec().encode(messageBuf, msg);
    out.add(Unpooled.wrappedBuffer(headerBuf, messageBuf));
}
Also used : EncoderException(io.netty.handler.codec.EncoderException) CodecRegistration(com.flowpowered.network.Codec.CodecRegistration) ByteBuf(io.netty.buffer.ByteBuf)

Example 12 with EncoderException

use of io.netty.handler.codec.EncoderException in project RecurrentComplex by Ivorforce.

the class RCPacketBuffer method readBigTag.

@Nullable
public NBTTagCompound readBigTag() throws IOException {
    int i = this.readerIndex();
    byte b0 = this.readByte();
    if (b0 == 0) {
        return null;
    } else {
        this.readerIndex(i);
        try {
            return CompressedStreamTools.read(new ByteBufInputStream(this), new NBTSizeTracker(2097152L * 4));
        } catch (IOException ioexception) {
            throw new EncoderException(ioexception);
        }
    }
}
Also used : NBTSizeTracker(net.minecraft.nbt.NBTSizeTracker) EncoderException(io.netty.handler.codec.EncoderException) ByteBufInputStream(io.netty.buffer.ByteBufInputStream) IOException(java.io.IOException) Nullable(javax.annotation.Nullable)

Example 13 with EncoderException

use of io.netty.handler.codec.EncoderException in project Galacticraft by micdoodle8.

the class PacketCustom method do_compress.

/**
 * Compresses the payload ByteBuf after the type byte
 */
private void do_compress() {
    Deflater deflater = new Deflater();
    try {
        readerIndex(1);
        int len = readableBytes();
        deflater.setInput(array(), readerIndex(), len);
        deflater.finish();
        byte[] out = new byte[len];
        int clen = deflater.deflate(out);
        if (// not worth compressing, gets larger
        clen >= len - 5 || !deflater.finished()) {
            return;
        }
        clear();
        writeByte(type | 0x80);
        writeVarInt(len);
        writeByteArray(out);
    } catch (Exception e) {
        throw new EncoderException(e);
    } finally {
        readerIndex(0);
        deflater.end();
    }
}
Also used : EncoderException(io.netty.handler.codec.EncoderException) Deflater(java.util.zip.Deflater) EncoderException(io.netty.handler.codec.EncoderException) IOException(java.io.IOException)

Example 14 with EncoderException

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

the class CodecPlayOutBossBar method encode.

@Override
public ByteBuffer encode(CodecContext context, MessagePlayOutBossBar message) throws CodecException {
    ByteBuffer buf = context.byteBufAlloc().buffer();
    buf.writeUniqueId(message.getUniqueId());
    if (message instanceof MessagePlayOutBossBar.Add) {
        MessagePlayOutBossBar.Add message0 = (MessagePlayOutBossBar.Add) message;
        buf.writeVarInt(0);
        buf.write(Types.LOCALIZED_TEXT, message0.getTitle());
        buf.writeFloat(message0.getHealth());
        buf.writeVarInt(((LanternBossBarColor) message0.getColor()).getInternalId());
        buf.writeVarInt(((LanternBossBarOverlay) message0.getOverlay()).getInternalId());
        buf.writeByte(toFlags(message0.isDarkenSky(), message0.isEndMusic()));
    } else if (message instanceof MessagePlayOutBossBar.Remove) {
        buf.writeVarInt(1);
    } else if (message instanceof MessagePlayOutBossBar.UpdatePercent) {
        buf.writeVarInt(2);
        buf.writeFloat(((MessagePlayOutBossBar.UpdatePercent) message).getPercent());
    } else if (message instanceof MessagePlayOutBossBar.UpdateTitle) {
        buf.writeVarInt(3);
        buf.write(Types.LOCALIZED_TEXT, ((MessagePlayOutBossBar.UpdateTitle) message).getTitle());
    } else if (message instanceof MessagePlayOutBossBar.UpdateStyle) {
        MessagePlayOutBossBar.UpdateStyle message0 = (MessagePlayOutBossBar.UpdateStyle) message;
        buf.writeVarInt(4);
        buf.writeVarInt(((LanternBossBarColor) message0.getColor()).getInternalId());
        buf.writeVarInt(((LanternBossBarOverlay) message0.getOverlay()).getInternalId());
    } else if (message instanceof MessagePlayOutBossBar.UpdateMisc) {
        MessagePlayOutBossBar.UpdateMisc message0 = (MessagePlayOutBossBar.UpdateMisc) message;
        buf.writeVarInt(5);
        buf.writeByte(toFlags(message0.isDarkenSky(), message0.isEndMusic()));
    } else {
        throw new EncoderException("Unsupported message type: " + message.getClass().getName());
    }
    return buf;
}
Also used : MessagePlayOutBossBar(org.lanternpowered.server.network.vanilla.message.type.play.MessagePlayOutBossBar) ByteBuffer(org.lanternpowered.server.network.buffer.ByteBuffer) EncoderException(io.netty.handler.codec.EncoderException)

Example 15 with EncoderException

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

the class CodecPlayOutEffect method encode.

@Override
public ByteBuffer encode(CodecContext context, Message message) throws CodecException {
    final ByteBuffer buf = context.byteBufAlloc().buffer();
    if (message instanceof MessagePlayOutEffect) {
        final MessagePlayOutEffect message1 = (MessagePlayOutEffect) message;
        buf.writeInteger(message1.getType());
        buf.write(Types.VECTOR_3_I, message1.getPosition());
        buf.writeInteger(message1.getData());
        buf.writeBoolean(message1.isBroadcast());
    } else if (message instanceof MessagePlayOutRecord) {
        final MessagePlayOutRecord message1 = (MessagePlayOutRecord) message;
        buf.writeInteger(1010);
        buf.write(Types.VECTOR_3_I, message1.getPosition());
        buf.writeInteger(message1.getRecord().map(type -> 2256 + ((LanternRecordType) type).getInternalId()).orElse(0));
        buf.writeBoolean(false);
    } else {
        throw new EncoderException("Unsupported message type: " + message.getClass().getName());
    }
    return buf;
}
Also used : EncoderException(io.netty.handler.codec.EncoderException) CodecException(io.netty.handler.codec.CodecException) Codec(org.lanternpowered.server.network.message.codec.Codec) MessagePlayOutRecord(org.lanternpowered.server.network.vanilla.message.type.play.MessagePlayOutRecord) LanternRecordType(org.lanternpowered.server.data.type.record.LanternRecordType) MessagePlayOutEffect(org.lanternpowered.server.network.vanilla.message.type.play.MessagePlayOutEffect) ByteBuffer(org.lanternpowered.server.network.buffer.ByteBuffer) Types(org.lanternpowered.server.network.buffer.objects.Types) CodecContext(org.lanternpowered.server.network.message.codec.CodecContext) Message(org.lanternpowered.server.network.message.Message) EncoderException(io.netty.handler.codec.EncoderException) MessagePlayOutEffect(org.lanternpowered.server.network.vanilla.message.type.play.MessagePlayOutEffect) MessagePlayOutRecord(org.lanternpowered.server.network.vanilla.message.type.play.MessagePlayOutRecord) ByteBuffer(org.lanternpowered.server.network.buffer.ByteBuffer)

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