public void RemoveAmqpUnit(AmqpUnit amqpUnit)
        {
            if (Logging.IsEnabled)
            {
                Logging.Enter(this, amqpUnit, nameof(RemoveAmqpUnit));
            }

            IDeviceIdentity deviceIdentity = amqpUnit.GetDeviceIdentity();

            if (deviceIdentity.IsPooling())
            {
                AmqpConnectionHolder amqpConnectionHolder;
                lock (_lock)
                {
                    AmqpConnectionHolder[] amqpConnectionHolders = ResolveConnectionGroup(deviceIdentity);
                    amqpConnectionHolder = ResolveConnectionByHashing(amqpConnectionHolders, deviceIdentity);

                    amqpConnectionHolder.RemoveAmqpUnit(amqpUnit);

                    // If the connection holder does not have any more units, the entry needs to be nullified.
                    if (amqpConnectionHolder.IsEmpty())
                    {
                        int index = GetDeviceIdentityIndex(deviceIdentity, amqpConnectionHolders.Length);
                        amqpConnectionHolders[index] = null;
                        amqpConnectionHolder?.Dispose();
                    }
                }
            }

            if (Logging.IsEnabled)
            {
                Logging.Exit(this, amqpUnit, nameof(RemoveAmqpUnit));
            }
        }
Exemplo n.º 2
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)}");
                }
            }
        }
Exemplo n.º 3
0
        private void Cleanup()
        {
            if (Logging.IsEnabled)
            {
                Logging.Enter(this, nameof(Cleanup));
            }

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

            if (Logging.IsEnabled)
            {
                Logging.Exit(this, nameof(Cleanup));
            }
        }
        public AmqpUnit CreateAmqpUnit(
            IDeviceIdentity deviceIdentity,
            Func <MethodRequestInternal, Task> onMethodCallback,
            Action <Twin, string, TwinCollection, IotHubException> twinMessageListener,
            Func <string, Message, Task> onModuleMessageReceivedCallback,
            Func <Message, Task> onDeviceMessageReceivedCallback,
            Action onUnitDisconnected)
        {
            if (Logging.IsEnabled)
            {
                Logging.Enter(this, deviceIdentity, nameof(CreateAmqpUnit));
            }

            if (deviceIdentity.IsPooling())
            {
                AmqpConnectionHolder amqpConnectionHolder;
                lock (_lock)
                {
                    AmqpConnectionHolder[] amqpConnectionHolders = ResolveConnectionGroup(deviceIdentity);
                    amqpConnectionHolder = ResolveConnectionByHashing(amqpConnectionHolders, deviceIdentity);
                }

                if (Logging.IsEnabled)
                {
                    Logging.Exit(this, deviceIdentity, nameof(CreateAmqpUnit));
                }

                return(amqpConnectionHolder.CreateAmqpUnit(
                           deviceIdentity,
                           onMethodCallback,
                           twinMessageListener,
                           onModuleMessageReceivedCallback,
                           onDeviceMessageReceivedCallback,
                           onUnitDisconnected));
            }
            else
            {
                if (Logging.IsEnabled)
                {
                    Logging.Exit(this, deviceIdentity, nameof(CreateAmqpUnit));
                }

                return(new AmqpConnectionHolder(deviceIdentity)
                       .CreateAmqpUnit(
                           deviceIdentity,
                           onMethodCallback,
                           twinMessageListener,
                           onModuleMessageReceivedCallback,
                           onDeviceMessageReceivedCallback,
                           onUnitDisconnected));
            }
        }