private void OnConnectionClosed(object o, EventArgs args)
        {
            if (Logging.IsEnabled)
            {
                Logging.Enter(this, o, nameof(OnConnectionClosed));
            }

            if (_amqpIotConnection != null && ReferenceEquals(_amqpIotConnection, o))
            {
                _amqpAuthenticationRefresher?.StopLoop();
                HashSet <AmqpUnit> amqpUnits;
                lock (_unitsLock)
                {
                    amqpUnits = new HashSet <AmqpUnit>(_amqpUnits);
                }
                foreach (AmqpUnit unit in amqpUnits)
                {
                    unit.OnConnectionDisconnected();
                }
            }
            if (Logging.IsEnabled)
            {
                Logging.Exit(this, o, nameof(OnConnectionClosed));
            }
        }
Ejemplo n.º 2
0
        private void Cleanup()
        {
            Logging.Enter(this, nameof(Cleanup));

            _amqpIotSession?.SafeClose();
            _amqpAuthenticationRefresher?.StopLoop();
            if (!_deviceIdentity.IsPooling())
            {
                _amqpConnectionHolder?.Shutdown();
            }

            Logging.Exit(this, nameof(Cleanup));
        }
Ejemplo n.º 3
0
        public async Task CloseAsync(TimeSpan timeout)
        {
            if (Logging.IsEnabled) Logging.Enter(this, timeout, $"{nameof(CloseAsync)}");

            if (SetNotUsable() == 0 && _amqpSession != null)
            {
                _amqpAuthenticationRefresher?.StopLoop();
                await _amqpSession.CloseAsync(timeout).ConfigureAwait(false);
                OnUnitDisconnected?.Invoke(true, EventArgs.Empty);
            }

            if (Logging.IsEnabled) Logging.Exit(this, timeout, $"{nameof(CloseAsync)}");
        }
Ejemplo n.º 4
0
 private void Cleanup()
 {
     if (Logging.IsEnabled)
     {
         Logging.Enter(this, $"{nameof(Cleanup)}");
     }
     _amqpIoTSession?.SafeClose();
     _amqpAuthenticationRefresher?.StopLoop();
     if (!_deviceIdentity.IsPooling())
     {
         _amqpConnectionHolder?.Dispose();
     }
     if (Logging.IsEnabled)
     {
         Logging.Exit(this, $"{nameof(Cleanup)}");
     }
 }
Ejemplo n.º 5
0
 private void OnConnectionClosed(object o, EventArgs args)
 {
     if (Logging.IsEnabled)
     {
         Logging.Enter(this, o, $"{nameof(OnConnectionClosed)}");
     }
     if (AmqpConnection != null && ReferenceEquals(AmqpConnection, o))
     {
         AmqpAuthenticationRefresher?.StopLoop();
         foreach (AmqpUnit unit in AmqpUnits.Values)
         {
             unit.OnConnectionDisconnected();
         }
         AmqpUnits.Clear();
         OnConnectionDisconnected?.Invoke(this, EventArgs.Empty);
     }
     if (Logging.IsEnabled)
     {
         Logging.Exit(this, o, $"{nameof(OnConnectionClosed)}");
     }
 }
Ejemplo n.º 6
0
        internal async Task <AmqpIoTSession> EnsureSessionAsync(TimeSpan timeout)
        {
            if (_closed)
            {
                throw new IotHubException("Device is now offline.", false);
            }

            if (Logging.IsEnabled)
            {
                Logging.Enter(this, timeout, $"{nameof(EnsureSessionAsync)}");
            }
            bool gain = await _sessionLock.WaitAsync(timeout).ConfigureAwait(false);

            if (!gain)
            {
                throw new TimeoutException();
            }

            try
            {
                if (_amqpIoTSession == null || _amqpIoTSession.IsClosing())
                {
                    _amqpIoTSession = await _amqpConnectionHolder.OpenSessionAsync(_deviceIdentity, timeout).ConfigureAwait(false);

                    if (Logging.IsEnabled)
                    {
                        Logging.Associate(this, _amqpIoTSession, $"{nameof(_amqpIoTSession)}");
                    }
                    if (_deviceIdentity.AuthenticationModel == AuthenticationModel.SasIndividual)
                    {
                        _amqpAuthenticationRefresher = await _amqpConnectionHolder.CreateRefresherAsync(_deviceIdentity, timeout).ConfigureAwait(false);

                        if (Logging.IsEnabled)
                        {
                            Logging.Associate(this, _amqpAuthenticationRefresher, $"{nameof(_amqpAuthenticationRefresher)}");
                        }
                    }

                    _amqpIoTSession.Closed += OnSessionDisconnected;
                    _messageSendingLink     = await _amqpIoTSession.OpenTelemetrySenderLinkAsync(_deviceIdentity, timeout).ConfigureAwait(false);

                    _messageSendingLink.Closed += (obj, arg) => {
                        _amqpIoTSession.SafeClose();
                    };

                    if (Logging.IsEnabled)
                    {
                        Logging.Associate(this, _messageSendingLink, $"{nameof(_messageSendingLink)}");
                    }
                }

                if (_disposed)
                {
                    _amqpAuthenticationRefresher?.StopLoop();
                    _amqpIoTSession.SafeClose();
                    if (!_deviceIdentity.IsPooling())
                    {
                        _amqpConnectionHolder.Dispose();
                    }
                    throw new IotHubException("Device is now offline.", false);
                }
            }
            finally
            {
                _sessionLock.Release();
            }


            if (Logging.IsEnabled)
            {
                Logging.Exit(this, timeout, $"{nameof(EnsureSessionAsync)}");
            }
            return(_amqpIoTSession);
        }