Add threshold for flushing

This commit is contained in:
themode 2021-03-16 04:36:43 +01:00
parent 19a84bcddd
commit effca76196

View File

@ -60,6 +60,7 @@ public class NettyPlayerConnection extends PlayerConnection {
private UUID bungeeUuid;
private PlayerSkin bungeeSkin;
private static final int FLUSH_SIZE = 20000;
private final ByteBuf tickBuffer = Unpooled.directBuffer();
public NettyPlayerConnection(@NotNull SocketChannel channel) {
@ -164,6 +165,7 @@ public class NettyPlayerConnection extends PlayerConnection {
synchronized (tickBuffer) {
final ByteBuf body = framedPacket.getBody();
tickBuffer.writeBytes(body, body.readerIndex(), body.readableBytes());
preventiveWrite();
}
return;
} else if (message instanceof ServerPacket) {
@ -171,12 +173,14 @@ public class NettyPlayerConnection extends PlayerConnection {
final ByteBuf buffer = PacketUtils.createFramedPacket(serverPacket, true);
synchronized (tickBuffer) {
tickBuffer.writeBytes(buffer);
preventiveWrite();
}
buffer.release();
return;
} else if (message instanceof ByteBuf) {
synchronized (tickBuffer) {
tickBuffer.writeBytes((ByteBuf) message);
preventiveWrite();
}
return;
}
@ -196,6 +200,12 @@ public class NettyPlayerConnection extends PlayerConnection {
}
}
private void preventiveWrite() {
if (tickBuffer.writerIndex() > FLUSH_SIZE) {
writeWaitingPackets();
}
}
private void writeWaitingPackets() {
synchronized (tickBuffer) {
final ByteBuf copy = tickBuffer.copy();