Catch SocketException when receiving messages - this can get thrown even if "socket.Available > 0" is true. Treat like timeout by disconnection and reconnecting the notication server

This commit is contained in:
Markus Stoll 2014-07-15 20:58:20 +02:00 committed by Hylke Bons
parent 2b789e1e4c
commit 69bf77d799

View file

@ -142,15 +142,7 @@ namespace SparkleLib {
// The ping failed: disconnect completely
} catch (SocketException e) {
this.is_connected = false;
this.is_connecting = false;
if (this.socket != null) {
this.socket.Close ();
this.socket = null;
}
OnDisconnected (reason, "Ping timeout: " + e.Message);
Disconnect(reason, "Ping timeout: " + e.Message);
return;
}
@ -162,23 +154,29 @@ namespace SparkleLib {
return;
}
if (this.socket.Available > 0)
bytes_read = this.socket.Receive (bytes);
try {
if (this.socket.Available > 0)
bytes_read = this.socket.Receive (bytes);
// Parse the received message
if (bytes_read > 0) {
string received = Encoding.UTF8.GetString (bytes);
string line = received.Substring (0, received.IndexOf ("\n"));
// Parse the received message
if (bytes_read > 0) {
string received = Encoding.UTF8.GetString (bytes);
string line = received.Substring (0, received.IndexOf ("\n"));
if (!line.Contains ("!"))
continue;
if (!line.Contains ("!"))
continue;
string folder_identifier = line.Substring (0, line.IndexOf ("!"));
string message = CleanMessage (line.Substring (line.IndexOf ("!") + 1));
string folder_identifier = line.Substring (0, line.IndexOf ("!"));
string message = CleanMessage (line.Substring (line.IndexOf ("!") + 1));
// We have a message!
if (!folder_identifier.Equals ("debug") && !String.IsNullOrEmpty (message))
OnAnnouncement (new SparkleAnnouncement (folder_identifier, message));
// We have a message!
if (!folder_identifier.Equals ("debug") && !string.IsNullOrEmpty (message))
OnAnnouncement (new SparkleAnnouncement (folder_identifier, message));
}
} catch (SocketException e) {
Disconnect (DisconnectReason.TimeOut, "Timeout during receiving: " + e.Message);
return;
}
}
});
@ -187,6 +185,20 @@ namespace SparkleLib {
}
private void Disconnect (DisconnectReason reason, string message)
{
this.is_connected = false;
this.is_connecting = false;
if (this.socket != null) {
this.socket.Close ();
this.socket = null;
}
OnDisconnected (reason, message);
}
protected override void AlsoListenToInternal (string folder_identifier)
{
string to_send = "subscribe " + folder_identifier + "\n";