Skip to content

Commit dd9f145

Browse files
committed
destory客户端逻辑修改、quecopen接口修改
1 parent 8873fa2 commit dd9f145

File tree

4 files changed

+89
-78
lines changed

4 files changed

+89
-78
lines changed

mqttclient/RyanMqttClient.c

Lines changed: 6 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,9 @@ RyanMqttError_e RyanMqttInit(RyanMqttClient_t **pClient)
9595

9696
/**
9797
* @brief 销毁mqtt客户端
98-
*
98+
* 由于直接删除mqtt线程是很危险的行为。这里设置标志位,由mqtt线程自己删除自己所有资源。
99+
* mqtt删除自己的延时最大不会超过config里面 recvTimeout + 1秒
100+
* mqtt删除自己前会调用 RyanMqttEventDestoryBefore 事件回调
99101
* @param client
100102
* @return RyanMqttError_e
101103
*/
@@ -104,77 +106,9 @@ RyanMqttError_e RyanMqttDestroy(RyanMqttClient_t *client)
104106

105107
RyanMqttCheck(NULL != client, RyanMqttParamInvalidError, rlog_d);
106108

107-
RyanMqttEventMachine(client, RyanMqttEventDestoryBefore, (void *)NULL);
108-
109-
// 先清除掉线程
110-
if (NULL != client->mqttThread)
111-
{
112-
platformThreadDestroy(client->config->userData, client->mqttThread);
113-
platformMemoryFree(client->mqttThread);
114-
client->mqttThread = NULL;
115-
}
116-
117-
// 清除网络组件
118-
if (NULL != client->network)
119-
{
120-
platformNetworkClose(client->config->userData, client->network);
121-
platformMemoryFree(client->network);
122-
client->network = NULL;
123-
}
124-
125-
// 清除互斥锁
126-
if (NULL != client->sendBufLock)
127-
{
128-
platformMutexDestroy(client->config->userData, client->sendBufLock);
129-
platformMemoryFree(client->sendBufLock);
130-
client->sendBufLock = NULL;
131-
}
132-
133-
// 清除config信息
134-
if (NULL != client->config)
135-
{
136-
if (RyanMqttTrue != client->config->recvBufferStaticFlag && NULL != client->config->recvBuffer)
137-
platformMemoryFree(client->config->recvBuffer);
138-
139-
if (RyanMqttTrue != client->config->sendBufferStaticFlag && NULL != client->config->sendBuffer)
140-
platformMemoryFree(client->config->sendBuffer);
141-
142-
if (NULL != client->config->clientId)
143-
platformMemoryFree(client->config->clientId);
144-
145-
if (NULL != client->config->host)
146-
platformMemoryFree(client->config->host);
147-
148-
if (NULL != client->config->port)
149-
platformMemoryFree(client->config->port);
150-
151-
if (NULL != client->config->userName)
152-
platformMemoryFree(client->config->userName);
153-
154-
if (NULL != client->config->password)
155-
platformMemoryFree(client->config->password);
156-
157-
if (NULL != client->config->taskName)
158-
platformMemoryFree(client->config->taskName);
159-
160-
if (NULL != client->config)
161-
platformMemoryFree(client->config);
162-
}
163-
164-
// 清除遗嘱相关配置
165-
if (RyanMqttTrue == client->lwtFlag && NULL != client->lwtOptions)
166-
{
167-
if (NULL != client->lwtOptions->topic)
168-
platformMemoryFree(client->lwtOptions->topic);
169-
170-
platformMemoryFree(client->lwtOptions);
171-
}
172-
173-
// 清除session ack链表和msg链表
174-
RyanMqttCleanSession(client);
175-
176-
platformMemoryFree(client);
177-
client = NULL;
109+
platformCriticalEnter();
110+
client->destoryFlag = RyanMqttTrue;
111+
platformCriticalExit();
178112

179113
return RyanMqttSuccessError;
180114
}

mqttclient/RyanMqttClient.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,8 @@ extern "C"
9494
typedef struct
9595
{
9696
uint8_t lwtFlag : 1; // 遗嘱标志位
97-
uint8_t keepaliveTimeoutCount : 7; // 心跳超时计数器
97+
uint8_t destoryFlag : 1; // 销毁标志位
98+
uint8_t keepaliveTimeoutCount : 6; // 心跳超时计数器
9899
uint16_t ackHandlerCount; // 等待ack的记录个数
99100
uint16_t packetId; // mqtt报文标识符,控制报文必须包含一个非零的 16 位报文标识符
100101
uint32_t eventFlag; // 事件标志位

mqttclient/RyanMqttThread.c

Lines changed: 77 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -655,7 +655,7 @@ void RyanMqttEventMachine(RyanMqttClient_t *client, RyanMqttEventId_e eventId, v
655655
case RyanMqttEventConnected: // 连接成功
656656
client->keepaliveTimeoutCount = 0; // 重置心跳超时计数器
657657
platformTimerCutdown(&client->keepaliveTimer, (client->config->keepaliveTimeoutS * 1000 / 2)); // 启动心跳定时器
658-
RyanMqttAckListScan(client, RyanMqttFalse); // 扫描确认列表,销毁已超时的确认处理程序或重新发送它们
658+
RyanMqttAckListScan(client, RyanMqttFalse); // 扫描确认列表,销毁已超时的确认处理程序或重新发送它们
659659
RyanMqttSetClientState(client, RyanMqttConnectState);
660660
break;
661661

@@ -697,6 +697,82 @@ void RyanMqttThread(void *argument)
697697

698698
while (1)
699699
{
700+
701+
if (RyanMqttTrue == client->destoryFlag)
702+
{
703+
704+
RyanMqttEventMachine(client, RyanMqttEventDestoryBefore, (void *)NULL);
705+
706+
// 清除网络组件
707+
if (NULL != client->network)
708+
{
709+
platformNetworkClose(client->config->userData, client->network);
710+
platformMemoryFree(client->network);
711+
client->network = NULL;
712+
}
713+
714+
// 清除互斥锁
715+
if (NULL != client->sendBufLock)
716+
{
717+
platformMutexDestroy(client->config->userData, client->sendBufLock);
718+
platformMemoryFree(client->sendBufLock);
719+
client->sendBufLock = NULL;
720+
}
721+
722+
// 清除config信息
723+
if (NULL != client->config)
724+
{
725+
if (RyanMqttTrue != client->config->recvBufferStaticFlag && NULL != client->config->recvBuffer)
726+
platformMemoryFree(client->config->recvBuffer);
727+
728+
if (RyanMqttTrue != client->config->sendBufferStaticFlag && NULL != client->config->sendBuffer)
729+
platformMemoryFree(client->config->sendBuffer);
730+
731+
if (NULL != client->config->clientId)
732+
platformMemoryFree(client->config->clientId);
733+
734+
if (NULL != client->config->host)
735+
platformMemoryFree(client->config->host);
736+
737+
if (NULL != client->config->port)
738+
platformMemoryFree(client->config->port);
739+
740+
if (NULL != client->config->userName)
741+
platformMemoryFree(client->config->userName);
742+
743+
if (NULL != client->config->password)
744+
platformMemoryFree(client->config->password);
745+
746+
if (NULL != client->config->taskName)
747+
platformMemoryFree(client->config->taskName);
748+
749+
if (NULL != client->config)
750+
platformMemoryFree(client->config);
751+
}
752+
753+
// 清除遗嘱相关配置
754+
if (RyanMqttTrue == client->lwtFlag && NULL != client->lwtOptions)
755+
{
756+
if (NULL != client->lwtOptions->topic)
757+
platformMemoryFree(client->lwtOptions->topic);
758+
759+
platformMemoryFree(client->lwtOptions);
760+
}
761+
762+
// 清除session ack链表和msg链表
763+
RyanMqttCleanSession(client);
764+
765+
// 清除掉线程动态资源
766+
platformMemoryFree(client->mqttThread);
767+
client->mqttThread = NULL;
768+
769+
platformMemoryFree(client);
770+
client = NULL;
771+
772+
// 销毁自身线程
773+
platformThreadDestroy(client->config->userData, client->mqttThread);
774+
}
775+
700776
switch (client->clientState)
701777
{
702778

platform/quecOpen/platformNetwork.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,10 @@
88
#include "platformNetwork.h"
99
#include "RyanMqttLog.h"
1010

11-
#define tcpConnect (RyanBit1)
12-
#define tcpSend (RyanBit2)
13-
#define tcpClose (RyanBit3)
14-
#define tcpRecv (RyanBit4)
11+
#define tcpConnect (RyanMqttBit1)
12+
#define tcpSend (RyanMqttBit2)
13+
#define tcpClose (RyanMqttBit3)
14+
#define tcpRecv (RyanMqttBit4)
1515

1616
static osEventFlagsId_t mqttNetEventHandle;
1717
static const osEventFlagsAttr_t mqttNetEvent_attributes = {

0 commit comments

Comments
 (0)