Archiviert
1
0

Cyclic port running to reduce failing server starts

Signed-off-by: Lixfel <agga-games@gmx.de>
Dieser Commit ist enthalten in:
Lixfel 2021-10-02 19:07:50 +02:00
Ursprung f270985f8f
Commit 3f417d8ecb

Datei anzeigen

@ -29,7 +29,9 @@ import net.md_5.bungee.api.connection.ProxiedPlayer;
import java.io.File; import java.io.File;
import java.net.InetSocketAddress; import java.net.InetSocketAddress;
import java.time.format.DateTimeFormatter; import java.time.format.DateTimeFormatter;
import java.util.Set;
import java.util.UUID; import java.util.UUID;
import java.util.stream.Collectors;
public class SubserverSystem { public class SubserverSystem {
private SubserverSystem(){} private SubserverSystem(){}
@ -39,14 +41,9 @@ public class SubserverSystem {
private static final String SERVER_PATH = BACKBONE + "server/"; private static final String SERVER_PATH = BACKBONE + "server/";
private static final String EVENT_PATH = BACKBONE + "event/"; private static final String EVENT_PATH = BACKBONE + "event/";
private static final int FIRST_ARENA_PORT; private static final boolean MAIN_SERVER = ProxyServer.getInstance().getConfig().getListeners().stream().anyMatch(info -> ((InetSocketAddress) info.getSocketAddress()).getPort() == 25565);
private static final Portrange bauPorts = MAIN_SERVER ? new Portrange(10100, 20000) : new Portrange(2100, 2200);
static { private static final Portrange arenaPorts = MAIN_SERVER ? new Portrange(3000, 3100) : (BungeeCore.EVENT_MODE ? new Portrange(4000, 5000) : bauPorts);
if(BungeeCore.EVENT_MODE)
FIRST_ARENA_PORT = 6000;
else
FIRST_ARENA_PORT = 2500;
}
/** /**
* This function starts every arena (even test- and eventarenas). * This function starts every arena (even test- and eventarenas).
@ -88,13 +85,13 @@ public class SubserverSystem {
public static synchronized Subserver startArena(ArenaMode modus, String map, int eventFightID, int checkSchemID, int prepareSchemID, String serverName, String mapName, UUID player1, UUID player2, boolean ranked){ public static synchronized Subserver startArena(ArenaMode modus, String map, int eventFightID, int checkSchemID, int prepareSchemID, String serverName, String mapName, UUID player1, UUID player2, boolean ranked){
//Generate missing parameters //Generate missing parameters
Node node = eventFightID > 0 ? Node.local : Node.getNode(); Node node = eventFightID > 0 ? Node.local : Node.getNode();
int port = freePort(FIRST_ARENA_PORT); int port = arenaPorts.freePort();
if(serverName == null){ if(serverName == null){
if(ranked) if(ranked)
serverName = "Ranked" + (port - FIRST_ARENA_PORT); serverName = "Ranked" + (port - arenaPorts.start);
else else
serverName = modus.getDisplayName() + (port - FIRST_ARENA_PORT); serverName = modus.getDisplayName() + (port - arenaPorts.start);
} }
if(mapName == null) if(mapName == null)
mapName = serverName; mapName = serverName;
@ -159,7 +156,7 @@ public class SubserverSystem {
SteamwarUser user = SteamwarUser.get(owner); SteamwarUser user = SteamwarUser.get(owner);
File directory = new File(SERVER_PATH, serverName); File directory = new File(SERVER_PATH, serverName);
Node node = Node.getNode(); Node node = Node.getNode();
int port = freePort(4000); int port = bauPorts.freePort();
new Bauserver(user.getUserName() + "s Bau", owner, port, node.startServer( new Bauserver(user.getUserName() + "s Bau", owner, port, node.startServer(
serverJar, directory, worldDir, worldName, port, xmx, "logPath=" + worldName serverJar, directory, worldDir, worldName, port, xmx, "logPath=" + worldName
@ -213,21 +210,40 @@ public class SubserverSystem {
Node.local.execute("cp", "-r", sourcePath, targetPath); Node.local.execute("cp", "-r", sourcePath, targetPath);
} }
private static int freePort(int start){
private static class Portrange {
private final int start;
private final int end;
private int current;
private Portrange(int start, int end) {
this.start = start;
this.end = end;
current = start;
}
private void increment() {
current++;
if(current == end)
current = start;
}
private synchronized int freePort() {
Set<Integer> usedPorts;
synchronized (Subserver.getServerList()) { synchronized (Subserver.getServerList()) {
int port = start; usedPorts = Subserver.getServerList().stream().map(server -> ((InetSocketAddress) server.getServer().getSocketAddress()).getPort()).collect(Collectors.toSet());
boolean isFree = false; }
while(!isFree){
port++; while(usedPorts.contains(current)) {
isFree = true; increment();
for(Subserver server : Subserver.getServerList()){ }
if(((InetSocketAddress)server.getServer().getSocketAddress()).getPort() == port){
isFree = false; int result = current;
break; increment();
} return result;
}
}
return port;
} }
} }
} }