コード例 #1
0
        public IDelegatingHandler Create(IPipelineContext context)
        {
            var connectionString             = context.Get <IotHubConnectionString>();
            var transportSetting             = context.Get <ITransportSettings>();
            var onMethodCallback             = context.Get <DeviceClient.OnMethodCalledDelegate>();
            var onReportedStatePatchReceived = context.Get <Action <TwinCollection> >();
            var OnConnectionClosedCallback   = context.Get <DeviceClient.OnConnectionClosedDelegate>();

            switch (transportSetting.GetTransportType())
            {
            case TransportType.Amqp_WebSocket_Only:
            case TransportType.Amqp_Tcp_Only:
                return(new AmqpTransportHandler(
                           context, connectionString, transportSetting as AmqpTransportSettings,
                           new Action <object, EventArgs>(OnConnectionClosedCallback),
                           new Func <MethodRequestInternal, Task>(onMethodCallback)));

            case TransportType.Http1:
                return(new HttpTransportHandler(context, connectionString, transportSetting as Http1TransportSettings));

#if !NETMF && !PCL
            case TransportType.Mqtt_Tcp_Only:
            case TransportType.Mqtt_WebSocket_Only:
                return(new MqttTransportHandler(
                           context, connectionString, transportSetting as MqttTransportSettings,
                           new Action <object, EventArgs>(OnConnectionClosedCallback),
                           new Func <MethodRequestInternal, Task>(onMethodCallback), onReportedStatePatchReceived));
#endif
            default:
                throw new InvalidOperationException("Unsupported Transport Setting {0}".FormatInvariant(transportSetting));
            }
        }
コード例 #2
0
        public IDelegatingHandler Create(IPipelineContext context)
        {
            // ProtocolRoutingDelegatingHandler configures the ITransportSettings configuration
            // which is different from ITransportSettings[] element.
            ITransportSettings     transportSetting = context.Get <ITransportSettings>();
            IotHubConnectionString connectionString = context.Get <IotHubConnectionString>();

            InternalClient.OnMethodCalledDelegate onMethodCallback = context.Get <InternalClient.OnMethodCalledDelegate>();
            Action <TwinCollection> onDesiredStatePatchReceived    = context.Get <Action <TwinCollection> >();

            InternalClient.OnModuleEventMessageReceivedDelegate onModuleEventReceivedCallback   = context.Get <InternalClient.OnModuleEventMessageReceivedDelegate>();
            InternalClient.OnDeviceMessageReceivedDelegate      onDeviceMessageReceivedCallback = context.Get <InternalClient.OnDeviceMessageReceivedDelegate>();

            switch (transportSetting.GetTransportType())
            {
            case TransportType.Amqp_WebSocket_Only:
            case TransportType.Amqp_Tcp_Only:
                return(new AmqpTransportHandler(
                           context,
                           connectionString,
                           transportSetting as AmqpTransportSettings,
                           new Func <MethodRequestInternal, Task>(onMethodCallback),
                           onDesiredStatePatchReceived,
                           new Func <string, Message, Task>(onModuleEventReceivedCallback),
                           new Func <Message, Task>(onDeviceMessageReceivedCallback)));

            case TransportType.Http1:
                return(new HttpTransportHandler(context, connectionString, transportSetting as Http1TransportSettings, isClientPrimaryTransportHandler: true));

            case TransportType.Mqtt_Tcp_Only:
            case TransportType.Mqtt_WebSocket_Only:
                return(new MqttTransportHandler(
                           context,
                           connectionString,
                           transportSetting as MqttTransportSettings,
                           new Func <MethodRequestInternal, Task>(onMethodCallback),
                           onDesiredStatePatchReceived,
                           new Func <string, Message, Task>(onModuleEventReceivedCallback),
                           new Func <Message, Task>(onDeviceMessageReceivedCallback)));

            default:
                throw new InvalidOperationException("Unsupported Transport Setting {0}".FormatInvariant(transportSetting));
            }
        }
コード例 #3
0
        public RetryDelegatingHandler(IPipelineContext context, IDelegatingHandler innerHandler)
            : base(context, innerHandler)
        {
            IRetryPolicy defaultRetryStrategy = new ExponentialBackoff(
                retryCount: RetryMaxCount,
                minBackoff: TimeSpan.FromMilliseconds(100),
                maxBackoff: TimeSpan.FromSeconds(10),
                deltaBackoff: TimeSpan.FromMilliseconds(100));

            _internalRetryPolicy       = new RetryPolicy(new TransientErrorStrategy(), new RetryStrategyAdapter(defaultRetryStrategy));
            _onConnectionStatusChanged = context.Get <ConnectionStatusChangesHandler>();

            Logging.Associate(this, _internalRetryPolicy, nameof(SetRetryPolicy));
        }
コード例 #4
0
        internal AmqpTransportHandler(
            IPipelineContext context,
            IotHubConnectionString connectionString,
            AmqpTransportSettings transportSettings,
            Func <MethodRequestInternal, Task> onMethodCallback  = null,
            Action <TwinCollection> onDesiredStatePatchReceived  = null,
            Func <string, Message, Task> onEventReceivedCallback = null)
            : base(context, transportSettings)
        {
            this.productInfo = context.Get <ProductInfo>();

            TransportType transportType = transportSettings.GetTransportType();

            this.deviceId = connectionString.DeviceId;
            this.moduleId = connectionString.ModuleId;

            if (!transportSettings.AmqpConnectionPoolSettings.Pooling)
            {
                this.IotHubConnection = new IotHubSingleTokenConnection(null, connectionString, transportSettings);
            }
            else
            {
                switch (transportType)
                {
                case TransportType.Amqp_Tcp_Only:
                    this.IotHubConnection = TcpConnectionCache.GetConnection(connectionString, transportSettings);
                    break;

                case TransportType.Amqp_WebSocket_Only:
                    this.IotHubConnection = WsConnectionCache.GetConnection(connectionString, transportSettings);
                    break;

                default:
                    throw new InvalidOperationException("Invalid Transport Type {0}".FormatInvariant(transportType));
                }
            }

            this.IotHubConnection.OnConnectionClose += OnAmqpConnectionClose;

            this.openTimeout      = transportSettings.OpenTimeout;
            this.operationTimeout = transportSettings.OperationTimeout;
            this.prefetchCount    = transportSettings.PrefetchCount;
            this.faultTolerantEventSendingLink         = new Client.FaultTolerantAmqpObject <SendingAmqpLink>(this.CreateEventSendingLinkAsync, OnAmqpLinkClose);
            this.faultTolerantDeviceBoundReceivingLink = new Client.FaultTolerantAmqpObject <ReceivingAmqpLink>(this.CreateDeviceBoundReceivingLinkAsync, OnAmqpLinkClose);
            this.iotHubConnectionString      = connectionString;
            this.methodReceivedListener      = onMethodCallback;
            this.onDesiredStatePatchListener = onDesiredStatePatchReceived;
            this.eventReceivedListener       = onEventReceivedCallback;
        }
コード例 #5
0
        internal HttpTransportHandler(IPipelineContext context, IotHubConnectionString iotHubConnectionString, Http1TransportSettings transportSettings)
            : base(context, transportSettings)
        {
            ProductInfo productInfo = context.Get <ProductInfo>();

            this.deviceId         = iotHubConnectionString.DeviceId;
            this.httpClientHelper = new HttpClientHelper(
                iotHubConnectionString.HttpsEndpoint,
                iotHubConnectionString,
                ExceptionHandlingHelper.GetDefaultErrorMapping(),
                DefaultOperationTimeout,
                null,
                transportSettings.ClientCertificate,
                productInfo);
        }
コード例 #6
0
        internal HttpTransportHandler(
            IPipelineContext context,
            IotHubConnectionString iotHubConnectionString,
            Http1TransportSettings transportSettings,
            HttpClientHandler httpClientHandler  = null,
            bool isClientPrimaryTransportHandler = false)
            : base(context, transportSettings)
        {
            ProductInfo productInfo = context.Get <ProductInfo>();

            _deviceId         = iotHubConnectionString.DeviceId;
            _moduleId         = iotHubConnectionString.ModuleId;
            _httpClientHelper = new HttpClientHelper(
                iotHubConnectionString.HttpsEndpoint,
                iotHubConnectionString,
                ExceptionHandlingHelper.GetDefaultErrorMapping(),
                s_defaultOperationTimeout,
                null,
                transportSettings.ClientCertificate,
                httpClientHandler,
                productInfo,
                transportSettings.Proxy,
                isClientPrimaryTransportHandler);
        }
コード例 #7
0
        internal MqttTransportHandler(
            IPipelineContext context,
            IotHubConnectionString iotHubConnectionString,
            MqttTransportSettings settings,
            Func<IPAddress[], int, Task<IChannel>> channelFactory)
            : base(context, settings)
        {
            this.mqttIotHubAdapterFactory = new MqttIotHubAdapterFactory(settings);
            this.messageQueue = new ConcurrentQueue<Message>();
            this.completionQueue = new Queue<string>();

            this.serverAddresses = null; // this will be resolved asynchronously in OpenAsync
            this.hostName = iotHubConnectionString.HostName;
            this.receiveEventMessageFilter = string.Format(CultureInfo.InvariantCulture, receiveEventMessagePatternFilter, iotHubConnectionString.DeviceId, iotHubConnectionString.ModuleId);
            this.receiveEventMessagePrefix = string.Format(CultureInfo.InvariantCulture, receiveEventMessagePrefixPattern, iotHubConnectionString.DeviceId, iotHubConnectionString.ModuleId);

            this.qos = settings.PublishToServerQoS;

            if (channelFactory == null)
            {
                switch (settings.GetTransportType())
                {
                    case TransportType.Mqtt_Tcp_Only:
                        this.channelFactory = this.CreateChannelFactory(iotHubConnectionString, settings, context.Get<ProductInfo>());
                        break;
                    case TransportType.Mqtt_WebSocket_Only:
                        this.channelFactory = this.CreateWebSocketChannelFactory(iotHubConnectionString, settings, context.Get<ProductInfo>());
                        break;
                    default:
                        throw new InvalidOperationException("Unsupported Transport Setting {0}".FormatInvariant(settings.GetTransportType()));
                }
            }
            else
            {
                this.channelFactory = channelFactory;
            }

            this.closeRetryPolicy = new RetryPolicy(new TransientErrorIgnoreStrategy(), 5, TimeSpan.FromSeconds(1), TimeSpan.FromSeconds(1));
        }
コード例 #8
0
        internal MqttTransportHandler(
            IPipelineContext context,
            IotHubConnectionString iotHubConnectionString,
            MqttTransportSettings settings,
            Func <IPAddress, int, Task <IChannel> > channelFactory,
            Action <object, ConnectionEventArgs> onConnectionOpenedCallback,
            Func <object, ConnectionEventArgs, Task> onConnectionClosedCallback)
            : base(context, settings)
        {
            this.connectionOpenedListener = onConnectionOpenedCallback;
            this.connectionClosedListener = onConnectionClosedCallback;

            this.mqttIotHubAdapterFactory = new MqttIotHubAdapterFactory(settings);
            this.messageQueue             = new ConcurrentQueue <Message>();
            this.completionQueue          = new Queue <string>();

            this.serverAddress = null; // this will be resolved asynchronously in OpenAsync
            this.hostName      = iotHubConnectionString.HostName;

            this.qos = settings.PublishToServerQoS;
            this.eventLoopGroupKey = iotHubConnectionString.IotHubName + "#" + iotHubConnectionString.DeviceId + "#" + iotHubConnectionString.Audience;

            if (channelFactory == null)
            {
                switch (settings.GetTransportType())
                {
                case TransportType.Mqtt_Tcp_Only:
                    this.channelFactory = this.CreateChannelFactory(iotHubConnectionString, settings, context.Get <ProductInfo>());
                    break;

                case TransportType.Mqtt_WebSocket_Only:
                    this.channelFactory = this.CreateWebSocketChannelFactory(iotHubConnectionString, settings, context.Get <ProductInfo>());
                    break;

                default:
                    throw new InvalidOperationException("Unsupported Transport Setting {0}".FormatInvariant(settings.GetTransportType()));
                }
            }
            else
            {
                this.channelFactory = channelFactory;
            }

            this.closeRetryPolicy = new RetryPolicy(new TransientErrorIgnoreStrategy(), 5, TimeSpan.FromSeconds(1), TimeSpan.FromSeconds(1));
        }
コード例 #9
0
        internal AmqpTransportHandler(
            IPipelineContext context,
            IotHubConnectionString connectionString,
            AmqpTransportSettings transportSettings,
            Func <MethodRequestInternal, Task> methodHandler = null,
            Action <TwinCollection> desiredPropertyListener  = null,
            Func <string, Message, Task> eventListener       = null)
            : base(context, transportSettings)
        {
            _operationTimeout        = transportSettings.OperationTimeout;
            _desiredPropertyListener = desiredPropertyListener;
            DeviceIdentity deviceIdentity = new DeviceIdentity(connectionString, transportSettings, context.Get <ProductInfo>());

            _amqpUnit = AmqpUnitManager.GetInstance().CreateAmqpUnit(
                deviceIdentity,
                methodHandler,
                TwinMessageListener,
                eventListener
                );

            _amqpUnit.OnUnitDisconnected += (o, args) =>
            {
                bool gracefulDisconnect = (bool)o;
                if (gracefulDisconnect)
                {
                    OnTransportClosedGracefully();
                }
                else
                {
                    OnTransportDisconnected();
                }
            };

            if (Logging.IsEnabled)
            {
                Logging.Associate(this, _amqpUnit, $"{nameof(_amqpUnit)}");
            }
        }
コード例 #10
0
        internal AmqpTransportHandler(
            IPipelineContext context,
            IotHubConnectionString connectionString,
            AmqpTransportSettings transportSettings,
            Func <MethodRequestInternal, Task> onMethodCallback          = null,
            Action <TwinCollection> onDesiredStatePatchReceivedCallback  = null,
            Func <string, Message, Task> onModuleMessageReceivedCallback = null,
            Func <Message, Task> onDeviceMessageReceivedCallback         = null)
            : base(context, transportSettings)
        {
            _operationTimeout            = transportSettings.OperationTimeout;
            _onDesiredStatePatchListener = onDesiredStatePatchReceivedCallback;
            var deviceIdentity = new DeviceIdentity(connectionString, transportSettings, context.Get <ProductInfo>(), context.Get <ClientOptions>());

            _amqpUnit = AmqpUnitManager.GetInstance().CreateAmqpUnit(
                deviceIdentity,
                onMethodCallback,
                TwinMessageListener,
                onModuleMessageReceivedCallback,
                onDeviceMessageReceivedCallback,
                OnDisconnected);

            Logging.Associate(this, _amqpUnit, nameof(_amqpUnit));
        }
コード例 #11
0
        internal AmqpTransportHandler(
            IPipelineContext context,
            IotHubConnectionString connectionString,
            AmqpTransportSettings transportSettings,
            Func <MethodRequestInternal, Task> methodHandler = null,
            Action <TwinCollection> desiredPropertyListener  = null,
            Func <string, Message, Task> eventListener       = null)
            : base(context, transportSettings)
        {
            _operationTimeout        = transportSettings.OperationTimeout;
            _desiredPropertyListener = desiredPropertyListener;
            var deviceIdentity = new DeviceIdentity(connectionString, transportSettings, context.Get <ProductInfo>(), context.Get <ClientOptions>());

            _amqpUnit = AmqpUnitManager.GetInstance().CreateAmqpUnit(
                deviceIdentity,
                methodHandler,
                TwinMessageListener,
                eventListener,
                OnDisconnected
                );

            if (Logging.IsEnabled)
            {
                Logging.Associate(this, _amqpUnit, $"{nameof(_amqpUnit)}");
            }
        }