Channel write cleanup

This commit is contained in:
TheMode 2021-08-04 20:34:27 +02:00
parent 0b23795fb0
commit 538d641d4b
2 changed files with 25 additions and 29 deletions

View File

@ -132,8 +132,7 @@ public class DynamicChunk extends Chunk {
final PlayerConnection connection = player.getPlayerConnection();
if (connection instanceof NettyPlayerConnection) {
final long lastChange = getLastChangeTime();
if (lastChange > cachedPacketTime ||
(cachedChunkBuffer == null || cachedLightBuffer == null)) {
if (lastChange > cachedPacketTime || (cachedChunkBuffer == null || cachedLightBuffer == null)) {
this.cachedChunkBuffer = PacketUtils.createFramedPacket(ByteBuffer.allocate(65000), createChunkPacket());
this.cachedLightBuffer = PacketUtils.createFramedPacket(ByteBuffer.allocate(65000), createLightPacket());
this.cachedPacketTime = lastChange;

View File

@ -188,7 +188,7 @@ public class NettyPlayerConnection extends PlayerConnection {
serverPacket = ((ComponentHoldingServerPacket) serverPacket).copyWithOperator(component ->
GlobalTranslator.render(component, Objects.requireNonNullElseGet(player.getLocale(), MinestomAdventure::getDefaultLocale)));
}
attemptWrite(serverPacket);
write(serverPacket);
} else {
// Player is probably not logged yet
writeAndFlush(serverPacket);
@ -196,22 +196,29 @@ public class NettyPlayerConnection extends PlayerConnection {
}
}
public void write(@NotNull FramedPacket framedPacket) {
attemptWrite(framedPacket.body());
}
public void write(@NotNull ByteBuffer buffer) {
attemptWrite(buffer);
}
public void writeAndFlush(@NotNull ServerPacket packet) {
synchronized (tickBuffer) {
attemptWrite(packet);
flush();
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) {
disconnect();
MinecraftServer.getExceptionManager().handleException(ex);
}
}
}
}
public void attemptWrite(ServerPacket packet) {
public void write(@NotNull FramedPacket framedPacket) {
write(framedPacket.body());
}
public void write(@NotNull ServerPacket packet) {
synchronized (tickBuffer) {
final int position = tickBuffer.position();
try {
@ -229,21 +236,10 @@ public class NettyPlayerConnection extends PlayerConnection {
}
}
public void attemptWrite(ByteBuffer buffer) {
public void writeAndFlush(@NotNull ServerPacket packet) {
synchronized (tickBuffer) {
try {
this.tickBuffer.put(buffer.flip());
} catch (BufferOverflowException e) {
try {
this.channel.write(tickBuffer.flip());
this.channel.write(buffer);
} catch (IOException ex) {
disconnect();
MinecraftServer.getExceptionManager().handleException(ex);
} finally {
this.tickBuffer.clear();
}
}
write(packet);
flush();
}
}
@ -257,10 +253,11 @@ public class NettyPlayerConnection extends PlayerConnection {
if (tickBuffer.position() == 0) return;
try {
this.channel.write(tickBuffer.flip());
this.tickBuffer.clear();
} catch (IOException e) {
disconnect();
MinecraftServer.getExceptionManager().handleException(e);
}
this.tickBuffer.clear();
}
}