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 df11e06 commit 50a15af
Show file tree
Hide file tree
Showing 12 changed files with 120 additions and 53 deletions.
18 changes: 10 additions & 8 deletions src/main/java/com/broll/networklib/client/LobbyGameClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
import com.broll.networklib.client.impl.LobbyConnectionSite;
import com.broll.networklib.client.tasks.AbstractClientTask;
import com.broll.networklib.client.tasks.DiscoveredLobbies;
import com.broll.networklib.client.tasks.LobbyDiscoveryResult;
import com.broll.networklib.client.tasks.LobbyListResult;
import com.broll.networklib.client.tasks.impl.CreateLobbyTask;
import com.broll.networklib.client.tasks.impl.JoinLobbyTask;
import com.broll.networklib.client.tasks.impl.LobbyDiscoveryTask;
Expand Down Expand Up @@ -125,24 +127,24 @@ public CompletableFuture<GameLobby> reconnectCheck(String ip) {
return updateLobby(runTask(new ReconnectTask(ip, clientAuthenticationKey)));
}

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

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

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

public CompletableFuture<GameLobby> joinLobby(GameLobby lobby, String playerName) {
return updateLobby(runTask(new JoinLobbyTask(lobby, playerName, clientAuthenticationKey, version)));
return updateLobby(runTask(new JoinLobbyTask(lobby, playerName, clientAuthenticationKey)));
}

public CompletableFuture<GameLobby> createLobby(String playerName, Object lobbySettings) {
return updateLobby(runTask(new CreateLobbyTask(playerName, lobbySettings, clientAuthenticationKey, version)));
return updateLobby(runTask(new CreateLobbyTask(playerName, lobbySettings, clientAuthenticationKey)));
}

public void clearClientAuthenticationKey() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,25 +4,28 @@
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.client.tasks.LobbyListResult;
import com.broll.networklib.network.nt.NT_ListLobbies;
import com.broll.networklib.network.nt.NT_LobbyNoJoin;
import com.broll.networklib.network.nt.NT_LobbyReconnected;
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 LobbyLookupSite extends AbstractTaskSite<DiscoveredLobbies> {
public class LobbyLookupSite extends AbstractTaskSite<LobbyListResult> {

private final static Logger Log = LoggerFactory.getLogger(LobbyLookupSite.class);



public void lookup(ClientAuthenticationKey key) {
public void lookup(ClientAuthenticationKey key, String version) {
Log.info("SEND LOOKUP");
NT_ListLobbies nt = new NT_ListLobbies();
nt.authenticationKey = key.getSecret();
nt.version = version;
client.sendTCP(nt);
}

Expand All @@ -35,7 +38,17 @@ public void receive(NT_ServerInformation info) {
LobbyChange.updateLobbyInfo(lobby, lobbyInfo);
return lobby;
}).collect(Collectors.toList());
complete(new DiscoveredLobbies(info.serverName, ip, lobbies));
complete(new LobbyListResult(new DiscoveredLobbies(info.serverName, ip, lobbies)));
}

@PackageReceiver
public void reconnected(NT_LobbyReconnected reconnected) {
complete(new LobbyListResult(LobbyChange.reconnectedLobby(getClient(), reconnected)));
}

@PackageReceiver
public void wrongVersion(NT_LobbyNoJoin nt) {
fail("Could not list lobbies: " + nt.reason);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package com.broll.networklib.client.tasks;

import com.broll.networklib.client.impl.GameLobby;

import java.util.List;

public class LobbyDiscoveryResult {

private List<DiscoveredLobbies> lobbies;
private GameLobby reconnected;

public LobbyDiscoveryResult(List<DiscoveredLobbies> lobbies){
this.lobbies = lobbies;
}

public LobbyDiscoveryResult(GameLobby reconnected){
this.reconnected = reconnected;
}

public List<DiscoveredLobbies> getLobbies() {
return lobbies;
}

public GameLobby getReconnectedLobby() {
return reconnected;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package com.broll.networklib.client.tasks;

import com.broll.networklib.client.impl.GameLobby;

public class LobbyListResult {

private DiscoveredLobbies lobbies;
private GameLobby reconnected;


public LobbyListResult(DiscoveredLobbies lobbies){
this.lobbies = lobbies;
}

public LobbyListResult(GameLobby reconnected){
this.reconnected = reconnected;
}

public DiscoveredLobbies getLobbies() {
return lobbies;
}

public GameLobby getReconnectedLobby() {
return reconnected;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,11 @@ public class CreateLobbyTask extends AbstractClientTask<GameLobby> {
private String playerName;
private Object settings;

private String version;

public CreateLobbyTask(String playerName, Object lobbySettings, ClientAuthenticationKey authKey, String version) {
public CreateLobbyTask(String playerName, Object lobbySettings, ClientAuthenticationKey authKey) {
super(authKey);
this.playerName = playerName;
this.settings = lobbySettings;
this.version = version;
}

@Override
Expand All @@ -41,7 +39,6 @@ public void create() {
create.lobbyName = playerName + "'s Lobby";
create.authenticationKey = authKey.getSecret();
create.settings = settings;
create.version = version;
client.sendTCP(create);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,11 @@ public class JoinLobbyTask extends AbstractClientTask<GameLobby> {

private GameLobby lobby;
private String playerName;
private String version;

public JoinLobbyTask(GameLobby lobby, String playerName, ClientAuthenticationKey authKey, String version) {
public JoinLobbyTask(GameLobby lobby, String playerName, ClientAuthenticationKey authKey) {
super(authKey);
this.lobby = lobby;
this.playerName = playerName;
this.version = version;
}

@Override
Expand All @@ -39,7 +37,6 @@ public void join() {
join.lobbyId = lobby.getLobbyId();
join.authenticationKey = authKey.getSecret();
join.playerName = playerName;
join.version = version;
client.sendTCP(join);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,19 @@
import com.broll.networklib.client.impl.LobbyLookupSite;
import com.broll.networklib.client.tasks.AbstractClientTask;
import com.broll.networklib.client.tasks.DiscoveredLobbies;
import com.broll.networklib.client.tasks.LobbyDiscoveryResult;
import com.broll.networklib.client.tasks.LobbyListResult;

import java.util.ArrayList;
import java.util.List;

public class LobbyDiscoveryTask extends AbstractClientTask<List<DiscoveredLobbies>> {
public class LobbyDiscoveryTask extends AbstractClientTask<LobbyDiscoveryResult> {
private GameClient basicClient;
private String version;

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

Expand All @@ -27,10 +31,13 @@ protected void run() {
servers.forEach(server -> {
LobbyLookupSite site = new LobbyLookupSite();
runOnTempClient(server, site);
site.lookup(authKey);
DiscoveredLobbies lobbies = waitFor(site.getFuture());
discoveredLobbies.add(lobbies);
site.lookup(authKey, version);
LobbyListResult result = waitFor(site.getFuture());
if(result.getReconnectedLobby()!=null){
complete(new LobbyDiscoveryResult(result.getReconnectedLobby()));
}
discoveredLobbies.add(result.getLobbies());
});
complete(discoveredLobbies);
complete(new LobbyDiscoveryResult(discoveredLobbies));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
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.client.tasks.LobbyListResult;
import com.broll.networklib.network.nt.NT_ServerInformation;

import org.slf4j.Logger;
Expand All @@ -17,18 +18,22 @@
import java.util.List;
import java.util.stream.Collectors;

public class LobbyListTask extends AbstractClientTask<DiscoveredLobbies> {
public class LobbyListTask extends AbstractClientTask<LobbyListResult> {
private final static Logger Log = LoggerFactory.getLogger(LobbyLookupSite.class);

private String ip;

public LobbyListTask(ClientAuthenticationKey key) {
this(null, key);

private String version;

public LobbyListTask(ClientAuthenticationKey key, String version) {
this(null, key, version);
}

public LobbyListTask(String ip, ClientAuthenticationKey key) {
public LobbyListTask(String ip, ClientAuthenticationKey key, String version) {
super(key);
this.ip = ip;
this.version = version;
}

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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,5 @@
public class NT_ListLobbies {
public String authenticationKey;

public String version;
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,4 @@ public class NT_LobbyCreate {
public Object settings;
public String lobbyName;

public String version;

}
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,4 @@ public class NT_LobbyJoin {
public int lobbyId;
public String authenticationKey;

public String version;
}
31 changes: 13 additions & 18 deletions src/main/java/com/broll/networklib/server/impl/ConnectionSite.java
Original file line number Diff line number Diff line change
Expand Up @@ -44,10 +44,13 @@ public void setVersion(String version) {
this.version = version;
}

@ConnectionRestriction(RestrictionType.NONE)
@ConnectionRestriction(RestrictionType.NOT_IN_LOBBY)
@PackageReceiver
public void listLobbies(NT_ListLobbies list) {
if(tryReconnect(list.authenticationKey)){
if (!checkJoiningClientVersion(list.version)) {
return;
}
if (tryReconnect(list.authenticationKey)) {
return;
}
NT_ServerInformation serverInfo = new NT_ServerInformation();
Expand All @@ -59,20 +62,15 @@ public void listLobbies(NT_ListLobbies list) {
@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);
}
initPlayerAndJoinLobby(join.lobbyId, join.playerName, join.authenticationKey);
}

@ConnectionRestriction(RestrictionType.NOT_IN_LOBBY)
@PackageReceiver
public void reconnectCheck(NT_ReconnectCheck check) {
Log.info("check reconnect");
String key = check.authenticationKey;
if(!tryReconnect(key)){
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());
Expand Down Expand Up @@ -102,7 +100,7 @@ private boolean tryReconnect(String key) {
public void switchLobby(NT_LobbyJoin join) {
ServerLobby from = getLobby();
if (from.isLocked() && !getPlayer().isAllowedToLeaveLockedLobby()) {
Log.warn("Player "+getPlayer()+" is not allowed to switch lobby!");
Log.warn("Player " + getPlayer() + " is not allowed to switch lobby!");
getConnection().sendTCP(new NT_LobbyNoJoin());
return;
}
Expand All @@ -128,15 +126,12 @@ public void switchLobby(NT_LobbyJoin join) {
@ConnectionRestriction(RestrictionType.NOT_IN_LOBBY)
@PackageReceiver
public void createLobby(NT_LobbyCreate create) {
if(!checkJoiningClientVersion(create.version)){
return;
}
boolean reconnected = initPlayerConnection(create.playerName, create.authenticationKey);
ServerLobby lobby = lobbyHandler.getLobbyCreationRequestHandler().createNewLobby(getPlayer(), create.lobbyName, create.settings);
if (lobby != null) {
joinLobby(lobby, reconnected);
} else {
Log.warn("Player "+getPlayer()+" was not allowed to create lobby!");
Log.warn("Player " + getPlayer() + " was not allowed to create lobby!");
//was not allowed to create lobby
getConnection().sendTCP(new NT_LobbyNoJoin());
}
Expand All @@ -147,7 +142,7 @@ public void onDisconnect(NetworkConnection connection) {
Player player = connection.getPlayer();
if (player != null) {
player.updateOnlineStatus(false);
player.getListeners().forEach(it->((IPlayerListener)it).disconnected(player));
player.getListeners().forEach(it -> ((IPlayerListener) it).disconnected(player));
if (player.inLobby()) {
ServerLobby lobby = player.getServerLobby();
if (lobby.isLocked() && !player.isAllowedToLeaveLockedLobby()) {
Expand All @@ -166,9 +161,9 @@ public void onDisconnect(NetworkConnection connection) {
}
}

private boolean checkJoiningClientVersion(String clientVersion){
private boolean checkJoiningClientVersion(String clientVersion) {
if (!Objects.equals(this.version, clientVersion)) {
Log.warn("Player "+getPlayer()+" version does not match server!");
Log.warn("Player " + getPlayer() + " version does not match server!");
NT_LobbyNoJoin nt = new NT_LobbyNoJoin();
nt.reason = "Version mismatch with server: " + version;
getConnection().sendTCP(nt);
Expand Down Expand Up @@ -205,7 +200,7 @@ private void reconnectedPlayer(Player player) {
getConnection().setPlayer(player);
if (!player.isOnline()) {
player.updateOnlineStatus(true);
player.getListeners().forEach(it -> ((IPlayerListener)it).reconnected(player));
player.getListeners().forEach(it -> ((IPlayerListener) it).reconnected(player));
}
}

Expand Down

0 comments on commit 50a15af

Please sign in to comment.