Ejemplo n.º 1
0
        private void Dispose(bool disposing)
        {
            if (_disposed)
            {
                return;
            }
            _disposed = true;

            if (disposing)
            {
                if (Logging.IsEnabled)
                {
                    Logging.Enter(this, disposing, $"{nameof(Dispose)}");
                }
                Cleanup();
                if (!_deviceIdentity.IsPooling())
                {
                    _amqpConnectionHolder?.Dispose();
                }

                if (Logging.IsEnabled)
                {
                    Logging.Exit(this, disposing, $"{nameof(Dispose)}");
                }
            }
        }
Ejemplo n.º 2
0
        private void Dispose(bool disposing)
        {
            if (_disposed)
            {
                return;
            }

            _disposed = true;

            if (disposing)
            {
                Logging.Enter(this, disposing, nameof(Dispose));

                Cleanup();
                if (!_deviceIdentity.IsPooling())
                {
                    _amqpConnectionHolder?.Dispose();
                }

                // For device sas authenticated clients the authentication refresher is associated with the AMQP unit itself,
                // so it needs to be explicitly disposed.
                _amqpAuthenticationRefresher?.StopLoop();
                _amqpAuthenticationRefresher?.Dispose();

                _sessionSemaphore?.Dispose();
                _messageReceivingLinkSemaphore?.Dispose();
                _messageReceivingCallbackSemaphore?.Dispose();
                _eventReceivingLinkSemaphore?.Dispose();
                _methodLinkSemaphore?.Dispose();
                _twinLinksSemaphore?.Dispose();

                Logging.Exit(this, disposing, nameof(Dispose));
            }
        }
Ejemplo n.º 3
0
        private void Dispose(bool disposing)
        {
            if (_disposed)
            {
                return;
            }

            _disposed = true;

            if (disposing)
            {
                Logging.Enter(this, disposing, nameof(Dispose));

                Cleanup();
                if (!_deviceIdentity.IsPooling())
                {
                    _amqpConnectionHolder?.Dispose();
                }

                _sessionSemaphore?.Dispose();
                _messageReceivingLinkSemaphore?.Dispose();
                _messageReceivingCallbackSemaphore?.Dispose();
                _eventReceivingLinkSemaphore?.Dispose();
                _methodLinkSemaphore?.Dispose();
                _twinLinksSemaphore?.Dispose();

                Logging.Exit(this, disposing, nameof(Dispose));
            }
        }
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 Dispose(bool disposing)
        {
            try
            {
                if (Logging.IsEnabled)
                {
                    Logging.Enter(this, $"Device pooling={_deviceIdentity?.IsPooling()}; disposed={_disposed}; disposing={disposing}", $"{nameof(AmqpUnit)}.{nameof(Dispose)}");
                }

                if (!_disposed)
                {
                    if (disposing)
                    {
                        Cleanup();
                        if (!_deviceIdentity.IsPooling())
                        {
                            _amqpConnectionHolder?.Dispose();
                        }

                        // For device sas authenticated clients the authentication refresher is associated with the AMQP unit itself,
                        // so it needs to be explicitly disposed.
                        _amqpAuthenticationRefresher?.StopLoop();
                        _amqpAuthenticationRefresher?.Dispose();

                        _sessionSemaphore?.Dispose();
                        _messageReceivingLinkSemaphore?.Dispose();
                        _messageReceivingCallbackSemaphore?.Dispose();
                        _eventReceivingLinkSemaphore?.Dispose();
                        _methodLinkSemaphore?.Dispose();
                        _twinLinksSemaphore?.Dispose();

                        Logging.Exit(this, disposing, nameof(Dispose));
                    }
                }

                _disposed = true;
            }
            finally
            {
                if (Logging.IsEnabled)
                {
                    Logging.Exit(this, $"Device pooling={_deviceIdentity?.IsPooling()}; disposed={_disposed}; disposing={disposing}", $"{nameof(AmqpUnit)}.{nameof(Dispose)}");
                }
            }
        }
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);
        }