Skip to content

Commit

Permalink
Client: Rename connection-related classes
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
StenAL committed Nov 3, 2024
1 parent 4930d27 commit 903bc31
Show file tree
Hide file tree
Showing 7 changed files with 62 additions and 61 deletions.
4 changes: 2 additions & 2 deletions client/src/main/java/agolf/GameApplet.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand Down
2 changes: 1 addition & 1 deletion client/src/main/java/agolf/GameContainer.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
@@ -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",
Expand Down Expand Up @@ -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;
}
Expand All @@ -100,7 +100,7 @@ public void dataReceived(String packet) {
}

this.gameContainer.gameApplet.setEndState(e);
this.connection.closeConnection();
this.socketConnection.closeConnection();
}
}

Expand All @@ -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() {
Expand All @@ -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();
}
}

Expand All @@ -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();
Expand Down
22 changes: 11 additions & 11 deletions client/src/main/java/com/aapeli/applet/AApplet.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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);
}

}
Expand Down
14 changes: 7 additions & 7 deletions client/src/main/java/com/aapeli/connection/GamePacketQueue.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,16 @@

class GamePacketQueue implements Runnable {

private Connection conn;
private ConnListener connListener;
private SocketConnection socketConnection;
private SocketConnectionListener socketConnectionListener;
private List<String> 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);
Expand All @@ -29,15 +29,15 @@ public void run() {

String packet;
while ((packet = this.nextGamePacket()) != null) {
this.connListener.dataReceived(packet);
this.socketConnectionListener.dataReceived(packet);
}

if (this.running) {
continue;
}
} catch (Exception e) {
this.running = false;
this.conn.handleCrash();
this.socketConnection.handleCrash();
}

return;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand All @@ -51,25 +51,25 @@ public final class Connection implements Runnable {
private int retryTimeoutS;
private GameQueue gameQueue;
private GamePacketQueue gamePacketQueue;
private List<String> thriftLogs;
private List<String> 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);
}
Expand All @@ -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;
Expand All @@ -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 {
Expand Down Expand Up @@ -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() {
Expand Down Expand Up @@ -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);
}
}

Expand Down Expand Up @@ -232,7 +232,7 @@ private void handleConnection() {
private void handleGameQueue() {
this.processGameQueueDisconnect();
if (this.state == STATE_CONNECTED) {
this.processThriftLogs();
this.processMetadataLogs();
}

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

Expand Down

0 comments on commit 903bc31

Please sign in to comment.