public async Task OpenAsync(TimeSpan timeout) { if (Logging.IsEnabled) Logging.Enter(this, timeout, $"{nameof(OpenAsync)}"); try { Debug.Assert(_amqpSession == null); Debug.Assert(IsUsable()); _amqpSession = await _amqpSessionCreator.Invoke( _deviceIdentity, AmqpLinkFactory.GetInstance(), _amqpSessionSettings, timeout).ConfigureAwait(false); if (Logging.IsEnabled) Logging.Associate(this, _amqpSession, $"{nameof(_amqpSession)}"); await _amqpSession.OpenAsync(timeout).ConfigureAwait(false); if (_deviceIdentity.AuthenticationModel == AuthenticationModel.SasIndividual) { _amqpAuthenticationRefresher = await _amqpAuthenticationRefresherCreator.Invoke(_deviceIdentity, timeout).ConfigureAwait(false); if (Logging.IsEnabled) Logging.Associate(this, _amqpAuthenticationRefresher, $"{nameof(_amqpAuthenticationRefresher)}"); } _amqpSession.Closed += OnSessionDisconnected; _messageSendingLink = await AmqpLinkHelper.OpenTelemetrySenderLinkAsync( _deviceIdentity, _amqpSession, timeout).ConfigureAwait(false); _messageSendingLink.Closed += OnLinkDisconnected; if (Logging.IsEnabled) Logging.Associate(this, _messageSendingLink, $"{nameof(_messageSendingLink)}"); } catch (Exception ex) when (!ex.IsFatal()) { if (SetNotUsable() == 0) { OnUnitDisconnected?.Invoke(false, EventArgs.Empty); } throw; } finally { if (Logging.IsEnabled) Logging.Exit(this, timeout, $"{nameof(OpenAsync)}"); } }
public async Task <IAmqpAuthenticationRefresher> CreateRefresherAsync(DeviceIdentity deviceIdentity, TimeSpan timeout) { if (Logging.IsEnabled) { Logging.Enter(this, deviceIdentity, timeout, $"{nameof(CreateRefresherAsync)}"); } AmqpIoTConnection amqpIoTConnection = await EnsureConnectionAsync(timeout).ConfigureAwait(false); IAmqpAuthenticationRefresher amqpAuthenticator = await amqpIoTConnection.CreateRefresherAsync(deviceIdentity, timeout).ConfigureAwait(false); if (Logging.IsEnabled) { Logging.Exit(this, deviceIdentity, timeout, $"{nameof(CreateRefresherAsync)}"); } return(amqpAuthenticator); }
public async Task <IAmqpAuthenticationRefresher> CreateRefresherAsync(IDeviceIdentity deviceIdentity, CancellationToken cancellationToken) { if (Logging.IsEnabled) { Logging.Enter(this, deviceIdentity, nameof(CreateRefresherAsync)); } AmqpIotConnection amqpIotConnection = await EnsureConnectionAsync(cancellationToken).ConfigureAwait(false); IAmqpAuthenticationRefresher amqpAuthenticator = await amqpIotConnection .CreateRefresherAsync(deviceIdentity, cancellationToken) .ConfigureAwait(false); if (Logging.IsEnabled) { Logging.Exit(this, deviceIdentity, nameof(CreateRefresherAsync)); } return(amqpAuthenticator); }
public async Task <AmqpIotConnection> EnsureConnectionAsync(TimeSpan timeout) { if (Logging.IsEnabled) { Logging.Enter(this, timeout, nameof(EnsureConnectionAsync)); } AmqpIotConnection amqpIotConnection = null; IAmqpAuthenticationRefresher amqpAuthenticationRefresher = null; bool gain = await _lock.WaitAsync(timeout).ConfigureAwait(false); if (!gain) { throw new TimeoutException(); } try { if (_amqpIotConnection == null || _amqpIotConnection.IsClosing()) { if (Logging.IsEnabled) { Logging.Info(this, "Creating new AmqpConnection", nameof(EnsureConnectionAsync)); } // Create AmqpConnection amqpIotConnection = await _amqpIotConnector.OpenConnectionAsync(timeout).ConfigureAwait(false); if (_deviceIdentity.AuthenticationModel != AuthenticationModel.X509) { if (_deviceIdentity.AuthenticationModel == AuthenticationModel.SasGrouped) { if (Logging.IsEnabled) { Logging.Info(this, "Creating connection width AmqpAuthenticationRefresher", nameof(EnsureConnectionAsync)); } amqpAuthenticationRefresher = new AmqpAuthenticationRefresher(_deviceIdentity, amqpIotConnection.GetCbsLink()); await amqpAuthenticationRefresher.InitLoopAsync(timeout).ConfigureAwait(false); } } _amqpIotConnection = amqpIotConnection; _amqpAuthenticationRefresher = amqpAuthenticationRefresher; _amqpIotConnection.Closed += OnConnectionClosed; if (Logging.IsEnabled) { Logging.Associate(this, _amqpIotConnection, nameof(_amqpIotConnection)); } } else { amqpIotConnection = _amqpIotConnection; } } catch (Exception ex) when(!ex.IsFatal()) { amqpAuthenticationRefresher?.StopLoop(); amqpIotConnection?.SafeClose(); throw; } finally { _lock.Release(); } if (Logging.IsEnabled) { Logging.Exit(this, timeout, nameof(EnsureConnectionAsync)); } return(amqpIotConnection); }
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); }
internal async Task <AmqpIotSession> EnsureSessionIsOpenAsync(TimeSpan timeout) { if (_closed) { throw new IotHubException("Device is now offline.", false); } Logging.Enter(this, timeout, nameof(EnsureSessionIsOpenAsync)); bool enteredSemaphore = await _sessionSemaphore.WaitAsync(timeout).ConfigureAwait(false); if (!enteredSemaphore) { throw new TimeoutException("Failed to enter the semaphore required for opening an AMQP session."); } try { if (_amqpIotSession == null || _amqpIotSession.IsClosing()) { _amqpIotSession?.SafeClose(); _amqpIotSession = await _amqpConnectionHolder.OpenSessionAsync(_deviceIdentity, timeout).ConfigureAwait(false); Logging.Associate(this, _amqpIotSession, nameof(_amqpIotSession)); if (_deviceIdentity.AuthenticationModel == AuthenticationModel.SasIndividual) { _amqpAuthenticationRefresher = await _amqpConnectionHolder.CreateRefresherAsync(_deviceIdentity, timeout).ConfigureAwait(false); Logging.Associate(this, _amqpAuthenticationRefresher, nameof(_amqpAuthenticationRefresher)); } _amqpIotSession.Closed += OnSessionDisconnected; _messageSendingLink = await _amqpIotSession.OpenTelemetrySenderLinkAsync(_deviceIdentity, timeout).ConfigureAwait(false); _messageSendingLink.Closed += (obj, arg) => { _amqpIotSession.SafeClose(); }; Logging.Associate(this, _messageSendingLink, nameof(_messageSendingLink)); } if (_disposed) { throw new IotHubException("Device is now offline.", false); } } catch (Exception) { Cleanup(); throw; } finally { _sessionSemaphore.Release(); } Logging.Exit(this, timeout, nameof(EnsureSessionIsOpenAsync)); return(_amqpIotSession); }
private async Task <AmqpConnection> EnsureConnection(TimeSpan timeout) { if (Logging.IsEnabled) { Logging.Enter(this, timeout, $"{nameof(EnsureConnection)}"); } AmqpConnection amqpConnection = null; IAmqpAuthenticationRefresher amqpAuthenticationRefresher = null; AmqpCbsLink amqpCbsLink = null; bool gain = await Lock.WaitAsync(timeout).ConfigureAwait(false); if (!gain) { throw new TimeoutException(); } try { if (AmqpConnection == null) { if (Logging.IsEnabled) { Logging.Info(this, "Creating new AmqpConnection", $"{nameof(EnsureConnection)}"); } // Create AmqpConnection amqpConnection = await Connector.OpenConnectionAsync(timeout).ConfigureAwait(false); if (DeviceIdentity.AuthenticationModel != AuthenticationModel.X509) { if (AmqpCbsLink == null) { if (Logging.IsEnabled) { Logging.Info(this, "Creating new AmqpCbsLink", $"{nameof(EnsureConnection)}"); } amqpCbsLink = new AmqpCbsLink(amqpConnection); } else { amqpCbsLink = AmqpCbsLink; } if (DeviceIdentity.AuthenticationModel == AuthenticationModel.SasGrouped) { if (Logging.IsEnabled) { Logging.Info(this, "Creating connection width AmqpAuthenticationRefresher", $"{nameof(EnsureConnection)}"); } amqpAuthenticationRefresher = new AmqpAuthenticationRefresher(DeviceIdentity, amqpCbsLink); await amqpAuthenticationRefresher.InitLoopAsync(timeout).ConfigureAwait(false); } } AmqpConnection = amqpConnection; AmqpCbsLink = amqpCbsLink; AmqpAuthenticationRefresher = amqpAuthenticationRefresher; AmqpConnection.Closed += OnConnectionClosed; if (Logging.IsEnabled) { Logging.Associate(this, AmqpConnection, $"{nameof(AmqpConnection)}"); } if (Logging.IsEnabled) { Logging.Associate(this, AmqpCbsLink, $"{nameof(AmqpCbsLink)}"); } } else if (AmqpConnection.IsClosing()) { throw new IotHubCommunicationException("AMQP connection is closing."); } else { amqpConnection = AmqpConnection; } } catch (Exception ex) when(!ex.IsFatal()) { amqpCbsLink?.Close(); amqpAuthenticationRefresher?.StopLoop(); amqpConnection?.SafeClose(); throw; } finally { Lock.Release(); } if (Logging.IsEnabled) { Logging.Exit(this, timeout, $"{nameof(EnsureConnection)}"); } return(amqpConnection); }