Example #1
0
        public async Task OpenAsync(TimeSpan timeout, bool useWebSocket, X509Certificate2 clientCert)
        {
            var hostName = _uri.Host;

            var tcpSettings = new TcpTransportSettings {
                Host = hostName, Port = _uri.Port != -1 ? _uri.Port : AmqpConstants.DefaultSecurePort
            };

            TransportSettings = new TlsTransportSettings(tcpSettings)
            {
                TargetHost  = hostName,
                Certificate = clientCert
            };

            TransportBase transport;

            if (useWebSocket)
            {
                transport = await CreateClientWebSocketTransportAsync(timeout).ConfigureAwait(false);

                SaslTransportProvider provider = _amqpSettings.GetTransportProvider <SaslTransportProvider>();
                if (provider != null)
                {
                    _sentHeader = new ProtocolHeader(provider.ProtocolId, provider.DefaultVersion);
                    ByteBuffer buffer = new ByteBuffer(new byte[AmqpConstants.ProtocolHeaderSize]);
                    _sentHeader.Encode(buffer);

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

                    _tcs      = new TaskCompletionSource <TransportBase>();
                    transport = await _tcs.Task.ConfigureAwait(false);

                    await transport.OpenAsync(timeout).ConfigureAwait(false);
                }
            }
            else
            {
                var tcpInitiator = new AmqpTransportInitiator(_amqpSettings, TransportSettings);
                transport = await tcpInitiator.ConnectTaskAsync(timeout).ConfigureAwait(false);
            }

            AmqpConnection         = new AmqpConnection(transport, _amqpSettings, AmqpConnectionSettings);
            AmqpConnection.Closed += OnConnectionClosed;
            await AmqpConnection.OpenAsync(timeout).ConfigureAwait(false);

            _isConnectionClosed = false;
        }
Example #2
0
        public void ValidateSettingsTest()
        {
            // Arrange
            string iotHubHostName      = "foo.azure-devices.net";
            var    authenticator       = Mock.Of <IAuthenticator>();
            var    identityFactory     = Mock.Of <IClientCredentialsFactory>();
            var    linkHandlerProvider = Mock.Of <ILinkHandlerProvider>();
            var    connectionProvider  = Mock.Of <IConnectionProvider>();

            // Act
            AmqpSettings settings = AmqpSettingsProvider.GetDefaultAmqpSettings(iotHubHostName, authenticator, identityFactory, linkHandlerProvider, connectionProvider, new NullCredentialsCache());

            // Assert
            Assert.NotNull(settings);
            Assert.Equal(2, settings.TransportProviders.Count);

            var saslTransportProvider = settings.GetTransportProvider <SaslTransportProvider>();

            Assert.NotNull(saslTransportProvider);

            SaslHandler anonHandler = saslTransportProvider.GetHandler("ANONYMOUS", false);

            Assert.NotNull(anonHandler);

            SaslHandler plainHandler = saslTransportProvider.GetHandler("PLAIN", false);

            Assert.NotNull(plainHandler);

            SaslHandler cbsHandler = saslTransportProvider.GetHandler(Amqp.Constants.ServiceBusCbsSaslMechanismName, false);

            Assert.NotNull(cbsHandler);

            var amqpTransportProvider = settings.GetTransportProvider <AmqpTransportProvider>();

            Assert.NotNull(amqpTransportProvider);

            Assert.Equal(Amqp.Constants.AmqpVersion100, amqpTransportProvider.Versions[0]);
        }
        public async Task OpenAsync(TimeSpan timeout, bool useWebSocket, X509Certificate2 clientCert, IWebProxy proxy, RemoteCertificateValidationCallback remoteCerificateValidationCallback)
        {
            if (Logging.IsEnabled)
            {
                Logging.Enter(this, $"{nameof(AmqpClientConnection)}.{nameof(OpenAsync)}");
            }

            var hostName = _uri.Host;

            var tcpSettings = new TcpTransportSettings {
                Host = hostName, Port = _uri.Port != -1 ? _uri.Port : AmqpConstants.DefaultSecurePort
            };

            TransportSettings = new TlsTransportSettings(tcpSettings)
            {
                TargetHost  = hostName,
                Certificate = clientCert,
                CertificateValidationCallback = remoteCerificateValidationCallback,
            };

            TransportBase transport;

            if (useWebSocket)
            {
                transport = await CreateClientWebSocketTransportAsync(timeout, proxy).ConfigureAwait(false);

                SaslTransportProvider provider = _amqpSettings.GetTransportProvider <SaslTransportProvider>();
                if (provider != null)
                {
                    if (Logging.IsEnabled)
                    {
                        Logging.Info(this, $"{nameof(AmqpClientConnection)}.{nameof(OpenAsync)}: Using SaslTransport");
                    }

                    _sentHeader = new ProtocolHeader(provider.ProtocolId, provider.DefaultVersion);
                    ByteBuffer buffer = new ByteBuffer(new byte[AmqpConstants.ProtocolHeaderSize]);
                    _sentHeader.Encode(buffer);

                    _tcs = 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(AmqpClientConnection)}.{nameof(OpenAsync)}: Sent Protocol Header: {_sentHeader.ToString()} operationPending: {operationPending} completedSynchronously: {args.CompletedSynchronously}");
                    }

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

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

                    await transport.OpenAsync(timeout).ConfigureAwait(false);
                }
            }
            else
            {
                var tcpInitiator = new AmqpTransportInitiator(_amqpSettings, TransportSettings);
                transport = await tcpInitiator.ConnectTaskAsync(timeout).ConfigureAwait(false);
            }

            AmqpConnection         = new AmqpConnection(transport, _amqpSettings, AmqpConnectionSettings);
            AmqpConnection.Closed += OnConnectionClosed;
            await AmqpConnection.OpenAsync(timeout).ConfigureAwait(false);

            _isConnectionClosed = false;
        }
Example #4
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);
        }