1
0
mirror of https://github.com/Minestom/Minestom.git synced 2025-01-26 10:01:36 +01:00

Optimized PacketCompressor

This commit is contained in:
themode 2020-11-16 11:41:03 +01:00
parent 310733b747
commit 540405e796
4 changed files with 32 additions and 35 deletions
src/main/java/net/minestom/server

View File

@ -420,11 +420,6 @@ public class Player extends LivingEntity implements CommandSender {
entityPositionAndRotationPacket.pitch = position.getPitch(); entityPositionAndRotationPacket.pitch = position.getPitch();
entityPositionAndRotationPacket.onGround = onGround; entityPositionAndRotationPacket.onGround = onGround;
lastX = position.getX();
lastY = position.getY();
lastZ = position.getZ();
lastYaw = position.getYaw();
lastPitch = position.getPitch();
updatePacket = entityPositionAndRotationPacket; updatePacket = entityPositionAndRotationPacket;
} else if (positionChanged) { } else if (positionChanged) {
EntityPositionPacket entityPositionPacket = new EntityPositionPacket(); EntityPositionPacket entityPositionPacket = new EntityPositionPacket();
@ -433,9 +428,7 @@ public class Player extends LivingEntity implements CommandSender {
entityPositionPacket.deltaY = (short) ((position.getY() * 32 - lastY * 32) * 128); entityPositionPacket.deltaY = (short) ((position.getY() * 32 - lastY * 32) * 128);
entityPositionPacket.deltaZ = (short) ((position.getZ() * 32 - lastZ * 32) * 128); entityPositionPacket.deltaZ = (short) ((position.getZ() * 32 - lastZ * 32) * 128);
entityPositionPacket.onGround = onGround; entityPositionPacket.onGround = onGround;
lastX = position.getX();
lastY = position.getY();
lastZ = position.getZ();
updatePacket = entityPositionPacket; updatePacket = entityPositionPacket;
} else { } else {
// View changed // View changed
@ -445,8 +438,6 @@ public class Player extends LivingEntity implements CommandSender {
entityRotationPacket.pitch = position.getPitch(); entityRotationPacket.pitch = position.getPitch();
entityRotationPacket.onGround = onGround; entityRotationPacket.onGround = onGround;
lastYaw = position.getYaw();
lastPitch = position.getPitch();
updatePacket = entityRotationPacket; updatePacket = entityRotationPacket;
} }
@ -473,6 +464,16 @@ public class Player extends LivingEntity implements CommandSender {
} }
} }
if (positionChanged) {
lastX = position.getX();
lastY = position.getY();
lastZ = position.getZ();
}
if (viewChanged) {
lastYaw = position.getYaw();
lastPitch = position.getPitch();
}
} }
@Override @Override

View File

@ -335,7 +335,7 @@ public class PlayerInventory implements InventoryModifier, InventoryClickHandler
/** /**
* Refreshes an inventory slot. * Refreshes an inventory slot.
* *
* @param slot the packet slot * @param slot the packet slot,
* see {@link net.minestom.server.utils.inventory.PlayerInventoryUtils#convertToPacketSlot(int)} * see {@link net.minestom.server.utils.inventory.PlayerInventoryUtils#convertToPacketSlot(int)}
* @param itemStack the item stack in the slot * @param itemStack the item stack in the slot
*/ */

View File

@ -8,6 +8,7 @@ import net.minestom.server.network.packet.client.play.ClientPlayerPositionPacket
import net.minestom.server.network.packet.client.play.ClientPlayerRotationPacket; import net.minestom.server.network.packet.client.play.ClientPlayerRotationPacket;
import net.minestom.server.utils.Position; import net.minestom.server.utils.Position;
import net.minestom.server.utils.chunk.ChunkUtils; import net.minestom.server.utils.chunk.ChunkUtils;
import org.jetbrains.annotations.NotNull;
public class PlayerPositionListener { public class PlayerPositionListener {
@ -47,7 +48,7 @@ public class PlayerPositionListener {
processMovement(player, x, y, z, yaw, pitch, onGround); processMovement(player, x, y, z, yaw, pitch, onGround);
} }
private static void processMovement(Player player, float x, float y, float z, private static void processMovement(@NotNull Player player, float x, float y, float z,
float yaw, float pitch, boolean onGround) { float yaw, float pitch, boolean onGround) {
// Try to move in an unloaded chunk, prevent it // Try to move in an unloaded chunk, prevent it

View File

@ -27,45 +27,38 @@ import java.util.List;
import java.util.zip.Deflater; import java.util.zip.Deflater;
import java.util.zip.Inflater; import java.util.zip.Inflater;
// TODO Optimize
public class PacketCompressor extends ByteToMessageCodec<ByteBuf> { public class PacketCompressor extends ByteToMessageCodec<ByteBuf> {
private final byte[] buffer = new byte[8192];
private final int threshold; private final int threshold;
private final Inflater inflater; private byte[] buffer = new byte[8192];
private final Deflater deflater;
private Deflater deflater = new Deflater();
private Inflater inflater = new Inflater();
public PacketCompressor(int threshold) { public PacketCompressor(int threshold) {
this.inflater = new Inflater();
this.deflater = new Deflater();
this.threshold = threshold; this.threshold = threshold;
} }
@Override @Override
protected void encode(ChannelHandlerContext ctx, ByteBuf from, ByteBuf to) { protected void encode(ChannelHandlerContext ctx, ByteBuf from, ByteBuf to) {
int i = from.readableBytes(); final int packetLength = from.readableBytes();
if (i < this.threshold) { if (packetLength < this.threshold) {
Utils.writeVarIntBuf(to, 0); Utils.writeVarIntBuf(to, 0);
to.writeBytes(from); to.writeBytes(from);
} else { } else {
byte[] abyte = new byte[i]; Utils.writeVarIntBuf(to, packetLength);
from.readBytes(abyte);
Utils.writeVarIntBuf(to, abyte.length); deflater.setInput(from.nioBuffer());
this.deflater.setInput(abyte, 0, i); deflater.finish();
this.deflater.finish();
while (!this.deflater.finished()) { while (!deflater.finished()) {
int j = this.deflater.deflate(this.buffer); final int length = deflater.deflate(buffer);
to.writeBytes(buffer, 0, length);
to.writeBytes(this.buffer, 0, j);
} }
this.deflater.reset(); deflater.reset();
} }
} }
@ -85,16 +78,18 @@ public class PacketCompressor extends ByteToMessageCodec<ByteBuf> {
throw new DecoderException("Badly compressed packet - size of " + i + " is larger than protocol maximum of 2097152"); throw new DecoderException("Badly compressed packet - size of " + i + " is larger than protocol maximum of 2097152");
} }
// TODO optimize to do not initialize arrays each time
byte[] abyte = new byte[buf.readableBytes()]; byte[] abyte = new byte[buf.readableBytes()];
buf.readBytes(abyte); buf.readBytes(abyte);
this.inflater.setInput(abyte); inflater.setInput(abyte);
byte[] abyte1 = new byte[i]; byte[] abyte1 = new byte[i];
this.inflater.inflate(abyte1); inflater.inflate(abyte1);
out.add(Unpooled.wrappedBuffer(abyte1)); out.add(Unpooled.wrappedBuffer(abyte1));
this.inflater.reset(); inflater.reset();
} }
} }
} }