mirror of
https://github.com/Minestom/Minestom.git
synced 2025-01-11 10:58:46 +01:00
Changes to networking
This commit is contained in:
parent
5320beddb8
commit
0f5bb0e4f2
@ -1 +1 @@
|
||||
Subproject commit 424c41d75e375c749640d608b9a3ae3c9592a25e
|
||||
Subproject commit 2b25cb297bdad56a0673ab54f2a631a9808ea3b6
|
@ -34,8 +34,6 @@ public class PacketCompressor extends ByteToMessageCodec<ByteBuf> {
|
||||
|
||||
private final int threshold;
|
||||
|
||||
private final byte[] buffer = new byte[8192];
|
||||
|
||||
private final Deflater deflater = new Deflater(3);
|
||||
private final Inflater inflater = new Inflater();
|
||||
|
||||
@ -45,7 +43,7 @@ public class PacketCompressor extends ByteToMessageCodec<ByteBuf> {
|
||||
|
||||
@Override
|
||||
protected void encode(ChannelHandlerContext ctx, ByteBuf from, ByteBuf to) {
|
||||
PacketUtils.compressBuffer(deflater, buffer, from, to);
|
||||
PacketUtils.compressBuffer(deflater, from, to);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
@ -143,9 +143,13 @@ public class NettyPlayerConnection extends PlayerConnection {
|
||||
timestamp > timedBuffer.getTimestamp();
|
||||
|
||||
if (shouldUpdate) {
|
||||
final ByteBuf buffer = PacketUtils.createFramedPacket(serverPacket, false);
|
||||
final ByteBuf buffer = PacketUtils.createFramedPacket(serverPacket, true);
|
||||
TimedBuffer oldBuffer = timedBuffer;
|
||||
timedBuffer = new TimedBuffer(buffer, timestamp);
|
||||
temporaryCache.cache(identifier, timedBuffer);
|
||||
if (oldBuffer != null) {
|
||||
oldBuffer.getBuffer().release();
|
||||
}
|
||||
}
|
||||
|
||||
write(new FramedPacket(timedBuffer.getBuffer()));
|
||||
@ -172,8 +176,9 @@ public class NettyPlayerConnection extends PlayerConnection {
|
||||
} else if (message instanceof ServerPacket) {
|
||||
final ServerPacket serverPacket = (ServerPacket) message;
|
||||
synchronized (tickBuffer) {
|
||||
final ByteBuf framedPacket = PacketUtils.createFramedPacket(serverPacket, false);
|
||||
final ByteBuf framedPacket = PacketUtils.createFramedPacket(serverPacket, true);
|
||||
tickBuffer.writeBytes(framedPacket);
|
||||
framedPacket.release();
|
||||
}
|
||||
return;
|
||||
} else if (message instanceof ByteBuf) {
|
||||
|
@ -158,29 +158,31 @@ public final class PacketUtils {
|
||||
* @param packetBuffer the buffer containing all the packet fields
|
||||
* @param compressionTarget the buffer which will receive the compressed version of {@code packetBuffer}
|
||||
*/
|
||||
public static void compressBuffer(@NotNull Deflater deflater, @Nullable byte[] buffer,
|
||||
@NotNull ByteBuf packetBuffer, @NotNull ByteBuf compressionTarget) {
|
||||
public static void compressBuffer(@NotNull Deflater deflater, @NotNull ByteBuf packetBuffer, @NotNull ByteBuf compressionTarget) {
|
||||
final int packetLength = packetBuffer.readableBytes();
|
||||
final boolean compression = packetLength > MinecraftServer.getCompressionThreshold();
|
||||
Utils.writeVarIntBuf(compressionTarget, compression ? packetLength : 0);
|
||||
if (compression) {
|
||||
compress(deflater, buffer, packetBuffer, compressionTarget);
|
||||
compress(deflater, packetBuffer, compressionTarget);
|
||||
} else {
|
||||
compressionTarget.writeBytes(packetBuffer);
|
||||
}
|
||||
}
|
||||
|
||||
private static void compress(@NotNull Deflater deflater, @Nullable byte[] buffer,
|
||||
@NotNull ByteBuf uncompressed, @NotNull ByteBuf compressed) {
|
||||
// Allocate buffer if not already
|
||||
byte[] output = buffer != null ? buffer : new byte[8192];
|
||||
private static void compress(@NotNull Deflater deflater, @NotNull ByteBuf uncompressed, @NotNull ByteBuf compressed) {
|
||||
|
||||
deflater.setInput(uncompressed.nioBuffer());
|
||||
deflater.finish();
|
||||
|
||||
while (!deflater.finished()) {
|
||||
final int length = deflater.deflate(output);
|
||||
compressed.writeBytes(output, 0, length);
|
||||
compressed.writerIndex(
|
||||
deflater.deflate(compressed.nioBuffer(compressed.writerIndex(), compressed.writableBytes()))
|
||||
+ compressed.writerIndex()
|
||||
);
|
||||
|
||||
if (compressed.writableBytes() == 0) {
|
||||
compressed.ensureWritable(8192);
|
||||
}
|
||||
}
|
||||
|
||||
deflater.reset();
|
||||
@ -197,7 +199,7 @@ public final class PacketUtils {
|
||||
*/
|
||||
@NotNull
|
||||
public static ByteBuf createFramedPacket(@NotNull ServerPacket serverPacket, boolean directBuffer) {
|
||||
ByteBuf packetBuf = Unpooled.buffer();
|
||||
ByteBuf packetBuf = directBuffer ? BufUtils.getBuffer(true) : Unpooled.buffer();
|
||||
writePacket(packetBuf, serverPacket);
|
||||
|
||||
final int dataLength = packetBuf.readableBytes();
|
||||
@ -211,18 +213,19 @@ public final class PacketUtils {
|
||||
ByteBuf compressedBuf = directBuffer ? BufUtils.getBuffer(true) : Unpooled.buffer();
|
||||
Utils.writeVarIntBuf(compressedBuf, dataLength);
|
||||
final Deflater deflater = DEFLATER.get();
|
||||
compress(deflater, null, packetBuf, compressedBuf);
|
||||
compress(deflater, packetBuf, compressedBuf);
|
||||
packetBuf.release();
|
||||
packetBuf = compressedBuf;
|
||||
} else {
|
||||
// Packet too small
|
||||
ByteBuf uncompressedLengthBuffer = Unpooled.buffer();
|
||||
ByteBuf uncompressedLengthBuffer = directBuffer ? BufUtils.getBuffer(true, 1) : Unpooled.buffer();
|
||||
Utils.writeVarIntBuf(uncompressedLengthBuffer, 0);
|
||||
packetBuf = Unpooled.wrappedBuffer(uncompressedLengthBuffer, packetBuf);
|
||||
}
|
||||
}
|
||||
|
||||
// Write the final length of the packet
|
||||
ByteBuf packetLengthBuffer = Unpooled.buffer();
|
||||
ByteBuf packetLengthBuffer = directBuffer ? BufUtils.getBuffer(true, 5) : Unpooled.buffer();
|
||||
Utils.writeVarIntBuf(packetLengthBuffer, packetBuf.readableBytes());
|
||||
|
||||
return Unpooled.wrappedBuffer(packetLengthBuffer, packetBuf);
|
||||
|
Loading…
Reference in New Issue
Block a user