From 903bc31d1a2d19855451e001d2e929324b710b51 Mon Sep 17 00:00:00 2001 From: Sten Laane <21343173+StenAL@users.noreply.github.com> Date: Thu, 19 Sep 2024 18:40:45 +0300 Subject: [PATCH] Client: Rename connection-related classes Having two classes named `Conn` and `Connection` was a bit confusing. Now it's more clear that one is the app-agnostic socket-based connection while the other handles all networking for the golf game. Also, rename thriftLogs to metadataLogs as that's what they're used for. --- client/src/main/java/agolf/GameApplet.java | 4 +- client/src/main/java/agolf/GameContainer.java | 2 +- .../agolf/{Conn.java => GolfConnection.java} | 26 +++++----- .../main/java/com/aapeli/applet/AApplet.java | 22 ++++---- .../aapeli/connection/GamePacketQueue.java | 14 ++--- ...{Connection.java => SocketConnection.java} | 51 ++++++++++--------- ...ner.java => SocketConnectionListener.java} | 4 +- 7 files changed, 62 insertions(+), 61 deletions(-) rename client/src/main/java/agolf/{Conn.java => GolfConnection.java} (91%) rename client/src/main/java/com/aapeli/connection/{Connection.java => SocketConnection.java} (89%) rename client/src/main/java/com/aapeli/connection/{ConnListener.java => SocketConnectionListener.java} (63%) diff --git a/client/src/main/java/agolf/GameApplet.java b/client/src/main/java/agolf/GameApplet.java index ec23eea1..82c9f1a2 100644 --- a/client/src/main/java/agolf/GameApplet.java +++ b/client/src/main/java/agolf/GameApplet.java @@ -91,8 +91,8 @@ public void createImages() { } public void connectToServer() { - this.gameContainer.connection = new Conn(this.gameContainer); - if (!this.gameContainer.connection.method1158()) { + this.gameContainer.connection = new GolfConnection(this.gameContainer); + if (!this.gameContainer.connection.openSocketConnection()) { this.setEndState(END_ERROR_CONNECTION); } diff --git a/client/src/main/java/agolf/GameContainer.java b/client/src/main/java/agolf/GameContainer.java index 7bac91ab..874b0c06 100644 --- a/client/src/main/java/agolf/GameContainer.java +++ b/client/src/main/java/agolf/GameContainer.java @@ -22,7 +22,7 @@ public class GameContainer {// some kind of a container for everything public BadWordFilter badWordFilter; public AutoPopups autoPopup; public TrackCollection trackCollection; - public Conn connection; + public GolfConnection connection; public LobbySelectPanel lobbySelectionPanel; public LobbyPanel lobbyPanel; public String defaultLobby; diff --git a/client/src/main/java/agolf/Conn.java b/client/src/main/java/agolf/GolfConnection.java similarity index 91% rename from client/src/main/java/agolf/Conn.java rename to client/src/main/java/agolf/GolfConnection.java index 1bb3aff5..c4d9abfb 100644 --- a/client/src/main/java/agolf/Conn.java +++ b/client/src/main/java/agolf/GolfConnection.java @@ -1,11 +1,11 @@ package agolf; import com.aapeli.applet.AApplet; -import com.aapeli.connection.ConnListener; -import com.aapeli.connection.Connection; +import com.aapeli.connection.SocketConnectionListener; +import com.aapeli.connection.SocketConnection; import com.aapeli.tools.Tools; -public class Conn implements ConnListener { +public class GolfConnection implements SocketConnectionListener { private static final String[] cipherCmds = new String[] { "status\t", @@ -78,12 +78,12 @@ public class Conn implements ConnListener { "error" }; private GameContainer gameContainer; - private Connection connection; + private SocketConnection socketConnection; private String lastPacketSent; private String lastPacketReceived; - protected Conn(GameContainer var1) { + protected GolfConnection(GameContainer var1) { this.gameContainer = var1; this.lastPacketSent = this.lastPacketReceived = null; } @@ -100,7 +100,7 @@ public void dataReceived(String packet) { } this.gameContainer.gameApplet.setEndState(e); - this.connection.closeConnection(); + this.socketConnection.closeConnection(); } } @@ -121,9 +121,9 @@ public void notifyConnectionDown() { public void notifyConnectionUp() { } - protected boolean method1158() { - this.connection = new Connection(this.gameContainer.gameApplet, this, cipherCmds); - return this.connection.openConnection(); + protected boolean openSocketConnection() { + this.socketConnection = new SocketConnection(this.gameContainer.gameApplet, this, cipherCmds); + return this.socketConnection.openConnection(); } protected void sendVersion() { @@ -133,12 +133,12 @@ protected void sendVersion() { public void writeData(String var1) { this.lastPacketSent = var1; - this.connection.writeData(var1); + this.socketConnection.writeData(var1); } protected void disconnect() { - if (this.connection != null) { - this.connection.closeConnection(); + if (this.socketConnection != null) { + this.socketConnection.closeConnection(); } } @@ -151,7 +151,7 @@ private void handlePacket(String cmd) { this.gameContainer.gameApplet.setEndState(AApplet.END_ERROR_SERVERFULL); } - this.connection.closeConnection(); + this.socketConnection.closeConnection(); } else if (args[0].equals("versok")) { this.writeData("language\t" + this.gameContainer.params.getChatLang()); String var4 = this.gameContainer.params.getSessionLocale(); diff --git a/client/src/main/java/com/aapeli/applet/AApplet.java b/client/src/main/java/com/aapeli/applet/AApplet.java index 3310a3fd..f115c95a 100644 --- a/client/src/main/java/com/aapeli/applet/AApplet.java +++ b/client/src/main/java/com/aapeli/applet/AApplet.java @@ -5,7 +5,7 @@ import com.aapeli.client.SoundManager; import com.aapeli.client.StringDraw; import com.aapeli.client.TextManager; -import com.aapeli.connection.Connection; +import com.aapeli.connection.SocketConnection; import com.aapeli.tools.QuickTimer; import com.aapeli.tools.QuickTimerListener; import com.aapeli.tools.Tools; @@ -67,7 +67,7 @@ public abstract class AApplet extends Applet implements Runnable, ActionListener private Image splashImage; private long splashTimestamp; private QuickTimer popupTimer; - private Connection connection; + private SocketConnection socketConnection; private Image appletImage; private Graphics appletGraphics; private boolean verbose; @@ -484,7 +484,7 @@ public void run() { } this.sendLoadTimes(readyTime, finishedTime, time1, time2, time3, time4, time5, time6); - this.writeThriftDebug("clientconnect", "loadtime:i:" + readyTime + "^loadertime:i:" + finishedTime); + this.writeMetadataLog1("clientconnect", "loadtime:i:" + readyTime + "^loadertime:i:" + finishedTime); this.loadingPanel.displayButtons(); if (this.endState == 0 && !this.destroyed) { this.remove(this.loadingPanel); @@ -692,20 +692,20 @@ public void allowExternalPopups() { this.callJavaScriptJSON("{\"block\":\"false\"}"); } - public void setConnectionReference(Connection var1) { - this.connection = var1; + public void setConnectionReference(SocketConnection var1) { + this.socketConnection = var1; } - public void writeThriftLog(String var1, String var2) { - if (this.connection != null) { - this.connection.writeThriftLog(0, var1, var2); + public void writeMetadataLog0(String dataType, String data) { + if (this.socketConnection != null) { + this.socketConnection.writeMetadataLog(0, dataType, data); } } - public void writeThriftDebug(String var1, String var2) { - if (this.connection != null) { - this.connection.writeThriftLog(1, var1, var2); + public void writeMetadataLog1(String dataType, String data) { + if (this.socketConnection != null) { + this.socketConnection.writeMetadataLog(1, dataType, data); } } diff --git a/client/src/main/java/com/aapeli/connection/GamePacketQueue.java b/client/src/main/java/com/aapeli/connection/GamePacketQueue.java index daa3efd4..3d2fb723 100644 --- a/client/src/main/java/com/aapeli/connection/GamePacketQueue.java +++ b/client/src/main/java/com/aapeli/connection/GamePacketQueue.java @@ -6,16 +6,16 @@ class GamePacketQueue implements Runnable { - private Connection conn; - private ConnListener connListener; + private SocketConnection socketConnection; + private SocketConnectionListener socketConnectionListener; private List packets; private boolean running; private Thread thread; - protected GamePacketQueue(Connection conn, ConnListener connListener) { - this.conn = conn; - this.connListener = connListener; + protected GamePacketQueue(SocketConnection socketConnection, SocketConnectionListener socketConnectionListener) { + this.socketConnection = socketConnection; + this.socketConnectionListener = socketConnectionListener; this.packets = new ArrayList<>(); this.running = true; this.thread = new Thread(this); @@ -29,7 +29,7 @@ public void run() { String packet; while ((packet = this.nextGamePacket()) != null) { - this.connListener.dataReceived(packet); + this.socketConnectionListener.dataReceived(packet); } if (this.running) { @@ -37,7 +37,7 @@ public void run() { } } catch (Exception e) { this.running = false; - this.conn.handleCrash(); + this.socketConnection.handleCrash(); } return; diff --git a/client/src/main/java/com/aapeli/connection/Connection.java b/client/src/main/java/com/aapeli/connection/SocketConnection.java similarity index 89% rename from client/src/main/java/com/aapeli/connection/Connection.java rename to client/src/main/java/com/aapeli/connection/SocketConnection.java index 3d6fa8a2..8e1a7b3a 100644 --- a/client/src/main/java/com/aapeli/connection/Connection.java +++ b/client/src/main/java/com/aapeli/connection/SocketConnection.java @@ -16,7 +16,7 @@ import java.util.ArrayList; import java.util.List; -public final class Connection implements Runnable { +public final class SocketConnection implements Runnable { /* DCR Constants */ public static final int DISCONNECT_REASON_UNDEFINED = 0; @@ -38,7 +38,7 @@ public final class Connection implements Runnable { private AApplet gameApplet; private Parameters params; - private ConnListener connListener; + private SocketConnectionListener socketConnectionListener; private GameCipher gameCipher; private int state; private int disconnectReason; @@ -51,25 +51,25 @@ public final class Connection implements Runnable { private int retryTimeoutS; private GameQueue gameQueue; private GamePacketQueue gamePacketQueue; - private List thriftLogs; + private List metadataLogs; private long numReceivedGamePackets; private long connActivityTime; private ConnCipher connCipher; private Thread thread; - public Connection(AApplet gameApplet, ConnListener connListener, String[] gameCipherCmds) { - this(gameApplet, gameApplet.param, connListener, gameCipherCmds); + public SocketConnection(AApplet gameApplet, SocketConnectionListener socketConnectionListener, String[] gameCipherCmds) { + this(gameApplet, gameApplet.param, socketConnectionListener, gameCipherCmds); } - public Connection(Parameters params, ConnListener connListener, String[] gameCipherCmds) { - this(null, params, connListener, gameCipherCmds); + public SocketConnection(Parameters params, SocketConnectionListener socketConnectionListener, String[] gameCipherCmds) { + this(null, params, socketConnectionListener, gameCipherCmds); } - private Connection(AApplet gameApplet, Parameters params, ConnListener connListener, String[] gameCipherCmds) { + private SocketConnection(AApplet gameApplet, Parameters params, SocketConnectionListener socketConnectionListener, String[] gameCipherCmds) { this.gameApplet = gameApplet; this.params = params; - this.connListener = connListener; + this.socketConnectionListener = socketConnectionListener; if (gameApplet != null) { gameApplet.setConnectionReference(this); } @@ -85,7 +85,7 @@ private Connection(AApplet gameApplet, Parameters params, ConnListener connListe this.clientId = -1L; this.retryTimeoutS = 25; this.gameQueue = new GameQueue(); - this.thriftLogs = new ArrayList<>(); + this.metadataLogs = new ArrayList<>(); this.numReceivedGamePackets = -1L; this.state = STATE_OPENING; this.disconnectReason = DISCONNECT_REASON_UNDEFINED; @@ -94,7 +94,7 @@ private Connection(AApplet gameApplet, Parameters params, ConnListener connListe } public void run() { - this.gamePacketQueue = new GamePacketQueue(this, this.connListener); + this.gamePacketQueue = new GamePacketQueue(this, this.socketConnectionListener); try { do { @@ -128,7 +128,7 @@ public void run() { this.close(); this.gamePacketQueue.stop(); - this.connListener.connectionLost(this.disconnectReason); + this.socketConnectionListener.connectionLost(this.disconnectReason); } public boolean openConnection() { @@ -159,14 +159,14 @@ public void writeData(String data) { } } - public void writeThriftLog(int var1, String var2, String var3) { // TODO: replace var1...var4 - String var4 = "tlog\t" + var1 + "\t" + var2; - if (var3 != null) { - var4 = var4 + "\t" + var3; + public void writeMetadataLog(int i, String dataType, String data) { + String log = "tlog\t" + i + "\t" + dataType; + if (data != null) { + log = log + "\t" + data; } - synchronized (this.thriftLogs) { - this.thriftLogs.add(var4); + synchronized (this.metadataLogs) { + this.metadataLogs.add(log); } } @@ -232,7 +232,7 @@ private void handleConnection() { private void handleGameQueue() { this.processGameQueueDisconnect(); if (this.state == STATE_CONNECTED) { - this.processThriftLogs(); + this.processMetadataLogs(); } } @@ -251,11 +251,12 @@ private void processGameQueueDisconnect() { } - private void processThriftLogs() { - synchronized (this.thriftLogs) { + private void processMetadataLogs() { + synchronized (this.metadataLogs) { while (true) { - if (this.state == STATE_CONNECTED && !this.thriftLogs.isEmpty()) { - String str = this.thriftLogs.removeFirst(); + if (this.state == STATE_CONNECTED && !this.metadataLogs.isEmpty()) { + String str = this.metadataLogs.getFirst(); + this.metadataLogs.removeFirst(); if (this.writeLineS(str)) { continue; } @@ -285,7 +286,7 @@ private void disconnect() { if (this.state == STATE_CONNECTED && this.retryTimeoutS > 0) { this.close(); this.state = STATE_DOWN; - this.connListener.notifyConnectionDown(); + this.socketConnectionListener.notifyConnectionDown(); } else { this.state = STATE_DISCONNECTED; this.disconnectReason = DISCONNECT_REASON_NORETRY; @@ -350,7 +351,7 @@ private void readInput() { this.state = STATE_CONNECTED; } else if (cmd.equals("rcok")) { // reconnect ok this.state = STATE_CONNECTED; - this.connListener.notifyConnectionUp(); + this.socketConnectionListener.notifyConnectionUp(); } else if (cmd.equals("rcf")) { // reconnect ok this.state = STATE_DISCONNECTED; this.disconnectReason = DISCONNECT_REASON_RETRYFAIL; diff --git a/client/src/main/java/com/aapeli/connection/ConnListener.java b/client/src/main/java/com/aapeli/connection/SocketConnectionListener.java similarity index 63% rename from client/src/main/java/com/aapeli/connection/ConnListener.java rename to client/src/main/java/com/aapeli/connection/SocketConnectionListener.java index 3ec82bd5..73134f7f 100644 --- a/client/src/main/java/com/aapeli/connection/ConnListener.java +++ b/client/src/main/java/com/aapeli/connection/SocketConnectionListener.java @@ -1,9 +1,9 @@ package com.aapeli.connection; -public interface ConnListener { +public interface SocketConnectionListener { - void dataReceived(String var1); + void dataReceived(String data); void connectionLost(int var1);