Skip to content

Commit

Permalink
improve reconnecting
Browse files Browse the repository at this point in the history
  • Loading branch information
Rolleander committed May 4, 2024
1 parent f5a7c14 commit df11e06
Show file tree
Hide file tree
Showing 11 changed files with 74 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -126,15 +126,15 @@ public CompletableFuture<GameLobby> reconnectCheck(String ip) {
}

public CompletableFuture<List<DiscoveredLobbies>> discoverLobbies() {
return runTask(new LobbyDiscoveryTask(client));
return runTask(new LobbyDiscoveryTask(client, clientAuthenticationKey));
}

public CompletableFuture<DiscoveredLobbies> listLobbies(String ip) {
return runTask(new LobbyListTask(ip));
return runTask(new LobbyListTask(ip, clientAuthenticationKey));
}

public CompletableFuture<DiscoveredLobbies> listLobbies() {
return runTask(new LobbyListTask());
return runTask(new LobbyListTask(clientAuthenticationKey));
}

public CompletableFuture<GameLobby> joinLobby(GameLobby lobby, String playerName) {
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -15,9 +17,13 @@ public class LobbyLookupSite extends AbstractTaskSite<DiscoveredLobbies> {

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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -20,12 +21,18 @@

public abstract class AbstractClientTask<T> {

protected ClientAuthenticationKey authKey;
private CompletableFuture<T> future;
private LobbyGameClient client;
private List<Runnable> onComplete = new ArrayList<>();
private IRegisterNetwork networkRegister;

private final static int TIMEOUT = 5;

public AbstractClientTask(ClientAuthenticationKey authKey){
this.authKey = authKey;
}

public CompletableFuture<T> run(LobbyGameClient client, IRegisterNetwork networkRegister) {
this.networkRegister = networkRegister;
this.client = client;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,13 @@
public class CreateLobbyTask extends AbstractClientTask<GameLobby> {

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;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,12 @@ public class JoinLobbyTask extends AbstractClientTask<GameLobby> {

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;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -11,7 +12,8 @@
public class LobbyDiscoveryTask extends AbstractClientTask<List<DiscoveredLobbies>> {
private GameClient basicClient;

public LobbyDiscoveryTask(GameClient basicClient) {
public LobbyDiscoveryTask(GameClient basicClient, ClientAuthenticationKey key) {
super(key);
this.basicClient = basicClient;
}

Expand All @@ -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);
});
Expand Down
Original file line number Diff line number Diff line change
@@ -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<DiscoveredLobbies> {
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;
}

Expand All @@ -24,7 +39,8 @@ protected void run() {
} else {
runOnClient(ip, site);
}
site.lookup();
site.lookup(authKey);
complete(waitFor(site.getFuture()));
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,10 @@
public class ReconnectTask extends AbstractClientTask<GameLobby> {

private String ip;
private ClientAuthenticationKey authenticationKey;

public ReconnectTask(String ip, ClientAuthenticationKey authenticationKey) {
super(authenticationKey);
this.ip = ip;
this.authenticationKey = authenticationKey;
}

@Override
Expand All @@ -32,7 +31,7 @@ private class ReconnectSite extends AbstractTaskSite<GameLobby> {

public void reconnectCheck() {
NT_ReconnectCheck reconnect = new NT_ReconnectCheck();
reconnect.authenticationKey = authenticationKey.getSecret();
reconnect.authenticationKey = authKey.getSecret();
client.sendTCP(reconnect);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;

Expand Down Expand Up @@ -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);
Expand All @@ -70,7 +73,7 @@ public static void register(Kryo kryo, String packagePath) {
ClassPath cp = ClassPath.from(NetworkRegistry.class.getClassLoader());
List<Class> 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);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package com.broll.networklib.network.nt;

public class NT_ListLobbies {
public String authenticationKey;

}
22 changes: 18 additions & 4 deletions src/main/java/com/broll/networklib/server/impl/ConnectionSite.java
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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);
Expand All @@ -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);
}
Expand All @@ -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) {
Expand All @@ -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;
}
}

Expand Down

0 comments on commit df11e06

Please sign in to comment.