forked from BLOCKFANTASY/LOOHP-Limbo
Performance improvements:
- Update dependencies - Use blocking queue for Tick.asyncTasksQueue - Do not call removed method Thread#stop in Tick#waitAndKillThreads - Use virtual threads for ClientConnection - Add build action Signed-off-by: Joshua Castle <26531652+Kas-tle@users.noreply.github.com>
This commit is contained in:
@@ -149,15 +149,18 @@ import java.util.TimerTask;
|
||||
import java.util.UUID;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
import java.util.concurrent.atomic.AtomicLong;
|
||||
import java.util.concurrent.locks.Lock;
|
||||
import java.util.concurrent.locks.ReentrantLock;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
public class ClientConnection extends Thread {
|
||||
public class ClientConnection implements Runnable {
|
||||
|
||||
private static final Key DEFAULT_HANDLER_NAMESPACE = Key.key("default");
|
||||
private static final String BRAND_ANNOUNCE_CHANNEL = Key.key("brand").toString();
|
||||
|
||||
private final Random random = new Random();
|
||||
private final Socket clientSocket;
|
||||
private final Lock packetSendLock = new ReentrantLock();
|
||||
protected Channel channel;
|
||||
private boolean running;
|
||||
private volatile ClientState state;
|
||||
@@ -232,9 +235,14 @@ public class ClientConnection extends Thread {
|
||||
sendPacket(packet);
|
||||
}
|
||||
|
||||
public synchronized void sendPacket(PacketOut packet) throws IOException {
|
||||
if (channel.writePacket(packet)) {
|
||||
setLastPacketTimestamp(System.currentTimeMillis());
|
||||
public void sendPacket(PacketOut packet) throws IOException {
|
||||
packetSendLock.lock();
|
||||
try {
|
||||
if (channel.writePacket(packet)) {
|
||||
setLastPacketTimestamp(System.currentTimeMillis());
|
||||
}
|
||||
} finally {
|
||||
packetSendLock.unlock();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -27,12 +27,15 @@ import java.net.ServerSocket;
|
||||
import java.net.Socket;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.concurrent.ExecutorService;
|
||||
import java.util.concurrent.Executors;
|
||||
|
||||
public class ServerConnection extends Thread {
|
||||
|
||||
private final String ip;
|
||||
private final int port;
|
||||
private final boolean silent;
|
||||
private final ExecutorService virtualThreadExecutor = Executors.newVirtualThreadPerTaskExecutor();
|
||||
private ServerSocket serverSocket;
|
||||
private List<ClientConnection> clients;
|
||||
|
||||
@@ -53,9 +56,9 @@ public class ServerConnection extends Thread {
|
||||
}
|
||||
while (true) {
|
||||
Socket connection = serverSocket.accept();
|
||||
ClientConnection sc = new ClientConnection(connection);
|
||||
clients.add(sc);
|
||||
sc.start();
|
||||
ClientConnection clientTask = new ClientConnection(connection);
|
||||
clients.add(clientTask);
|
||||
virtualThreadExecutor.submit(clientTask);
|
||||
}
|
||||
} catch(IOException e) {
|
||||
e.printStackTrace();
|
||||
|
||||
Reference in New Issue
Block a user