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; }
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); } }
Task ProcessAckAsync(IChannelHandlerContext context, PublishWorkItem publish) { publish.Completion.Complete(); return TaskConstants.Completed; }