Exemple #1
0
        protected override bool SendData(byte[] buffer, object state)
        {
            var message = new MqttApplicationMessageBuilder()
                          .WithTopic(topic)
                          .WithPayload(buffer)
                          .WithExactlyOnceQoS()
                          .Build();

            if (!mqttClient.PublishAsync(message).Wait(TIMEOUT, cancellationToken.Token))
            {
                var e = new TimeoutException();
                Tracker.LogE(TAG, e);
                using (new MethodUtils.Unlocker(this)) {
                    OnExceptionCallback?.Invoke(e);
                }
                return(false);
            }

            using (new MethodUtils.Unlocker(this)) {
                OnSendCompletedCallback?.Invoke(state);
            }
            return(true);
        }
Exemple #2
0
        /// <summary>
        /// 异步连接
        /// </summary>
        private async Task ConnectAsync()
        {
            while (true)
            {
                try {
                    // 连接
                    await mqttClient.ConnectAsync(options, cancellationToken.Token);

                    using (new MethodUtils.Unlocker(this)) {
                        OnConnectedCallback?.Invoke();
                    }

                    return;
                }
                catch (Exception e) {
                    Tracker.LogNW(TAG, "connect fail");
                    using (new MethodUtils.Unlocker(this)) {
                        OnExceptionCallback?.Invoke(e);
                    }
                    await Task.Delay(TimeSpan.FromMilliseconds(RETRY_DURATION));
                }
            }
        }
Exemple #3
0
 private void OnException(Exception e)
 {
     using (new MethodUtils.Unlocker(this)) {
         OnExceptionCallback?.Invoke(e);
     }
 }