Ejemplo n.º 1
0
        /// <inheritdoc cref="IMqttClientConfigBuilder.UseProxy(string,string?,string?,string?,bool,string[])" />
        public IMqttClientConfigBuilder UseProxy(
            string address,
            string?username     = null,
            string?password     = null,
            string?domain       = null,
            bool bypassOnLocal  = false,
            string[]?bypassList = null)
        {
            Check.NotNull(address, nameof(address));

            _builder.WithProxy(address, username, password, domain, bypassOnLocal, bypassList);
            return(this);
        }
        /// <summary>
        /// Provides the MQTT client options to create an authenticated MQTT
        /// over WebSocket connection to an AWS IoT Device Gateway endpoint.
        /// </summary>
        /// <param name="client">The authenticated AWS IoT Device Gateway client.</param>
        /// <param name="iotEndpointAddress">The AWS account-specific AWS IoT endpoint address.</param>
        /// <param name="cancelToken">A cancellation token that can be used by other objects or threads to receive notice of cancellation.</param>
        public static async Task <IMqttClientOptions> CreateMqttWebSocketClientOptionsAsync(this AmazonIoTDeviceGatewayClient client, string iotEndpointAddress, CancellationToken cancelToken = default)
        {
            if (client is null)
            {
                throw new ArgumentNullException(nameof(client));
            }
            var uriDetails = await client.CreateMqttWebSocketUriAsync(new Model.CreateMqttWebSocketUriRequest
            {
                EndpointAddress = iotEndpointAddress
            }, cancelToken).ConfigureAwait(continueOnCapturedContext: false);

            var optionsBuilder = new MqttClientOptionsBuilder();

            optionsBuilder = optionsBuilder.WithTls();
            optionsBuilder = optionsBuilder.WithWebSocketServer(uriDetails.RequestUri?.ToString());

            IWebProxy iProxy = client.Config.GetWebProxy();

            if (!(iProxy is null))
            {
                Uri proxyUri;
                if (iProxy is Amazon.Runtime.Internal.Util.WebProxy awssdkProxy)
                {
                    proxyUri = awssdkProxy.ProxyUri;
                }
                else
                {
                    proxyUri = new Uri("http://" + client.Config.ProxyHost + ":" + client.Config.ProxyPort);
                }
                var iCreds   = iProxy.Credentials ?? client.Config.ProxyCredentials;
                var netCreds = iCreds?.GetCredential(proxyUri, default);
                optionsBuilder = optionsBuilder.WithProxy(proxyUri.ToString(),
                                                          username: netCreds?.UserName, password: netCreds?.Password, domain: netCreds?.Domain,
                                                          bypassOnLocal: iProxy.IsBypassed(localhostUri)
                                                          );
            }

            var options = optionsBuilder.Build();

            if (options.ChannelOptions is MqttClientWebSocketOptions webSocketOptions)
            {
                webSocketOptions.RequestHeaders = uriDetails.Headers;
            }

            return(options);
        }
Ejemplo n.º 3
0
        /// <summary>
        /// Connects the Sparkplug node to the MQTT broker.
        /// </summary>
        /// <param name="options">The configuration option.</param>
        /// <returns>A <see cref="Task"/> representing any asynchronous operation.</returns>
        private async Task ConnectInternal(SparkplugNodeOptions options)
        {
            options.CancellationToken ??= CancellationToken.None;

            var builder = new MqttClientOptionsBuilder()
                          .WithClientId(options.ClientId)
                          .WithCredentials(options.UserName, options.Password)
                          .WithCleanSession(false)
                          .WithProtocolVersion(MqttProtocolVersion.V311);

            if (options.UseTls)
            {
                builder.WithTls();
            }

            if (options.WebSocketParameters is null)
            {
                builder.WithTcpServer(options.BrokerAddress, options.Port);
            }
            else
            {
                builder.WithWebSocketServer(options.BrokerAddress, options.WebSocketParameters);
            }

            if (options.ProxyOptions != null)
            {
                builder.WithProxy(
                    options.ProxyOptions.Address,
                    options.ProxyOptions.Username,
                    options.ProxyOptions.Password,
                    options.ProxyOptions.Domain,
                    options.ProxyOptions.BypassOnLocal);
            }

            if (this.willMessage != null)
            {
                builder.WithWillMessage(this.willMessage);
            }

            this.ClientOptions = builder.Build();

            await this.Client.ConnectAsync(this.ClientOptions, options.CancellationToken.Value);
        }
Ejemplo n.º 4
0
    /// <summary>
    /// Connects the Sparkplug node to the MQTT broker.
    /// </summary>
    /// <exception cref="ArgumentNullException">The options are null.</exception>
    /// <returns>A <see cref="Task"/> representing any asynchronous operation.</returns>
    private async Task ConnectInternal()
    {
        if (this.options is null)
        {
            throw new ArgumentNullException(nameof(this.options));
        }

        // Increment the session number.
        this.IncrementLastSessionNumber();

        // Reset the sequence number.
        this.ResetLastSequenceNumber();

        // Get the will message.
        var willMessage = this.MessageGenerator.GetSparkPlugNodeDeathMessage(
            this.NameSpace,
            this.options.GroupIdentifier,
            this.options.EdgeNodeIdentifier,
            this.LastSessionNumber);

        // Build up the MQTT client and connect.
        this.options.CancellationToken ??= CancellationToken.None;

        var builder = new MqttClientOptionsBuilder()
                      .WithClientId(this.options.ClientId)
                      .WithCredentials(this.options.UserName, this.options.Password)
                      .WithCleanSession(false)
                      .WithProtocolVersion(MqttProtocolVersion.V311);

        if (this.options.UseTls)
        {
            builder.WithTls();
        }

        if (this.options.WebSocketParameters is null)
        {
            builder.WithTcpServer(this.options.BrokerAddress, this.options.Port);
        }
        else
        {
            builder.WithWebSocketServer(this.options.BrokerAddress, this.options.WebSocketParameters);
        }

        if (this.options.ProxyOptions != null)
        {
            builder.WithProxy(
                this.options.ProxyOptions.Address,
                this.options.ProxyOptions.Username,
                this.options.ProxyOptions.Password,
                this.options.ProxyOptions.Domain,
                this.options.ProxyOptions.BypassOnLocal);
        }

        builder.WithWillMessage(willMessage);
        this.ClientOptions = builder.Build();

        // Debug output.
        this.Logger?.Debug("CONNECT Message: {@ClientOptions}", this.ClientOptions);

        await this.Client.ConnectAsync(this.ClientOptions, this.options.CancellationToken.Value);
    }