listener tcp: don't ping anymore when we've disconnected but haven't noticed. Fixes #629
This commit is contained in:
parent
e5f206b0e9
commit
82a88cb504
|
@ -106,73 +106,80 @@ namespace SparkleLib {
|
|||
// Wait for messages
|
||||
while (this.is_connected) {
|
||||
|
||||
// This blocks the thread
|
||||
int i = 0;
|
||||
while (this.socket.Available < 1) {
|
||||
Thread.Sleep (1000);
|
||||
i++;
|
||||
|
||||
try {
|
||||
// We've timed out, let's ping the server to
|
||||
// see if the connection is still up
|
||||
if (i == 180) {
|
||||
SparkleHelpers.DebugInfo ("ListenerTcp",
|
||||
"Pinging " + Server);
|
||||
|
||||
byte [] ping_bytes = Encoding.UTF8.GetBytes ("ping\n");
|
||||
byte [] pong_bytes = new byte [4096];
|
||||
|
||||
lock (this.socket_lock)
|
||||
this.socket.Send (ping_bytes);
|
||||
|
||||
if (this.socket.Receive (pong_bytes) < 1)
|
||||
// 10057 means "Socket is not connected"
|
||||
throw new SocketException (10057);
|
||||
|
||||
SparkleHelpers.DebugInfo ("ListenerTcp",
|
||||
"Received pong from " + Server);
|
||||
|
||||
i = 0;
|
||||
this.last_ping = DateTime.Now;
|
||||
|
||||
} else {
|
||||
// Check when the last ping occured. If it's
|
||||
// significantly longer than our regular interval the
|
||||
// system likely woke up from sleep and we want to
|
||||
// simulate a disconnect
|
||||
int sleepiness = DateTime.Compare (
|
||||
this.last_ping.AddMilliseconds (180 * 1000 * 1.2),
|
||||
DateTime.Now
|
||||
);
|
||||
|
||||
if (sleepiness <= 0) {
|
||||
try {
|
||||
// This blocks the thread
|
||||
int i = 0;
|
||||
while (this.socket.Available < 1) {
|
||||
try {
|
||||
// We've timed out, let's ping the server to
|
||||
// see if the connection is still up
|
||||
if (i == 180) {
|
||||
SparkleHelpers.DebugInfo ("ListenerTcp",
|
||||
"System woke up from sleep");
|
||||
"Pinging " + Server);
|
||||
|
||||
// 10057 means "Socket is not connected"
|
||||
throw new SocketException (10057);
|
||||
byte [] ping_bytes = Encoding.UTF8.GetBytes ("ping\n");
|
||||
byte [] pong_bytes = new byte [4096];
|
||||
|
||||
lock (this.socket_lock)
|
||||
this.socket.Send (ping_bytes);
|
||||
|
||||
if (this.socket.Receive (pong_bytes) < 1)
|
||||
// 10057 means "Socket is not connected"
|
||||
throw new SocketException (10057);
|
||||
|
||||
SparkleHelpers.DebugInfo ("ListenerTcp",
|
||||
"Received pong from " + Server);
|
||||
|
||||
i = 0;
|
||||
this.last_ping = DateTime.Now;
|
||||
|
||||
} else {
|
||||
// Check when the last ping occured. If it's
|
||||
// significantly longer than our regular interval the
|
||||
// system likely woke up from sleep and we want to
|
||||
// simulate a disconnect
|
||||
int sleepiness = DateTime.Compare (
|
||||
this.last_ping.AddMilliseconds (180 * 1000 * 1.2),
|
||||
DateTime.Now
|
||||
);
|
||||
|
||||
if (sleepiness <= 0) {
|
||||
SparkleHelpers.DebugInfo ("ListenerTcp",
|
||||
"System woke up from sleep");
|
||||
|
||||
// 10057 means "Socket is not connected"
|
||||
throw new SocketException (10057);
|
||||
}
|
||||
}
|
||||
|
||||
// The ping failed: disconnect completely
|
||||
} catch (SocketException) {
|
||||
this.is_connected = false;
|
||||
this.is_connecting = false;
|
||||
|
||||
if (this.socket != null)
|
||||
this.socket.Close ();
|
||||
|
||||
OnDisconnected ("Ping timeout");
|
||||
return;
|
||||
|
||||
}
|
||||
|
||||
// The ping failed: disconnect completely
|
||||
} catch (SocketException) {
|
||||
this.is_connected = false;
|
||||
this.is_connecting = false;;
|
||||
|
||||
if (this.socket != null)
|
||||
this.socket.Close ();
|
||||
|
||||
OnDisconnected ("Ping timeout");
|
||||
return;
|
||||
Thread.Sleep (1000);
|
||||
i++;
|
||||
}
|
||||
}
|
||||
|
||||
} catch (ObjectDisposedException) {
|
||||
this.is_connected = false;
|
||||
this.is_connecting = false;
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
if (this.socket.Available > 0)
|
||||
lock (this.socket_lock)
|
||||
bytes_read = this.socket.Receive (bytes);
|
||||
|
||||
|
||||
// Parse the received message
|
||||
if (bytes_read > 0) {
|
||||
string received = Encoding.UTF8.GetString (bytes);
|
||||
|
|
Loading…
Reference in a new issue