public async Task <AmqpObject> CreateAndOpenAmqpLinkAsync() { TimeoutHelper timeoutHelper = new TimeoutHelper(this.serviceBusConnection.OperationTimeout); AmqpConnection connection = await this.serviceBusConnection.ConnectionManager.GetOrCreateAsync(timeoutHelper.RemainingTime()).ConfigureAwait(false); // Authenticate over CBS AmqpCbsLink cbsLink = connection.Extensions.Find <AmqpCbsLink>(); Uri address = new Uri(this.serviceBusConnection.Endpoint, this.entityPath); string audience = address.AbsoluteUri; string resource = address.AbsoluteUri; await cbsLink.SendTokenAsync(this.cbsTokenProvider, address, audience, resource, this.requiredClaims, timeoutHelper.RemainingTime()).ConfigureAwait(false); AmqpSession session = null; try { // Create our Session AmqpSessionSettings sessionSettings = new AmqpSessionSettings { Properties = new Fields() }; session = connection.CreateSession(sessionSettings); await session.OpenAsync(timeoutHelper.RemainingTime()).ConfigureAwait(false); // Create our Link AmqpObject link = this.OnCreateAmqpLink(connection, this.amqpLinkSettings, session); await link.OpenAsync(timeoutHelper.RemainingTime()).ConfigureAwait(false); return(link); } catch (Exception) { session?.Abort(); throw; } }
private async Task <Controller> CreateControllerAsync(TimeSpan timeout) { var timeoutHelper = new TimeoutHelper(timeout, true); AmqpConnection connection = await ActiveConnection.GetOrCreateAsync(timeoutHelper.RemainingTime()).ConfigureAwait(false); var sessionSettings = new AmqpSessionSettings { Properties = new Fields() }; AmqpSession amqpSession = null; Controller controller; try { amqpSession = connection.CreateSession(sessionSettings); await amqpSession.OpenAsync(timeoutHelper.RemainingTime()).ConfigureAwait(false); controller = new Controller(amqpSession, timeoutHelper.RemainingTime()); await controller.OpenAsync(timeoutHelper.RemainingTime()).ConfigureAwait(false); } catch (Exception exception) { if (amqpSession != null) { await amqpSession.CloseAsync(timeout).ConfigureAwait(false); } MessagingEventSource.Log.AmqpCreateControllerException(ActiveConnection.ToString(), exception); throw; } return(controller); }
private async Task <Controller> CreateControllerAsync(TimeSpan timeout) { var stopWatch = ValueStopwatch.StartNew(); AmqpConnection connection = await ActiveConnection.GetOrCreateAsync(timeout.CalculateRemaining(stopWatch.GetElapsedTime())).ConfigureAwait(false); var sessionSettings = new AmqpSessionSettings { Properties = new Fields() }; AmqpSession amqpSession = null; Controller controller; try { amqpSession = connection.CreateSession(sessionSettings); await amqpSession.OpenAsync(timeout.CalculateRemaining(stopWatch.GetElapsedTime())).ConfigureAwait(false); controller = new Controller(amqpSession, timeout.CalculateRemaining(stopWatch.GetElapsedTime())); await controller.OpenAsync(timeout.CalculateRemaining(stopWatch.GetElapsedTime())).ConfigureAwait(false); } catch (Exception exception) { if (amqpSession != null) { await amqpSession.CloseAsync(timeout).ConfigureAwait(false); } ServiceBusEventSource.Log.CreateControllerException(ActiveConnection.ToString(), exception.ToString()); throw; } return(controller); }
public async Task <Tuple <AmqpObject, DateTime> > CreateAndOpenAmqpLinkAsync() { var timeoutHelper = new TimeoutHelper(this.serviceBusConnection.OperationTimeout); MessagingEventSource.Log.AmqpGetOrCreateConnectionStart(); var amqpConnection = await this.serviceBusConnection.ConnectionManager.GetOrCreateAsync(timeoutHelper.RemainingTime()).ConfigureAwait(false); MessagingEventSource.Log.AmqpGetOrCreateConnectionStop(this.entityPath, amqpConnection.ToString(), amqpConnection.State.ToString()); // Authenticate over CBS var cbsLink = amqpConnection.Extensions.Find <AmqpCbsLink>(); var resource = this.endpointAddress.AbsoluteUri; MessagingEventSource.Log.AmqpSendAuthenticationTokenStart(this.endpointAddress, resource, resource, this.requiredClaims); var cbsTokenExpiresAtUtc = await cbsLink.SendTokenAsync(this.cbsTokenProvider, this.endpointAddress, resource, resource, this.requiredClaims, timeoutHelper.RemainingTime()).ConfigureAwait(false); MessagingEventSource.Log.AmqpSendAuthenticationTokenStop(); AmqpSession session = null; try { // Create Session var amqpSessionSettings = new AmqpSessionSettings { Properties = new Fields() }; session = amqpConnection.CreateSession(amqpSessionSettings); await session.OpenAsync(timeoutHelper.RemainingTime()).ConfigureAwait(false); } catch (Exception exception) { MessagingEventSource.Log.AmqpSessionCreationException(this.entityPath, amqpConnection, exception); session?.Abort(); throw AmqpExceptionHelper.GetClientException(exception, null, session.GetInnerException()); } AmqpObject link = null; try { // Create Link link = this.OnCreateAmqpLink(amqpConnection, this.amqpLinkSettings, session); await link.OpenAsync(timeoutHelper.RemainingTime()).ConfigureAwait(false); return(new Tuple <AmqpObject, DateTime>(link, cbsTokenExpiresAtUtc)); } catch (Exception exception) { MessagingEventSource.Log.AmqpLinkCreationException( this.entityPath, session, amqpConnection, exception); session.SafeClose(exception); throw AmqpExceptionHelper.GetClientException(exception, null, link?.GetInnerException(), session.IsClosing()); } }
internal async Task <ActiveClientRequestResponseLink> OpenRequestResponseLinkAsync( string type, string address, MessagingEntityType?entityType, string[] requiredClaims, TimeSpan timeout) { var timeoutHelper = new TimeoutHelper(timeout, true); AmqpSession session = null; try { // Don't need to get token for namespace scope operations, included in request bool isNamespaceScope = address.Equals(AmqpClientConstants.ManagementAddress, StringComparison.OrdinalIgnoreCase); var connection = await this.ConnectionManager.GetOrCreateAsync(timeoutHelper.RemainingTime()); var sessionSettings = new AmqpSessionSettings { Properties = new Fields() }; //sessionSettings.Properties[AmqpClientConstants.BatchFlushIntervalName] = (uint)batchFlushInterval.TotalMilliseconds; session = connection.CreateSession(sessionSettings); await session.OpenAsync(timeoutHelper.RemainingTime()); var linkSettings = new AmqpLinkSettings(); linkSettings.AddProperty(AmqpClientConstants.TimeoutName, (uint)timeoutHelper.RemainingTime().TotalMilliseconds); if (entityType != null) { linkSettings.AddProperty(AmqpClientConstants.EntityTypeName, (int)entityType.Value); } // Create the link var link = new RequestResponseAmqpLink(type, session, address, linkSettings.Properties); var authorizationValidToUtc = DateTime.MaxValue; if (!isNamespaceScope) { // TODO: Get Entity level token here } await link.OpenAsync(timeoutHelper.RemainingTime()); // Redirected scenario requires entityPath as the audience, otherwise we // should always use the full EndpointUri as audience. return(new ActiveClientRequestResponseLink( link, this.ConnectionStringBuilder.Endpoint.AbsoluteUri, // audience this.ConnectionStringBuilder.Endpoint.AbsoluteUri, // endpointUri requiredClaims, false, authorizationValidToUtc)); } catch (Exception) { // Aborting the session will cleanup the link as well. session?.Abort(); throw; } }
public async Task OpenAsync(TimeSpan timeout) { // Create the Session var amqpTestLinkFactory = new AmqpLinkFactory(); amqpTestLinkFactory.LinkCreated += OnLinkCreated; AmqpSession = new AmqpSession(_amqpConnection.AmqpConnection, AmqpSessionSettings, amqpTestLinkFactory); _amqpConnection.AmqpConnection.AddSession(AmqpSession, new ushort?()); AmqpSession.Closed += OnSessionClosed; await AmqpSession.OpenAsync(timeout).ConfigureAwait(false); _isSessionClosed = false; }
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)}"); } }
async Task RunClientAsync(string address) { AmqpConnectionFactory factory = new AmqpConnectionFactory(); factory.Settings.TransportProviders.Add( new TlsTransportProvider( new TlsTransportSettings() { CertificateValidationCallback = (a, b, c, d) => true }, AmqpVersion.V100)); AmqpConnection connection = await factory.OpenConnectionAsync(address); AmqpSession session = connection.CreateSession(new AmqpSessionSettings()); await session.OpenAsync(TimeSpan.FromSeconds(20)); SendingAmqpLink sLink = new SendingAmqpLink(session, AmqpUtils.GetLinkSettings(true, queue, SettleMode.SettleOnSend)); await sLink.OpenAsync(TimeSpan.FromSeconds(20)); AmqpMessage message = AmqpMessage.Create(new AmqpValue() { Value = "AmqpConnectionFactoryTest" }); Outcome outcome = await sLink.SendMessageAsync(message, EmptyBinary, NullBinary, TimeSpan.FromSeconds(10)); Assert.Equal(Accepted.Code, outcome.DescriptorCode); ReceivingAmqpLink rLink = new ReceivingAmqpLink(session, AmqpUtils.GetLinkSettings(false, queue, SettleMode.SettleOnDispose, 10)); await rLink.OpenAsync(TimeSpan.FromSeconds(20)); var receivedMessage = await rLink.ReceiveMessageAsync(TimeSpan.FromSeconds(20)); Assert.NotNull(receivedMessage); outcome = await rLink.DisposeMessageAsync(receivedMessage.DeliveryTag, new Accepted(), false, TimeSpan.FromSeconds(20)); Assert.Equal(Accepted.Code, outcome.DescriptorCode); await connection.CloseAsync(TimeSpan.FromSeconds(20)); }
internal async Task <AmqpIotSession> OpenSessionAsync(TimeSpan timeout) { if (_amqpConnection.IsClosing()) { throw new IotHubCommunicationException("Amqp connection is disconnected."); } var amqpSessionSettings = new AmqpSessionSettings { Properties = new Fields(), }; try { var amqpSession = new AmqpSession(_amqpConnection, amqpSessionSettings, AmqpIotLinkFactory.GetInstance()); _amqpConnection.AddSession(amqpSession, new ushort?()); await amqpSession.OpenAsync(timeout).ConfigureAwait(false); return(new AmqpIotSession(amqpSession)); } catch (Exception e) when(!e.IsFatal()) { Exception ex = AmqpIotExceptionAdapter.ConvertToIotHubException(e, _amqpConnection); if (ReferenceEquals(e, ex)) { throw; } else { if (ex is AmqpIotResourceException) { _amqpConnection.SafeClose(); throw new IotHubCommunicationException(ex.Message, ex); } throw ex; } } }
private async Task <SendingAmqpLink> CreateLinkAsync(TimeSpan timeout) { var amqpEventHubClient = ((AmqpEventHubClient)EventHubClient); var timeoutHelper = new TimeoutHelper(timeout); AmqpConnection connection = await amqpEventHubClient.ConnectionManager.GetOrCreateAsync(timeoutHelper.RemainingTime()).ConfigureAwait(false); // Authenticate over CBS AmqpCbsLink cbsLink = connection.Extensions.Find <AmqpCbsLink>(); ICbsTokenProvider cbsTokenProvider = amqpEventHubClient.CbsTokenProvider; Uri address = new Uri(amqpEventHubClient.ConnectionStringBuilder.Endpoint, Path); string audience = address.AbsoluteUri; string resource = address.AbsoluteUri; DateTime expiresAt = await cbsLink.SendTokenAsync( cbsTokenProvider, address, audience, resource, new[] { ClaimConstants.Send }, timeoutHelper.RemainingTime()).ConfigureAwait(false); AmqpSession session = null; try { // Create our Session var sessionSettings = new AmqpSessionSettings { Properties = new Fields() }; session = connection.CreateSession(sessionSettings); await session.OpenAsync(timeoutHelper.RemainingTime()).ConfigureAwait(false); // Create our Link var linkSettings = new AmqpLinkSettings(); linkSettings.AddProperty(AmqpClientConstants.TimeoutName, (uint)timeoutHelper.RemainingTime().TotalMilliseconds); linkSettings.AddProperty(AmqpClientConstants.EntityTypeName, (int)MessagingEntityType.EventHub); linkSettings.Role = false; linkSettings.InitialDeliveryCount = 0; linkSettings.Target = new Target { Address = address.AbsolutePath }; linkSettings.Source = new Source { Address = ClientId }; var link = new SendingAmqpLink(linkSettings); linkSettings.LinkName = $"{amqpEventHubClient.ContainerId};{connection.Identifier}:{session.Identifier}:{link.Identifier}"; link.AttachTo(session); await link.OpenAsync(timeoutHelper.RemainingTime()).ConfigureAwait(false); var activeClientLink = new ActiveClientLink( link, audience, // audience EventHubClient.ConnectionStringBuilder.Endpoint.AbsoluteUri, // endpointUri new[] { ClaimConstants.Send }, true, expiresAt); MaxMessageSize = (long)activeClientLink.Link.Settings.MaxMessageSize(); _clientLinkManager.SetActiveLink(activeClientLink); _linkCreated = true; return(link); } catch { // Cleanup any session (and thus link) in case of exception. session?.Abort(); throw; } }
async Task <ReceivingAmqpLink> CreateLinkAsync(TimeSpan timeout) { var amqpEventHubClient = ((AmqpEventHubClient)this.EventHubClient); var timeoutHelper = new TimeoutHelper(timeout); AmqpConnection connection = await amqpEventHubClient.ConnectionManager.GetOrCreateAsync(timeoutHelper.RemainingTime()).ConfigureAwait(false); // Authenticate over CBS var cbsLink = connection.Extensions.Find <AmqpCbsLink>(); ICbsTokenProvider cbsTokenProvider = amqpEventHubClient.CbsTokenProvider; Uri address = new Uri(amqpEventHubClient.ConnectionStringBuilder.Endpoint, this.Path); string audience = address.AbsoluteUri; string resource = address.AbsoluteUri; var expiresAt = await cbsLink.SendTokenAsync(cbsTokenProvider, address, audience, resource, new[] { ClaimConstants.Listen }, timeoutHelper.RemainingTime()).ConfigureAwait(false); AmqpSession session = null; try { // Create our Session var sessionSettings = new AmqpSessionSettings { Properties = new Fields() }; session = connection.CreateSession(sessionSettings); await session.OpenAsync(timeoutHelper.RemainingTime()).ConfigureAwait(false); FilterSet filterMap = null; var filters = this.CreateFilters(); if (filters != null && filters.Count > 0) { filterMap = new FilterSet(); foreach (var filter in filters) { filterMap.Add(filter.DescriptorName, filter); } } // Create our Link var linkSettings = new AmqpLinkSettings { Role = true, TotalLinkCredit = (uint)this.PrefetchCount, AutoSendFlow = this.PrefetchCount > 0 }; linkSettings.AddProperty(AmqpClientConstants.EntityTypeName, (int)MessagingEntityType.ConsumerGroup); linkSettings.Source = new Source { Address = address.AbsolutePath, FilterSet = filterMap }; linkSettings.Target = new Target { Address = this.ClientId }; linkSettings.SettleType = SettleMode.SettleOnSend; // Receiver metrics enabled? if (this.ReceiverRuntimeMetricEnabled) { linkSettings.DesiredCapabilities = new Multiple <AmqpSymbol>(new List <AmqpSymbol> { AmqpClientConstants.EnableReceiverRuntimeMetricName }); } if (this.Epoch.HasValue) { linkSettings.AddProperty(AmqpClientConstants.AttachEpoch, this.Epoch.Value); } if (!string.IsNullOrWhiteSpace(this.Identifier)) { linkSettings.AddProperty(AmqpClientConstants.ReceiverIdentifierName, this.Identifier); } var link = new ReceivingAmqpLink(linkSettings); linkSettings.LinkName = $"{amqpEventHubClient.ContainerId};{connection.Identifier}:{session.Identifier}:{link.Identifier}"; link.AttachTo(session); await link.OpenAsync(timeoutHelper.RemainingTime()).ConfigureAwait(false); var activeClientLink = new ActiveClientLink( link, audience, // audience this.EventHubClient.ConnectionStringBuilder.Endpoint.AbsoluteUri, // endpointUri new[] { ClaimConstants.Listen }, true, expiresAt); this.clientLinkManager.SetActiveLink(activeClientLink); return(link); } catch { // Cleanup any session (and thus link) in case of exception. session?.SafeClose(); throw; } }
protected virtual async Task <AmqpSession> CreateSessionAsync(TimeSpan timeout, CancellationToken token) { try { if (Logging.IsEnabled) { Logging.Enter(this, timeout, token, $"{nameof(IotHubConnection)}.{nameof(CreateSessionAsync)}"); } this.OnCreateSession(); var timeoutHelper = new TimeoutHelper(timeout); AmqpSettings amqpSettings = CreateAmqpSettings(); TransportBase transport; token.ThrowIfCancellationRequested(); switch (this.AmqpTransportSettings.GetTransportType()) { case TransportType.Amqp_WebSocket_Only: transport = await this.CreateClientWebSocketTransportAsync(timeoutHelper.RemainingTime()).ConfigureAwait(false); break; case TransportType.Amqp_Tcp_Only: TlsTransportSettings tlsTransportSettings = this.CreateTlsTransportSettings(); var amqpTransportInitiator = new AmqpTransportInitiator(amqpSettings, tlsTransportSettings); transport = await amqpTransportInitiator.ConnectTaskAsync(timeoutHelper.RemainingTime()).ConfigureAwait(false); break; default: throw new InvalidOperationException("AmqpTransportSettings must specify WebSocketOnly or TcpOnly"); } var amqpConnectionSettings = new AmqpConnectionSettings() { MaxFrameSize = AmqpConstants.DefaultMaxFrameSize, ContainerId = Guid.NewGuid().ToString("N"), HostName = this.hostName }; var amqpConnection = new AmqpConnection(transport, amqpSettings, amqpConnectionSettings); try { token.ThrowIfCancellationRequested(); await amqpConnection.OpenAsync(timeoutHelper.RemainingTime()).ConfigureAwait(false); var sessionSettings = new AmqpSessionSettings() { Properties = new Fields() }; AmqpSession amqpSession = amqpConnection.CreateSession(sessionSettings); token.ThrowIfCancellationRequested(); await amqpSession.OpenAsync(timeoutHelper.RemainingTime()).ConfigureAwait(false); // This adds itself to amqpConnection.Extensions var cbsLink = new AmqpCbsLink(amqpConnection); return(amqpSession); } catch (Exception ex) when(!ex.IsFatal()) { if (amqpConnection.TerminalException != null) { throw AmqpClientHelper.ToIotHubClientContract(amqpConnection.TerminalException); } amqpConnection.SafeClose(ex); throw; } } finally { if (Logging.IsEnabled) { Logging.Exit(this, timeout, token, $"{nameof(IotHubConnection)}.{nameof(CreateSessionAsync)}"); } } }
private async Task <AmqpSession> CreateSessionAsync(TimeSpan timeout) { var timeoutHelper = new TimeoutHelper(timeout); _refreshTokenTimer.Cancel(); AmqpSettings amqpSettings = CreateAmqpSettings(); TransportBase transport; if (_useWebSocketOnly) { // Try only Amqp transport over WebSocket transport = await CreateClientWebSocketTransport(timeoutHelper.RemainingTime()).ConfigureAwait(false); } else { TlsTransportSettings tlsTransportSettings = CreateTlsTransportSettings(); var amqpTransportInitiator = new AmqpTransportInitiator(amqpSettings, tlsTransportSettings); try { transport = await amqpTransportInitiator.ConnectTaskAsync(timeoutHelper.RemainingTime()).ConfigureAwait(false); } #if !NETSTANDARD1_3 catch (AuthenticationException) { throw; } #endif catch (Exception e) { if (Fx.IsFatal(e)) { throw; } // Amqp transport over TCP failed. Retry Amqp transport over WebSocket if (timeoutHelper.RemainingTime() != TimeSpan.Zero) { transport = await CreateClientWebSocketTransport(timeoutHelper.RemainingTime()).ConfigureAwait(false); } else { throw; } } } var amqpConnectionSettings = new AmqpConnectionSettings { MaxFrameSize = AmqpConstants.DefaultMaxFrameSize, ContainerId = Guid.NewGuid() #if NETSTANDARD1_3 .ToString("N"), #else .ToString("N", CultureInfo.InvariantCulture), // Use a human readable link name to help with debugging #endif HostName = ConnectionString.AmqpEndpoint.Host, }; var amqpConnection = new AmqpConnection(transport, amqpSettings, amqpConnectionSettings); await amqpConnection.OpenAsync(timeoutHelper.RemainingTime()).ConfigureAwait(false); var sessionSettings = new AmqpSessionSettings { Properties = new Fields(), }; try { AmqpSession amqpSession = amqpConnection.CreateSession(sessionSettings); await amqpSession.OpenAsync(timeoutHelper.RemainingTime()).ConfigureAwait(false); // This adds itself to amqpConnection.Extensions var cbsLink = new AmqpCbsLink(amqpConnection); await SendCbsTokenAsync(cbsLink, timeoutHelper.RemainingTime()).ConfigureAwait(false); return(amqpSession); } catch (Exception ex) when(!ex.IsFatal()) { if (amqpConnection.TerminalException != null) { throw AmqpClientHelper.ToIotHubClientContract(amqpConnection.TerminalException); } amqpConnection.SafeClose(ex); throw; } }
protected override IEnumerator <IteratorTask <object> .TaskStep> GetTasks() { ConnectivityMode connectivityMode; object obj = null; bool flag = false; try { object thisLock = this.relay.ThisLock; object obj1 = thisLock; obj = thisLock; Monitor.Enter(obj1, ref flag); if (this.relay.State != AmqpObjectState.OpenReceived && this.relay.State != AmqpObjectState.Opened) { goto Label0; } } finally { if (flag) { Monitor.Exit(obj); } } Microsoft.ServiceBus.Common.TimeoutHelper timeoutHelper = new Microsoft.ServiceBus.Common.TimeoutHelper(this.timeout); string host = this.relay.serviceBusUri.Host; AmqpSettings amqpSetting = AmqpRelay.ConnectTask.CreateAmqpSettings(); connectivityMode = (this.relay.connectivitySettings != null ? this.relay.connectivitySettings.Mode : ServiceBusEnvironment.SystemConnectivity.Mode); ConnectivityMode connectivityMode1 = connectivityMode; if (connectivityMode1 == ConnectivityMode.Tcp) { TcpTransportSettings tcpTransportSetting = new TcpTransportSettings() { Host = host, Port = 5671 }; TlsTransportSettings tlsTransportSetting = new TlsTransportSettings(tcpTransportSetting) { TargetHost = host }; AmqpTransportInitiator amqpTransportInitiator = new AmqpTransportInitiator(amqpSetting, tlsTransportSetting); yield return(base.CallTask(amqpTransportInitiator.ConnectTaskAsync(timeoutHelper.RemainingTime()), IteratorTask <TResult> .ExceptionPolicy.Transfer)); } else if (connectivityMode1 == ConnectivityMode.Http || this.relay.httpConnectivitySettings != null) { WebSocketTransportSettings webSocketTransportSetting = new WebSocketTransportSettings(this.relay.serviceBusUri); AmqpTransportInitiator amqpTransportInitiator1 = new AmqpTransportInitiator(amqpSetting, webSocketTransportSetting); yield return(base.CallTask(amqpTransportInitiator1.ConnectTaskAsync(timeoutHelper.RemainingTime()), IteratorTask <TResult> .ExceptionPolicy.Transfer)); } else { TcpTransportSettings tcpTransportSetting1 = new TcpTransportSettings() { Host = host, Port = 5671 }; TlsTransportSettings tlsTransportSetting1 = new TlsTransportSettings(tcpTransportSetting1) { TargetHost = host }; AmqpTransportInitiator amqpTransportInitiator2 = new AmqpTransportInitiator(amqpSetting, tlsTransportSetting1); yield return(base.CallTask(amqpTransportInitiator2.ConnectTaskAsync(Microsoft.ServiceBus.Common.TimeoutHelper.Divide(timeoutHelper.RemainingTime(), 2)), IteratorTask <TResult> .ExceptionPolicy.Continue)); if (base.LastTask.Exception != null) { if (timeoutHelper.RemainingTime() == TimeSpan.Zero) { throw base.LastTask.Exception; } WebSocketTransportSettings webSocketTransportSetting1 = new WebSocketTransportSettings(this.relay.serviceBusUri); AmqpTransportInitiator amqpTransportInitiator3 = new AmqpTransportInitiator(amqpSetting, webSocketTransportSetting1); yield return(base.CallTask(amqpTransportInitiator3.ConnectTaskAsync(timeoutHelper.RemainingTime()), IteratorTask <TResult> .ExceptionPolicy.Transfer)); } } TransportBase transportBase = base.LastTaskResult <TransportBase>(); string[] strArrays = host.Split(new char[] { '.' }); strArrays[0] = string.Concat(strArrays[0], "-relay"); AmqpConnectionSettings amqpConnectionSetting = new AmqpConnectionSettings() { ContainerId = Guid.NewGuid().ToString(), HostName = string.Join(".", strArrays) }; this.relay.connection = new AmqpConnection(transportBase, amqpSetting, amqpConnectionSetting); yield return(base.CallTask(this.relay.connection.OpenAsync(timeoutHelper.RemainingTime()), IteratorTask <TResult> .ExceptionPolicy.Transfer)); AmqpSessionSettings amqpSessionSetting = new AmqpSessionSettings(); AmqpSession amqpSession = this.relay.connection.CreateSession(amqpSessionSetting); yield return(base.CallTask(amqpSession.OpenAsync(timeoutHelper.RemainingTime()), IteratorTask <TResult> .ExceptionPolicy.Transfer)); AmqpLinkSettings amqpLinkSetting = new AmqpLinkSettings() { Role = new bool?(false), InitialDeliveryCount = new uint?(0), LinkName = string.Concat("HttpRelayServer_Link_", Guid.NewGuid()), Target = new Target(this.relay.serviceBusUri), Source = new Source(this.relay.serviceBusUri), TotalLinkCredit = 1000, AutoSendFlow = true }; AmqpLinkSettings amqpLinkSetting1 = amqpLinkSetting; if (this.relay.tokenRenewer != null) { amqpLinkSetting1.AddProperty(AmqpConstants.SimpleWebTokenPropertyName, this.relay.tokenRenewer.CurrentToken.Token); } if (!this.relay.TransportSecurityRequired) { amqpLinkSetting1.AddProperty(ClientConstants.TransportSecurityRequiredName, false); } if (!this.relay.RelayClientAuthorizationRequired) { amqpLinkSetting1.AddProperty(ClientConstants.ClientAuthenticationRequiredName, false); } if (this.relay.PublishToRegistry) { amqpLinkSetting1.AddProperty(ClientConstants.RequiresPublicRegistry, true); } if (!string.IsNullOrEmpty(this.relay.ClientAgent)) { amqpLinkSetting1.AddProperty(ClientConstants.ClientAgent, this.relay.ClientAgent); } if (!string.IsNullOrEmpty(this.relay.DisplayName)) { amqpLinkSetting1.AddProperty(ClientConstants.DisplayName, this.relay.DisplayName); } amqpLinkSetting1.AddProperty(ClientConstants.DynamicRelay, this.relay.IsDynamic); amqpLinkSetting1.AddProperty(ClientConstants.ListenerTypeName, this.relay.ListenerType.ToString()); this.relay.link = new DuplexAmqpLink(amqpSession, amqpLinkSetting1); yield return(base.CallTask(this.relay.link.OpenAsync(timeoutHelper.RemainingTime()), IteratorTask <TResult> .ExceptionPolicy.Transfer)); this.relay.link.SafeAddClosed(this.relay.onAmqpObjectClosed); this.relay.link.RegisterMessageListener((AmqpMessage msg) => this.relay.messageListener(this.relay.link, msg)); this.relay.OnOnline(); Label0: yield break; }
protected virtual async Task <AmqpSession> CreateSessionAsync(TimeSpan timeout) { this.OnCreateSession(); var timeoutHelper = new TimeoutHelper(timeout); AmqpSettings amqpSettings = CreateAmqpSettings(); TransportBase transport; switch (this.AmqpTransportSettings.GetTransportType()) { #if !WINDOWS_UWP case TransportType.Amqp_WebSocket_Only: transport = await this.CreateClientWebSocketTransportAsync(timeoutHelper.RemainingTime()); break; #endif case TransportType.Amqp_Tcp_Only: TlsTransportSettings tlsTransportSettings = this.CreateTlsTransportSettings(); var amqpTransportInitiator = new AmqpTransportInitiator(amqpSettings, tlsTransportSettings); transport = await amqpTransportInitiator.ConnectTaskAsync(timeoutHelper.RemainingTime()); break; default: throw new InvalidOperationException("AmqpTransportSettings must specify WebSocketOnly or TcpOnly"); } var amqpConnectionSettings = new AmqpConnectionSettings() { MaxFrameSize = AmqpConstants.DefaultMaxFrameSize, ContainerId = Guid.NewGuid().ToString("N"), HostName = this.hostName }; var amqpConnection = new AmqpConnection(transport, amqpSettings, amqpConnectionSettings); try { await amqpConnection.OpenAsync(timeoutHelper.RemainingTime()); var sessionSettings = new AmqpSessionSettings() { Properties = new Fields() }; AmqpSession amqpSession = amqpConnection.CreateSession(sessionSettings); await amqpSession.OpenAsync(timeoutHelper.RemainingTime()); // This adds itself to amqpConnection.Extensions var cbsLink = new AmqpCbsLink(amqpConnection); return(amqpSession); } catch (Exception ex) when(!ex.IsFatal()) { if (amqpConnection.TerminalException != null) { throw AmqpClientHelper.ToIotHubClientContract(amqpConnection.TerminalException); } amqpConnection.SafeClose(ex); throw; } }
public async Task <Tuple <AmqpObject, DateTime> > CreateAndOpenAmqpLinkAsync() { var timeoutHelper = new TimeoutHelper(this.serviceBusConnection.OperationTimeout, true); MessagingEventSource.Log.AmqpGetOrCreateConnectionStart(); var amqpConnection = await this.serviceBusConnection.ConnectionManager.GetOrCreateAsync(timeoutHelper.RemainingTime()).ConfigureAwait(false); MessagingEventSource.Log.AmqpGetOrCreateConnectionStop(this.entityPath, amqpConnection.ToString(), amqpConnection.State.ToString()); // Authenticate over CBS var cbsLink = amqpConnection.Extensions.Find <AmqpCbsLink>(); DateTime cbsTokenExpiresAtUtc = DateTime.MaxValue; foreach (var resource in this.audience) { MessagingEventSource.Log.AmqpSendAuthenticationTokenStart(this.endpointAddress, resource, resource, this.requiredClaims); cbsTokenExpiresAtUtc = TimeoutHelper.Min( cbsTokenExpiresAtUtc, await cbsLink.SendTokenAsync(this.cbsTokenProvider, this.endpointAddress, resource, resource, this.requiredClaims, timeoutHelper.RemainingTime()).ConfigureAwait(false)); MessagingEventSource.Log.AmqpSendAuthenticationTokenStop(); } AmqpSession session = null; try { // Create Session var amqpSessionSettings = new AmqpSessionSettings { Properties = new Fields() }; if (this.amqpLinkSettings.IsReceiver()) { // This is the maximum number of unsettled transfers across all receive links on this session. // This will allow the session to accept unlimited number of transfers, even if the recevier(s) // are not settling any of the deliveries. amqpSessionSettings.IncomingWindow = uint.MaxValue; } session = amqpConnection.CreateSession(amqpSessionSettings); await session.OpenAsync(timeoutHelper.RemainingTime()).ConfigureAwait(false); } catch (Exception exception) { MessagingEventSource.Log.AmqpSessionCreationException(this.entityPath, amqpConnection, exception); session?.Abort(); throw AmqpExceptionHelper.GetClientException(exception, null, session.GetInnerException(), amqpConnection.IsClosing()); } AmqpObject link = null; try { // Create Link link = this.OnCreateAmqpLink(amqpConnection, this.amqpLinkSettings, session); await link.OpenAsync(timeoutHelper.RemainingTime()).ConfigureAwait(false); return(new Tuple <AmqpObject, DateTime>(link, cbsTokenExpiresAtUtc)); } catch (Exception exception) { MessagingEventSource.Log.AmqpLinkCreationException( this.entityPath, session, amqpConnection, exception); session.SafeClose(exception); throw AmqpExceptionHelper.GetClientException(exception, null, link?.GetInnerException(), amqpConnection.IsClosing()); } }
private async Task <AmqpSession> CreateSessionAsync(TimeSpan timeout) { Logging.Enter(this, timeout, nameof(CreateSessionAsync)); TransportBase transport = null; try { var timeoutHelper = new TimeoutHelper(timeout); _refreshTokenTimer.Cancel(); AmqpSettings amqpSettings = CreateAmqpSettings(); if (_useWebSocketOnly) { // Try only AMQP transport over WebSocket transport = _clientWebSocketTransport = (ClientWebSocketTransport) await CreateClientWebSocketTransportAsync(timeoutHelper.RemainingTime()) .ConfigureAwait(false); } else { TlsTransportSettings tlsTransportSettings = CreateTlsTransportSettings(); var amqpTransportInitiator = new AmqpTransportInitiator(amqpSettings, tlsTransportSettings); try { transport = await amqpTransportInitiator.ConnectTaskAsync(timeoutHelper.RemainingTime()).ConfigureAwait(false); } catch (Exception e) when(!(e is AuthenticationException)) { Logging.Error(this, e, nameof(CreateSessionAsync)); if (Fx.IsFatal(e)) { throw; } // AMQP transport over TCP failed. Retry AMQP transport over WebSocket if (timeoutHelper.RemainingTime() != TimeSpan.Zero) { transport = _clientWebSocketTransport = (ClientWebSocketTransport) await CreateClientWebSocketTransportAsync(timeoutHelper.RemainingTime()).ConfigureAwait(false); } else { throw; } } } Logging.Info(this, $"Initialized {nameof(TransportBase)}, ws={_useWebSocketOnly}"); var amqpConnectionSettings = new AmqpConnectionSettings { MaxFrameSize = AmqpConstants.DefaultMaxFrameSize, ContainerId = Guid.NewGuid().ToString("N", CultureInfo.InvariantCulture), // Use a human readable link name to help with debugging HostName = ConnectionString.AmqpEndpoint.Host, }; var amqpConnection = new AmqpConnection(transport, amqpSettings, amqpConnectionSettings); await amqpConnection.OpenAsync(timeoutHelper.RemainingTime()).ConfigureAwait(false); Logging.Info(this, $"{nameof(AmqpConnection)} opened."); var sessionSettings = new AmqpSessionSettings { Properties = new Fields(), }; try { AmqpSession amqpSession = amqpConnection.CreateSession(sessionSettings); await amqpSession.OpenAsync(timeoutHelper.RemainingTime()).ConfigureAwait(false); Logging.Info(this, $"{nameof(AmqpSession)} opened."); // This adds itself to amqpConnection.Extensions var cbsLink = new AmqpCbsLink(amqpConnection); await SendCbsTokenAsync(cbsLink, timeoutHelper.RemainingTime()).ConfigureAwait(false); return(amqpSession); } catch (Exception ex) when(!ex.IsFatal()) { Logging.Error(this, ex, nameof(CreateSessionAsync)); _clientWebSocketTransport?.Dispose(); _clientWebSocketTransport = null; if (amqpConnection.TerminalException != null) { throw AmqpClientHelper.ToIotHubClientContract(amqpConnection.TerminalException); } amqpConnection.SafeClose(ex); throw; } } finally { Logging.Exit(this, timeout, nameof(CreateSessionAsync)); } }
internal Task OpenAsync(TimeSpan timeout) { return(AmqpSession.OpenAsync(timeout)); }