Aggregated client connection data/state into ClientData

This commit is contained in:
Collin Smith
2020-06-27 03:04:46 -07:00
parent 3906fbdf4c
commit 413ebc8f6c

View File

@ -58,10 +58,7 @@ public class Main extends ApplicationAdapter implements PacketProcessor {
private final ConnectionLimiter connectionLimiter = new ConnectionLimiter(); private final ConnectionLimiter connectionLimiter = new ConnectionLimiter();
private final ConcurrentHashMap<SocketAddress, Integer> connectionIds = new ConcurrentHashMap<>(32); private final ConcurrentHashMap<SocketAddress, Integer> connectionIds = new ConcurrentHashMap<>(32);
private final byte[] connectionState = new byte[MAX_CLIENTS]; private final ConcurrentHashMap<SocketAddress, ClientData> clientDatas = new ConcurrentHashMap<>(32);
private final ConcurrentHashMap<SocketAddress, Long> clientSalts = new ConcurrentHashMap<>(32);
private final ConcurrentHashMap<SocketAddress, Long> serverSalts = new ConcurrentHashMap<>(32);
private final ConcurrentHashMap<SocketAddress, Long> xor = new ConcurrentHashMap<>(32);
@Override @Override
public void create() { public void create() {
@ -155,39 +152,38 @@ public class Main extends ApplicationAdapter implements PacketProcessor {
boolean generateSalt = true; boolean generateSalt = true;
long clientSalt = connection.salt(); long clientSalt = connection.salt();
Gdx.app.debug(TAG, " " + String.format("client salt=%016x", clientSalt)); Gdx.app.debug(TAG, " " + String.format("client salt=%016x", clientSalt));
if (clientSalts.containsKey(from)) { ClientData client = clientDatas.get(from);
long storedClientSalt = clientSalts.get(from); if (client != null) {
long storedClientSalt = client.clientSalt;
if (storedClientSalt == clientSalt) { if (storedClientSalt == clientSalt) {
Gdx.app.debug(TAG, " " + "client salt matches server record"); Gdx.app.debug(TAG, " " + "client salt matches server record");
generateSalt = false; generateSalt = false;
} else { } else {
Gdx.app.debug(TAG, " " + "client salt mismatch with server record"); Gdx.app.debug(TAG, " " + "client salt mismatch with server record");
Gdx.app.debug(TAG, " " + "updating client salt to server record"); Gdx.app.debug(TAG, " " + "updating client salt to server record");
clientSalts.put(from, clientSalt); client.clientSalt = clientSalt;
clientSalt = storedClientSalt; clientSalt = storedClientSalt;
Gdx.app.debug(TAG, " " + String.format("client salt=%016x", clientSalt)); Gdx.app.debug(TAG, " " + String.format("client salt=%016x", clientSalt));
} }
} else { } else {
Gdx.app.debug(TAG, " " + "no server record found matching client salt"); Gdx.app.debug(TAG, " " + "no server record found matching client salt");
clientSalts.put(from, clientSalt); clientDatas.put(from, client = new ClientData(clientSalt));
} }
long serverSalt; long serverSalt;
if (generateSalt) { if (generateSalt) {
Gdx.app.debug(TAG, " " + "generating server salt"); Gdx.app.debug(TAG, " " + "generating server salt");
serverSalt = MathUtils.random.nextLong(); serverSalt = MathUtils.random.nextLong();
Long previousValue = serverSalts.put(from, serverSalt); if (client.serverSalt != 0L) {
if (previousValue != null) { Gdx.app.debug(TAG, " " + String.format("overwriting existing server salt %016x", client.serverSalt));
Gdx.app.debug(TAG, " " + String.format("overwriting existing server salt %016x", previousValue));
} }
client.serverSalt = serverSalt;
Gdx.app.debug(TAG, " " + String.format("server salt=%016x", serverSalt)); Gdx.app.debug(TAG, " " + String.format("server salt=%016x", serverSalt));
} else { } else {
serverSalt = serverSalts.get(from); serverSalt = client.serverSalt;
} }
long salt = clientSalt ^ serverSalt; long salt = client.xor = clientSalt ^ serverSalt;
xor.put(from, salt);
Gdx.app.debug(TAG, " " + String.format("salt=%016x", salt)); Gdx.app.debug(TAG, " " + String.format("salt=%016x", salt));
} }
@ -216,4 +212,15 @@ public class Main extends ApplicationAdapter implements PacketProcessor {
Gdx.app.debug(TAG, String.format("connection closed. %d / %d", count, MAX_CLIENTS)); Gdx.app.debug(TAG, String.format("connection closed. %d / %d", count, MAX_CLIENTS));
} }
} }
private static class ClientData {
long clientSalt;
long serverSalt;
long xor;
byte state;
ClientData(long clientSalt) {
this.clientSalt = clientSalt;
}
}
} }