Beispiel #1
0
        public async Task EnableEventReceiveAsync(TimeSpan timeout)
        {
            if (Logging.IsEnabled)
            {
                Logging.Enter(this, timeout, $"{nameof(EnableEventReceiveAsync)}");
            }

            try
            {
                Debug.Assert(_eventReceivingLink == null);
                _eventReceivingLink = await _amqpIoTSession.OpenEventsReceiverLinkAsync(
                    _deviceIdentity,
                    timeout
                    ).ConfigureAwait(false);

                _eventReceivingLink.RegisterEventListener(OnEventsReceived);
                _eventReceivingLink.Closed += OnLinkDisconnected;

                if (Logging.IsEnabled)
                {
                    Logging.Associate(this, this, _eventReceivingLink, $"{nameof(EnableEventReceiveAsync)}");
                }
            }
            finally
            {
                if (Logging.IsEnabled)
                {
                    Logging.Exit(this, timeout, $"{nameof(EnableEventReceiveAsync)}");
                }
            }
        }
Beispiel #2
0
        public async Task EnableMethodsAsync(TimeSpan timeout)
        {
            if (Logging.IsEnabled)
            {
                Logging.Enter(this, timeout, $"{nameof(EnableMethodsAsync)}");
            }

            try
            {
                Debug.Assert(_methodSendingLink == null);
                Debug.Assert(_methodReceivingLink == null);

                string correlationIdSuffix = Guid.NewGuid().ToString();

                Task <AmqpIoTReceivingLink> receiveLinkCreator = _amqpIoTSession.OpenMethodsReceiverLinkAsync(_deviceIdentity, correlationIdSuffix, timeout);
                Task <AmqpIoTSendingLink>   sendingLinkCreator = _amqpIoTSession.OpenMethodsSenderLinkAsync(_deviceIdentity, correlationIdSuffix, timeout);
                await Task.WhenAll(receiveLinkCreator, sendingLinkCreator).ConfigureAwait(false);

                _methodReceivingLink = receiveLinkCreator.Result;
                _methodSendingLink   = sendingLinkCreator.Result;

                _methodReceivingLink.RegisterMethodListener(OnMethodReceived);
                _methodSendingLink.Closed   += OnLinkDisconnected;
                _methodReceivingLink.Closed += OnLinkDisconnected;

                if (Logging.IsEnabled)
                {
                    Logging.Associate(this, _methodReceivingLink, $"{nameof(_methodReceivingLink)}");
                }
                if (Logging.IsEnabled)
                {
                    Logging.Associate(this, _methodSendingLink, $"{nameof(_methodSendingLink)}");
                }
            }
            catch (Exception)
            {
                _methodReceivingLink?.Abort();
                _methodReceivingLink = null;

                _methodSendingLink?.Abort();
                _methodSendingLink = null;

                throw;
            }
            finally
            {
                if (Logging.IsEnabled)
                {
                    Logging.Exit(this, timeout, $"{nameof(EnableMethodsAsync)}");
                }
            }
        }
        public async Task EnableEventReceiveAsync(TimeSpan timeout)
        {
            if (_closed)
            {
                throw new IotHubException("Device is now offline.", false);
            }

            if (Logging.IsEnabled)
            {
                Logging.Enter(this, timeout, $"{nameof(EnableEventReceiveAsync)}");
            }

            AmqpIoTSession amqpIoTSession = await EnsureSessionAsync(timeout).ConfigureAwait(false);

            bool gain = await _eventReceivingLinkLock.WaitAsync(timeout).ConfigureAwait(false);

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

            try
            {
                if (_eventReceivingLink == null || _eventReceivingLink.IsClosing())
                {
                    _eventReceivingLink?.SafeClose();

                    _eventReceivingLink = await amqpIoTSession.OpenEventsReceiverLinkAsync(_deviceIdentity, timeout).ConfigureAwait(false);

                    _eventReceivingLink.Closed += (obj, arg) =>
                    {
                        amqpIoTSession.SafeClose();
                    };
                    _eventReceivingLink.RegisterEventListener(OnEventsReceived);
                    if (Logging.IsEnabled)
                    {
                        Logging.Associate(this, this, _eventReceivingLink, $"{nameof(EnableEventReceiveAsync)}");
                    }
                }
            }
            finally
            {
                _eventReceivingLinkLock.Release();
                if (Logging.IsEnabled)
                {
                    Logging.Exit(this, timeout, $"{nameof(EnableEventReceiveAsync)}");
                }
            }
        }
Beispiel #4
0
        private async Task OpenTwinReceiverLinkAsync(AmqpIoTSession amqpIoTSession, string correlationIdSuffix, TimeSpan timeout)
        {
            if (_twinReceivingLink == null || _twinReceivingLink.IsClosing())
            {
                _twinReceivingLink = await amqpIoTSession.OpenTwinReceiverLinkAsync(_deviceIdentity, correlationIdSuffix, timeout).ConfigureAwait(false);

                _twinReceivingLink.Closed += (obj, arg) => {
                    amqpIoTSession.SafeClose();
                };
                _twinReceivingLink.RegisterTwinListener(OnDesiredPropertyReceived);
                if (Logging.IsEnabled)
                {
                    Logging.Associate(this, _twinReceivingLink, $"{nameof(_twinReceivingLink)}");
                }
            }
        }
Beispiel #5
0
        private async Task OpenMethodsReceiverLinkAsync(AmqpIoTSession amqpIoTSession, string correlationIdSuffix, TimeSpan timeout)
        {
            if (_methodReceivingLink == null || _methodReceivingLink.IsClosing())
            {
                _methodReceivingLink?.SafeClose();

                _methodReceivingLink = await amqpIoTSession.OpenMethodsReceiverLinkAsync(_deviceIdentity, correlationIdSuffix, timeout).ConfigureAwait(false);

                _methodReceivingLink.Closed += (obj, arg) =>
                {
                    amqpIoTSession.SafeClose();
                };
                _methodReceivingLink.RegisterMethodListener(OnMethodReceived);
                Logging.Associate(this, _methodReceivingLink, nameof(_methodReceivingLink));
            }
        }
Beispiel #6
0
        private async Task EnsureMessageReceivingLinkIsOpenAsync(TimeSpan timeout, bool enableCallback = false)
        {
            if (_closed)
            {
                throw new IotHubException("Device is now offline.", false);
            }

            Logging.Enter(this, timeout, nameof(EnsureMessageReceivingLinkIsOpenAsync));

            AmqpIoTSession amqpIoTSession = await EnsureSessionIsOpenAsync(timeout).ConfigureAwait(false);

            bool enteredSemaphore = await _messageReceivingLinkSemaphore.WaitAsync(timeout).ConfigureAwait(false);

            if (!enteredSemaphore)
            {
                throw new TimeoutException("Failed to enter the semaphore required for ensuring that AMQP message receiver links are open.");
            }

            try
            {
                if (_messageReceivingLink == null || _messageReceivingLink.IsClosing())
                {
                    _messageReceivingLink?.SafeClose();

                    _messageReceivingLink = await amqpIoTSession.OpenMessageReceiverLinkAsync(_deviceIdentity, timeout).ConfigureAwait(false);

                    _messageReceivingLink.Closed += (obj, arg) =>
                    {
                        amqpIoTSession.SafeClose();
                    };
                    Logging.Associate(this, this, _messageReceivingLink, nameof(EnsureMessageReceivingLinkIsOpenAsync));
                }

                if (enableCallback)
                {
                    _messageReceivingLink.RegisterReceiveMessageListener(OnDeviceMessageReceived);
                }
            }
            finally
            {
                _messageReceivingLinkSemaphore.Release();
                Logging.Exit(this, timeout, nameof(EnsureMessageReceivingLinkIsOpenAsync));
            }
        }
Beispiel #7
0
        private async Task EnsureReceivingLinkIsOpenedAsync(TimeSpan timeout)
        {
            if (Volatile.Read(ref _messageReceivingLink) != null)
            {
                return;
            }

            if (Logging.IsEnabled)
            {
                Logging.Enter(this, timeout, $"{nameof(EnsureReceivingLinkIsOpenedAsync)}");
            }

            try
            {
                await _messageReceivingLinkLock.WaitAsync().ConfigureAwait(false);

                if (_messageReceivingLink != null)
                {
                    return;
                }

                _messageReceivingLink = await _amqpIoTSession.OpenTelemetryReceiverLinkAsync(
                    _deviceIdentity,
                    timeout
                    ).ConfigureAwait(false);

                _messageReceivingLink.Closed += OnLinkDisconnected;
                if (Logging.IsEnabled)
                {
                    Logging.Associate(this, this, _messageReceivingLink, $"{nameof(EnsureReceivingLinkIsOpenedAsync)}");
                }
            }
            finally
            {
                _messageReceivingLinkLock.Release();
                if (Logging.IsEnabled)
                {
                    Logging.Exit(this, timeout, $"{nameof(EnsureReceivingLinkIsOpenedAsync)}");
                }
            }
        }
Beispiel #8
0
        public async Task DisableTwinLinksAsync(TimeSpan timeout)
        {
            Logging.Enter(this, timeout, nameof(DisableTwinLinksAsync));

            Debug.Assert(_twinSendingLink != null);
            Debug.Assert(_twinReceivingLink != null);

            bool enteredSemaphore = await _twinLinksSemaphore.WaitAsync(timeout).ConfigureAwait(false);

            if (!enteredSemaphore)
            {
                throw new TimeoutException("Failed to enter the semaphore required for ensuring that AMQP twin sender and receiver links are closed.");
            }

            try
            {
                ICollection <Task> tasks = new List <Task>();
                if (_twinReceivingLink != null)
                {
                    tasks.Add(_twinReceivingLink.CloseAsync(timeout));
                }

                if (_twinSendingLink != null)
                {
                    tasks.Add(_twinSendingLink.CloseAsync(timeout));
                }

                if (tasks.Count > 0)
                {
                    await Task.WhenAll(tasks).ConfigureAwait(false);

                    _twinReceivingLink = null;
                    _twinSendingLink   = null;
                }
            }
            finally
            {
                Logging.Exit(this, timeout, nameof(DisableTwinLinksAsync));
                _twinLinksSemaphore.Release();
            }
        }
Beispiel #9
0
        public async Task DisableMethodsAsync(TimeSpan timeout)
        {
            if (Logging.IsEnabled)
            {
                Logging.Enter(this, timeout, $"{nameof(DisableMethodsAsync)}");
            }

            Debug.Assert(_methodSendingLink != null);
            Debug.Assert(_methodReceivingLink != null);

            try
            {
                ICollection <Task> tasks = new List <Task>();
                if (_methodReceivingLink != null)
                {
                    tasks.Add(_methodReceivingLink.CloseAsync(timeout));
                }

                if (_methodSendingLink != null)
                {
                    tasks.Add(_methodSendingLink.CloseAsync(timeout));
                }

                if (tasks.Count > 0)
                {
                    await Task.WhenAll(tasks).ConfigureAwait(false);

                    _methodReceivingLink = null;
                    _methodSendingLink   = null;
                }
            }
            finally
            {
                if (Logging.IsEnabled)
                {
                    Logging.Exit(this, timeout, $"{nameof(DisableMethodsAsync)}");
                }
            }
        }
Beispiel #10
0
        public async Task EnsureTwinLinksAreOpenedAsync(TimeSpan timeout)
        {
            if (Volatile.Read(ref _twinLinksOpened) == true)
            {
                return;
            }
            if (Logging.IsEnabled)
            {
                Logging.Enter(this, timeout, $"{nameof(EnsureTwinLinksAreOpenedAsync)}");
            }

            try
            {
                await _twinLinksLock.WaitAsync().ConfigureAwait(false);

                if (_twinLinksOpened)
                {
                    return;
                }

                Debug.Assert(_twinSendingLink == null);
                Debug.Assert(_twinReceivingLink == null);

                string correlationIdSuffix = Guid.NewGuid().ToString();

                Task <AmqpIoTReceivingLink> receiveLinkCreator = _amqpIoTSession.OpenTwinReceiverLinkAsync(_deviceIdentity, correlationIdSuffix, timeout);
                Task <AmqpIoTSendingLink>   sendingLinkCreator = _amqpIoTSession.OpenTwinSenderLinkAsync(_deviceIdentity, correlationIdSuffix, timeout);
                await Task.WhenAll(receiveLinkCreator, sendingLinkCreator).ConfigureAwait(false);

                _twinSendingLink         = sendingLinkCreator.Result;
                _twinSendingLink.Closed += OnLinkDisconnected;

                _twinReceivingLink = receiveLinkCreator.Result;
                _twinReceivingLink.RegisterTwinListener(OnDesiredPropertyReceived);
                _twinReceivingLink.Closed += OnLinkDisconnected;

                _twinLinksOpened = true;

                if (Logging.IsEnabled)
                {
                    Logging.Associate(this, this, _twinReceivingLink, $"{nameof(EnsureTwinLinksAreOpenedAsync)}");
                }
                if (Logging.IsEnabled)
                {
                    Logging.Associate(this, this, _twinSendingLink, $"{nameof(EnsureTwinLinksAreOpenedAsync)}");
                }
            }
            catch (Exception ex) when(!ex.IsFatal())
            {
                _twinReceivingLink?.Abort();
                _twinSendingLink?.Abort();
                _twinReceivingLink = null;
                _twinSendingLink   = null;

                throw;
            }
            finally
            {
                _twinLinksLock.Release();
                if (Logging.IsEnabled)
                {
                    Logging.Exit(this, timeout, $"{nameof(EnsureTwinLinksAreOpenedAsync)}");
                }
            }
        }