From 7454481411b1d934367018cbc70f334714182282 Mon Sep 17 00:00:00 2001 From: Josh Larson Date: Sun, 10 Jan 2016 21:53:18 -0600 Subject: [PATCH] Fixed null pointers in ClientSender and increased reliability of Forwarder --- README.md | 2 +- src/com/projectswg/ClientSender.java | 15 ++++--- src/com/projectswg/Forwarder.java | 58 ++++++++++++++++------------ 3 files changed, 44 insertions(+), 31 deletions(-) diff --git a/README.md b/README.md index 979b762..922da3f 100644 --- a/README.md +++ b/README.md @@ -21,7 +21,7 @@ The way it works is by creating a very basic UDP server on each client, and havi ### Single Instance ### 1. Start up the forwarder -2. Open up login.cfg and set "loginServerAddress" to "127.0.0.1" and "loginServerPort" to whatever is listed next to the server connection status (e.x. 44453) +2. Open up login.cfg and set "loginServerAddress" to "127.0.0.1" and "loginServerPort" to whatever is listed next to the client connection status (e.x. 44453) 3. Start up the SWG client ### Multiple Instances ### diff --git a/src/com/projectswg/ClientSender.java b/src/com/projectswg/ClientSender.java index 6aaee44..6151182 100644 --- a/src/com/projectswg/ClientSender.java +++ b/src/com/projectswg/ClientSender.java @@ -73,16 +73,19 @@ public class ClientSender { public void stop() { disconnect(DisconnectReason.APPLICATION); - executor.shutdownNow(); + if (executor != null) + executor.shutdownNow(); safeCloseServers(); } public void setLoginCallback(UDPCallback callback) { - loginServer.setCallback(callback); + if (loginServer != null) + loginServer.setCallback(callback); } public void setZoneCallback(UDPCallback callback) { - zoneServer.setCallback(callback); + if (zoneServer != null) + zoneServer.setCallback(callback); } public void setSenderCallback(ClientSenderCallback callback) { @@ -175,10 +178,12 @@ public class ClientSender { } public void sendRaw(int port, byte [] data) { - if (zone) + if (zone && zoneServer != null) zoneServer.send(port, ADDR, data); - else + else if (!zone && loginServer != null) loginServer.send(port, ADDR, data); + else + return; if (callback != null) callback.onUdpSent(zone, data); } diff --git a/src/com/projectswg/Forwarder.java b/src/com/projectswg/Forwarder.java index cffcc16..822fb78 100644 --- a/src/com/projectswg/Forwarder.java +++ b/src/com/projectswg/Forwarder.java @@ -34,7 +34,7 @@ public class Forwarder extends Application implements ConnectionCallback { private final Button serverSetButton; private final Text serverConnectionText; private final Text clientConnectionText; - private final Text serverConnectionPort; + private final Text clientConnectionPort; private final Text serverRxText; private final Text serverTxText; private final Text clientRxText; @@ -52,7 +52,7 @@ public class Forwarder extends Application implements ConnectionCallback { serverSetButton = new Button("Set"); serverConnectionText = new Text(getConnectionStatus(false)); clientConnectionText = new Text(getConnectionStatus(false)); - serverConnectionPort = new Text(Integer.toString(connections.getLoginPort())); + clientConnectionPort = new Text(Integer.toString(connections.getLoginPort())); serverRxText = new Text(getByteName(connections.getTcpRecv())); serverTxText = new Text(getByteName(connections.getTcpSent())); clientRxText = new Text(getByteName(connections.getUdpRecv())); @@ -158,29 +158,9 @@ public class Forwarder extends Application implements ConnectionCallback { @Override public void start(Stage primaryStage) throws Exception { connections.initialize(); - serverConnectionPort.setText(Integer.toString(connections.getLoginPort())); + clientConnectionPort.setText(Integer.toString(connections.getLoginPort())); GridPane root = new GridPane(); - for (int i = 0; i < 4; i++) { - ColumnConstraints cc = new ColumnConstraints(); - cc.setPercentWidth(25); - root.getColumnConstraints().add(cc); - } - root.add(serverIpField, 0, 0, 2, 1); - root.add(serverPortField, 2, 0, 1, 1); - root.add(serverSetButton, 3, 0, 1, 1); - root.add(new Text("Server Connection:"), 0, 1, 2, 1); - root.add(serverConnectionText, 2, 1, 1, 1); - root.add(serverConnectionPort, 3, 1, 1, 1); - root.add(new Text("Client Connection:"), 0, 2, 2, 1); - root.add(clientConnectionText, 2, 2, 2, 1); - root.add(new Text("Sent"), 1, 3, 1, 1); - root.add(new Text("Recv"), 2, 3, 1, 1); - root.add(new Text("TCP"), 0, 4, 1, 1); - root.add(serverTxText, 1, 4, 1, 1); - root.add(serverRxText, 2, 4, 1, 1); - root.add(new Text("UDP"), 0, 5, 1, 1); - root.add(clientTxText, 1, 5, 1, 1); - root.add(clientRxText, 2, 5, 1, 1); + setupGridPane(root); Scene scene = new Scene(root, 300, 160); primaryStage.setTitle("Holocore Forwarder"); primaryStage.setScene(scene); @@ -193,13 +173,41 @@ public class Forwarder extends Application implements ConnectionCallback { primaryStage.show(); primaryStage.setOnCloseRequest(new EventHandler() { public void handle(WindowEvent we) { - connections.terminate(); + try { + connections.terminate(); + } catch (Exception e) { + e.printStackTrace(); + } primaryStage.close(); System.exit(0); } }); } + private void setupGridPane(GridPane root) { + for (int i = 0; i < 4; i++) { + ColumnConstraints cc = new ColumnConstraints(); + cc.setPercentWidth(25); + root.getColumnConstraints().add(cc); + } + root.add(serverIpField, 0, 0, 2, 1); + root.add(serverPortField, 2, 0, 1, 1); + root.add(serverSetButton, 3, 0, 1, 1); + root.add(new Text("Server Connection:"), 0, 1, 2, 1); + root.add(serverConnectionText, 2, 1, 1, 1); + root.add(new Text("Client Connection:"), 0, 2, 2, 1); + root.add(clientConnectionText, 2, 2, 2, 1); + root.add(clientConnectionPort, 3, 2, 1, 1); + root.add(new Text("Sent"), 1, 3, 1, 1); + root.add(new Text("Recv"), 2, 3, 1, 1); + root.add(new Text("TCP"), 0, 4, 1, 1); + root.add(serverTxText, 1, 4, 1, 1); + root.add(serverRxText, 2, 4, 1, 1); + root.add(new Text("UDP"), 0, 5, 1, 1); + root.add(clientTxText, 1, 5, 1, 1); + root.add(clientRxText, 2, 5, 1, 1); + } + private static String getByteName(long bytes) { int index = 0; double reduced = bytes;