Beispiel #1
0
        private void Session_OnRetry(object sender, MqttMessageEventArgs args)
        {
            MqttMessage msg = args.Message;

            msg.Dup = true;
            channel.SendAsync(msg.Encode()).GetAwaiter();
        }
Beispiel #2
0
 public void SendMessage(MqttMessage message)
 {
     Debug.WriteLine("onSend:{0}", message.FixedHeader.MessageType);
     using (var stream = new MemoryStream())
     {
         message.Encode(stream);
         stream.Seek(0, SeekOrigin.Begin);
         socket.Send(stream.ToArray(), SocketFlags.None);
     }
 }
Beispiel #3
0
        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));
            }
        }
Beispiel #5
0
        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));
            //}
        }
Beispiel #6
0
        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));
            }
        }