mirror of
https://github.com/Minestom/Minestom.git
synced 2025-01-21 15:41:38 +01:00
Fix memory leak, decrease socket size
This commit is contained in:
parent
5adeed392b
commit
9bb50430d4
@ -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();
|
||||
}
|
||||
|
@ -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;
|
||||
|
||||
|
@ -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
|
||||
|
@ -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
|
||||
|
Loading…
Reference in New Issue
Block a user