Example #1
0
        internal void OnHeartbeat(long timeout)
        {
            if (this.PingTimeoutTimer != null)
            {
                PingTimeoutTimer.Stop();
                PingTimeoutTimer = null;
            }

            if (timeout <= 0)
            {
                timeout = this.PingInterval + this.PingTimeout;
            }

            PingTimeoutTimer = EasyTimer.SetTimeout(() =>
            {
                var log2 = LogManager.GetLogger(GlobalHelper.CallerName());
                log2.Info("EasyTimer OnHeartbeat start");
                if (ReadyState == ReadyStateEnum.CLOSED)
                {
                    log2.Info("EasyTimer OnHeartbeat ReadyState == ReadyStateEnum.CLOSED finish");
                    return;
                }
                OnClose("ping timeout");
                log2.Info("EasyTimer OnHeartbeat finish");
            }, (int)timeout);
        }
Example #2
0
        private void OnClose(string reason, Exception desc = null)
        {
            if (this.ReadyState == ReadyStateEnum.OPENING || this.ReadyState == ReadyStateEnum.OPEN)
            {
                var log = LogManager.GetLogger(GlobalHelper.CallerName());

                log.Info(string.Format("OnClose socket close with reason: {0}", reason));

                // clear timers
                if (this.PingIntervalTimer != null)
                {
                    this.PingIntervalTimer.Stop();
                }
                if (this.PingTimeoutTimer != null)
                {
                    this.PingTimeoutTimer.Stop();
                }


                //WriteBuffer = WriteBuffer.Clear();
                //CallbackBuffer = CallbackBuffer.Clear();
                //PrevBufferLen = 0;

                var temporaryTimer = EasyTimer.SetTimeout(() =>
                {
                    lock (WriteBuffer)
                    {
                        WriteBuffer.Clear();
                    }
                    CallbackBuffer = new List <Action>();

                    PrevBufferLen = 0;
                }, 1);

                if (this.Transport != null)
                {
                    // stop event from firing again for transport
                    this.Transport.Off(EVENT_CLOSE);

                    // ensure transport won't stay open
                    this.Transport.Close();

                    // ignore further transport communication
                    this.Transport.Off();
                }

                // set ready state
                this.ReadyState = ReadyStateEnum.CLOSED;

                // clear session id
                this.Id = null;

                // emit close events
                this.Emit(EVENT_CLOSE, reason, desc);

                temporaryTimer.Stop();
            }
        }
Example #3
0
        private void SetPing()
        {
            var log = LogManager.GetLogger(GlobalHelper.CallerName());

            if (this.PingIntervalTimer != null)
            {
                PingIntervalTimer.Stop();
            }
            log.Info(string.Format("writing ping packet - expecting pong within {0}ms", PingTimeout));

            PingIntervalTimer = EasyTimer.SetTimeout(() =>
            {
                var log2 = LogManager.GetLogger(GlobalHelper.CallerName());
                log2.Info("EasyTimer SetPing start");

                Ping();
                OnHeartbeat(PingTimeout);
                log2.Info("EasyTimer SetPing finish");
            }, (int)PingInterval);
        }
        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);
        }