Пример #1
0
        public async Task ConnectAsync(MqttClientOptions options)
        {
            if (options == null)
            {
                throw new ArgumentNullException(nameof(options));
            }
            try
            {
                if (!options.TlsOptions.UseTls)
                {
                    await _socket.ConnectAsync(new HostName(options.Server), options.GetPort().ToString());
                }
                else
                {
                    _socket.Control.ClientCertificate = LoadCertificate(options);

                    if (!options.TlsOptions.CheckCertificateRevocation)
                    {
                        _socket.Control.IgnorableServerCertificateErrors.Add(ChainValidationResult.IncompleteChain);
                        _socket.Control.IgnorableServerCertificateErrors.Add(ChainValidationResult.RevocationInformationMissing);
                    }

                    await _socket.ConnectAsync(new HostName(options.Server), options.GetPort().ToString(), SocketProtectionLevel.Tls12);
                }
            }
            catch (SocketException exception)
            {
                throw new MqttCommunicationException(exception);
            }
        }
Пример #2
0
        public async Task ConnectAsync(MqttClientOptions options)
        {
            if (options == null)
            {
                throw new ArgumentNullException(nameof(options));
            }

            if (_socket == null)
            {
                _socket = new StreamSocket();
            }

            if (!options.TlsOptions.UseTls)
            {
                await _socket.ConnectAsync(new HostName(options.Server), options.GetPort().ToString());
            }
            else
            {
                _socket.Control.ClientCertificate = LoadCertificate(options);

                if (!options.TlsOptions.CheckCertificateRevocation)
                {
                    _socket.Control.IgnorableServerCertificateErrors.Add(ChainValidationResult.IncompleteChain);
                    _socket.Control.IgnorableServerCertificateErrors.Add(ChainValidationResult.RevocationInformationMissing);
                }

                await _socket.ConnectAsync(new HostName(options.Server), options.GetPort().ToString(), SocketProtectionLevel.Tls12);
            }

            SendStream       = _socket.OutputStream.AsStreamForWrite();
            ReceiveStream    = _socket.InputStream.AsStreamForRead();
            RawReceiveStream = ReceiveStream;
        }
Пример #3
0
        public async Task ConnectAsync(MqttClientOptions options)
        {
            if (options == null)
            {
                throw new ArgumentNullException(nameof(options));
            }

            if (_socket == null)
            {
                _socket = new Socket(SocketType.Stream, ProtocolType.Tcp);
            }

            await _socket.ConnectAsync(options.Server, options.GetPort()).ConfigureAwait(false);

            if (options.TlsOptions.UseTls)
            {
                _sslStream    = new SslStream(new NetworkStream(_socket, true));
                ReceiveStream = _sslStream;
                await _sslStream.AuthenticateAsClientAsync(options.Server, LoadCertificates(options), SslProtocols.Tls12, options.TlsOptions.CheckCertificateRevocation).ConfigureAwait(false);
            }
            else
            {
                ReceiveStream = new NetworkStream(_socket);
            }
        }
Пример #4
0
        public async Task ConnectAsync(MqttClientOptions options)
        {
            if (options == null)
            {
                throw new ArgumentNullException(nameof(options));
            }
            try
            {
                if (_socket == null)
                {
                    _socket = new Socket(SocketType.Stream, ProtocolType.Tcp);
                }

                await _socket.ConnectAsync(options.Server, options.GetPort());

                if (options.TlsOptions.UseTls)
                {
                    _sslStream = new SslStream(new NetworkStream(_socket, true), true, (sender, certificate, chain, errors) => true);
                    //await _sslStream.AuthenticateAsClientAsync(options.Server, LoadCertificates(options), SslProtocols.Tls11, options.TlsOptions.CheckCertificateRevocation);
                    await _sslStream.AuthenticateAsClientAsync(options.Server, _certificateCollection, SslProtocols.Tls11, options.TlsOptions.CheckCertificateRevocation);
                }
            }
            catch (SocketException exception)
            {
                throw new MqttCommunicationException(exception);
            }
        }
Пример #5
0
        public async Task ConnectAsync(MqttClientOptions options)
        {
            if (options == null)
            {
                throw new ArgumentNullException(nameof(options));
            }
            try
            {
                await _socket.ConnectAsync(options.Server, options.GetPort());

                if (options.TlsOptions.UseTls)
                {
                    _sslStream = new SslStream(new NetworkStream(_socket, true));
                    await _sslStream.AuthenticateAsClientAsync(options.Server, LoadCertificates(options), SslProtocols.Tls12, options.TlsOptions.CheckCertificateRevocation);
                }
            }
            catch (SocketException exception)
            {
                throw new MqttCommunicationException(exception);
            }
        }
Пример #6
0
        public async Task ConnectAsync(MqttClientOptions options)
        {
            if (options == null)
            {
                throw new ArgumentNullException(nameof(options));
            }

            if (_socket == null)
            {
                _socket = new Socket(SocketType.Stream, ProtocolType.Tcp);
            }

            await Task.Factory.FromAsync(_socket.BeginConnect, _socket.EndConnect, options.Server, options.GetPort(), null).ConfigureAwait(false);

            if (options.TlsOptions.UseTls)
            {
                _sslStream = new SslStream(new NetworkStream(_socket, true));

                await _sslStream.AuthenticateAsClientAsync(options.Server, LoadCertificates(options), SslProtocols.Tls12, options.TlsOptions.CheckCertificateRevocation).ConfigureAwait(false);
            }

            CreateStreams(_socket, _sslStream);
        }