Reuse flush method

This commit is contained in:
TheMode 2021-08-05 01:10:03 +02:00
parent 58487f4455
commit eb43dc0cc0

View File

@ -198,17 +198,11 @@ public class NettyPlayerConnection extends PlayerConnection {
public void write(@NotNull ByteBuffer buffer) { public void write(@NotNull ByteBuffer buffer) {
synchronized (tickBuffer) { synchronized (tickBuffer) {
buffer.flip(); buffer.flip();
if (tickBuffer.remaining() >= buffer.remaining()) { if (buffer.remaining() > tickBuffer.remaining()) {
// Enough buffer space // Tick buffer is full, flush before appending
this.tickBuffer.put(buffer); flush();
} else {
try {
this.channel.write(tickBuffer.flip());
this.tickBuffer.clear().put(buffer);
} catch (IOException ex) {
MinecraftServer.getExceptionManager().handleException(ex);
}
} }
this.tickBuffer.put(buffer);
} }
} }
@ -218,17 +212,13 @@ public class NettyPlayerConnection extends PlayerConnection {
public void write(@NotNull ServerPacket packet) { public void write(@NotNull ServerPacket packet) {
synchronized (tickBuffer) { synchronized (tickBuffer) {
final int position = tickBuffer.position(); this.tickBuffer.mark();
try { try {
PacketUtils.writeFramedPacket(tickBuffer, packet, compressed); PacketUtils.writeFramedPacket(tickBuffer, packet, compressed);
} catch (BufferOverflowException e) { } catch (BufferOverflowException e) {
try { this.tickBuffer.reset();
this.channel.write(tickBuffer.position(position).flip()); flush();
this.tickBuffer.clear(); PacketUtils.writeFramedPacket(tickBuffer, packet, compressed);
PacketUtils.writeFramedPacket(tickBuffer, packet, compressed);
} catch (IOException ex) {
MinecraftServer.getExceptionManager().handleException(ex);
}
} }
} }
} }
@ -241,18 +231,15 @@ public class NettyPlayerConnection extends PlayerConnection {
} }
public void flush() { public void flush() {
if (tickBuffer.position() == 0) {
// Nothing to write
return;
}
synchronized (tickBuffer) { synchronized (tickBuffer) {
if (!channel.isOpen()) return; if (!channel.isOpen()) return;
if (tickBuffer.position() == 0) return; if (tickBuffer.position() == 0) return;
try { try {
this.channel.write(tickBuffer.flip()); this.channel.write(tickBuffer.flip());
this.tickBuffer.clear();
} catch (IOException e) { } catch (IOException e) {
MinecraftServer.getExceptionManager().handleException(e); MinecraftServer.getExceptionManager().handleException(e);
} finally {
this.tickBuffer.clear();
} }
} }
} }