tcr/java/src/game/network/NettyCompressionDecoder.java
2025-03-16 17:40:47 +01:00

62 lines
2 KiB
Java
Executable file

package game.network;
import java.util.List;
import java.util.zip.DataFormatException;
import java.util.zip.Inflater;
import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.ByteToMessageDecoder;
import io.netty.handler.codec.DecoderException;
public class NettyCompressionDecoder extends ByteToMessageDecoder
{
private final Inflater inflater;
private int treshold;
public NettyCompressionDecoder(int treshold)
{
this.treshold = treshold;
this.inflater = new Inflater();
}
protected void decode(ChannelHandlerContext p_decode_1_, ByteBuf p_decode_2_, List<Object> p_decode_3_) throws DataFormatException, Exception
{
if (p_decode_2_.readableBytes() != 0)
{
PacketBuffer packetbuffer = new PacketBuffer(p_decode_2_);
int i = packetbuffer.readVarIntFromBuffer();
if (i == 0)
{
p_decode_3_.add(packetbuffer.readBytes(packetbuffer.readableBytes()));
}
else
{
if (i < this.treshold)
{
throw new DecoderException("Badly compressed packet - size of " + i + " is below server threshold of " + this.treshold);
}
if (i > 2097152)
{
throw new DecoderException("Badly compressed packet - size of " + i + " is larger than protocol maximum of " + 2097152);
}
byte[] abyte = new byte[packetbuffer.readableBytes()];
packetbuffer.readBytes(abyte);
this.inflater.setInput(abyte);
byte[] abyte1 = new byte[i];
this.inflater.inflate(abyte1);
p_decode_3_.add(Unpooled.wrappedBuffer(abyte1));
this.inflater.reset();
}
}
}
public void setCompressionTreshold(int treshold)
{
this.treshold = treshold;
}
}