Exemplo n.º 1
0
        public void WriteAsync(SocketEventArgs args)
        {
            SentMessages.Add(args.MessageToSend.MessageType);
            args.Complete();

            // Mock a server that does not send appropriate response
            if (DoNotRespond)
            {
                return;
            }

            // This section mocks the expected response behavior from an MQTT broker
            switch (args.MessageToSend.MessageType)
            {
                case MessageType.Connect:
                    var conAck = new MqttConnectAckMessageBuilder();
                    MessageReceived(new MqttNetEventArgs
                    {
                        Message = conAck.GetMessage(),
                        ClientUid = args.ClientUid
                    });
                    break;

                case MessageType.Subscribe:
                    var subMsg = args.MessageToSend as IMqttIdMessage;
                    var subAck = new MqttSubscribeAckMessageBuilder
                    {
                        MessageId = subMsg.MessageId
                   };
                    MessageReceived(new MqttNetEventArgs
                    {
                        Message = subAck.GetMessage(),
                        ClientUid = args.ClientUid
                    });
                    break;

                case MessageType.Unsubscribe:
                    var unsubMsg = args.MessageToSend as IMqttIdMessage;
                    var unsubAck = new MqttUnsubscribeAckMessageBuilder
                    {
                        MessageId = unsubMsg.MessageId
                    };
                    MessageReceived(new MqttNetEventArgs
                    {
                        Message = unsubAck.GetMessage(),
                        ClientUid = args.ClientUid
                    });
                    break;

                case MessageType.PingReq:
                    var pingResp = new MqttPingResponseMessageBuilder();
                    MessageReceived(new MqttNetEventArgs
                    {
                        Message = pingResp.GetMessage(),
                        ClientUid = args.ClientUid
                    });
                    break;

                case MessageType.Publish:
                    var publishMsg = args.MessageToSend as IMqttIdMessage;
                    // Mock publish response behavior
                    if (args.MessageToSend.QualityOfService == QualityOfService.AtLeastOnce)
                    {
                        var msgRcv = new MqttPublishAckMessageBuilder
                        {
                            MessageId = publishMsg.MessageId
                        };
                        MessageReceived(new MqttNetEventArgs
                        {
                            Message = msgRcv.GetMessage(),
                            ClientUid = args.ClientUid
                        });
                    }
                    else if (args.MessageToSend.QualityOfService == QualityOfService.ExactlyOnce)
                    {
                        var msgRcv = new MqttPublishReceivedMessageBuilder()
                        {
                            MessageId = publishMsg.MessageId
                        };
                        MessageReceived(new MqttNetEventArgs
                        {
                            Message = msgRcv.GetMessage(),
                            ClientUid = args.ClientUid
                        });
                    }
                    break;

                case MessageType.PubRec:
                    var pubRec = args.MessageToSend as IMqttIdMessage;
                    var pubRel1 = new MqttPublishReleaseMessageBuilder
                    {
                        MessageId = pubRec.MessageId
                    };
                    MessageReceived(new MqttNetEventArgs
                    {
                        Message = pubRel1.GetMessage(),
                        ClientUid = args.ClientUid
                    });
                    break;

                case MessageType.PubRel:
                    var pubRel2 = args.MessageToSend as IMqttIdMessage;
                    var pubComp = new MqttPublishCompleteMessageBuilder
                    {
                        MessageId = pubRel2.MessageId
                    };
                    MessageReceived(new MqttNetEventArgs
                    {
                        Message = pubComp.GetMessage(),
                        ClientUid = args.ClientUid
                    });
                    break;
            }
        }
Exemplo n.º 2
0
        private void DoClientPubRelWorkflow(MqttNetEventArgs args)
        {
            var publishRelMsg = (IMqttIdMessage)args.Message;

            // Pop the original publish message to pass to the client
            object data = _messageStore.Remove(args.Message, args.ClientUid);
            args.Message = data as MqttPublishMessage;
            if (args.Message != null)
            {
                var pubComp = new MqttPublishCompleteMessageBuilder
                {
                    MessageId = publishRelMsg.MessageId,
                };
                SendMessageAsync(pubComp, null, args.ClientUid);
                OnPublishReceived(args);
            }
            else
            {
                Logger.LogMessage("Protocol", LogLevel.Warning, "Received PubRel message but no pending publishes were found. Client Identifier=" + args.ClientUid);
            }
        }