Ver Fonte

refactor: 优化

RyanCW há 4 meses atrás
pai
commit
ffd47dd2c0

+ 47 - 0
.github/copilot-instructions.md

@@ -0,0 +1,47 @@
+# 概述
+
+RyanMqtt是一个严格遵循 [MQTT 3.1.1](https://docs.oasis-open.org/mqtt/mqtt/v3.1.1/mqtt-v3.1.1.html) 协议标准实现的客户端库,专为资源受限的嵌入式设备设计并优化。
+
+## 目录结构
+
+- `/common`: RyanMqtt需要的链表和log组件
+- `/coreMqtt`: MQTT 协议序列化与反序列化
+- `/docs`: md文件的图片和MQTT3.1.1文档
+- `/example`: 基于RT-Thread平台的msh测试例程
+- `/mqttclient`: RyanMqtt核心源码实现
+- `/platform`: RyanMqtt的跨平台兼容层
+- `/test`: RyanMqtt的测试文件
+
+## 代码审查指南
+
+### 语言要求
+
+在审查代码时,请同时使用**英文和中文**提供反馈,以确保所有贡献者都能理解。
+
+### 审查重点领域
+- 严格遵循 MQTT3.1.1 协议标准
+- 检查内存泄漏、缓冲区管理、内存分配与释放
+- 检查 RTOS 环境下的线程安全问题
+- 检查语法错误和异常处理
+- 代码执行效率与性能
+- 代码风格一致性
+- 重点审查 common、coreMqtt、mqttclient、platform文件夹使用严格模式,test文件夹只需保证程序正常运行不要使用严格模式
+
+### 常见问题检查
+- 缺少的错误处理 / 不当的错误传播 / 静默失败
+- 对外开放的公共API是否具备完善的校验
+- 模块解耦和抽象是否合理
+- 代码的性能问题
+
+## RyanMqtt内部实现参考
+- RyanMqtt内部使用`msg`链表管理订阅主题列表,使用`ack`链表存储QOS等级的ack消息。
+- Qos2实现使用MQTT3.1.1手册推荐的方法B
+- RyanMqtt心跳使用1.5倍时间,当心跳周期到达设置的0.9倍后会发送心跳包,如果超过1.5倍就通知用户断开连接
+
+## 代码格式
+- 所有代码须符合项目 `.clang-format` 配置
+
+## 最佳实践
+- 始终考虑嵌入式约束,有限的RAM和ROM,资源利用效率
+- 优化内存管理,保证高效和实时性
+- 关注模块解耦和可维护性

+ 4 - 6
mqttclient/RyanMqttThread.c

@@ -5,13 +5,11 @@
 #include "RyanMqttLog.h"
 #include "RyanMqttUtil.h"
 
-// mqtt标准是1.5倍,大部分mqtt服务器也是这个配置,RyanMqtt设置为1.4倍,给发送心跳包留一定的时间
-#define RyanMqttKeepAliveMultiplier (1.4)
+// mqtt标准是1.5倍,大部分mqtt服务器也是这个配置
+#define RyanMqttKeepAliveMultiplier (1.5)
 
 void RyanMqttRefreshKeepaliveTime(RyanMqttClient_t *client)
 {
-	// 服务器在心跳时间的1.5倍内没有收到keeplive消息则会断开连接
-	// 这里算 1.4 b倍时间内没有收到心跳就断开连接
 	platformCriticalEnter(client->config.userData, &client->criticalLock);
 	uint32_t timeout = (uint32_t)(client->config.keepaliveTimeoutS * 1000 * RyanMqttKeepAliveMultiplier);
 	RyanMqttTimerCutdown(&client->keepaliveTimer, timeout); // 启动心跳定时器
@@ -36,7 +34,7 @@ static RyanMqttError_e RyanMqttKeepalive(RyanMqttClient_t *client)
 
 	uint32_t timeRemain = RyanMqttTimerRemain(&client->keepaliveTimer);
 
-	// 超过设置的 1.4 倍心跳周期,主动通知用户断开连接
+	// 超过设置的 1.5 倍心跳周期,主动通知用户断开连接
 	if (0 == timeRemain)
 	{
 		RyanMqttConnectStatus_e connectState = RyanMqttKeepaliveTimeout;
@@ -49,7 +47,7 @@ static RyanMqttError_e RyanMqttKeepalive(RyanMqttClient_t *client)
 	// 当剩余时间小于 recvtimeout 时强制发送心跳包
 	if (timeRemain > client->config.recvTimeout)
 	{
-		// 当到达 keepaliveTimeoutS的0.9 倍时间时发送心跳包
+		// 当没有到达 keepaliveTimeoutS  0.9 倍时间时不进行发送心跳包
 		if (timeRemain > client->config.keepaliveTimeoutS * 1000 * (RyanMqttKeepAliveMultiplier - 0.9))
 		{
 			return RyanMqttSuccessError;

+ 1 - 15
mqttclient/RyanMqttThreadProcessPacket.c

@@ -6,7 +6,7 @@
 #include "RyanMqttUtil.h"
 
 /**
- * @brief qos1或者qos2接收消息成功
+ * @brief qos1或者qos2接收消息成功确认处理
  *
  * @param client
  * @return RyanMqttError_e
@@ -524,20 +524,6 @@ RyanMqttError_e RyanMqttProcessPacketHandler(RyanMqttClient_t *client)
 
 	case MQTT_PACKET_TYPE_CONNACK: // 连接报文确认
 	{
-		// if (RyanMqttTrue == isConnect)
-		// {
-		// 	uint16_t packetId;
-		// 	bool sessionPresent; // 会话位
-		// 	MQTTStatus_t status;
-
-		// 	// 反序列化ack包
-		// 	status = MQTT_DeserializeAck(&pIncomingPacket, &packetId, &sessionPresent);
-		// 	if (MQTTSuccess != status)
-		// 	{
-		// 		result = RyanMqttFailedError;
-		// 	}
-		// }
-
 		// 客户端已处于连接状态时又收到CONNACK报文,应该视为严重错误,断开连接
 		RyanMqttLog_e("收到 CONNACK 时已连接,正在断开连接");
 		RyanMqttConnectStatus_e connectState = RyanMqttConnectProtocolError;

+ 4 - 4
mqttclient/RyanMqttUtileAck.c

@@ -25,18 +25,18 @@ RyanMqttError_e RyanMqttAckHandlerCreate(RyanMqttClient_t *client, uint8_t packe
 	RyanMqttAssert(NULL != client);
 	RyanMqttAssert(NULL != pAckHandler);
 
-	uint32_t mallocLen = sizeof(RyanMqttAckHandler_t);
+	uint32_t mallocSize = sizeof(RyanMqttAckHandler_t);
 
 	// 为非预分配的数据包分配额外空间
 	if (RyanMqttTrue != isPreallocatedPacket)
 	{
-		mallocLen += packetLen + 1;
+		mallocSize += packetLen + 1;
 	}
 
 	// 为非预分配包申请额外空间
-	RyanMqttAckHandler_t *ackHandler = (RyanMqttAckHandler_t *)platformMemoryMalloc(mallocLen);
+	RyanMqttAckHandler_t *ackHandler = (RyanMqttAckHandler_t *)platformMemoryMalloc(mallocSize);
 	RyanMqttCheck(NULL != ackHandler, RyanMqttNotEnoughMemError, RyanMqttLog_d);
-	RyanMqttMemset(ackHandler, 0, mallocLen);
+	RyanMqttMemset(ackHandler, 0, mallocSize);
 
 	RyanMqttListInit(&ackHandler->list);
 	// 超时内没有响应将被销毁或重新发送

+ 4 - 4
mqttclient/RyanMqttUtileMsg.c

@@ -183,10 +183,10 @@ RyanMqttError_e RyanMqttMsgHandlerCreate(RyanMqttClient_t *client, const char *t
 	RyanMqttAssert(NULL != pMsgHandler);
 	RyanMqttAssert(RyanMqttQos0 == qos || RyanMqttQos1 == qos || RyanMqttQos2 == qos);
 
-	RyanMqttMsgHandler_t *msgHandler =
-		(RyanMqttMsgHandler_t *)platformMemoryMalloc(sizeof(RyanMqttMsgHandler_t) + topicLen + 1);
+	uint32_t mallocSize = sizeof(RyanMqttMsgHandler_t) + topicLen + 1;
+	RyanMqttMsgHandler_t *msgHandler = (RyanMqttMsgHandler_t *)platformMemoryMalloc(mallocSize);
 	RyanMqttCheck(NULL != msgHandler, RyanMqttNotEnoughMemError, RyanMqttLog_d);
-	RyanMqttMemset(msgHandler, 0, sizeof(RyanMqttMsgHandler_t) + topicLen + 1);
+	RyanMqttMemset(msgHandler, 0, mallocSize);
 
 	// 初始化链表
 	RyanMqttListInit(&msgHandler->list);
@@ -349,7 +349,7 @@ RyanMqttError_e RyanMqttMsgHandlerAddToMsgList(RyanMqttClient_t *client, RyanMqt
 
 	platformMutexLock(client->config.userData, &client->msgHandleLock);
 	RyanMqttListAddTail(&msgHandler->list,
-			&client->msgHandlerList); // 将msgHandler节点添加到链表尾部
+			    &client->msgHandlerList); // 将msgHandler节点添加到链表尾部
 	platformMutexUnLock(client->config.userData, &client->msgHandleLock);
 
 	return RyanMqttSuccessError;

+ 3 - 3
test/RyanMqttKeepAliveTest.c

@@ -31,8 +31,8 @@ static RyanMqttError_e keepAliveTest(void)
 		RyanMqttLog_w("心跳倒计时: %d", keepAliveRemain);
 		RyanMqttCheckCodeNoReturn(0 != keepAliveRemain, RyanMqttFailedError, RyanMqttLog_e, { break; });
 
-		// 超时判断:如果剩余心跳时间小于 3 秒,视为超时/异常
-		if (keepAliveRemain < 3000)
+		// 超时判断:如果剩余心跳时间小于 4 秒,视为超时/异常
+		if (keepAliveRemain < 4000)
 		{
 			RyanMqttLog_e("心跳剩余时间过短: %d 秒,心跳包发送周期不对", keepAliveRemain);
 			result = RyanMqttFailedError;
@@ -44,7 +44,7 @@ static RyanMqttError_e keepAliveTest(void)
 
 	RyanMqttTestDestroyClient(client);
 
-	if (minKeepAliveRemain > 6 * 1000)
+	if (minKeepAliveRemain > 7 * 1000)
 	{
 		RyanMqttLog_e("心跳剩余时间过短: %d 秒,可能频繁的发送心跳包", minKeepAliveRemain);
 		result = RyanMqttFailedError;

+ 2 - 1
xmake.lua

@@ -8,9 +8,10 @@ target("RyanMqtt",function()
     set_languages("gnu99") -- 关键!启用 GNU 扩展
     set_warnings("everything") -- 启用全部警告 -Wall -Wextra -Weffc++ / -Weverything
 
-    set_optimize("smallest") -- -Os
+    -- set_optimize("smallest") -- -Os
     -- set_optimize("faster") -- -O2
     -- set_optimize("fastest") -- -O3
+    set_optimize("aggressive") -- -Ofast
 
     add_defines("PKG_USING_RYANMQTT_IS_ENABLE_ASSERT") -- 开启assert
     add_ldflags("-Wl,-Map=$(buildir)/RyanMqtt.map")