Fix ping on timeout. Fixes broken reconnect

This commit is contained in:
Hylke Bons 2012-02-08 01:44:56 +01:00
parent 7a0527b7a7
commit e9373b87d5
3 changed files with 22 additions and 27 deletions

View file

@ -105,6 +105,7 @@ namespace SparkleLib {
} }
// TODO: rename override method instead?
public void AlsoListenToBase (string channel) public void AlsoListenToBase (string channel)
{ {
if (!this.channels.Contains (channel) && IsConnected) { if (!this.channels.Contains (channel) && IsConnected) {

View file

@ -25,10 +25,11 @@ namespace SparkleLib {
public class SparkleListenerTcp : SparkleListenerBase { public class SparkleListenerTcp : SparkleListenerBase {
private Socket socket; private Socket socket;
private Object socket_lock = new Object ();
private Thread thread; private Thread thread;
private bool is_connected = false; private Object socket_lock = new Object ();
private bool is_connecting = false; private bool is_connected = false;
private bool is_connecting = false;
private int receive_timeout = 180 * 1000;
public SparkleListenerTcp (Uri server, string folder_identifier) : public SparkleListenerTcp (Uri server, string folder_identifier) :
@ -56,8 +57,6 @@ namespace SparkleLib {
// Starts a new thread and listens to the channel // Starts a new thread and listens to the channel
public override void Connect () public override void Connect ()
{ {
SparkleHelpers.DebugInfo ("ListenerTcp", "Connecting to " + Server.Host);
this.is_connecting = true; this.is_connecting = true;
this.thread = new Thread ( this.thread = new Thread (
@ -72,8 +71,8 @@ namespace SparkleLib {
this.socket = new Socket (AddressFamily.InterNetwork, this.socket = new Socket (AddressFamily.InterNetwork,
SocketType.Stream, ProtocolType.Tcp) { SocketType.Stream, ProtocolType.Tcp) {
ReceiveTimeout = 60 * 1000, ReceiveTimeout = this.receive_timeout,
SendTimeout = 3 * 1000 SendTimeout = 10 * 1000
}; };
// Try to connect to the server // Try to connect to the server
@ -87,7 +86,7 @@ namespace SparkleLib {
// Subscribe to channels of interest to us // Subscribe to channels of interest to us
foreach (string channel in base.channels) { foreach (string channel in base.channels) {
SparkleHelpers.DebugInfo ("ListenerTcp", SparkleHelpers.DebugInfo ("ListenerTcp",
"Subscribing to channel " + channel); "Subscribing to channel " + channel + " on " + Server);
byte [] subscribe_bytes = byte [] subscribe_bytes =
Encoding.UTF8.GetBytes ("subscribe " + channel + "\n"); Encoding.UTF8.GetBytes ("subscribe " + channel + "\n");
@ -116,40 +115,34 @@ namespace SparkleLib {
// We've timed out, let's ping the server to // We've timed out, let's ping the server to
// see if the connection is still up // see if the connection is still up
} catch (SocketException e) { } catch (SocketException) {
Console.WriteLine ("1st catch block");
try { try {
byte [] ping_bytes = byte [] ping_bytes = Encoding.UTF8.GetBytes ("ping\n");
Encoding.UTF8.GetBytes ("ping"); byte [] ping_back_bytes = new byte [4096];
Console.WriteLine ("1"); SparkleHelpers.DebugInfo ("ListenerTcp", "Pinging " + Server);
this.socket.Send (ping_bytes); lock (this.socket_lock)
this.socket.ReceiveTimeout = 3 * 1000; this.socket.Send (ping_bytes);
Console.WriteLine ("2"); this.socket.ReceiveTimeout = 10 * 1000;
// 10057 means "Socket is not connected" // 10057 means "Socket is not connected"
if (this.socket.Receive (bytes) < 1) { if (this.socket.Receive (ping_back_bytes) < 1)
Console.WriteLine ("3");
throw new SocketException (10057); throw new SocketException (10057);
}
Console.WriteLine ("4"); SparkleHelpers.DebugInfo ("ListenerTcp", "Received pong from " + Server);
this.socket.ReceiveTimeout = this.receive_timeout;
// The ping failed: disconnect completely // The ping failed: disconnect completely
} catch (SocketException) { } catch (SocketException) {
this.is_connected = false; this.is_connected = false;
this.is_connecting = false; this.is_connecting = false;
this.socket.ReceiveTimeout = 60 * 1000; this.socket.ReceiveTimeout = this.receive_timeout;
Console.WriteLine ("2nd catch block"); OnDisconnected ("Ping timeout");
return;
OnDisconnected (e.Message);
break;
} }
} }

View file

@ -191,6 +191,7 @@ namespace SparkleLib {
public string Domain { public string Domain {
get { get {
// TODO: use Uri class
Regex regex = new Regex (@"(@|://)([a-z0-9\.-]+)(/|:)"); Regex regex = new Regex (@"(@|://)([a-z0-9\.-]+)(/|:)");
Match match = regex.Match (SparkleConfig.DefaultConfig.GetUrlForFolder (Name)); Match match = regex.Match (SparkleConfig.DefaultConfig.GetUrlForFolder (Name));