private void Channel_OnReceive(object sender, ChannelReceivedEventArgs args) { MqttMessage msg = MqttMessage.DecodeMessage(args.Message); MqttMessageHandler handler = MqttMessageHandler.Create(session, msg); MqttMessage response = handler.ProcessAsync().GetAwaiter().GetResult(); if (response != null) { channel.SendAsync(response.Encode()).GetAwaiter(); } }
private async Task ProcessMessageAsync(MqttMessage message) { try { MqttMessageHandler handler = MqttMessageHandler.Create(session, message); MqttMessage msg = await handler.ProcessAsync(); if (msg != null) { await Channel.SendAsync(msg.Encode()); } } catch (Exception ex) { logger.LogError(ex, $"MQTT adapter ProcessMessageAsync error on channel '{Channel.Id}'."); OnError.Invoke(this, new ProtocolAdapterErrorEventArgs(Channel.Id, ex)); } }
private void Channel_OnReceive(object sender, ChannelReceivedEventArgs args) { MqttMessage msg = MqttMessage.DecodeMessage(args.Message); MqttMessageHandler handler = MqttMessageHandler.Create(session, msg, dispatcher); try { MqttMessage message = handler.ProcessAsync().GetAwaiter().GetResult(); if (message != null) { channel.SendAsync(message.Encode()).GetAwaiter(); } } catch (Exception ex) { Console.WriteLine(ex.Message); Trace.TraceError(ex.Message); OnChannelError?.Invoke(this, new ChannelErrorEventArgs(channel.Id, ex)); } //Task task = Task.Factory.StartNew(async () => //{ // try // { // MqttMessage message = await handler.ProcessAsync(); // if (message != null) // { // await channel.SendAsync(message.Encode()); // } // } // catch(Exception ex) // { // Console.WriteLine(ex.Message); // Trace.TraceError(ex.Message); // } //}); //Task.WaitAll(task); //if (task.Exception != null) //{ // OnChannelError?.Invoke(this, new ChannelErrorEventArgs(channel.Id, task.Exception.InnerException)); //} }
private void Channel_OnReceive(object sender, ChannelReceivedEventArgs args) { MqttMessage msg = MqttMessage.DecodeMessage(args.Message); MqttMessageHandler handler = MqttMessageHandler.Create(session, msg, dispatcher); try { MqttMessage message = handler.ProcessAsync().GetAwaiter().GetResult(); if (message != null) { Channel.SendAsync(message.Encode()).GetAwaiter(); } } catch (Exception ex) { Console.WriteLine(ex.Message); Trace.TraceError(ex.Message); OnChannelError?.Invoke(this, new ChannelErrorEventArgs(Channel.Id, ex)); } }
/// <summary> /// Processes a receive MQTT message and a response or null (no response). /// </summary> /// <param name="message"></param> /// <returns></returns> public async Task <MqttMessage> ReceiveAsync(MqttMessage message) { MqttMessageHandler handler = MqttMessageHandler.Create(this, message); return(await handler.ProcessAsync()); }