IotHubClient(DeviceClient deviceClient, string deviceId, IotHubClientSettings settings, IByteBufferAllocator allocator, IMessageAddressConverter messageAddressConverter)
 {
     this.deviceClient            = deviceClient;
     this.deviceId                = deviceId;
     this.settings                = settings;
     this.allocator               = allocator;
     this.messageAddressConverter = messageAddressConverter;
 }
 IotHubBridge(DeviceClient deviceClient, string deviceId, IotHubClientSettings settings)
 {
     this.routes       = new List <Tuple <Func <string, bool>, IMessagingServiceClient> >();
     this.sources      = new List <IMessagingSource>();
     this.DeviceClient = deviceClient;
     this.DeviceId     = deviceId;
     this.Settings     = settings;
 }
 IotHubClient(DeviceClient deviceClient, string deviceId, IotHubClientSettings settings, IByteBufferAllocator allocator, IMessageAddressConverter messageAddressConverter)
 {
     this.deviceClient = deviceClient;
     this.deviceId = deviceId;
     this.settings = settings;
     this.allocator = allocator;
     this.messageAddressConverter = messageAddressConverter;
 }
        IotHubClient(DeviceClient deviceClient, string deviceId, IotHubClientSettings settings, IByteBufferAllocator allocator, IMessageAddressConverter messageAddressConverter)
        {
            this.deviceClient            = deviceClient;
            this.deviceId                = deviceId;
            this.settings                = settings;
            this.allocator               = allocator;
            this.messageAddressConverter = messageAddressConverter;

            deviceClient.OperationTimeoutInMilliseconds = 0;// uint.MaxValue;// 6 * 60 * 1000;
        }
 public static Func<IDeviceIdentity, Task<IMessagingServiceClient>> PreparePoolFactory(string baseConnectionString, int connectionPoolSize,
     TimeSpan? connectionIdleTimeout, IotHubClientSettings settings, IByteBufferAllocator allocator, IMessageAddressConverter messageAddressConverter)
 {
     IotHubConnectionStringBuilder csb = IotHubConnectionStringBuilder.Create(baseConnectionString);
     Func<IDeviceIdentity, Task<IMessagingServiceClient>> mqttCommunicatorFactory = deviceIdentity =>
     {
         var identity = (IotHubDeviceIdentity)deviceIdentity;
         csb.AuthenticationMethod = DeriveAuthenticationMethod(csb.AuthenticationMethod, identity);
         csb.HostName = identity.IotHubHostName;
         string connectionString = csb.ToString();
         return CreateFromConnectionStringAsync(identity.Id, connectionString, connectionPoolSize, connectionIdleTimeout, settings, allocator, messageAddressConverter);
     };
     return mqttCommunicatorFactory;
 }
        Bootstrapper(ISettingsProvider settingsProvider, ISessionStatePersistenceProvider sessionStateManager, IQos2StatePersistenceProvider qos2StateProvider, IMessageAddressConverter addressConverter)
        {
            Contract.Requires(settingsProvider != null);
            Contract.Requires(sessionStateManager != null);

            this.closeCompletionSource = new TaskCompletionSource();

            this.settingsProvider = settingsProvider;
            this.settings = new Settings(this.settingsProvider);
            this.iotHubClientSettings = new IotHubClientSettings(this.settingsProvider);
            this.sessionStateManager = sessionStateManager;
            this.qos2StateProvider = qos2StateProvider;
            this.authProvider = new SasTokenDeviceIdentityProvider();
            this.topicNameConverter = addressConverter;
        }
 public static async Task<IMessagingServiceClient> CreateFromConnectionStringAsync(string deviceId, string connectionString,
     int connectionPoolSize, TimeSpan? connectionIdleTimeout, IotHubClientSettings settings, IByteBufferAllocator allocator, IMessageAddressConverter messageAddressConverter)
 {
     int maxPendingOutboundMessages = settings.MaxPendingOutboundMessages;
     var tcpSettings = new AmqpTransportSettings(TransportType.Amqp_Tcp_Only);
     var webSocketSettings = new AmqpTransportSettings(TransportType.Amqp_WebSocket_Only);
     webSocketSettings.PrefetchCount = tcpSettings.PrefetchCount = (uint)maxPendingOutboundMessages;
     if (connectionPoolSize > 0)
     {
         var amqpConnectionPoolSettings = new AmqpConnectionPoolSettings
         {
             MaxPoolSize = unchecked ((uint)connectionPoolSize),
             Pooling = connectionPoolSize > 0
         };
         if (connectionIdleTimeout.HasValue)
         {
             amqpConnectionPoolSettings.ConnectionIdleTimeout = connectionIdleTimeout.Value;
         }
         tcpSettings.AmqpConnectionPoolSettings = amqpConnectionPoolSettings;
         webSocketSettings.AmqpConnectionPoolSettings = amqpConnectionPoolSettings;
     }
     DeviceClient client = DeviceClient.CreateFromConnectionString(connectionString, new ITransportSettings[]
     {
         tcpSettings,
         webSocketSettings
     });
     try
     {
         await client.OpenAsync();
     }
     catch (IotHubException ex)
     {
         throw ComposeIotHubCommunicationException(ex);
     }
     return new IotHubClient(client, deviceId, settings, allocator, messageAddressConverter);
 }
        static async Task <IotHubBridge> CreateFromConnectionStringAsync(string deviceId, string connectionString,
                                                                         int connectionPoolSize, TimeSpan?connectionIdleTimeout, IotHubClientSettings settings, CancellationToken cancellationToken)
        {
            int maxPendingOutboundMessages = settings.MaxPendingOutboundMessages;
            var tcpSettings       = new AmqpTransportSettings(TransportType.Amqp_Tcp_Only);
            var webSocketSettings = new AmqpTransportSettings(TransportType.Amqp_WebSocket_Only);

            webSocketSettings.PrefetchCount = tcpSettings.PrefetchCount = (uint)maxPendingOutboundMessages;
            if (connectionPoolSize > 0)
            {
                var amqpConnectionPoolSettings = new AmqpConnectionPoolSettings
                {
                    MaxPoolSize = unchecked ((uint)connectionPoolSize),
                    Pooling     = connectionPoolSize > 0
                };
                if (connectionIdleTimeout.HasValue)
                {
                    amqpConnectionPoolSettings.ConnectionIdleTimeout = connectionIdleTimeout.Value;
                }
                tcpSettings.AmqpConnectionPoolSettings       = amqpConnectionPoolSettings;
                webSocketSettings.AmqpConnectionPoolSettings = amqpConnectionPoolSettings;
            }
            var client = DeviceClient.CreateFromConnectionString(connectionString, new ITransportSettings[]
            {
                tcpSettings,
                webSocketSettings
            });

            client.SetRetryPolicy(DeviceClientRetryPolicy.Instance);
            var bridge = new IotHubBridge(client, deviceId, settings);

            client.SetConnectionStatusChangesHandler((status, reason) => {
                if (status != ConnectionStatus.Connected)
                {
                    var cause = new IotHubCommunicationException("Connection to IoT Hub is closed");
                    if (Interlocked.CompareExchange(ref bridge.closedCause, cause, null) == null)
                    {
                        bridge.messagingChannel?.Close(cause);
                    }
                }
            });

            // This helps in usage instrumentation at IotHub service.
            client.ProductInfo = $"protocolgateway/poolsize={connectionPoolSize}";

            try
            {
                await client.OpenAsync(cancellationToken);

                cancellationToken.ThrowIfCancellationRequested(); // in case SDK does not always honor cancellation token in async operations
            }
            catch (IotHubException ex)
            {
                client.Dispose();
                throw ex.ToMessagingException();
            }
            catch (Exception)
            {
                client.Dispose();
                throw;
            }
            return(bridge);
        }
        public static MessagingBridgeFactoryFunc PrepareFactory(string baseConnectionString, int connectionPoolSize,
                                                                TimeSpan?connectionIdleTimeout, IotHubClientSettings settings, Action <IotHubBridge> initHandler)
        {
            MessagingBridgeFactoryFunc mqttCommunicatorFactory = async(deviceIdentity, cancellationToken) =>
            {
                var csb      = IotHubConnectionStringBuilder.Create(baseConnectionString);
                var identity = (IotHubDeviceIdentity)deviceIdentity;
                csb.AuthenticationMethod = DeriveAuthenticationMethod(csb.AuthenticationMethod, identity);
                csb.HostName             = identity.IotHubHostName;
                string connectionString = csb.ToString();
                var    bridge           = await CreateFromConnectionStringAsync(identity.Id, connectionString, connectionPoolSize, connectionIdleTimeout, settings, cancellationToken);

                initHandler(bridge);
                return(bridge);
            };

            return(mqttCommunicatorFactory);
        }
        public static Func <IDeviceIdentity, Task <IMessagingServiceClient> > PreparePoolFactory(string baseConnectionString, int connectionPoolSize,
                                                                                                 TimeSpan?connectionIdleTimeout, IotHubClientSettings settings, IByteBufferAllocator allocator, IMessageAddressConverter messageAddressConverter)
        {
            IotHubConnectionStringBuilder csb = IotHubConnectionStringBuilder.Create(baseConnectionString);
            Func <IDeviceIdentity, Task <IMessagingServiceClient> > mqttCommunicatorFactory = deviceIdentity =>
            {
                var identity = (IotHubDeviceIdentity)deviceIdentity;
                csb.AuthenticationMethod = DeriveAuthenticationMethod(csb.AuthenticationMethod, identity);
                csb.HostName             = identity.IotHubHostName;
                string connectionString = csb.ToString();
                return(CreateFromConnectionStringAsync(identity.Id, connectionString, connectionPoolSize, connectionIdleTimeout, settings, allocator, messageAddressConverter));
            };

            return(mqttCommunicatorFactory);
        }
        public static async Task <IMessagingServiceClient> CreateFromConnectionStringAsync(string deviceId, string connectionString,
                                                                                           int connectionPoolSize, TimeSpan?connectionIdleTimeout, IotHubClientSettings settings, IByteBufferAllocator allocator, IMessageAddressConverter messageAddressConverter)
        {
            int maxPendingOutboundMessages = settings.MaxPendingOutboundMessages;
            var tcpSettings       = new AmqpTransportSettings(TransportType.Amqp_Tcp_Only);
            var webSocketSettings = new AmqpTransportSettings(TransportType.Amqp_WebSocket_Only);

            webSocketSettings.PrefetchCount = tcpSettings.PrefetchCount = (uint)maxPendingOutboundMessages;
            if (connectionPoolSize > 0)
            {
                var amqpConnectionPoolSettings = new AmqpConnectionPoolSettings
                {
                    MaxPoolSize = unchecked ((uint)connectionPoolSize),
                    Pooling     = connectionPoolSize > 0
                };
                if (connectionIdleTimeout.HasValue)
                {
                    amqpConnectionPoolSettings.ConnectionIdleTimeout = connectionIdleTimeout.Value;
                }
                tcpSettings.AmqpConnectionPoolSettings       = amqpConnectionPoolSettings;
                webSocketSettings.AmqpConnectionPoolSettings = amqpConnectionPoolSettings;
            }
            DeviceClient client = DeviceClient.CreateFromConnectionString(connectionString, new ITransportSettings[]
            {
                tcpSettings,
                webSocketSettings
            });

            try
            {
                await client.OpenAsync();
            }
            catch (IotHubException ex)
            {
                throw ComposeIotHubCommunicationException(ex);
            }
            return(new IotHubClient(client, deviceId, settings, allocator, messageAddressConverter));
        }
Exemplo n.º 12
0
        public static async Task <IMessagingServiceClient> CreateFromX509CertificateAsync(string deviceId, string connectionString,
                                                                                          int connectionPoolSize, TimeSpan?connectionIdleTimeout, IotHubClientSettings settings, IByteBufferAllocator allocator, IMessageAddressConverter messageAddressConverter, X509Certificate2 clientCertificate)
        {
            int maxPendingOutboundMessages = settings.MaxPendingOutboundMessages;
            var tcpSettings = new AmqpTransportSettings(TransportType.Amqp_Tcp_Only)
            {
                ClientCertificate = clientCertificate
            };
            var webSocketSettings = new AmqpTransportSettings(TransportType.Amqp_WebSocket_Only)
            {
                ClientCertificate = clientCertificate
            };

            // COE --> There is something in the settings that prevents this from working correctly
            //webSocketSettings.PrefetchCount = tcpSettings.PrefetchCount = (uint)maxPendingOutboundMessages;
            //if (connectionPoolSize > 0)
            //{
            //    var amqpConnectionPoolSettings = new AmqpConnectionPoolSettings
            //    {
            //        MaxPoolSize = unchecked((uint)connectionPoolSize),
            //        Pooling = connectionPoolSize > 0
            //    };
            //    if (connectionIdleTimeout.HasValue)
            //    {
            //        amqpConnectionPoolSettings.ConnectionIdleTimeout = connectionIdleTimeout.Value;
            //    }
            //    tcpSettings.AmqpConnectionPoolSettings = amqpConnectionPoolSettings;

            //    webSocketSettings.AmqpConnectionPoolSettings = amqpConnectionPoolSettings;

            //}
            DeviceClient client = DeviceClient.CreateFromConnectionString(connectionString, new ITransportSettings[]
            {
                tcpSettings,
                webSocketSettings
            });

            try
            {
                await client.OpenAsync();
            }
            catch (IotHubException ex)
            {
                throw ComposeIotHubCommunicationException(ex);
            }
            return(new IotHubClient(client, deviceId, settings, allocator, messageAddressConverter));
        }