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);
        }
Exemplo n.º 2
0
 public CommandReceiver(IotHubBridge bridge, IByteBufferAllocator allocator, TryFormatAddress addressFormatter)
 {
     this.bridge           = bridge;
     this.allocator        = allocator;
     this.addressFormatter = addressFormatter;
 }
 public TelemetrySender(IotHubBridge bridge, TryProcessMessage messageProcessor)
 {
     this.bridge           = bridge;
     this.messageProcessor = messageProcessor;
 }
Exemplo n.º 4
0
 public MethodHandler(string methodName, IotHubBridge bridge, MethodHandlerCallback dispatchCallback)
 {
     this.methodName       = methodName;
     this.bridge           = bridge;
     this.dispatchCallback = dispatchCallback;
 }