diff --git a/src/main/java/com/broll/networklib/client/LobbyGameClient.java b/src/main/java/com/broll/networklib/client/LobbyGameClient.java index 8c8a3e7..8f8955a 100644 --- a/src/main/java/com/broll/networklib/client/LobbyGameClient.java +++ b/src/main/java/com/broll/networklib/client/LobbyGameClient.java @@ -126,15 +126,15 @@ public CompletableFuture reconnectCheck(String ip) { } public CompletableFuture> discoverLobbies() { - return runTask(new LobbyDiscoveryTask(client)); + return runTask(new LobbyDiscoveryTask(client, clientAuthenticationKey)); } public CompletableFuture listLobbies(String ip) { - return runTask(new LobbyListTask(ip)); + return runTask(new LobbyListTask(ip, clientAuthenticationKey)); } public CompletableFuture listLobbies() { - return runTask(new LobbyListTask()); + return runTask(new LobbyListTask(clientAuthenticationKey)); } public CompletableFuture joinLobby(GameLobby lobby, String playerName) { diff --git a/src/main/java/com/broll/networklib/client/impl/LobbyLookupSite.java b/src/main/java/com/broll/networklib/client/impl/LobbyLookupSite.java index 00f3fee..a2fa9a7 100644 --- a/src/main/java/com/broll/networklib/client/impl/LobbyLookupSite.java +++ b/src/main/java/com/broll/networklib/client/impl/LobbyLookupSite.java @@ -1,8 +1,10 @@ package com.broll.networklib.client.impl; import com.broll.networklib.PackageReceiver; +import com.broll.networklib.client.auth.ClientAuthenticationKey; import com.broll.networklib.client.tasks.AbstractTaskSite; import com.broll.networklib.client.tasks.DiscoveredLobbies; +import com.broll.networklib.network.nt.NT_ListLobbies; import com.broll.networklib.network.nt.NT_ServerInformation; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -15,9 +17,13 @@ public class LobbyLookupSite extends AbstractTaskSite { private final static Logger Log = LoggerFactory.getLogger(LobbyLookupSite.class); - public void lookup() { + + + public void lookup(ClientAuthenticationKey key) { Log.info("SEND LOOKUP"); - client.sendTCP(new NT_ServerInformation()); + NT_ListLobbies nt = new NT_ListLobbies(); + nt.authenticationKey = key.getSecret(); + client.sendTCP(nt); } @PackageReceiver diff --git a/src/main/java/com/broll/networklib/client/tasks/AbstractClientTask.java b/src/main/java/com/broll/networklib/client/tasks/AbstractClientTask.java index 25876a9..b2e4103 100644 --- a/src/main/java/com/broll/networklib/client/tasks/AbstractClientTask.java +++ b/src/main/java/com/broll/networklib/client/tasks/AbstractClientTask.java @@ -4,6 +4,7 @@ import com.broll.networklib.client.GameClient; import com.broll.networklib.client.LobbyClientSite; import com.broll.networklib.client.LobbyGameClient; +import com.broll.networklib.client.auth.ClientAuthenticationKey; import com.broll.networklib.network.IRegisterNetwork; import com.broll.networklib.network.NetworkException; @@ -20,12 +21,18 @@ public abstract class AbstractClientTask { + protected ClientAuthenticationKey authKey; private CompletableFuture future; private LobbyGameClient client; private List onComplete = new ArrayList<>(); private IRegisterNetwork networkRegister; + private final static int TIMEOUT = 5; + public AbstractClientTask(ClientAuthenticationKey authKey){ + this.authKey = authKey; + } + public CompletableFuture run(LobbyGameClient client, IRegisterNetwork networkRegister) { this.networkRegister = networkRegister; this.client = client; diff --git a/src/main/java/com/broll/networklib/client/tasks/impl/CreateLobbyTask.java b/src/main/java/com/broll/networklib/client/tasks/impl/CreateLobbyTask.java index 2bb1540..b10e79b 100644 --- a/src/main/java/com/broll/networklib/client/tasks/impl/CreateLobbyTask.java +++ b/src/main/java/com/broll/networklib/client/tasks/impl/CreateLobbyTask.java @@ -14,14 +14,13 @@ public class CreateLobbyTask extends AbstractClientTask { private String playerName; - private ClientAuthenticationKey authKey; private Object settings; private String version; public CreateLobbyTask(String playerName, Object lobbySettings, ClientAuthenticationKey authKey, String version) { + super(authKey); this.playerName = playerName; - this.authKey = authKey; this.settings = lobbySettings; this.version = version; } diff --git a/src/main/java/com/broll/networklib/client/tasks/impl/JoinLobbyTask.java b/src/main/java/com/broll/networklib/client/tasks/impl/JoinLobbyTask.java index b625e8d..e0d7fd4 100644 --- a/src/main/java/com/broll/networklib/client/tasks/impl/JoinLobbyTask.java +++ b/src/main/java/com/broll/networklib/client/tasks/impl/JoinLobbyTask.java @@ -15,14 +15,12 @@ public class JoinLobbyTask extends AbstractClientTask { private GameLobby lobby; private String playerName; - private ClientAuthenticationKey authKey; - private String version; public JoinLobbyTask(GameLobby lobby, String playerName, ClientAuthenticationKey authKey, String version) { + super(authKey); this.lobby = lobby; this.playerName = playerName; - this.authKey = authKey; this.version = version; } diff --git a/src/main/java/com/broll/networklib/client/tasks/impl/LobbyDiscoveryTask.java b/src/main/java/com/broll/networklib/client/tasks/impl/LobbyDiscoveryTask.java index b9a5a59..aa37a02 100644 --- a/src/main/java/com/broll/networklib/client/tasks/impl/LobbyDiscoveryTask.java +++ b/src/main/java/com/broll/networklib/client/tasks/impl/LobbyDiscoveryTask.java @@ -1,6 +1,7 @@ package com.broll.networklib.client.tasks.impl; import com.broll.networklib.client.GameClient; +import com.broll.networklib.client.auth.ClientAuthenticationKey; import com.broll.networklib.client.impl.LobbyLookupSite; import com.broll.networklib.client.tasks.AbstractClientTask; import com.broll.networklib.client.tasks.DiscoveredLobbies; @@ -11,7 +12,8 @@ public class LobbyDiscoveryTask extends AbstractClientTask> { private GameClient basicClient; - public LobbyDiscoveryTask(GameClient basicClient) { + public LobbyDiscoveryTask(GameClient basicClient, ClientAuthenticationKey key) { + super(key); this.basicClient = basicClient; } @@ -25,7 +27,7 @@ protected void run() { servers.forEach(server -> { LobbyLookupSite site = new LobbyLookupSite(); runOnTempClient(server, site); - site.lookup(); + site.lookup(authKey); DiscoveredLobbies lobbies = waitFor(site.getFuture()); discoveredLobbies.add(lobbies); }); diff --git a/src/main/java/com/broll/networklib/client/tasks/impl/LobbyListTask.java b/src/main/java/com/broll/networklib/client/tasks/impl/LobbyListTask.java index 2be3c6d..9a9a26a 100644 --- a/src/main/java/com/broll/networklib/client/tasks/impl/LobbyListTask.java +++ b/src/main/java/com/broll/networklib/client/tasks/impl/LobbyListTask.java @@ -1,18 +1,33 @@ package com.broll.networklib.client.tasks.impl; +import com.broll.networklib.PackageReceiver; +import com.broll.networklib.client.auth.ClientAuthenticationKey; +import com.broll.networklib.client.impl.GameLobby; +import com.broll.networklib.client.impl.LobbyChange; import com.broll.networklib.client.impl.LobbyLookupSite; import com.broll.networklib.client.tasks.AbstractClientTask; +import com.broll.networklib.client.tasks.AbstractTaskSite; import com.broll.networklib.client.tasks.DiscoveredLobbies; +import com.broll.networklib.network.nt.NT_ServerInformation; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import java.util.Arrays; +import java.util.List; +import java.util.stream.Collectors; public class LobbyListTask extends AbstractClientTask { + private final static Logger Log = LoggerFactory.getLogger(LobbyLookupSite.class); private String ip; - public LobbyListTask() { - this(null); + public LobbyListTask(ClientAuthenticationKey key) { + this(null, key); } - public LobbyListTask(String ip) { + public LobbyListTask(String ip, ClientAuthenticationKey key) { + super(key); this.ip = ip; } @@ -24,7 +39,8 @@ protected void run() { } else { runOnClient(ip, site); } - site.lookup(); + site.lookup(authKey); complete(waitFor(site.getFuture())); } + } diff --git a/src/main/java/com/broll/networklib/client/tasks/impl/ReconnectTask.java b/src/main/java/com/broll/networklib/client/tasks/impl/ReconnectTask.java index e9dde53..4f94863 100644 --- a/src/main/java/com/broll/networklib/client/tasks/impl/ReconnectTask.java +++ b/src/main/java/com/broll/networklib/client/tasks/impl/ReconnectTask.java @@ -13,11 +13,10 @@ public class ReconnectTask extends AbstractClientTask { private String ip; - private ClientAuthenticationKey authenticationKey; public ReconnectTask(String ip, ClientAuthenticationKey authenticationKey) { + super(authenticationKey); this.ip = ip; - this.authenticationKey = authenticationKey; } @Override @@ -32,7 +31,7 @@ private class ReconnectSite extends AbstractTaskSite { public void reconnectCheck() { NT_ReconnectCheck reconnect = new NT_ReconnectCheck(); - reconnect.authenticationKey = authenticationKey.getSecret(); + reconnect.authenticationKey = authKey.getSecret(); client.sendTCP(reconnect); } diff --git a/src/main/java/com/broll/networklib/network/NetworkRegistry.java b/src/main/java/com/broll/networklib/network/NetworkRegistry.java index 2947134..1d0897e 100644 --- a/src/main/java/com/broll/networklib/network/NetworkRegistry.java +++ b/src/main/java/com/broll/networklib/network/NetworkRegistry.java @@ -2,6 +2,7 @@ import com.broll.networklib.NetworkRegister; import com.broll.networklib.network.nt.NT_ChatMessage; +import com.broll.networklib.network.nt.NT_ListLobbies; import com.broll.networklib.network.nt.NT_LobbyClosed; import com.broll.networklib.network.nt.NT_LobbyCreate; import com.broll.networklib.network.nt.NT_LobbyInformation; @@ -27,6 +28,7 @@ import java.io.IOException; import java.util.ArrayList; import java.util.Collections; +import java.util.Comparator; import java.util.HashMap; import java.util.List; @@ -59,6 +61,7 @@ public static void registerStandard(NetworkRegister network) { network.registerNetworkType(NT_LobbyUpdate.class); network.registerNetworkType(NT_LobbyLeave.class); network.registerNetworkType(NT_ReconnectCheck.class); + network.registerNetworkType(NT_ListLobbies.class); network.registerNetworkType(NT_ServerInformation.class); network.registerNetworkType(NT_LobbyInformation[].class); network.registerNetworkType(NT_LobbyPlayerInfo[].class); @@ -70,7 +73,7 @@ public static void register(Kryo kryo, String packagePath) { ClassPath cp = ClassPath.from(NetworkRegistry.class.getClassLoader()); List classes = new ArrayList<>(); cp.getTopLevelClasses(packagePath).forEach(c->classes.add(c.load())); - Collections.sort(classes,(c1,c2)->c1.getName().compareTo(c2.getName())); + Collections.sort(classes, Comparator.comparing(Class::getName)); classes.forEach(loadedClass->{ Log.trace("Register " + loadedClass); kryo.register(loadedClass); diff --git a/src/main/java/com/broll/networklib/network/nt/NT_ListLobbies.java b/src/main/java/com/broll/networklib/network/nt/NT_ListLobbies.java new file mode 100644 index 0000000..8064291 --- /dev/null +++ b/src/main/java/com/broll/networklib/network/nt/NT_ListLobbies.java @@ -0,0 +1,6 @@ +package com.broll.networklib.network.nt; + +public class NT_ListLobbies { + public String authenticationKey; + +} diff --git a/src/main/java/com/broll/networklib/server/impl/ConnectionSite.java b/src/main/java/com/broll/networklib/server/impl/ConnectionSite.java index 56e1c15..e98de52 100644 --- a/src/main/java/com/broll/networklib/server/impl/ConnectionSite.java +++ b/src/main/java/com/broll/networklib/server/impl/ConnectionSite.java @@ -1,6 +1,7 @@ package com.broll.networklib.server.impl; import com.broll.networklib.PackageReceiver; +import com.broll.networklib.network.nt.NT_ListLobbies; import com.broll.networklib.network.nt.NT_LobbyClosed; import com.broll.networklib.network.nt.NT_LobbyCreate; import com.broll.networklib.network.nt.NT_LobbyInformation; @@ -45,7 +46,10 @@ public void setVersion(String version) { @ConnectionRestriction(RestrictionType.NONE) @PackageReceiver - public void receive(NT_ServerInformation info) { + public void listLobbies(NT_ListLobbies list) { + if(tryReconnect(list.authenticationKey)){ + return; + } NT_ServerInformation serverInfo = new NT_ServerInformation(); serverInfo.serverName = serverName; serverInfo.lobbies = lobbyHandler.getLobbies().stream().filter(ServerLobby::isVisible).map(ServerLobby::getLobbyInfo).toArray(NT_LobbyInformation[]::new); @@ -55,6 +59,9 @@ public void receive(NT_ServerInformation info) { @ConnectionRestriction(RestrictionType.NOT_IN_LOBBY) @PackageReceiver public void joinLobby(NT_LobbyJoin join) { + if(tryReconnect(join.authenticationKey)){ + return; + } if(checkJoiningClientVersion(join.version)){ initPlayerAndJoinLobby(join.lobbyId, join.playerName, join.authenticationKey); } @@ -65,6 +72,14 @@ public void joinLobby(NT_LobbyJoin join) { public void reconnectCheck(NT_ReconnectCheck check) { Log.info("check reconnect"); String key = check.authenticationKey; + if(!tryReconnect(key)){ + //is a new player, cant be reconnected + Log.warn("Reconnect check failed: player is new and cannot be reconnected!"); + getConnection().sendTCP(new NT_LobbyNoJoin()); + } + } + + private boolean tryReconnect(String key) { Player player = playerRegister.getPlayer(key); //player exists, has an inactive connection and is party of a lobby if (player != null && !player.getConnection().isActive() && player.getServerLobby() != null) { @@ -76,10 +91,9 @@ public void reconnectCheck(NT_ReconnectCheck check) { reconnectedPlayer(player); getConnection().sendTCP(reconnected); lobby.playerChangedConnectionStatus(player, true); + return true; } else { - //is a new player, cant be reconnected - Log.warn("Reconnect check failed: player is new and cannot be reconnected!"); - getConnection().sendTCP(new NT_LobbyNoJoin()); + return false; } }