async AsyncTask TryOpenPrioritizedTransportsAsync() { Exception lastException = null; // Concrete Device Client creation was deferred. Use prioritized list of transports. foreach (var transportSetting in this.transportSettings) { TransportHandlerBase helper; try { switch (transportSetting.GetTransportType()) { case TransportType.Amqp_WebSocket_Only: case TransportType.Amqp_Tcp_Only: helper = new AmqpTransportHandler(this.iotHubConnectionString, transportSetting as AmqpTransportSettings); break; case TransportType.Http1: helper = new HttpTransportHandler(this.iotHubConnectionString, transportSetting as Http1TransportSettings); break; case TransportType.Mqtt: helper = new MqttTransportHandler(this.iotHubConnectionString, transportSetting as MqttTransportSettings); break; default: throw new InvalidOperationException("Unsupported Transport Setting {0}".FormatInvariant(transportSetting)); } // Try to open a connection with this transport await helper.OpenAsync(); } catch (Exception exception) { if (exception.IsFatal() || exception is UnauthorizedException || exception is InvalidOperationException) { throw; } lastException = exception; // open connection failed. Move to next transport type continue; } // Success - return this transport type this.impl = helper; this.TransportTypeInUse = transportSetting.GetTransportType(); return; } throw lastException; }
MqttTransportHandler CreateTransportHandlerWithMockChannel(out IChannel channel, Action <object, ConnectionEventArgs> onConnectionOpenedCallback, Func <object, ConnectionEventArgs, Task> onConnectionClosedCallback) { var _channel = Substitute.For <IChannel>(); channel = _channel; MqttTransportHandler transport = null; // The channel factory creates the channel. This gets called from inside OpenAsync. // Unfortunately, it needs access to the internals of the transport (like being able to call OnConnceted, which is passed into the Mqtt channel constructor, but we're not using that) Func <IPAddress, int, Task <IChannel> > factory = (a, i) => { transport.OnConnected(); return(Task <IChannel> .FromResult <IChannel>(_channel)); }; transport = new MqttTransportHandler(new PipelineContext(), IotHubConnectionStringExtensions.Parse(DumpyConnectionString), new MqttTransportSettings(Microsoft.Azure.Devices.Client.TransportType.Mqtt_Tcp_Only), factory, onConnectionOpenedCallback, onConnectionClosedCallback); return(transport); }
async AsyncTask TryOpenPrioritizedTransportsAsync() { Exception lastException = null; // Concrete Device Client creation was deferred. Use prioritized list of transports. foreach (var transportSetting in this.transportSettings) { TransportHandlerBase helper = null; try { switch (transportSetting.GetTransportType()) { case TransportType.Amqp_WebSocket_Only: case TransportType.Amqp_Tcp_Only: helper = new AmqpTransportHandler(this.iotHubConnectionString, transportSetting as AmqpTransportSettings); break; case TransportType.Http1: helper = new HttpTransportHandler(this.iotHubConnectionString, transportSetting as Http1TransportSettings); break; case TransportType.Mqtt: helper = new MqttTransportHandler(this.iotHubConnectionString, transportSetting as MqttTransportSettings); break; default: throw new InvalidOperationException("Unsupported Transport Setting {0}".FormatInvariant(transportSetting)); } // Try to open a connection with this transport await helper.OpenAsync(); } catch (Exception exception) { await helper.CloseAsync(); if (!(exception is IotHubCommunicationException || exception is TimeoutException || exception is SocketException || exception is AggregateException)) { throw; } if (exception is AggregateException) { var aggregateException = (AggregateException)exception; var innerExceptions = aggregateException.Flatten().InnerExceptions; if (!innerExceptions.Any(x => x is IotHubCommunicationException || x is SocketException || x is TimeoutException)) { throw; } } lastException = exception; // open connection failed. Move to next transport type continue; } // Success - return this transport type this.impl = helper; this.TransportTypeInUse = transportSetting.GetTransportType(); return; } throw lastException; }
public async Task MqttTransportHandler_CompleteAsync_TokenCancellationRequested() { await TestOperationCanceledByToken(token => MqttTransportHandler.CreateFromConnectionString(DumpyConnectionString).CompleteAsync(Guid.NewGuid().ToString(), token)); }
public async Task MqttTransportHandler_ReceiveAsync_TokenCancellationRequested() { await TestOperationCanceledByToken(token => MqttTransportHandler.CreateFromConnectionString(DumpyConnectionString).ReceiveAsync(new TimeSpan(0, 10, 0), token)); }
public async Task MqttTransportHandler_SendEventAsync_TokenCancellationRequested() { await TestOperationCanceledByToken(token => MqttTransportHandler.CreateFromConnectionString(DumpyConnectionString).SendEventAsync(new Message(), token)); }
public async Task MqttTransportHandler_OpenAsync_TokenCancellationRequested() { await TestOperationCanceledByToken(token => MqttTransportHandler.CreateFromConnectionString(DumpyConnectionString).OpenAsync(true, token)); }