mirror of
https://github.com/Minestom/Minestom.git
synced 2025-01-21 23:51:36 +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.
|
* It is the implementation used for all network client.
|
||||||
*/
|
*/
|
||||||
public class NettyPlayerConnection extends PlayerConnection {
|
public class NettyPlayerConnection extends PlayerConnection {
|
||||||
|
private final Worker worker;
|
||||||
private final SocketChannel channel;
|
private final SocketChannel channel;
|
||||||
|
|
||||||
private SocketAddress remoteAddress;
|
private SocketAddress remoteAddress;
|
||||||
|
|
||||||
private volatile boolean encrypted = false;
|
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 final ByteBuffer tickBuffer = ByteBuffer.allocateDirect(Server.SOCKET_BUFFER_SIZE);
|
||||||
private volatile ByteBuffer cacheBuffer;
|
private volatile ByteBuffer cacheBuffer;
|
||||||
|
|
||||||
public NettyPlayerConnection(@NotNull SocketChannel channel, SocketAddress remoteAddress) {
|
public NettyPlayerConnection(@NotNull Worker worker, @NotNull SocketChannel channel, SocketAddress remoteAddress) {
|
||||||
super();
|
super();
|
||||||
|
this.worker = worker;
|
||||||
this.channel = channel;
|
this.channel = channel;
|
||||||
this.remoteAddress = remoteAddress;
|
this.remoteAddress = remoteAddress;
|
||||||
}
|
}
|
||||||
@ -283,9 +283,8 @@ public class NettyPlayerConnection extends PlayerConnection {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void disconnect() {
|
public void disconnect() {
|
||||||
refreshOnline(false);
|
|
||||||
try {
|
try {
|
||||||
this.channel.close();
|
this.worker.disconnect(this, channel);
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
}
|
}
|
||||||
|
@ -18,7 +18,7 @@ public class Server {
|
|||||||
public static final Logger LOGGER = LoggerFactory.getLogger(Server.class);
|
public static final Logger LOGGER = LoggerFactory.getLogger(Server.class);
|
||||||
public static final int WORKER_COUNT = Integer.getInteger("minestom.workers",
|
public static final int WORKER_COUNT = Integer.getInteger("minestom.workers",
|
||||||
Runtime.getRuntime().availableProcessors() * 2);
|
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 int MAX_PACKET_SIZE = 2097151; // 3 bytes var-int
|
||||||
public static final boolean NO_DELAY = true;
|
public static final boolean NO_DELAY = true;
|
||||||
|
|
||||||
|
@ -73,22 +73,19 @@ public class Worker {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public void receiveConnection(SocketChannel channel) throws IOException {
|
public void receiveConnection(SocketChannel channel) throws IOException {
|
||||||
this.connectionMap.put(channel, new NettyPlayerConnection(channel, channel.getRemoteAddress()));
|
this.connectionMap.put(channel, new NettyPlayerConnection(this, channel, channel.getRemoteAddress()));
|
||||||
register(channel);
|
|
||||||
this.selector.wakeup();
|
|
||||||
}
|
|
||||||
|
|
||||||
private void register(SocketChannel channel) throws IOException {
|
|
||||||
channel.configureBlocking(false);
|
channel.configureBlocking(false);
|
||||||
channel.register(selector, SelectionKey.OP_READ);
|
channel.register(selector, SelectionKey.OP_READ);
|
||||||
var socket = channel.socket();
|
var socket = channel.socket();
|
||||||
socket.setSendBufferSize(Server.SOCKET_BUFFER_SIZE);
|
socket.setSendBufferSize(Server.SOCKET_BUFFER_SIZE);
|
||||||
socket.setReceiveBufferSize(Server.SOCKET_BUFFER_SIZE);
|
socket.setReceiveBufferSize(Server.SOCKET_BUFFER_SIZE);
|
||||||
socket.setTcpNoDelay(Server.NO_DELAY);
|
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
|
// Client close
|
||||||
|
connection.refreshOnline(false);
|
||||||
channel.close();
|
channel.close();
|
||||||
this.connectionMap.remove(channel);
|
this.connectionMap.remove(channel);
|
||||||
// Remove the connection
|
// Remove the connection
|
||||||
|
@ -89,7 +89,7 @@ public final class PacketUtils {
|
|||||||
// Send grouped packet...
|
// Send grouped packet...
|
||||||
final boolean success = PACKET_LISTENER_MANAGER.processServerPacket(packet, players);
|
final boolean success = PACKET_LISTENER_MANAGER.processServerPacket(packet, players);
|
||||||
if (success) {
|
if (success) {
|
||||||
ByteBuffer finalBuffer = ByteBuffer.allocate(2_000_000);
|
ByteBuffer finalBuffer = ByteBuffer.allocate(200_000);
|
||||||
writeFramedPacket(finalBuffer, packet, MinecraftServer.getCompressionThreshold() > 0);
|
writeFramedPacket(finalBuffer, packet, MinecraftServer.getCompressionThreshold() > 0);
|
||||||
final FramedPacket framedPacket = new FramedPacket(finalBuffer);
|
final FramedPacket framedPacket = new FramedPacket(finalBuffer);
|
||||||
// Send packet to all players
|
// Send packet to all players
|
||||||
|
Loading…
Reference in New Issue
Block a user