mirror of
https://github.com/Minestom/Minestom.git
synced 2025-01-23 16:41:35 +01:00
Channel write cleanup
This commit is contained in:
parent
0b23795fb0
commit
538d641d4b
@ -132,8 +132,7 @@ public class DynamicChunk extends Chunk {
|
|||||||
final PlayerConnection connection = player.getPlayerConnection();
|
final PlayerConnection connection = player.getPlayerConnection();
|
||||||
if (connection instanceof NettyPlayerConnection) {
|
if (connection instanceof NettyPlayerConnection) {
|
||||||
final long lastChange = getLastChangeTime();
|
final long lastChange = getLastChangeTime();
|
||||||
if (lastChange > cachedPacketTime ||
|
if (lastChange > cachedPacketTime || (cachedChunkBuffer == null || cachedLightBuffer == null)) {
|
||||||
(cachedChunkBuffer == null || cachedLightBuffer == null)) {
|
|
||||||
this.cachedChunkBuffer = PacketUtils.createFramedPacket(ByteBuffer.allocate(65000), createChunkPacket());
|
this.cachedChunkBuffer = PacketUtils.createFramedPacket(ByteBuffer.allocate(65000), createChunkPacket());
|
||||||
this.cachedLightBuffer = PacketUtils.createFramedPacket(ByteBuffer.allocate(65000), createLightPacket());
|
this.cachedLightBuffer = PacketUtils.createFramedPacket(ByteBuffer.allocate(65000), createLightPacket());
|
||||||
this.cachedPacketTime = lastChange;
|
this.cachedPacketTime = lastChange;
|
||||||
|
@ -188,7 +188,7 @@ public class NettyPlayerConnection extends PlayerConnection {
|
|||||||
serverPacket = ((ComponentHoldingServerPacket) serverPacket).copyWithOperator(component ->
|
serverPacket = ((ComponentHoldingServerPacket) serverPacket).copyWithOperator(component ->
|
||||||
GlobalTranslator.render(component, Objects.requireNonNullElseGet(player.getLocale(), MinestomAdventure::getDefaultLocale)));
|
GlobalTranslator.render(component, Objects.requireNonNullElseGet(player.getLocale(), MinestomAdventure::getDefaultLocale)));
|
||||||
}
|
}
|
||||||
attemptWrite(serverPacket);
|
write(serverPacket);
|
||||||
} else {
|
} else {
|
||||||
// Player is probably not logged yet
|
// Player is probably not logged yet
|
||||||
writeAndFlush(serverPacket);
|
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) {
|
public void write(@NotNull ByteBuffer buffer) {
|
||||||
attemptWrite(buffer);
|
|
||||||
}
|
|
||||||
|
|
||||||
public void writeAndFlush(@NotNull ServerPacket packet) {
|
|
||||||
synchronized (tickBuffer) {
|
synchronized (tickBuffer) {
|
||||||
attemptWrite(packet);
|
buffer.flip();
|
||||||
flush();
|
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) {
|
synchronized (tickBuffer) {
|
||||||
final int position = tickBuffer.position();
|
final int position = tickBuffer.position();
|
||||||
try {
|
try {
|
||||||
@ -229,21 +236,10 @@ public class NettyPlayerConnection extends PlayerConnection {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public void attemptWrite(ByteBuffer buffer) {
|
public void writeAndFlush(@NotNull ServerPacket packet) {
|
||||||
synchronized (tickBuffer) {
|
synchronized (tickBuffer) {
|
||||||
try {
|
write(packet);
|
||||||
this.tickBuffer.put(buffer.flip());
|
flush();
|
||||||
} 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();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -257,10 +253,11 @@ public class NettyPlayerConnection extends PlayerConnection {
|
|||||||
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) {
|
||||||
|
disconnect();
|
||||||
MinecraftServer.getExceptionManager().handleException(e);
|
MinecraftServer.getExceptionManager().handleException(e);
|
||||||
}
|
}
|
||||||
this.tickBuffer.clear();
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user