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)}"); } } }
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)}"); } } }
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)); } }
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)); } }