Network upgrade and new reconnection mode (#11527)

Network upgrade and new reconnection mode:
* users can disconnect or close app without game progress loose now;
* disconnect dialog will show active tables stats and additional options;
* all active tables will be restored on reconnect (tables, tourneys, games, drafts, sideboarding, constructing);
* user must use same server and username on next connection;
* there are few minutes for reconnect until server kick off a disconnected player from all player's tables (concede/loose);
* now you can safety reconnect after IP change (after proxy/vpn/wifi/router restart);

Other improvements and fixes:
* gui: main menu - improved switch panel button, added stats about current tables/panels;
* gui: improved data sync and updates (fixes many use cases with empty battlefield, not started games/drafts/tourneys, not updatable drafts, etc);
* gui: improved stability on game updates (fixes some random errors related to wrong threads);
* server: fixed miss messages about player's disconnection problems for other players in the chat;
* refactor: simplified and improved connection and network related code, deleted outdated code, added docs;
* tests: improved load test to support lands only set for more stable performance/network testing (set TEST_AI_RANDOM_DECK_SETS = PELP and run test_TwoAIPlayGame_Multiple);
This commit is contained in:
Oleg Agafonov 2023-12-07 19:56:52 +03:00 committed by GitHub
parent 7f0558ff3c
commit 960e896903
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
71 changed files with 1274 additions and 802 deletions

View file

@ -1,16 +1,17 @@
package mage.server;
import mage.cards.ExpansionSet;
import mage.cards.RateCard;
import mage.cards.Sets;
import mage.cards.decks.DeckValidatorFactory;
import mage.cards.repository.CardScanner;
import mage.cards.repository.PluginClassloaderRegistery;
import mage.cards.repository.RepositoryUtil;
import mage.cards.RateCard;
import mage.game.match.MatchType;
import mage.game.tournament.TournamentType;
import mage.interfaces.MageServer;
import mage.remote.Connection;
import mage.remote.SessionImpl;
import mage.server.draft.CubeFactory;
import mage.server.game.GameFactory;
import mage.server.game.PlayerFactory;
@ -18,7 +19,10 @@ import mage.server.managers.ConfigSettings;
import mage.server.managers.ManagerFactory;
import mage.server.record.UserStatsRepository;
import mage.server.tournament.TournamentFactory;
import mage.server.util.*;
import mage.server.util.ConfigFactory;
import mage.server.util.ConfigWrapper;
import mage.server.util.PluginClassLoader;
import mage.server.util.ServerMessagesUtil;
import mage.server.util.config.GamePlugin;
import mage.server.util.config.Plugin;
import mage.utils.MageVersion;
@ -44,7 +48,7 @@ import java.nio.file.Paths;
import java.util.*;
/**
* @author BetaSteward_at_googlemail.com
* @author BetaSteward_at_googlemail.com, JayDi85
*/
public final class Main {
@ -56,6 +60,8 @@ public final class Main {
// priority: default setting -> prop setting -> arg setting
private static final String testModeArg = "-testMode=";
private static final String testModeProp = "xmage.testMode";
private static final String detailsModeArg = "-detailsMode=";
private static final String detailsModeProp = "xmage.detailsMode";
private static final String adminPasswordArg = "-adminPassword=";
private static final String adminPasswordProp = "xmage.adminPassword";
private static final String configPathProp = "xmage.config.path";
@ -75,6 +81,7 @@ public final class Main {
// - simplified registration and login (no password check);
// - debug main menu for GUI and rendering testing (must use -debug arg for client app);
private static boolean testMode;
private static boolean detailsMode;
public static void main(String[] args) {
System.setProperty("java.util.Arrays.useLegacyMergeSort", "true");
@ -85,6 +92,7 @@ public final class Main {
// enable test mode by default for developer build (if you run it from source code)
testMode |= version.isDeveloperBuild();
detailsMode = false;
// settings by properties (-Dxxx=yyy from a launcher)
if (System.getProperty(testModeProp) != null) {
@ -93,6 +101,9 @@ public final class Main {
if (System.getProperty(adminPasswordProp) != null) {
adminPassword = SystemUtil.sanitize(System.getProperty(adminPasswordProp));
}
if (System.getProperty(detailsModeProp) != null) {
detailsMode = Boolean.parseBoolean(System.getProperty(detailsModeProp));
}
final String configPath;
if (System.getProperty(configPathProp) != null) {
configPath = System.getProperty(configPathProp);
@ -108,6 +119,9 @@ public final class Main {
adminPassword = arg.replace(adminPasswordArg, "");
adminPassword = SystemUtil.sanitize(adminPassword);
}
if (arg.startsWith(detailsModeArg)) {
detailsMode = Boolean.parseBoolean(arg.replace(detailsModeArg, ""));
}
}
logger.info(String.format("Reading configuration from path=%s", configPath));
@ -234,7 +248,8 @@ public final class Main {
logger.info("Config - max pool size : " + config.getMaxPoolSize());
logger.info("Config - num accp.threads: " + config.getNumAcceptThreads());
logger.info("Config - second.bind port: " + config.getSecondaryBindPort());
logger.info("Config - auth. activated : " + (config.isAuthenticationActivated() ? "true" : "false"));
logger.info("Config - users registration: " + (config.isAuthenticationActivated() ? "true" : "false"));
logger.info("Config - users anon: " + (!config.isAuthenticationActivated() ? "true" : "false"));
logger.info("Config - mailgun api key : " + config.getMailgunApiKey());
logger.info("Config - mailgun domain : " + config.getMailgunDomain());
logger.info("Config - mail smtp Host : " + config.getMailSmtpHost());
@ -261,7 +276,13 @@ public final class Main {
// Parameter: serializationtype => jboss
InvokerLocator serverLocator = new InvokerLocator(connection.getURI());
if (!isAlreadyRunning(config, serverLocator)) {
server = new MageTransporterServer(managerFactory, serverLocator, new MageServerImpl(managerFactory, adminPassword, testMode), MageServer.class.getName(), new MageServerInvocationHandler(managerFactory));
server = new MageTransporterServer(
managerFactory,
serverLocator,
new MageServerImpl(managerFactory, adminPassword, testMode, detailsMode),
MageServer.class.getName(),
new MageServerInvocationHandler(managerFactory)
);
server.start();
logger.info("Started MAGE server - listening on " + connection.toString());
@ -297,67 +318,75 @@ public final class Main {
return false;
}
static class ClientConnectionListener implements ConnectionListener {
/**
* Network, server side: connection monitoring and error processing
*/
static class MageServerConnectionListener implements ConnectionListener {
private final ManagerFactory managerFactory;
public ClientConnectionListener(ManagerFactory managerFactory) {
public MageServerConnectionListener(ManagerFactory managerFactory) {
this.managerFactory = managerFactory;
}
@Override
public void handleConnectionException(Throwable throwable, Client client) {
String sessionId = client.getSessionId();
Optional<Session> session = managerFactory.sessionManager().getSession(sessionId);
if (!session.isPresent()) {
logger.trace("Session not found : " + sessionId);
} else {
UUID userId = session.get().getUserId();
StringBuilder sessionInfo = new StringBuilder();
Optional<User> user = managerFactory.userManager().getUser(userId);
if (user.isPresent()) {
sessionInfo.append(user.get().getName()).append(" [").append(user.get().getGameInfo()).append(']');
} else {
sessionInfo.append("[user missing] ");
}
sessionInfo.append(" at ").append(session.get().getHost()).append(" sessionId: ").append(session.get().getId());
if (throwable instanceof ClientDisconnectedException) {
// Seems like the random diconnects from public server land here and should not be handled as explicit disconnects
// So it should be possible to reconnect to server and continue games if DisconnectReason is set to LostConnection
//managerFactory.sessionManager().disconnect(client.getSessionId(), DisconnectReason.Disconnected);
managerFactory.sessionManager().disconnect(client.getSessionId(), DisconnectReason.LostConnection);
logger.info("CLIENT DISCONNECTED - " + sessionInfo);
logger.debug("Stack Trace", throwable);
} else {
managerFactory.sessionManager().disconnect(client.getSessionId(), DisconnectReason.LostConnection);
logger.info("LOST CONNECTION - " + sessionInfo);
if (logger.isDebugEnabled()) {
if (throwable == null) {
logger.debug("- cause: Lease expired");
} else {
logger.debug(" - cause: " + Session.getBasicCause(throwable).toString());
}
}
}
Session session = managerFactory.sessionManager().getSession(sessionId).orElse(null);
if (session == null) {
logger.debug("Connection error, session not found : " + sessionId + " - " + throwable);
return;
}
// fill by user's info
StringBuilder sessionInfo = new StringBuilder();
User user = managerFactory.userManager().getUser(session.getUserId()).orElse(null);
if (user != null) {
sessionInfo.append(user.getName()).append(" [").append(user.getGameInfo()).append(']');
} else {
sessionInfo.append("[no user]");
}
sessionInfo.append(" at ").append(session.getHost()).append(", sessionId: ").append(session.getId());
// check disconnection reason
// lease ping is inner jboss feature to check connection status
// xmage ping is app's feature to check connection and send additional data like ping statistics
if (throwable instanceof ClientDisconnectedException) {
// client called a disconnect command (full disconnect without tables keep)
// no need to keep session
logger.info("CLIENT DISCONNECTED - " + sessionInfo);
logger.debug("- cause: client called disconnect command");
managerFactory.sessionManager().disconnect(client.getSessionId(), DisconnectReason.DisconnectedByUser, true);
} else if (throwable == null) {
// lease timeout (ping), so server lost connection with a client
// must keep tables
logger.info("LOST CONNECTION - " + sessionInfo);
logger.debug("- cause: lease expired");
managerFactory.sessionManager().disconnect(client.getSessionId(), DisconnectReason.LostConnection, true);
} else {
// unknown error
// must keep tables
logger.info("LOST CONNECTION - " + sessionInfo);
logger.debug("- cause: unknown error - " + throwable);
managerFactory.sessionManager().disconnect(client.getSessionId(), DisconnectReason.LostConnection, true);
}
}
}
/**
* Network, server side: data transport layer
*/
static class MageTransporterServer extends TransporterServer {
protected Connector connector;
public MageTransporterServer(ManagerFactory managerFactory, InvokerLocator locator, Object target, String subsystem, MageServerInvocationHandler serverInvocationHandler) throws Exception {
super(locator, target, subsystem);
connector.addInvocationHandler("callback", serverInvocationHandler);
connector.setLeasePeriod(managerFactory.configSettings().getLeasePeriod());
connector.addConnectionListener(new ClientConnectionListener(managerFactory));
}
connector.addInvocationHandler("callback", serverInvocationHandler); // commands processing
public Connector getConnector() throws Exception {
return connector;
// connection monitoring and errors processing
connector.setLeasePeriod(managerFactory.configSettings().getLeasePeriod());
connector.addConnectionListener(new MageServerConnectionListener(managerFactory));
}
@Override
@ -368,6 +397,9 @@ public final class Main {
}
}
/**
* Network, server side: commands layer for execute (creates one thread per each client connection)
*/
static class MageServerInvocationHandler implements ServerInvocationHandler {
private final ManagerFactory managerFactory;
@ -396,6 +428,7 @@ public final class Main {
@Override
public void setInvoker(ServerInvoker invoker) {
// connection settings
((BisocketServerInvoker) invoker).setSecondaryBindPort(managerFactory.configSettings().getSecondaryBindPort());
((BisocketServerInvoker) invoker).setBacklog(managerFactory.configSettings().getBacklogSize());
((BisocketServerInvoker) invoker).setNumAcceptThreads(managerFactory.configSettings().getNumAcceptThreads());
@ -403,7 +436,10 @@ public final class Main {
@Override
public void addListener(InvokerCallbackHandler callbackHandler) {
// Called for every client connecting to the server
// on client connect: remember client session
// TODO: add ban by IP here?
// need creates on first call, required by ping
// but can be removed and re-creates on reconnection (if server contains another user instance)
ServerInvokerCallbackHandler handler = (ServerInvokerCallbackHandler) callbackHandler;
try {
String sessionId = handler.getClientSessionId();
@ -415,8 +451,9 @@ public final class Main {
@Override
public Object invoke(final InvocationRequest invocation) throws Throwable {
// on command:
// called for every client connecting to the server (after add Listener)
// TODO: add ban by IP here?
// save client ip-address
String sessionId = invocation.getSessionId();
Map map = invocation.getRequestPayload();
@ -438,11 +475,17 @@ public final class Main {
@Override
public void removeListener(InvokerCallbackHandler callbackHandler) {
// on client disconnect: remember client session
// if user want to keep session then sessionId will be faked here
ServerInvokerCallbackHandler handler = (ServerInvokerCallbackHandler) callbackHandler;
String sessionId = handler.getClientSessionId();
managerFactory.sessionManager().disconnect(sessionId, DisconnectReason.Disconnected);
DisconnectReason reason = DisconnectReason.DisconnectedByUser;
if (SessionImpl.KEEP_MY_OLD_SESSION.equals(sessionId)) {
// no need in additional code, server will keep original session until inactive timeout
reason = DisconnectReason.DisconnectedByUserButKeepTables;
}
managerFactory.sessionManager().disconnect(sessionId, reason, true);
}
}
private static Class<?> loadPlugin(Plugin plugin) {