فهرست منبع

fix: msgData字段添加topicLen字段

RyanCW 1 سال پیش
والد
کامیت
2e8f0ce3fe
4فایلهای تغییر یافته به همراه8 افزوده شده و 6 حذف شده
  1. 2 2
      example/RyanMqttTest.c
  2. 1 0
      mqttclient/RyanMqttClient.h
  3. 1 0
      mqttclient/RyanMqttThread.c
  4. 4 4
      test/RyanMqttTestLinux.c

+ 2 - 2
example/RyanMqttTest.c

@@ -109,8 +109,8 @@ static void mqttEventHandle(void *pclient, RyanMqttEventId_e event, const void c
     case RyanMqttEventData:
     {
         RyanMqttMsgData_t *msgData = (RyanMqttMsgData_t *)eventData;
-        rlog_i("接收到mqtt消息事件回调 topic: %s, packetId: %d, payload len: %d",
-               msgData->topic, msgData->packetId, msgData->payloadLen);
+        rlog_i("接收到mqtt消息事件回调 topic: %.*s, packetId: %d, payload len: %d",
+               msgData->topicLen, msgData->topic, msgData->packetId, msgData->payloadLen);
 
         rlog_i("%.*s", msgData->payloadLen, msgData->payload);
 

+ 1 - 0
mqttclient/RyanMqttClient.h

@@ -27,6 +27,7 @@ extern "C"
         uint16_t packetId;   // packetId 系统生成
         RyanMqttQos_e qos;   // QOS等级
         uint32_t payloadLen; // 数据长度
+        uint32_t topicLen;   // topic长度
         char *topic;         // 主题信息
         char *payload;       // 数据内容
     } RyanMqttMsgData_t;

+ 1 - 0
mqttclient/RyanMqttThread.c

@@ -240,6 +240,7 @@ static RyanMqttError_e RyanMqttPublishPacketHandler(RyanMqttClient_t *client)
     RyanMqttCheck(1 == result, RyanMqttDeserializePacketError, rlog_d);
 
     msgData.topic = topicName.lenstring.data;
+    msgData.topicLen = topicName.lenstring.len;
 
     // 查看订阅列表是否包含此消息主题,进行通配符匹配。不包含就直接退出在一定程度上可以防止恶意攻击
     result = RyanMqttMsgHandlerFind(client, topicName.lenstring.data, topicName.lenstring.len, RyanMqttTrue, &msgHandler);

+ 4 - 4
test/RyanMqttTestLinux.c

@@ -2,7 +2,7 @@
 #define RyanMqttClientId ("RyanMqttTest888") // 填写mqtt客户端id,要求唯一
 // #define RyanMqttHost ("broker.emqx.io")    // 填写你的mqtt服务器ip
 #define RyanMqttHost ("127.0.0.1") // 填写你的mqtt服务器ip
-#define RyanMqttPort (1883)      // mqtt服务器端口
+#define RyanMqttPort (1883)        // mqtt服务器端口
 #define RyanMqttUserName (NULL)    // 填写你的用户名
 #define RyanMqttPassword (NULL)    // 填写你的密码
 
@@ -119,8 +119,8 @@ static void mqttEventHandle(void *pclient, RyanMqttEventId_e event, const void c
     case RyanMqttEventData:
     {
         RyanMqttMsgData_t *msgData = (RyanMqttMsgData_t *)eventData;
-        rlog_i("接收到mqtt消息事件回调 topic: %s, packetId: %d, payload len: %d",
-               msgData->topic, msgData->packetId, msgData->payloadLen);
+        rlog_i("接收到mqtt消息事件回调 topic: %.*s, packetId: %d, payload len: %d",
+               msgData->topicLen, msgData->topic, msgData->packetId, msgData->payloadLen);
 
         rlog_i("%.*s", msgData->payloadLen, msgData->payload);
         mqttTest[dataEventCount]++;
@@ -604,7 +604,7 @@ int main()
     if (result != 0)
         goto __exit;
     checkMemory;
-    
+
     result = RyanMqttPublishTest(RyanMqttQos2, 1000, 1);
     if (result != 0)
         goto __exit;