Changes to networking

This commit is contained in:
Eoghanmc22 2021-03-25 10:44:02 -04:00
parent 5320beddb8
commit 0f5bb0e4f2
4 changed files with 25 additions and 19 deletions

@ -1 +1 @@
Subproject commit 424c41d75e375c749640d608b9a3ae3c9592a25e Subproject commit 2b25cb297bdad56a0673ab54f2a631a9808ea3b6

View File

@ -34,8 +34,6 @@ public class PacketCompressor extends ByteToMessageCodec<ByteBuf> {
private final int threshold; private final int threshold;
private final byte[] buffer = new byte[8192];
private final Deflater deflater = new Deflater(3); private final Deflater deflater = new Deflater(3);
private final Inflater inflater = new Inflater(); private final Inflater inflater = new Inflater();
@ -45,7 +43,7 @@ public class PacketCompressor extends ByteToMessageCodec<ByteBuf> {
@Override @Override
protected void encode(ChannelHandlerContext ctx, ByteBuf from, ByteBuf to) { protected void encode(ChannelHandlerContext ctx, ByteBuf from, ByteBuf to) {
PacketUtils.compressBuffer(deflater, buffer, from, to); PacketUtils.compressBuffer(deflater, from, to);
} }
@Override @Override

View File

@ -143,9 +143,13 @@ public class NettyPlayerConnection extends PlayerConnection {
timestamp > timedBuffer.getTimestamp(); timestamp > timedBuffer.getTimestamp();
if (shouldUpdate) { if (shouldUpdate) {
final ByteBuf buffer = PacketUtils.createFramedPacket(serverPacket, false); final ByteBuf buffer = PacketUtils.createFramedPacket(serverPacket, true);
TimedBuffer oldBuffer = timedBuffer;
timedBuffer = new TimedBuffer(buffer, timestamp); timedBuffer = new TimedBuffer(buffer, timestamp);
temporaryCache.cache(identifier, timedBuffer); temporaryCache.cache(identifier, timedBuffer);
if (oldBuffer != null) {
oldBuffer.getBuffer().release();
}
} }
write(new FramedPacket(timedBuffer.getBuffer())); write(new FramedPacket(timedBuffer.getBuffer()));
@ -172,8 +176,9 @@ public class NettyPlayerConnection extends PlayerConnection {
} else if (message instanceof ServerPacket) { } else if (message instanceof ServerPacket) {
final ServerPacket serverPacket = (ServerPacket) message; final ServerPacket serverPacket = (ServerPacket) message;
synchronized (tickBuffer) { synchronized (tickBuffer) {
final ByteBuf framedPacket = PacketUtils.createFramedPacket(serverPacket, false); final ByteBuf framedPacket = PacketUtils.createFramedPacket(serverPacket, true);
tickBuffer.writeBytes(framedPacket); tickBuffer.writeBytes(framedPacket);
framedPacket.release();
} }
return; return;
} else if (message instanceof ByteBuf) { } else if (message instanceof ByteBuf) {

View File

@ -158,29 +158,31 @@ public final class PacketUtils {
* @param packetBuffer the buffer containing all the packet fields * @param packetBuffer the buffer containing all the packet fields
* @param compressionTarget the buffer which will receive the compressed version of {@code packetBuffer} * @param compressionTarget the buffer which will receive the compressed version of {@code packetBuffer}
*/ */
public static void compressBuffer(@NotNull Deflater deflater, @Nullable byte[] buffer, public static void compressBuffer(@NotNull Deflater deflater, @NotNull ByteBuf packetBuffer, @NotNull ByteBuf compressionTarget) {
@NotNull ByteBuf packetBuffer, @NotNull ByteBuf compressionTarget) {
final int packetLength = packetBuffer.readableBytes(); final int packetLength = packetBuffer.readableBytes();
final boolean compression = packetLength > MinecraftServer.getCompressionThreshold(); final boolean compression = packetLength > MinecraftServer.getCompressionThreshold();
Utils.writeVarIntBuf(compressionTarget, compression ? packetLength : 0); Utils.writeVarIntBuf(compressionTarget, compression ? packetLength : 0);
if (compression) { if (compression) {
compress(deflater, buffer, packetBuffer, compressionTarget); compress(deflater, packetBuffer, compressionTarget);
} else { } else {
compressionTarget.writeBytes(packetBuffer); compressionTarget.writeBytes(packetBuffer);
} }
} }
private static void compress(@NotNull Deflater deflater, @Nullable byte[] buffer, private static void compress(@NotNull Deflater deflater, @NotNull ByteBuf uncompressed, @NotNull ByteBuf compressed) {
@NotNull ByteBuf uncompressed, @NotNull ByteBuf compressed) {
// Allocate buffer if not already
byte[] output = buffer != null ? buffer : new byte[8192];
deflater.setInput(uncompressed.nioBuffer()); deflater.setInput(uncompressed.nioBuffer());
deflater.finish(); deflater.finish();
while (!deflater.finished()) { while (!deflater.finished()) {
final int length = deflater.deflate(output); compressed.writerIndex(
compressed.writeBytes(output, 0, length); deflater.deflate(compressed.nioBuffer(compressed.writerIndex(), compressed.writableBytes()))
+ compressed.writerIndex()
);
if (compressed.writableBytes() == 0) {
compressed.ensureWritable(8192);
}
} }
deflater.reset(); deflater.reset();
@ -197,7 +199,7 @@ public final class PacketUtils {
*/ */
@NotNull @NotNull
public static ByteBuf createFramedPacket(@NotNull ServerPacket serverPacket, boolean directBuffer) { public static ByteBuf createFramedPacket(@NotNull ServerPacket serverPacket, boolean directBuffer) {
ByteBuf packetBuf = Unpooled.buffer(); ByteBuf packetBuf = directBuffer ? BufUtils.getBuffer(true) : Unpooled.buffer();
writePacket(packetBuf, serverPacket); writePacket(packetBuf, serverPacket);
final int dataLength = packetBuf.readableBytes(); final int dataLength = packetBuf.readableBytes();
@ -211,18 +213,19 @@ public final class PacketUtils {
ByteBuf compressedBuf = directBuffer ? BufUtils.getBuffer(true) : Unpooled.buffer(); ByteBuf compressedBuf = directBuffer ? BufUtils.getBuffer(true) : Unpooled.buffer();
Utils.writeVarIntBuf(compressedBuf, dataLength); Utils.writeVarIntBuf(compressedBuf, dataLength);
final Deflater deflater = DEFLATER.get(); final Deflater deflater = DEFLATER.get();
compress(deflater, null, packetBuf, compressedBuf); compress(deflater, packetBuf, compressedBuf);
packetBuf.release();
packetBuf = compressedBuf; packetBuf = compressedBuf;
} else { } else {
// Packet too small // Packet too small
ByteBuf uncompressedLengthBuffer = Unpooled.buffer(); ByteBuf uncompressedLengthBuffer = directBuffer ? BufUtils.getBuffer(true, 1) : Unpooled.buffer();
Utils.writeVarIntBuf(uncompressedLengthBuffer, 0); Utils.writeVarIntBuf(uncompressedLengthBuffer, 0);
packetBuf = Unpooled.wrappedBuffer(uncompressedLengthBuffer, packetBuf); packetBuf = Unpooled.wrappedBuffer(uncompressedLengthBuffer, packetBuf);
} }
} }
// Write the final length of the packet // 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()); Utils.writeVarIntBuf(packetLengthBuffer, packetBuf.readableBytes());
return Unpooled.wrappedBuffer(packetLengthBuffer, packetBuf); return Unpooled.wrappedBuffer(packetLengthBuffer, packetBuf);