Fixed client not reconnecting after disconnect

This commit is contained in:
Josh Larson
2016-01-10 11:55:51 -06:00
parent 0f8378b5fb
commit e776df9506
6 changed files with 21 additions and 20 deletions
+5 -4
View File
@@ -50,8 +50,7 @@ public class ClientConnection {
pinger.shutdownNow();
sender.stop();
receiver.stop();
if (callback != null)
callback.onDisconnected();
onDisconnected();
}
public void setCallback(ClientCallback callback) {
@@ -90,16 +89,18 @@ public class ClientConnection {
}
private void onDisconnected() {
boolean prev = connected;
connected = false;
if (callback != null)
if (callback != null && prev)
callback.onDisconnected();
sender.reset();
receiver.reset();
}
private void onConnected() {
boolean prev = connected;
connected = true;
if (callback != null)
if (callback != null && !prev)
callback.onConnected();
sender.reset();
receiver.reset();
-2
View File
@@ -176,7 +176,6 @@ public class ClientReceiver {
private void onDisconnect(Disconnect disconnect) {
if (callback != null)
callback.onDisconnected();
System.out.println("Disconnected");
setConnectionState(ConnectionState.DISCONNECTED);
zone = false;
}
@@ -212,7 +211,6 @@ public class ClientReceiver {
}
private void onOutOfOrder(OutOfOrder ooo) {
System.out.println("OOO " + ooo.getSequence());
sender.onOutOfOrder(ooo.getSequence());
}
+2 -3
View File
@@ -56,6 +56,7 @@ public class ClientSender {
try {
loginServer = new UDPServer(loginPort, 496);
zoneServer = new UDPServer(0, 496);
loginPort = loginServer.getPort();
executor = Executors.newFixedThreadPool(2);
executor.execute(() -> outboundRunnable());
executor.execute(() -> inboundRunnable());
@@ -93,9 +94,7 @@ public class ClientSender {
}
public int getLoginPort() {
if (loginServer == null)
return -1;
return loginServer.getPort();
return loginPort;
}
public int getZonePort() {
+3 -2
View File
@@ -116,7 +116,7 @@ public class Connections {
}
private void onServerDisconnected() {
client.stop();
terminate();
if (callback != null)
callback.onServerDisconnected();
}
@@ -128,9 +128,10 @@ public class Connections {
}
private void onClientDisconnected() {
server.stop();
terminate();
if (callback != null)
callback.onClientDisconnected();
client.start();
}
private void onDataRecvTcp(byte [] data) {
+1 -1
View File
@@ -46,7 +46,7 @@ public class Forwarder extends Application implements ConnectionCallback {
public Forwarder() {
executor = Executors.newSingleThreadExecutor();
connections = new Connections();
connections = new Connections(InetAddress.getLoopbackAddress(), 44463, 0);
serverIpField = new TextField(connections.getRemoteAddress().getHostAddress());
serverPortField = new TextField(Integer.toString(connections.getRemotePort()));
serverSetButton = new Button("Set");
+10 -8
View File
@@ -19,9 +19,9 @@ public class ServerConnection {
private final Object bufferMutex;
private final Object socketMutex;
private final ExecutorService processor;
private final ExecutorService callbackExecutor;
private final Queue<byte []> outQueue;
private ExecutorService processor;
private ExecutorService callbackExecutor;
private ByteBuffer buffer;
private long lastBufferSizeModification;
private SocketChannel socket;
@@ -36,8 +36,6 @@ public class ServerConnection {
public ServerConnection(InetAddress addr, int port) {
this.bufferMutex = new Object();
this.socketMutex = new Object();
this.processor = Executors.newSingleThreadExecutor();
this.callbackExecutor = Executors.newSingleThreadExecutor();
this.outQueue = new LinkedList<>();
this.buffer = ByteBuffer.allocate(DEFAULT_BUFFER).order(ByteOrder.LITTLE_ENDIAN);
lastBufferSizeModification = System.nanoTime();
@@ -52,6 +50,8 @@ public class ServerConnection {
public void start() {
stop();
processor = Executors.newSingleThreadExecutor();
callbackExecutor = Executors.newSingleThreadExecutor();
running = true;
thread = new Thread(() -> run());
thread.start();
@@ -63,6 +63,12 @@ public class ServerConnection {
if (thread != null)
thread.interrupt();
thread = null;
if (processor != null)
processor.shutdownNow();
if (callbackExecutor != null)
callbackExecutor.shutdownNow();
processor = null;
callbackExecutor = null;
}
public void setRemoteAddress(InetAddress addr, int port) {
@@ -192,7 +198,6 @@ public class ServerConnection {
int nCapacity = buffer.capacity() * 2;
while (nCapacity < buffer.position()+data.remaining())
nCapacity *= 2;
System.out.println("Expanding buffer to " + nCapacity);
ByteBuffer bb = ByteBuffer.allocate(nCapacity).order(ByteOrder.LITTLE_ENDIAN);
buffer.flip();
bb.put(buffer);
@@ -215,7 +220,6 @@ public class ServerConnection {
nCapacity *= 2;
if (nCapacity >= buffer.capacity())
return;
System.out.println("Shrinking buffer to " + nCapacity);
ByteBuffer bb = ByteBuffer.allocate(nCapacity).order(ByteOrder.LITTLE_ENDIAN);
buffer.flip();
bb.put(buffer);
@@ -248,7 +252,6 @@ public class ServerConnection {
if (socket != null)
disconnect();
socket = SocketChannel.open(new InetSocketAddress(addr, port));
System.out.println("Connected");
reset();
return true;
} catch (IOException e) {
@@ -266,7 +269,6 @@ public class ServerConnection {
if (socket == null)
return true;
try {
System.out.println("Disconnected");
socket.close();
socket = null;
reset();