Merge branch 'issue47_disconnect_crash_rebranch' into 'develop'

Issue47 disconnect crash rebranch

Fix for all disconnection issues. Fix is not robust. Need consistent interface between disconnection of ServerToClientThreads and MainServerThread.

See merge request !66
This commit is contained in:
Alistair McIntyre
2017-08-17 14:55:45 +12:00
10 changed files with 232 additions and 158 deletions
@@ -92,6 +92,9 @@ public class MainServerThread implements Runnable, ClientConnectionDelegate {
// TODO: 14/07/17 wmu16 - Send out disconnect packet to clients
try {
for (ServerToClientThread serverToClientThread : serverToClientThreads) {
serverToClientThread.terminate();
}
serverSocket.close();
return;
} catch (IOException e) {
@@ -172,6 +175,7 @@ public class MainServerThread implements Runnable, ClientConnectionDelegate {
thread.sendSetupMessages();
}
});
serverToClientThread.addDisconnectListener(this::clientDisconnected);
}
/**
@@ -181,11 +185,11 @@ public class MainServerThread implements Runnable, ClientConnectionDelegate {
*/
@Override
public void clientDisconnected(Player player) {
try {
player.getSocket().close();
} catch (Exception e) {
serverLog("Cannot disconnect the socket for the disconnected player.", 0);
}
// try {
// player.getSocket().close();
// } catch (Exception e) {
// serverLog("Cannot disconnect the socket for the disconnected player.", 0);
// }
serverLog("Player " + player.getYacht().getSourceId() + "'s socket disconnected", 0);
GameState.removeYacht(player.getYacht().getSourceId());
GameState.removePlayer(player);
@@ -193,11 +197,12 @@ public class MainServerThread implements Runnable, ClientConnectionDelegate {
for (ServerToClientThread serverToClientThread : serverToClientThreads) {
if (serverToClientThread.getSocket() == player.getSocket()) {
closedConnection = serverToClientThread;
} else {
} else if (GameState.getCurrentStage() != GameStages.RACING){
serverToClientThread.sendSetupMessages();
}
}
serverToClientThreads.remove(closedConnection);
closedConnection.terminate();
}
public void startGame() {