Exemplo n.º 1
0
 private void OnConnected(object sender, MqttClientConnectedEventArgs args)
 {
     if (Connected != null)
     {
         _dispatcher.Post(() => Connected.Invoke(_id, sender, args));
     }
 }
        public void ConnectAsync()
        {
            ThreadPool.QueueUserWorkItem(payload =>
            {
                try
                {
                    byte ret = _client.Connect(_clientId, _username, _password, _cleanSession, 30);
                    if (ret == 0)
                    {
                        if (Connected != null)
                        {
                            _dispatcher.Post(() => Connected.Invoke(this, _client.SessionPresent));
                        }
                    }
                    else
                    {
                        switch (ret)
                        {
                        case 5:
                            OnDisconnected(false, new FizzMqttAuthException());
                            break;

                        default:
                            OnDisconnected(false, new FizzMqttConnectException(ret, "connect_failed"));
                            break;
                        }
                    }
                }
                catch (Exception ex)
                {
                    OnDisconnected(false, ex);
                }
            });
        }
Exemplo n.º 3
0
        private void SendRequestAsync(string host,
                                      string path,
                                      string verb,
                                      IDictionary <string, string> headers,
                                      string contentType,
                                      int contentLength,
                                      byte[] content,
                                      Action <HttpStatusCode, byte[], FizzException> callback)
        {
            if (host == null)
            {
                callback(HttpStatusCode.NotFound, null, ERROR_INVALID_HOST);
                return;
            }
            if (path == null)
            {
                callback(HttpStatusCode.NotFound, null, ERROR_INVALID_PATH);
                return;
            }

            try
            {
                HttpWebRequest request = BuildRequest(host + path, verb, contentType, contentLength);

                if (headers != null)
                {
                    foreach (string key in headers.Keys)
                    {
                        request.Headers.Add(key, headers[key]);
                    }
                }

                if (content != null)
                {
                    SendRequestAsync(request, contentLength, content, (status, response, ex) =>
                    {
                        _dispatcher.Post(() => callback(status, response, ex));
                    });
                }
                else
                {
                    GetResponseAsync(request, (status, response, ex) =>
                    {
                        _dispatcher.Post(() => callback(status, response, ex));
                    });
                }
            }
            catch (Exception ex)
            {
                callback(HttpStatusCode.NotFound, null, new FizzException(FizzError.ERROR_REQUEST_FAILED, ex.Message));
            }
        }
Exemplo n.º 4
0
        public void DisconnectAsync()
        {
            _manualDisconnect = true;

            if (IsConnected)
            {
                _client.DisconnectAsync();
            }
            else
            {
                if (Disconnected != null)
                {
                    _dispatcher.Post(() => Disconnected.Invoke(this, new FizzMqttDisconnectedArgs(false, null)));
                }
            }
        }
Exemplo n.º 5
0
        public FizzMQTTConnection(string username,
                                  string password,
                                  string clientId,
                                  bool retry,
                                  bool cleanSession,
                                  IFizzActionDispatcher dispatcher)
        {
            if (string.IsNullOrEmpty(clientId))
            {
                throw ERROR_INVALID_CLIENT_ID;
            }
            if (string.IsNullOrEmpty(username))
            {
                throw ERROR_INVALID_USERNAME;
            }
            if (string.IsNullOrEmpty(password))
            {
                throw ERROR_INVALID_PASSWORD;
            }
            if (dispatcher == null)
            {
                throw ERROR_INVALID_DISPATCHER;
            }

            _clientOptions = new MqttClientOptionsBuilder()
                             .WithClientId(clientId)
                             .WithTcpServer(FizzConfig.MQTT_HOST_ENDPOINT)
                             .WithCredentials(username, password)
                             .WithTls()
                             .WithCleanSession(cleanSession)
                             .WithKeepAlivePeriod(TimeSpan.FromSeconds(30))
                             .Build();

            _retry      = retry;
            _dispatcher = dispatcher;

            var clientFactory = new MqttFactory();

            _client = clientFactory.CreateMqttClient();


            _client.UseDisconnectedHandler(discArgs =>
            {
                if (discArgs.Exception is MqttConnectingFailedException failedException)
                {
                    if (failedException.ResultCode == MqttClientConnectResultCode.BadUserNameOrPassword ||
                        failedException.ResultCode == MqttClientConnectResultCode.NotAuthorized)
                    {
                        OnDisconnected(discArgs.ClientWasConnected, new FizzMqttAuthException());
                    }
                }
                else if (discArgs.Exception != null)
                {
                    OnDisconnected(discArgs.ClientWasConnected, discArgs.Exception);
                }
                else
                {
                    OnDisconnected(discArgs.ClientWasConnected, null);
                }
            });

            _client.UseConnectedHandler(conArgs => {
                if (_manualDisconnect)
                {
                    return;
                }

                if (Connected != null)
                {
                    _dispatcher.Post(() => Connected.Invoke(this, conArgs.AuthenticateResult.IsSessionPresent));
                }
            });

            _client.UseApplicationMessageReceivedHandler(OnMqttMessageReceived);
        }