Do not write empty buffer

This commit is contained in:
themode 2021-03-26 11:09:30 +01:00
parent 1b162c1139
commit 1ceffd8540

View File

@ -71,12 +71,15 @@ public class NettyPlayerConnection extends PlayerConnection {
@Override @Override
public void update() { public void update() {
// Flush // Flush
final int bufferSize = tickBuffer.writerIndex();
if (bufferSize > 0) {
this.channel.eventLoop().submit(() -> { this.channel.eventLoop().submit(() -> {
if (channel.isActive()) { if (channel.isActive()) {
writeWaitingPackets(); writeWaitingPackets();
this.channel.flush(); channel.flush();
} }
}); });
}
// Network stats // Network stats
super.update(); super.update();
} }
@ -208,6 +211,11 @@ public class NettyPlayerConnection extends PlayerConnection {
private void writeWaitingPackets() { private void writeWaitingPackets() {
synchronized (tickBuffer) { synchronized (tickBuffer) {
final ByteBuf copy = tickBuffer.copy(); final ByteBuf copy = tickBuffer.copy();
if (copy.writerIndex() == 0) {
// Nothing to write
copy.release();
return;
}
ChannelFuture channelFuture = channel.write(new FramedPacket(copy)); ChannelFuture channelFuture = channel.write(new FramedPacket(copy));
channelFuture.addListener(future -> copy.release()); channelFuture.addListener(future -> copy.release());