Exemple #1
0
        public MqttClient(MqttClientOptions options, IMqttCommunicationAdapter adapter)
        {
            _options = options ?? throw new ArgumentNullException(nameof(options));
            _adapter = adapter ?? throw new ArgumentNullException(nameof(adapter));

            _adapter.PacketSerializer.ProtocolVersion = options.ProtocolVersion;
        }
Exemple #2
0
        public static int GetPort(this MqttClientOptions options)
        {
            if (options == null)
            {
                throw new ArgumentNullException(nameof(options));
            }

            if (options.Port.HasValue)
            {
                return(options.Port.Value);
            }

            return(!options.TlsOptions.UseTls ? 1883 : 8883);
        }
Exemple #3
0
        public async Task ConnectAsync(MqttClientOptions options)
        {
            if (options == null)
            {
                throw new ArgumentNullException(nameof(options));
            }

            ThrowIfConnected("It is not allowed to connect with a server after the connection is established.");

            try
            {
                _options = options;
                _cancellationTokenSource = new CancellationTokenSource();
                _latestPacketIdentifier  = 0;
                _packetDispatcher.Reset();

                _adapter = _communicationChannelFactory.CreateMqttCommunicationAdapter(options);

                MqttNetTrace.Verbose(nameof(MqttClient), "Trying to connect with server.");
                await _adapter.ConnectAsync(_options.DefaultCommunicationTimeout).ConfigureAwait(false);

                MqttNetTrace.Verbose(nameof(MqttClient), "Connection with server established.");

                await SetupIncomingPacketProcessingAsync();
                await AuthenticateAsync(options.WillMessage);

                MqttNetTrace.Verbose(nameof(MqttClient), "MQTT connection with server established.");

                if (_options.KeepAlivePeriod != TimeSpan.Zero)
                {
                    StartSendKeepAliveMessages(_cancellationTokenSource.Token);
                }

                Connected?.Invoke(this, EventArgs.Empty);
            }
            catch (Exception)
            {
                await DisconnectInternalAsync().ConfigureAwait(false);

                throw;
            }
        }
Exemple #4
0
 public MqttClient(MqttClientOptions options, IMqttCommunicationAdapter adapter)
 {
     _options = options ?? throw new ArgumentNullException(nameof(options));
     _adapter = adapter ?? throw new ArgumentNullException(nameof(adapter));
 }