Exemple #1
0
        internal async Task <TransportBase> InitializeAsync(TimeSpan timeout)
        {
            Logging.Enter(this, timeout, nameof(InitializeAsync));

            TransportBase transport;

            switch (_amqpTransportSettings.GetTransportType())
            {
            case TransportType.Amqp_WebSocket_Only:
                transport = _clientWebSocketTransport = (ClientWebSocketTransport) await CreateClientWebSocketTransportAsync(timeout)
                                                        .ConfigureAwait(false);

                break;

            case TransportType.Amqp_Tcp_Only:
                var amqpTransportInitiator = new AmqpTransportInitiator(_amqpSettings, _tlsTransportSettings);
                transport = await amqpTransportInitiator.ConnectTaskAsync(timeout).ConfigureAwait(false);

                break;

            default:
                throw new InvalidOperationException("AmqpTransportSettings must specify WebSocketOnly or TcpOnly");
            }
            Logging.Exit(this, timeout, nameof(InitializeAsync));

            return(transport);
        }
        public AmqpTransportHandler(IotHubConnectionString connectionString, AmqpTransportSettings transportSettings)
        {
            this.transportType = transportSettings.GetTransportType();
            switch (this.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(this.transportType));
            }

            this.deviceId                              = connectionString.DeviceId;
            this.openTimeout                           = IotHubConnection.DefaultOpenTimeout;
            this.operationTimeout                      = IotHubConnection.DefaultOperationTimeout;
            this.DefaultReceiveTimeout                 = IotHubConnection.DefaultOperationTimeout;
            this.faultTolerantEventSendingLink         = new Client.FaultTolerantAmqpObject <SendingAmqpLink>(this.CreateEventSendingLinkAsync, this.IotHubConnection.CloseLink);
            this.faultTolerantDeviceBoundReceivingLink = new Client.FaultTolerantAmqpObject <ReceivingAmqpLink>(this.CreateDeviceBoundReceivingLinkAsync, this.IotHubConnection.CloseLink);
            this.prefetchCount                         = transportSettings.PrefetchCount;
        }
Exemple #3
0
        public void TransportSettingsTest_TransportType_Amqp_WebSocket_Tcp()
        {
            var transportSetting = new AmqpTransportSettings(TransportType.Amqp_Tcp_Only, 200);

            Assert.IsTrue(transportSetting.GetTransportType() == TransportType.Amqp_Tcp_Only, "Should be TransportType.Amqp_Tcp_Only");
            Assert.IsTrue(transportSetting.PrefetchCount == 200, "Should be value of 200");
        }
Exemple #4
0
        public void TransportSettingsTest_TransportType_Amqp_WebSocket()
        {
            var transportSetting = new AmqpTransportSettings(TransportType.Amqp_WebSocket_Only);

            Assert.IsTrue(transportSetting.GetTransportType() == TransportType.Amqp_WebSocket_Only, "Should be TransportType.Amqp_WebSocket_Only");
            Assert.IsTrue(transportSetting.PrefetchCount == 50, "Should be default value of 50");
        }
        internal AmqpTransportHandler(
            IPipelineContext context, IotHubConnectionString connectionString,
            AmqpTransportSettings transportSettings,
            Action <object, EventArgs> onLinkClosedCallback,
            Func <MethodRequestInternal, Task> onMethodCallback = null)
            : base(context, transportSettings)
        {
            this.linkClosedListener = onLinkClosedCallback;

            TransportType transportType = transportSettings.GetTransportType();

            this.deviceId = connectionString.DeviceId;
            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.openTimeout      = transportSettings.OpenTimeout;
            this.operationTimeout = transportSettings.OperationTimeout;
            this.prefetchCount    = transportSettings.PrefetchCount;
            this.faultTolerantEventSendingLink         = new Client.FaultTolerantAmqpObject <SendingAmqpLink>(this.CreateEventSendingLinkAsync, this.IotHubConnection.CloseLink);
            this.faultTolerantDeviceBoundReceivingLink = new Client.FaultTolerantAmqpObject <ReceivingAmqpLink>(this.CreateDeviceBoundReceivingLinkAsync, this.IotHubConnection.CloseLink);
            this.iotHubConnectionString = connectionString;
            this.messageListener        = onMethodCallback;
        }
        public void TransportSettingsTestTransportTypeAmqpWebSocketTcp()
        {
            var amqpConnectionPoolSettings = new AmqpConnectionPoolSettings();
            var transportSetting           = new AmqpTransportSettings(TransportType.Amqp_Tcp_Only, 200, amqpConnectionPoolSettings);

            Assert.IsTrue(transportSetting.GetTransportType() == TransportType.Amqp_Tcp_Only, "Should be TransportType.Amqp_Tcp_Only");
            Assert.IsTrue(transportSetting.PrefetchCount == 200, "Should be value of 200");
        }
Exemple #7
0
        public override int GetHashCode()
        {
            int hashCode = UpdateHashCode(620602339, IotHubConnectionString.DeviceId);

            hashCode = UpdateHashCode(hashCode, IotHubConnectionString.HostName);
            hashCode = UpdateHashCode(hashCode, IotHubConnectionString.ModuleId);
            hashCode = UpdateHashCode(hashCode, AmqpTransportSettings.GetTransportType());
            hashCode = UpdateHashCode(hashCode, AuthenticationModel);
            return(hashCode);
        }
Exemple #8
0
 public override bool Equals(object obj)
 {
     return(obj is DeviceIdentity identity &&
            GetHashCode() == identity.GetHashCode() &&
            Equals(IotHubConnectionString.DeviceId, identity.IotHubConnectionString.DeviceId) &&
            Equals(IotHubConnectionString.HostName, identity.IotHubConnectionString.HostName) &&
            Equals(IotHubConnectionString.ModuleId, identity.IotHubConnectionString.ModuleId) &&
            Equals(AmqpTransportSettings.GetTransportType(), identity.AmqpTransportSettings.GetTransportType()) &&
            Equals(AuthenticationModel.GetHashCode(), identity.AuthenticationModel.GetHashCode()));
 }
        public void AmqpTransportSettings_RespectsCtorParameters()
        {
            // arrange
            const TransportType transportType = TransportType.Amqp_Tcp_Only;
            const uint          prefetchCount = 200;

            // act
            var transportSetting = new AmqpTransportSettings(transportType, prefetchCount, new AmqpConnectionPoolSettings());

            // assert
            Assert.AreEqual(transportType, transportSetting.GetTransportType(), "Should match initialized value");
            Assert.AreEqual(prefetchCount, transportSetting.PrefetchCount, "Should match initialized value");
        }
        public void AmqpTransportSettings_DefaultPropertyValues()
        {
            // arrange
            const TransportType transportType = TransportType.Amqp_WebSocket_Only;
            const uint          prefetchCount = 50;

            // act
            var transportSetting = new AmqpTransportSettings(transportType);

            // assert
            Assert.AreEqual(transportType, transportSetting.GetTransportType(), "Should match initialized value");
            Assert.AreEqual(prefetchCount, transportSetting.PrefetchCount, "Should default to 50");
        }
Exemple #11
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;
        }
Exemple #12
0
        public AmqpIotTransport(
            AmqpSettings amqpSettings,
            AmqpTransportSettings amqpTransportSettings,
            string hostName,
            bool disableServerCertificateValidation)
        {
            _amqpSettings          = amqpSettings;
            _amqpTransportSettings = amqpTransportSettings;
            _hostName = hostName;
            _disableServerCertificateValidation = disableServerCertificateValidation;

            var tcpTransportSettings = new TcpTransportSettings
            {
                Host = hostName,
                Port = AmqpConstants.DefaultSecurePort,
            };

            SslProtocols protocols = TlsVersions.Instance.Preferred;

#if NET451
            // Requires hardcoding in NET451 otherwise yields error:
            //    System.ArgumentException: The specified value is not valid in the 'SslProtocolType' enumeration.
            if (amqpTransportSettings.GetTransportType() == TransportType.Amqp_Tcp_Only &&
                protocols == SslProtocols.None)
            {
                protocols = TlsVersions.Instance.MinimumTlsVersions;
            }
#endif

            _tlsTransportSettings = new TlsTransportSettings(tcpTransportSettings)
            {
                TargetHost  = hostName,
                Certificate = null,
                CertificateValidationCallback = _amqpTransportSettings.RemoteCertificateValidationCallback
                                                ?? OnRemoteCertificateValidation,
                Protocols = protocols,
            };

            if (_amqpTransportSettings.ClientCertificate != null)
            {
                _tlsTransportSettings.Certificate = _amqpTransportSettings.ClientCertificate;
            }
        }
 public AmqpTransportHandler(IotHubConnectionString connectionString, AmqpTransportSettings transportSettings)
 {
     this.transportType = transportSettings.GetTransportType();
     switch (this.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(this.transportType));
     }
     
     this.deviceId = connectionString.DeviceId;
     this.openTimeout = IotHubConnection.DefaultOpenTimeout;
     this.operationTimeout = IotHubConnection.DefaultOperationTimeout;
     this.DefaultReceiveTimeout = IotHubConnection.DefaultOperationTimeout;
     this.faultTolerantEventSendingLink = new Client.FaultTolerantAmqpObject<SendingAmqpLink>(this.CreateEventSendingLinkAsync, this.IotHubConnection.CloseLink);
     this.faultTolerantDeviceBoundReceivingLink = new Client.FaultTolerantAmqpObject<ReceivingAmqpLink>(this.CreateDeviceBoundReceivingLinkAsync, this.IotHubConnection.CloseLink);
     this.prefetchCount = transportSettings.PrefetchCount;
 }
Exemple #14
0
        private async Task <TransportBase> InitializeTransport(TimeSpan timeout)
        {
            if (Logging.IsEnabled)
            {
                Logging.Enter(this, timeout, $"{nameof(InitializeTransport)}");
            }
            TransportBase transport;

            switch (AmqpTransportSettings.GetTransportType())
            {
            case TransportType.Amqp_WebSocket_Only:
                transport = await CreateClientWebSocketTransportAsync(timeout).ConfigureAwait(false);

                SaslTransportProvider provider = AmqpSettings.GetTransportProvider <SaslTransportProvider>();
                if (provider != null)
                {
                    if (Logging.IsEnabled)
                    {
                        Logging.Info(this, $"{nameof(InitializeTransport)}: Using SaslTransport");
                    }
                    SentProtocolHeader = new ProtocolHeader(provider.ProtocolId, provider.DefaultVersion);
                    ByteBuffer buffer = new ByteBuffer(new byte[AmqpConstants.ProtocolHeaderSize]);
                    SentProtocolHeader.Encode(buffer);

                    TaskCompletionSource = new TaskCompletionSource <TransportBase>();

                    var args = new TransportAsyncCallbackArgs();
                    args.SetBuffer(buffer.Buffer, buffer.Offset, buffer.Length);
                    args.CompletedCallback = OnWriteHeaderComplete;
                    args.Transport         = transport;
                    bool operationPending = transport.WriteAsync(args);

                    if (Logging.IsEnabled)
                    {
                        Logging.Info(this, $"{nameof(InitializeTransport)}: Sent Protocol Header: {SentProtocolHeader.ToString()} operationPending: {operationPending} completedSynchronously: {args.CompletedSynchronously}");
                    }

                    if (!operationPending)
                    {
                        args.CompletedCallback(args);
                    }

                    transport = await TaskCompletionSource.Task.ConfigureAwait(false);

                    await transport.OpenAsync(timeout).ConfigureAwait(false);
                }
                break;

            case TransportType.Amqp_Tcp_Only:
                var amqpTransportInitiator = new AmqpTransportInitiator(AmqpSettings, TlsTransportSettings);
                transport = await amqpTransportInitiator.ConnectTaskAsync(timeout).ConfigureAwait(false);

                break;

            default:
                throw new InvalidOperationException("AmqpTransportSettings must specify WebSocketOnly or TcpOnly");
            }
            if (Logging.IsEnabled)
            {
                Logging.Exit(this, timeout, $"{nameof(InitializeTransport)}");
            }
            return(transport);
        }