Improved socket handling for the server (#1998)

* Improves socket handling from the server.
A logging is added which catches errors as soon as the server socket connection cannot be closed. In addition, the program terminates itself immediately to avoid further errors.

* Worker stop has been transferred.
The worker stop was implemented in a close method in the worker class and is now called from the server. The access modifier was adapted for this

* Adding a wakup call before the server closes its socket
This commit is contained in:
Phillipp Glanz 2024-02-18 01:43:05 +01:00 committed by GitHub
parent ffb33e608d
commit 7ec3e3021e
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 27 additions and 3 deletions

View File

@ -15,9 +15,13 @@ import java.nio.channels.SocketChannel;
import java.nio.file.Files;
import java.util.Arrays;
import java.util.List;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public final class Server {
private static final Logger LOGGER = LoggerFactory.getLogger(Server.class);
public static final boolean NO_DELAY = true;
private volatile boolean stop;
@ -112,8 +116,14 @@ public final class Server {
} catch (IOException e) {
MinecraftServer.getExceptionManager().handleException(e);
}
this.selector.wakeup();
this.workers.forEach(worker -> worker.selector.wakeup());
try {
this.selector.wakeup();
this.selector.close();
} catch (IOException e) {
LOGGER.error("Server socket sector could not be closed", e);
System.exit(-1);
}
this.workers.forEach(Worker::close);
}
@ApiStatus.Internal

View File

@ -19,12 +19,16 @@ import java.nio.channels.SocketChannel;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.atomic.AtomicInteger;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@ApiStatus.Internal
public final class Worker extends MinestomThread {
private static final AtomicInteger COUNTER = new AtomicInteger();
final Selector selector;
private static final Logger LOGGER = LoggerFactory.getLogger(Server.class);
private final Selector selector;
private final Map<SocketChannel, PlayerSocketConnection> connectionMap = new ConcurrentHashMap<>();
private final Server server;
private final MpscUnboundedXaddArrayQueue<Runnable> queue = new MpscUnboundedXaddArrayQueue<>(1024);
@ -43,6 +47,16 @@ public final class Worker extends MinestomThread {
this.selector.wakeup();
}
public void close() {
this.selector.wakeup();
try {
this.selector.close();
} catch (IOException e) {
LOGGER.error("Worker Socket Sector could not be closed", e);
System.exit(-1);
}
}
@Override
public void run() {
while (server.isOpen()) {