ace-of-shades-2/server/src/main/java/nl/andrewl/aos2_server/Server.java

122 lines
4.0 KiB
Java
Raw Normal View History

package nl.andrewl.aos2_server;
2022-07-17 17:37:34 +00:00
import nl.andrewl.aos2_server.config.ServerConfig;
import nl.andrewl.aos2_server.logic.WorldUpdater;
2022-07-17 17:37:34 +00:00
import nl.andrewl.aos_core.config.Config;
import nl.andrewl.aos_core.model.world.World;
import nl.andrewl.aos_core.model.world.Worlds;
import nl.andrewl.aos_core.net.UdpReceiver;
2022-07-07 14:51:29 +00:00
import nl.andrewl.aos_core.net.udp.ClientInputState;
import nl.andrewl.aos_core.net.udp.ClientOrientationState;
import nl.andrewl.aos_core.net.udp.DatagramInit;
import nl.andrewl.record_net.Message;
2022-07-07 14:51:29 +00:00
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.IOException;
import java.net.*;
import java.nio.file.Path;
2022-07-17 17:37:34 +00:00
import java.util.List;
import java.util.concurrent.ForkJoinPool;
public class Server implements Runnable {
2022-07-07 14:51:29 +00:00
private static final Logger log = LoggerFactory.getLogger(Server.class);
private final ServerSocket serverSocket;
private final DatagramSocket datagramSocket;
private volatile boolean running;
2022-07-17 17:37:34 +00:00
private final ServerConfig config;
2022-07-07 14:51:29 +00:00
private final PlayerManager playerManager;
private final World world;
2022-07-07 14:51:29 +00:00
private final WorldUpdater worldUpdater;
2022-07-17 17:37:34 +00:00
public Server(ServerConfig config) throws IOException {
this.config = config;
this.serverSocket = new ServerSocket(config.port, config.connectionBacklog);
this.serverSocket.setReuseAddress(true);
2022-07-17 17:37:34 +00:00
this.datagramSocket = new DatagramSocket(config.port);
this.datagramSocket.setReuseAddress(true);
2022-07-07 14:51:29 +00:00
this.playerManager = new PlayerManager();
2022-07-17 17:37:34 +00:00
this.worldUpdater = new WorldUpdater(this, config.ticksPerSecond);
this.world = Worlds.testingWorld();
}
@Override
public void run() {
running = true;
new Thread(new UdpReceiver(datagramSocket, this::handleUdpMessage)).start();
2022-07-07 14:51:29 +00:00
new Thread(worldUpdater).start();
log.info("Started AoS2 Server on TCP/UDP port {}; now accepting connections.", serverSocket.getLocalPort());
while (running) {
acceptClientConnection();
}
2022-07-07 14:51:29 +00:00
playerManager.deregisterAll();
worldUpdater.shutdown();
datagramSocket.close(); // Shuts down the UdpReceiver.
try {
serverSocket.close();
} catch (IOException e) {
throw new RuntimeException(e);
}
}
public void handleUdpMessage(Message msg, DatagramPacket packet) {
if (msg instanceof DatagramInit init) {
2022-07-07 14:51:29 +00:00
playerManager.handleUdpInit(init, packet);
} else if (msg instanceof ClientInputState inputState) {
ServerPlayer player = playerManager.getPlayer(inputState.clientId());
if (player != null) {
player.getActionManager().setLastInputState(inputState);
playerManager.broadcastUdpMessage(player.getUpdateMessage());
2022-07-07 14:51:29 +00:00
}
} else if (msg instanceof ClientOrientationState orientationState) {
ServerPlayer player = playerManager.getPlayer(orientationState.clientId());
if (player != null) {
player.setOrientation(orientationState.x(), orientationState.y());
playerManager.broadcastUdpMessageToAllBut(player.getUpdateMessage(), player);
}
}
}
private void acceptClientConnection() {
try {
Socket clientSocket = serverSocket.accept();
var handler = new ClientCommunicationHandler(this, clientSocket, datagramSocket);
2022-07-07 14:51:29 +00:00
// Establish the connection in a separate thread so that we can continue accepting clients.
ForkJoinPool.commonPool().submit(() -> {
try {
handler.establishConnection();
} catch (IOException e) {
e.printStackTrace();
}
});
} catch (IOException e) {
if (e instanceof SocketException && !this.running && e.getMessage().equalsIgnoreCase("Socket closed")) {
return; // Ignore this exception, since it is expected on shutdown.
}
e.printStackTrace();
}
}
2022-07-17 17:37:34 +00:00
public ServerConfig getConfig() {
return config;
}
2022-07-07 14:51:29 +00:00
public World getWorld() {
return world;
}
public PlayerManager getPlayerManager() {
return playerManager;
}
public static void main(String[] args) throws IOException {
2022-07-17 17:37:34 +00:00
List<Path> configPaths = Config.getCommonConfigPaths();
if (args.length > 0) {
configPaths.add(Path.of(args[0].trim()));
}
ServerConfig cfg = Config.loadConfig(ServerConfig.class, configPaths, new ServerConfig());
new Server(cfg).run();
}
}