From eb43dc0cc0f2603d8e55206769fd8eb6dce201fc Mon Sep 17 00:00:00 2001 From: TheMode Date: Thu, 5 Aug 2021 01:10:03 +0200 Subject: [PATCH] Reuse flush method --- .../network/player/NettyPlayerConnection.java | 33 ++++++------------- 1 file changed, 10 insertions(+), 23 deletions(-) diff --git a/src/main/java/net/minestom/server/network/player/NettyPlayerConnection.java b/src/main/java/net/minestom/server/network/player/NettyPlayerConnection.java index 5ca363d0b..6399887e5 100644 --- a/src/main/java/net/minestom/server/network/player/NettyPlayerConnection.java +++ b/src/main/java/net/minestom/server/network/player/NettyPlayerConnection.java @@ -198,17 +198,11 @@ public class NettyPlayerConnection extends PlayerConnection { public void write(@NotNull ByteBuffer buffer) { synchronized (tickBuffer) { buffer.flip(); - if (tickBuffer.remaining() >= buffer.remaining()) { - // Enough buffer space - this.tickBuffer.put(buffer); - } else { - try { - this.channel.write(tickBuffer.flip()); - this.tickBuffer.clear().put(buffer); - } catch (IOException ex) { - MinecraftServer.getExceptionManager().handleException(ex); - } + if (buffer.remaining() > tickBuffer.remaining()) { + // Tick buffer is full, flush before appending + flush(); } + this.tickBuffer.put(buffer); } } @@ -218,17 +212,13 @@ public class NettyPlayerConnection extends PlayerConnection { public void write(@NotNull ServerPacket packet) { synchronized (tickBuffer) { - final int position = tickBuffer.position(); + this.tickBuffer.mark(); try { PacketUtils.writeFramedPacket(tickBuffer, packet, compressed); } catch (BufferOverflowException e) { - try { - this.channel.write(tickBuffer.position(position).flip()); - this.tickBuffer.clear(); - PacketUtils.writeFramedPacket(tickBuffer, packet, compressed); - } catch (IOException ex) { - MinecraftServer.getExceptionManager().handleException(ex); - } + this.tickBuffer.reset(); + flush(); + PacketUtils.writeFramedPacket(tickBuffer, packet, compressed); } } } @@ -241,18 +231,15 @@ public class NettyPlayerConnection extends PlayerConnection { } public void flush() { - if (tickBuffer.position() == 0) { - // Nothing to write - return; - } synchronized (tickBuffer) { if (!channel.isOpen()) return; if (tickBuffer.position() == 0) return; try { this.channel.write(tickBuffer.flip()); - this.tickBuffer.clear(); } catch (IOException e) { MinecraftServer.getExceptionManager().handleException(e); + } finally { + this.tickBuffer.clear(); } } }