/// <summary> /// Method used by trying to reconnect. /// </summary> /// <param name="connectionVersion">A value indicating witch connection version is being used. Useful for ensuring that only one thread performs this action.</param> /// <returns>false if it failed to reconnect or false indicating that reconnection was sucessfull</returns> private bool OnIoFailure(long connectionVersion) { lock (this) { if (closed) { return(false); } // Check connection version if (connectionVersion < this.connectionVersion) { // Another thread already sinalized the failure so return return(!closed); // If the reconnection was sucessful then it's not closed, otherwise it is. } log.ErrorFormat("Connection failed. {0}", hosts.Peek()); // Fire event IoSyncStatus syncStatus = new IoSyncStatus(IoStatus.Failed, new NotifiableEvent <IoStatus>()); IoFailureHandler handler = this.IoFailed; if (handler != null) { handler(syncStatus); } // Close socket CloseCommunication(); // Reconnect bool retry = false; int tryCount = 0; do { try { ++this.connectionVersion; StartReading(hostInfo); retry = false; } catch (Exception) { retry = (++tryCount) != retriesCount; if (!retry) { closed = true; //Notify clients syncStatus.Status = IoStatus.Closed; syncStatus.OnChange.Fire(syncStatus.Status); // Return return(false); } // wait for while, give the agent time to get back to life. System.Threading.Thread.Sleep(1000); hostInfo = hosts.Get(); } if (retry) { log.ErrorFormat("Re-connection failed. {0}", hosts.Peek()); } } while (retry); syncStatus.Status = IoStatus.Ok; syncStatus.OnChange.Fire(syncStatus.Status); } return(true); }