From 9bb50430d497936235ca2a64a9feda90c6314509 Mon Sep 17 00:00:00 2001 From: TheMode Date: Wed, 4 Aug 2021 03:53:01 +0200 Subject: [PATCH] Fix memory leak, decrease socket size --- .../server/network/player/NettyPlayerConnection.java | 9 ++++----- .../net/minestom/server/network/socket/Server.java | 2 +- .../net/minestom/server/network/socket/Worker.java | 11 ++++------- .../java/net/minestom/server/utils/PacketUtils.java | 2 +- 4 files changed, 10 insertions(+), 14 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 112d8c96c..8945e1bd6 100644 --- a/src/main/java/net/minestom/server/network/player/NettyPlayerConnection.java +++ b/src/main/java/net/minestom/server/network/player/NettyPlayerConnection.java @@ -38,9 +38,8 @@ import java.util.zip.DataFormatException; * It is the implementation used for all network client. */ public class NettyPlayerConnection extends PlayerConnection { - + private final Worker worker; private final SocketChannel channel; - private SocketAddress remoteAddress; private volatile boolean encrypted = false; @@ -66,8 +65,9 @@ public class NettyPlayerConnection extends PlayerConnection { private final ByteBuffer tickBuffer = ByteBuffer.allocateDirect(Server.SOCKET_BUFFER_SIZE); private volatile ByteBuffer cacheBuffer; - public NettyPlayerConnection(@NotNull SocketChannel channel, SocketAddress remoteAddress) { + public NettyPlayerConnection(@NotNull Worker worker, @NotNull SocketChannel channel, SocketAddress remoteAddress) { super(); + this.worker = worker; this.channel = channel; this.remoteAddress = remoteAddress; } @@ -283,9 +283,8 @@ public class NettyPlayerConnection extends PlayerConnection { @Override public void disconnect() { - refreshOnline(false); try { - this.channel.close(); + this.worker.disconnect(this, channel); } catch (IOException e) { e.printStackTrace(); } diff --git a/src/main/java/net/minestom/server/network/socket/Server.java b/src/main/java/net/minestom/server/network/socket/Server.java index b5c98dbf9..434141a42 100644 --- a/src/main/java/net/minestom/server/network/socket/Server.java +++ b/src/main/java/net/minestom/server/network/socket/Server.java @@ -18,7 +18,7 @@ public class Server { public static final Logger LOGGER = LoggerFactory.getLogger(Server.class); public static final int WORKER_COUNT = Integer.getInteger("minestom.workers", Runtime.getRuntime().availableProcessors() * 2); - public static final int SOCKET_BUFFER_SIZE = Integer.getInteger("minestom.buffer-size", 262143); + public static final int SOCKET_BUFFER_SIZE = Integer.getInteger("minestom.buffer-size", 65535); public static final int MAX_PACKET_SIZE = 2097151; // 3 bytes var-int public static final boolean NO_DELAY = true; diff --git a/src/main/java/net/minestom/server/network/socket/Worker.java b/src/main/java/net/minestom/server/network/socket/Worker.java index 685484ba2..0846bb96a 100644 --- a/src/main/java/net/minestom/server/network/socket/Worker.java +++ b/src/main/java/net/minestom/server/network/socket/Worker.java @@ -73,22 +73,19 @@ public class Worker { } public void receiveConnection(SocketChannel channel) throws IOException { - this.connectionMap.put(channel, new NettyPlayerConnection(channel, channel.getRemoteAddress())); - register(channel); - this.selector.wakeup(); - } - - private void register(SocketChannel channel) throws IOException { + this.connectionMap.put(channel, new NettyPlayerConnection(this, channel, channel.getRemoteAddress())); channel.configureBlocking(false); channel.register(selector, SelectionKey.OP_READ); var socket = channel.socket(); socket.setSendBufferSize(Server.SOCKET_BUFFER_SIZE); socket.setReceiveBufferSize(Server.SOCKET_BUFFER_SIZE); socket.setTcpNoDelay(Server.NO_DELAY); + this.selector.wakeup(); } - private void disconnect(NettyPlayerConnection connection, SocketChannel channel) throws IOException { + public void disconnect(NettyPlayerConnection connection, SocketChannel channel) throws IOException { // Client close + connection.refreshOnline(false); channel.close(); this.connectionMap.remove(channel); // Remove the connection diff --git a/src/main/java/net/minestom/server/utils/PacketUtils.java b/src/main/java/net/minestom/server/utils/PacketUtils.java index f8298a113..afda163dd 100644 --- a/src/main/java/net/minestom/server/utils/PacketUtils.java +++ b/src/main/java/net/minestom/server/utils/PacketUtils.java @@ -89,7 +89,7 @@ public final class PacketUtils { // Send grouped packet... final boolean success = PACKET_LISTENER_MANAGER.processServerPacket(packet, players); if (success) { - ByteBuffer finalBuffer = ByteBuffer.allocate(2_000_000); + ByteBuffer finalBuffer = ByteBuffer.allocate(200_000); writeFramedPacket(finalBuffer, packet, MinecraftServer.getCompressionThreshold() > 0); final FramedPacket framedPacket = new FramedPacket(finalBuffer); // Send packet to all players