private void ConnectionOnTerminatedUnexpectedly()
        {
            _isTerminating = true;

            try
            {
                foreach (var kvp in _clientSessions)
                {
                    var session = kvp.Value;
                    try
                    {
                        _log.Info($"Disconnected client due to unhandled error: {session}");
                        ClientDisconnected?.Invoke(session.ToIdentity());
                    }
                    catch (Exception ex)
                    {
                        _log.Error(ex);
                    }
                }
            }
            finally
            {
                TerminatedUnexpectedly?.Invoke();
                DisposeImpl(false);
            }
        }
Exemplo n.º 2
0
        internal void UnexpectedTermination(Exception exception)
        {
            TerminateAsync();
            OngoingInitialisation = false;

            var secondsOfActiveConnection = (DateTime.Now - (_currentConnectionStarted ?? DateTime.Now)).TotalSeconds;

            Debug.WriteLine($"Event Listener terminated unexpectedly after {secondsOfActiveConnection} seconds.");

            if (AutoReconnect)
            {
                Debug.WriteLine($"{exception.GetType()} occured.");
                if (_firstReconnectAttempt == null)
                {
                    _firstReconnectAttempt = DateTime.Now;
                }
                if (((DateTime)_firstReconnectAttempt).AddMinutes(1) < DateTime.Now)
                {
                    _abortionRequested     = false;
                    _attempts              = 0;
                    _firstReconnectAttempt = null;

                    Debug.WriteLine("Attempting to reconnect...");
                    AttemptedReconnect?.Invoke(this, new AttemptedReconnectEvent(_attempts));
                    InitializeAsync();
                }
                else //recently attempted to reconnect
                {
                    _attempts++;
                    Debug.WriteLine($"Attempting to reconnect... {_attempts}");
                    AttemptedReconnect?.Invoke(this, new AttemptedReconnectEvent(_attempts));

                    if (_attempts >= 3)
                    {
                        _abortionRequested     = true;
                        _attempts              = 0;
                        _firstReconnectAttempt = null;

                        Debug.WriteLine($"Too many reconnection attempts ({_attempts})");
                        Debug.WriteLine($"Event subscription will be terminated. ({_attempts})");
                        TerminatedUnexpectedly?.Invoke(this, new TerminatedUnexpectedlyEvent(exception));
                        Debug.WriteLine("New initialisation required.");
                    }
                    else
                    {
                        InitializeAsync();
                    }
                }
            }
            else
            {
                Debug.WriteLine($"Event subscription will be terminated. ({_attempts})");
                TerminatedUnexpectedly?.Invoke(this, new TerminatedUnexpectedlyEvent(exception));
                AutoReconnect = false;
                Debug.WriteLine("New initialisation required.");
            }
        }
Exemplo n.º 3
0
 private void ConnectionOnTerminatedUnexpectedly()
 {
     _isTerminatedUnexpectedly = true;
     TerminatedUnexpectedly?.Invoke();
     Dispose();
 }