Skip to content

Commit

Permalink
Handle socket disconnections reasonably
Browse files Browse the repository at this point in the history
  • Loading branch information
Morphan1 committed Mar 3, 2015
1 parent f1614ad commit e8848dd
Showing 1 changed file with 13 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.InetSocketAddress;
import java.net.Socket;
import java.security.MessageDigest;
Expand Down Expand Up @@ -45,7 +46,7 @@ public void send(String message) {
this.output.flush();
} catch(Exception e) {
dB.echoError(e);
this.close();
this.close("Error sending data to client: " + e.getMessage());
}
}

Expand All @@ -65,7 +66,14 @@ public void connect() {
}

public void close() {
close(null);
}

public void close(String reason) {
if (isListening) {
if (reason != null) {
dB.log("Disconnected from BungeeCord Socket: " + reason);
}
this.isListening = false;
this.task.cancel();
this.task = null;
Expand All @@ -91,8 +99,7 @@ public void run() {
while (this.isListening) {
int receivedEncryptedLength = this.input.readInt();
if (receivedEncryptedLength == -1 || System.currentTimeMillis() >= timeoutExpired) {
this.isListening = false;
this.close();
this.close(receivedEncryptedLength == -1 ? "Connection failed" : "Connection timed out");
break;
}

Expand All @@ -112,9 +119,10 @@ public void run() {
this.handle(content);
}

} catch (IOException e) {
this.close("Server socket closed");
} catch (Exception e) {
this.isListening = false;
this.close();
this.close("Error receiving data from server: " + e.getMessage());
dB.echoError(e);
}
}
Expand Down

0 comments on commit e8848dd

Please sign in to comment.