async Task SendMessageAsync(IChannelHandlerContext context, Message message)
        {
            string topicName = string.Format(TelemetryTopicFormat, this.deviceId);

            PublishPacket packet = await Util.ComposePublishPacketAsync(context, message, this.mqttTransportSettings.PublishToServerQoS, topicName);

            var publishCompletion = new TaskCompletionSource();
            var workItem          = new PublishWorkItem
            {
                Completion = publishCompletion,
                Value      = packet
            };

            switch (this.mqttTransportSettings.PublishToServerQoS)
            {
            case QualityOfService.AtMostOnce:
                this.serviceBoundOneWayProcessor.Post(context, workItem);
                break;

            case QualityOfService.AtLeastOnce:
                this.serviceBoundTwoWayProcessor.Post(context, workItem);
                break;

            default:
                throw new NotSupportedException($"Unsupported telemetry QoS: '{this.mqttTransportSettings.PublishToServerQoS}'");
            }
            await publishCompletion.Task;
        }
Example #2
0
        async Task SendMessageAsync(IChannelHandlerContext context, Message message)
        {
            if (Logging.IsEnabled)
            {
                Logging.Enter(this, context.Name, message, nameof(SendMessageAsync));
            }

            string           topicName;
            QualityOfService qos;

            if (string.IsNullOrEmpty(message.MqttTopicName))
            {
                topicName = this.GetTelemetryTopicName();
                qos       = this.mqttTransportSettings.PublishToServerQoS;
            }
            else
            {
                topicName = message.MqttTopicName;
                qos       = QualityOfService.AtMostOnce;
            }

            PublishPacket packet = await Util.ComposePublishPacketAsync(context, message, qos, topicName).ConfigureAwait(true);

            var publishCompletion = new TaskCompletionSource();
            var workItem          = new PublishWorkItem
            {
                Completion = publishCompletion,
                Value      = packet
            };

            if (Logging.IsEnabled)
            {
                Logging.Info(this, $"id={packet.PacketId} qos={packet.QualityOfService} topic={packet.TopicName}", nameof(SendMessageAsync));
            }

            switch (qos)
            {
            case QualityOfService.AtMostOnce:
                this.serviceBoundOneWayProcessor.Post(context, workItem);
                break;

            case QualityOfService.AtLeastOnce:
                this.serviceBoundTwoWayProcessor.Post(context, workItem);
                break;

            default:
                throw new NotSupportedException($"Unsupported telemetry QoS: '{this.mqttTransportSettings.PublishToServerQoS}'");
            }

            await publishCompletion.Task.ConfigureAwait(true);

            if (Logging.IsEnabled)
            {
                Logging.Exit(this, context.Name, message, nameof(SendMessageAsync));
            }
        }
Example #3
0
        Task ProcessAckAsync(IChannelHandlerContext context, PublishWorkItem publish)
        {
            if (Logging.IsEnabled)
            {
                Logging.Enter(this, context.Name, publish?.Value, nameof(ProcessAckAsync));
            }

            publish.Completion.Complete();

            if (Logging.IsEnabled)
            {
                Logging.Exit(this, context.Name, publish?.Value, nameof(ProcessAckAsync));
            }

            return(TaskConstants.Completed);
        }
        async Task SendMessageToServerAsync(IChannelHandlerContext context, PublishWorkItem publish)
        {
            if (!this.IsInState(StateFlags.Connected))
            {
                publish.Completion.TrySetCanceled();
            }
            try
            {
                await Util.WriteMessageAsync(context, publish.Value, ShutdownOnWriteErrorHandler);

                if (publish.Value.QualityOfService == QualityOfService.AtMostOnce)
                {
                    publish.Completion.TryComplete();
                }
            }
            catch (Exception ex)
            {
                publish.Completion.TrySetException(ex);
            }
        }
Example #5
0
        async Task SendMessageToServerAsync(IChannelHandlerContext context, PublishWorkItem publish)
        {
            if (Logging.IsEnabled)
            {
                Logging.Enter(this, context.Name, publish?.Value, nameof(SendMessageToServerAsync));
            }

            if (!this.IsInState(StateFlags.Connected))
            {
                publish.Completion.TrySetCanceled();
            }
            try
            {
                await Util.WriteMessageAsync(context, publish.Value, ShutdownOnWriteErrorHandler).ConfigureAwait(true);

                if (publish.Value.QualityOfService == QualityOfService.AtMostOnce)
                {
                    publish.Completion.TryComplete();
                }
            }
            catch (Exception ex)
            {
                if (Logging.IsEnabled)
                {
                    Logging.Error(context.Handler, $"Context: {context.Name}: {ex.ToString()}", nameof(SendMessageToServerAsync));
                }

                publish.Completion.TrySetException(ex);
            }
            finally
            {
                if (Logging.IsEnabled)
                {
                    Logging.Exit(this, context.Name, publish?.Value, nameof(SendMessageToServerAsync));
                }
            }
        }
 Task ProcessAckAsync(IChannelHandlerContext context, PublishWorkItem publish)
 {
     publish.Completion.Complete();
     return(TaskConstants.Completed);
 }