mirror of
https://github.com/Minestom/Minestom.git
synced 2025-02-16 04:11:39 +01:00
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:
parent
ffb33e608d
commit
7ec3e3021e
@ -15,9 +15,13 @@ import java.nio.channels.SocketChannel;
|
|||||||
import java.nio.file.Files;
|
import java.nio.file.Files;
|
||||||
import java.util.Arrays;
|
import java.util.Arrays;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
import org.slf4j.Logger;
|
||||||
|
import org.slf4j.LoggerFactory;
|
||||||
|
|
||||||
public final class Server {
|
public final class Server {
|
||||||
|
|
||||||
|
private static final Logger LOGGER = LoggerFactory.getLogger(Server.class);
|
||||||
|
|
||||||
public static final boolean NO_DELAY = true;
|
public static final boolean NO_DELAY = true;
|
||||||
|
|
||||||
private volatile boolean stop;
|
private volatile boolean stop;
|
||||||
@ -112,8 +116,14 @@ public final class Server {
|
|||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
MinecraftServer.getExceptionManager().handleException(e);
|
MinecraftServer.getExceptionManager().handleException(e);
|
||||||
}
|
}
|
||||||
this.selector.wakeup();
|
try {
|
||||||
this.workers.forEach(worker -> worker.selector.wakeup());
|
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
|
@ApiStatus.Internal
|
||||||
|
@ -19,12 +19,16 @@ import java.nio.channels.SocketChannel;
|
|||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
import java.util.concurrent.ConcurrentHashMap;
|
import java.util.concurrent.ConcurrentHashMap;
|
||||||
import java.util.concurrent.atomic.AtomicInteger;
|
import java.util.concurrent.atomic.AtomicInteger;
|
||||||
|
import org.slf4j.Logger;
|
||||||
|
import org.slf4j.LoggerFactory;
|
||||||
|
|
||||||
@ApiStatus.Internal
|
@ApiStatus.Internal
|
||||||
public final class Worker extends MinestomThread {
|
public final class Worker extends MinestomThread {
|
||||||
private static final AtomicInteger COUNTER = new AtomicInteger();
|
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 Map<SocketChannel, PlayerSocketConnection> connectionMap = new ConcurrentHashMap<>();
|
||||||
private final Server server;
|
private final Server server;
|
||||||
private final MpscUnboundedXaddArrayQueue<Runnable> queue = new MpscUnboundedXaddArrayQueue<>(1024);
|
private final MpscUnboundedXaddArrayQueue<Runnable> queue = new MpscUnboundedXaddArrayQueue<>(1024);
|
||||||
@ -43,6 +47,16 @@ public final class Worker extends MinestomThread {
|
|||||||
this.selector.wakeup();
|
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
|
@Override
|
||||||
public void run() {
|
public void run() {
|
||||||
while (server.isOpen()) {
|
while (server.isOpen()) {
|
||||||
|
Loading…
Reference in New Issue
Block a user