// Making this virtual to allow Moq to override
        public virtual IotHubConnection GetConnection(IotHubConnectionString connectionString, AmqpTransportSettings amqpTransportSetting)
        {
            // Only the initial transportSetting is used, subsequent ones are ignored
            if (this.amqpTransportSettings == null)
            {
                Interlocked.CompareExchange(ref this.amqpTransportSettings, amqpTransportSetting, null);
            }
            else
            {
#if !WINDOWS_UWP && !PCL
                // Client certificate is per device and must be overriden
                this.amqpTransportSettings.ClientCertificate = amqpTransportSetting.ClientCertificate;
#endif
            }

            // Uncomment when we rev the ApiVersion since this is a breaking change
            //if (!this.amqpTransportSettings.Equals(amqpTransportSetting))
            //{
            //    throw new ArgumentException("AmqpTransportSettings cannot be modified from the initial settings.");
            //}

            IotHubConnection iotHubConnection;
            if (connectionString.SharedAccessKeyName != null || connectionString.SharedAccessSignature != null)
            {
                // Connections are not pooled when SAS signatures are used. However, we still use a connection pool object
                // Connections are not shared since the SAS signature will not match another connection string
                IotHubScopeConnectionPool iotHubScopeConnectionPool;
                do
                {
                    iotHubScopeConnectionPool =
                        this.hubScopeConnectionPools.GetOrAdd(
                            connectionString,
                            k => new IotHubScopeConnectionPool(this, k, this.amqpTransportSettings)
                            );
                }while (!iotHubScopeConnectionPool.TryAddRef());

                iotHubConnection = iotHubScopeConnectionPool.Connection;
            }
            else if (this.amqpTransportSettings.AmqpConnectionPoolSettings.Pooling)
            {
                // use connection pooling for device scope connection string
                do
                {
                    IotHubDeviceScopeConnectionPool iotHubDeviceScopeConnectionPool =
                        this.deviceScopeConnectionPools.GetOrAdd(
                            connectionString,
                            k => new IotHubDeviceScopeConnectionPool(this, k, this.amqpTransportSettings)
                            );
                    iotHubConnection = iotHubDeviceScopeConnectionPool.GetConnection(connectionString.DeviceId);
                }while (iotHubConnection == null);
            }
            else
            {
                // Connection pooling is turned off for device-scope connection strings
                iotHubConnection = new IotHubSingleTokenConnection(null, connectionString, this.amqpTransportSettings);
            }

            return(iotHubConnection);
        }
Пример #2
0
 public IotHubDeviceMuxConnection(IotHubDeviceScopeConnectionPool deviceScopeConnectionPool, long cacheKey, IotHubConnectionString connectionString, AmqpTransportSettings amqpTransportSettings)
     : base(connectionString.HostName, connectionString.AmqpEndpoint.Port, amqpTransportSettings)
 {
     this.deviceScopeConnectionPool = deviceScopeConnectionPool;
     this.cacheKey              = cacheKey;
     this.FaultTolerantSession  = new FaultTolerantAmqpObject <AmqpSession>(this.CreateSessionAsync, this.CloseConnection);
     this.iotHubTokenRefreshers = new ConcurrentDictionary <AmqpObject, IotHubTokenRefresher>();
 }
        // Making this virtual to allow Moq to override
        public virtual IotHubConnection GetConnection(IotHubConnectionString connectionString, AmqpTransportSettings amqpTransportSetting)
        {
            // Only the initial transportSetting is used, subsequent ones are ignored
            if (this.amqpTransportSettings == null)
            {
                Interlocked.CompareExchange(ref this.amqpTransportSettings, amqpTransportSetting, null);
            }

            IotHubConnection iotHubConnection;

            if (connectionString.SharedAccessKeyName != null || connectionString.SharedAccessSignature != null)
            {
                // Connections are not pooled when SAS signatures are used. However, we still use a connection pool object
                // Connections are not shared since the SAS signature will not match another connection string
                IotHubScopeConnectionPool iotHubScopeConnectionPool;
                do
                {
                    iotHubScopeConnectionPool =
                        this.hubScopeConnectionPools.GetOrAdd(
                            connectionString,
                            k => new IotHubScopeConnectionPool(this, k, this.amqpTransportSettings)
                            );
                }while (!iotHubScopeConnectionPool.TryAddRef());

                iotHubConnection = iotHubScopeConnectionPool.Connection;
            }
            else if (this.amqpTransportSettings.AmqpConnectionPoolSettings.Pooling)
            {
                // use connection pooling for device scope connection string
                do
                {
                    IotHubDeviceScopeConnectionPool iotHubDeviceScopeConnectionPool =
                        this.deviceScopeConnectionPools.GetOrAdd(
                            connectionString,
                            k => new IotHubDeviceScopeConnectionPool(this, k, this.amqpTransportSettings)
                            );
                    iotHubConnection = iotHubDeviceScopeConnectionPool.GetConnection(connectionString.DeviceId);
                }while (iotHubConnection == null);
            }
            else
            {
                // Connection pooling is turned off for device-scope connection strings
                iotHubConnection = new IotHubDeviceMuxConnection(null, long.MaxValue, connectionString, this.amqpTransportSettings);
            }

            return(iotHubConnection);
        }