Fix memory leak, decrease socket size

This commit is contained in:
TheMode 2021-08-04 03:53:01 +02:00
parent 5adeed392b
commit 9bb50430d4
4 changed files with 10 additions and 14 deletions

View File

@ -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();
}

View File

@ -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;

View File

@ -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

View File

@ -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