Skip to content

Commit

Permalink
destory客户端逻辑修改、quecopen接口修改
Browse files Browse the repository at this point in the history
  • Loading branch information
Ryan-CW-Code committed Aug 10, 2023
1 parent 8873fa2 commit dd9f145
Show file tree
Hide file tree
Showing 4 changed files with 89 additions and 78 deletions.
78 changes: 6 additions & 72 deletions mqttclient/RyanMqttClient.c
Original file line number Diff line number Diff line change
Expand Up @@ -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
*/
Expand All @@ -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;
}
Expand Down
3 changes: 2 additions & 1 deletion mqttclient/RyanMqttClient.h
Original file line number Diff line number Diff line change
Expand Up @@ -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; // 事件标志位
Expand Down
78 changes: 77 additions & 1 deletion mqttclient/RyanMqttThread.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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)
{

Expand Down
8 changes: 4 additions & 4 deletions platform/quecOpen/platformNetwork.c
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {
Expand Down

0 comments on commit dd9f145

Please sign in to comment.