Explorar o código

destory客户端逻辑修改、quecopen接口修改

ryancw %!s(int64=2) %!d(string=hai) anos
pai
achega
dd9f1458bc

+ 6 - 72
mqttclient/RyanMqttClient.c

@@ -95,7 +95,9 @@ RyanMqttError_e RyanMqttInit(RyanMqttClient_t **pClient)
 
 /**
  * @brief 销毁mqtt客户端
- *
+ *  由于直接删除mqtt线程是很危险的行为。这里设置标志位,由mqtt线程自己删除自己所有资源。
+ *  mqtt删除自己的延时最大不会超过config里面 recvTimeout + 1秒
+ *  mqtt删除自己前会调用 RyanMqttEventDestoryBefore 事件回调
  * @param client
  * @return RyanMqttError_e
  */
@@ -104,77 +106,9 @@ RyanMqttError_e RyanMqttDestroy(RyanMqttClient_t *client)
 
     RyanMqttCheck(NULL != client, RyanMqttParamInvalidError, rlog_d);
 
-    RyanMqttEventMachine(client, RyanMqttEventDestoryBefore, (void *)NULL);
-
-    // 先清除掉线程
-    if (NULL != client->mqttThread)
-    {
-        platformThreadDestroy(client->config->userData, client->mqttThread);
-        platformMemoryFree(client->mqttThread);
-        client->mqttThread = NULL;
-    }
-
-    // 清除网络组件
-    if (NULL != client->network)
-    {
-        platformNetworkClose(client->config->userData, client->network);
-        platformMemoryFree(client->network);
-        client->network = NULL;
-    }
-
-    // 清除互斥锁
-    if (NULL != client->sendBufLock)
-    {
-        platformMutexDestroy(client->config->userData, client->sendBufLock);
-        platformMemoryFree(client->sendBufLock);
-        client->sendBufLock = NULL;
-    }
-
-    // 清除config信息
-    if (NULL != client->config)
-    {
-        if (RyanMqttTrue != client->config->recvBufferStaticFlag && NULL != client->config->recvBuffer)
-            platformMemoryFree(client->config->recvBuffer);
-
-        if (RyanMqttTrue != client->config->sendBufferStaticFlag && NULL != client->config->sendBuffer)
-            platformMemoryFree(client->config->sendBuffer);
-
-        if (NULL != client->config->clientId)
-            platformMemoryFree(client->config->clientId);
-
-        if (NULL != client->config->host)
-            platformMemoryFree(client->config->host);
-
-        if (NULL != client->config->port)
-            platformMemoryFree(client->config->port);
-
-        if (NULL != client->config->userName)
-            platformMemoryFree(client->config->userName);
-
-        if (NULL != client->config->password)
-            platformMemoryFree(client->config->password);
-
-        if (NULL != client->config->taskName)
-            platformMemoryFree(client->config->taskName);
-
-        if (NULL != client->config)
-            platformMemoryFree(client->config);
-    }
-
-    // 清除遗嘱相关配置
-    if (RyanMqttTrue == client->lwtFlag && NULL != client->lwtOptions)
-    {
-        if (NULL != client->lwtOptions->topic)
-            platformMemoryFree(client->lwtOptions->topic);
-
-        platformMemoryFree(client->lwtOptions);
-    }
-
-    // 清除session  ack链表和msg链表
-    RyanMqttCleanSession(client);
-
-    platformMemoryFree(client);
-    client = NULL;
+    platformCriticalEnter();
+    client->destoryFlag = RyanMqttTrue;
+    platformCriticalExit();
 
     return RyanMqttSuccessError;
 }

+ 2 - 1
mqttclient/RyanMqttClient.h

@@ -94,7 +94,8 @@ extern "C"
     typedef struct
     {
         uint8_t lwtFlag : 1;               // 遗嘱标志位
-        uint8_t keepaliveTimeoutCount : 7; // 心跳超时计数器
+        uint8_t destoryFlag : 1;           // 销毁标志位
+        uint8_t keepaliveTimeoutCount : 6; // 心跳超时计数器
         uint16_t ackHandlerCount;          // 等待ack的记录个数
         uint16_t packetId;                 // mqtt报文标识符,控制报文必须包含一个非零的 16 位报文标识符
         uint32_t eventFlag;                // 事件标志位

+ 77 - 1
mqttclient/RyanMqttThread.c

@@ -655,7 +655,7 @@ void RyanMqttEventMachine(RyanMqttClient_t *client, RyanMqttEventId_e eventId, v
     case RyanMqttEventConnected:                                                                       // 连接成功
         client->keepaliveTimeoutCount = 0;                                                             // 重置心跳超时计数器
         platformTimerCutdown(&client->keepaliveTimer, (client->config->keepaliveTimeoutS * 1000 / 2)); // 启动心跳定时器
-        RyanMqttAckListScan(client, RyanMqttFalse);                                                        // 扫描确认列表,销毁已超时的确认处理程序或重新发送它们
+        RyanMqttAckListScan(client, RyanMqttFalse);                                                    // 扫描确认列表,销毁已超时的确认处理程序或重新发送它们
         RyanMqttSetClientState(client, RyanMqttConnectState);
         break;
 
@@ -697,6 +697,82 @@ void RyanMqttThread(void *argument)
 
     while (1)
     {
+
+        if (RyanMqttTrue == client->destoryFlag)
+        {
+
+            RyanMqttEventMachine(client, RyanMqttEventDestoryBefore, (void *)NULL);
+
+            // 清除网络组件
+            if (NULL != client->network)
+            {
+                platformNetworkClose(client->config->userData, client->network);
+                platformMemoryFree(client->network);
+                client->network = NULL;
+            }
+
+            // 清除互斥锁
+            if (NULL != client->sendBufLock)
+            {
+                platformMutexDestroy(client->config->userData, client->sendBufLock);
+                platformMemoryFree(client->sendBufLock);
+                client->sendBufLock = NULL;
+            }
+
+            // 清除config信息
+            if (NULL != client->config)
+            {
+                if (RyanMqttTrue != client->config->recvBufferStaticFlag && NULL != client->config->recvBuffer)
+                    platformMemoryFree(client->config->recvBuffer);
+
+                if (RyanMqttTrue != client->config->sendBufferStaticFlag && NULL != client->config->sendBuffer)
+                    platformMemoryFree(client->config->sendBuffer);
+
+                if (NULL != client->config->clientId)
+                    platformMemoryFree(client->config->clientId);
+
+                if (NULL != client->config->host)
+                    platformMemoryFree(client->config->host);
+
+                if (NULL != client->config->port)
+                    platformMemoryFree(client->config->port);
+
+                if (NULL != client->config->userName)
+                    platformMemoryFree(client->config->userName);
+
+                if (NULL != client->config->password)
+                    platformMemoryFree(client->config->password);
+
+                if (NULL != client->config->taskName)
+                    platformMemoryFree(client->config->taskName);
+
+                if (NULL != client->config)
+                    platformMemoryFree(client->config);
+            }
+
+            // 清除遗嘱相关配置
+            if (RyanMqttTrue == client->lwtFlag && NULL != client->lwtOptions)
+            {
+                if (NULL != client->lwtOptions->topic)
+                    platformMemoryFree(client->lwtOptions->topic);
+
+                platformMemoryFree(client->lwtOptions);
+            }
+
+            // 清除session  ack链表和msg链表
+            RyanMqttCleanSession(client);
+
+            // 清除掉线程动态资源
+            platformMemoryFree(client->mqttThread);
+            client->mqttThread = NULL;
+
+            platformMemoryFree(client);
+            client = NULL;
+
+            // 销毁自身线程
+            platformThreadDestroy(client->config->userData, client->mqttThread);
+        }
+
         switch (client->clientState)
         {
 

+ 4 - 4
platform/quecOpen/platformNetwork.c

@@ -8,10 +8,10 @@
 #include "platformNetwork.h"
 #include "RyanMqttLog.h"
 
-#define tcpConnect (RyanBit1)
-#define tcpSend (RyanBit2)
-#define tcpClose (RyanBit3)
-#define tcpRecv (RyanBit4)
+#define tcpConnect (RyanMqttBit1)
+#define tcpSend (RyanMqttBit2)
+#define tcpClose (RyanMqttBit3)
+#define tcpRecv (RyanMqttBit4)
 
 static osEventFlagsId_t mqttNetEventHandle;
 static const osEventFlagsAttr_t mqttNetEvent_attributes = {