private void Reconnect() { var log = LogManager.GetLogger(GlobalHelper.CallerName()); if (Reconnecting) { return; } Attempts++; if (Attempts > _reconnectionAttempts) { log.Info("reconnect failed"); EmitAll(EVENT_RECONNECT_FAILED); Reconnecting = false; } else { var delay = Attempts * ReconnectionDelay(); delay = Math.Min(delay, ReconnectionDelayMax()); log.Info(string.Format("will wait {0}ms before reconnect attempt", delay)); Reconnecting = true; var timer = EasyTimer.SetTimeout(() => { var log2 = LogManager.GetLogger(GlobalHelper.CallerName()); log2.Info("EasyTimer Reconnect start"); log2.Info(string.Format("attempting reconnect")); EmitAll(EVENT_RECONNECT_ATTEMPT, Attempts); EmitAll(EVENT_RECONNECTING, Attempts); Open(new OpenCallbackImp((err) => { if (err != null) { log.Error("reconnect attempt error", (Exception)err); Reconnecting = false; Reconnect(); EmitAll(EVENT_RECONNECT_ERROR, (Exception)err); } else { log.Info("reconnect success"); OnReconnect(); } })); log2.Info("EasyTimer Reconnect finish"); }, (int)delay); //var taskWorker = new TaskWorker(this); //taskWorker.QueueWorkerWithDelay( // TasksQueue, // null, // (x, e) => { // var log2 = LogManager.GetLogger(GlobalHelper.CallerName()); // log2.Info("EasyTimer Reconnect start"); // log2.Info(string.Format("attempting reconnect")); // EmitAll(EVENT_RECONNECT_ATTEMPT, Attempts); // EmitAll(EVENT_RECONNECTING, Attempts); // Open(new OpenCallbackImp((err) => // { // if (err != null) // { // log.Error("reconnect attempt error", (Exception)err); // Reconnecting = false; // Reconnect(); // EmitAll(EVENT_RECONNECT_ERROR, (Exception)err); // } // else // { // log.Info("reconnect success"); // OnReconnect(); // } // })); // log2.Info("EasyTimer Reconnect finish"); // }, // (x, e) => { // //action complete // }, // (e) => { // //display error // }, // (x, e) => { // // progress change // }, // (int)delay); //TasksQueue.Enqueue(taskWorker); lock (Subs) { Subs.Enqueue(new On.TimeoutActionHandle(timer.Stop)); //Subs.Enqueue(new On.ActionHandleImpl(taskWorker.Stop)); } } }
private Manager Open(IOpenCallback fn) { var log = LogManager.GetLogger(GlobalHelper.CallerName()); log.Info(string.Format("Manager readyState {0}", ReadyState)); if (ReadyState == ReadyStateEnum.OPEN) { return(this); // allready open } log.Info(string.Format("Manager opening {0}", Uri)); EngineSocket = new Engine(Uri, Options); SocketEngine socket = EngineSocket; ReadyState = ReadyStateEnum.OPENING; var openSub = SOCKETNET35.On.Create(socket, EVENT_OPEN, new ListenerImpl(() => { OnOpen(); if (fn != null) { fn.Call(null); } })); var errorSub = SOCKETNET35.On.Create(socket, EVENT_ERROR, new ListenerImpl((data) => { log.Info("connect_error"); Cleanup(); ReadyState = ReadyStateEnum.CLOSED; EmitAll(EVENT_CONNECT_ERROR, data); if (fn != null) { var err = new SocketException("Connection error", data is Exception ? (Exception)data : null); fn.Call(err); } MaybeReconnectOnOpen(); })); if (_timeout >= 0) { var timeout = (int)_timeout; log.Info(string.Format("connection attempt will timeout after {0}", timeout)); var timer = EasyTimer.SetTimeout(() => { var log2 = LogManager.GetLogger(GlobalHelper.CallerName()); log2.Info("Manager Open start"); log2.Info(string.Format("connect attempt timed out after {0}", timeout)); openSub.Destroy(); socket.Close(); socket.Emit(EVENT_ERROR, new SocketException("timeout")); EmitAll(EVENT_CONNECT_TIMEOUT, timeout); log2.Info("Manager Open finish"); }, timeout); //var taskWorker = new TaskWorker(this); //taskWorker.QueueWorkerWithDelay( // TasksQueue, // null, // (x, e) => // { // var log2 = LogManager.GetLogger(GlobalHelper.CallerName()); // log2.Info("Manager Open start"); // log2.Info(string.Format("connect attempt timed out after {0}", timeout)); // openSub.Destroy(); // socket.Close(); // socket.Emit(EVENT_ERROR, new SocketException("timeout")); // EmitAll(EVENT_CONNECT_TIMEOUT, timeout); // log2.Info("Manager Open finish"); // }, // (x, e) => // { // //action complete // }, // (e) => // { // //display error // }, // (x, e) => // { // // progress change // }, // timeout); lock (Subs) { Subs.Enqueue(new On.TimeoutActionHandle(timer.Stop)); //Subs.Enqueue(new On.ActionHandleImpl(taskWorker.Stop)); } } lock (Subs) { Subs.Enqueue(openSub); Subs.Enqueue(errorSub); } EngineSocket.Open(); return(this); }