Skip to content

Commit a8fe9cc

Browse files
committed
1.优化MQTT keep alive连接机制。
2.增加一个MQTT错误类型用于unsub不存在的主题。
1 parent 072f8c4 commit a8fe9cc

File tree

5 files changed

+42
-20
lines changed

5 files changed

+42
-20
lines changed

src/mqtt/src/mqtt_client_common.c

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1272,7 +1272,8 @@ static void _handle_pingresp_packet(Qcloud_IoT_Client *pClient) {
12721272
IOT_FUNC_ENTRY;
12731273

12741274
HAL_MutexLock(pClient->lock_generic);
1275-
pClient->is_ping_outstanding = 0;
1275+
pClient->is_ping_outstanding = 0;
1276+
countdown(&pClient->ping_timer, pClient->options.keep_alive_interval);
12761277
HAL_MutexUnlock(pClient->lock_generic);
12771278

12781279
IOT_FUNC_EXIT;
@@ -1296,10 +1297,6 @@ int cycle_for_read(Qcloud_IoT_Client *pClient, Timer *timer, uint8_t *packet_typ
12961297
IOT_FUNC_EXIT_RC(rc);
12971298
}
12981299

1299-
HAL_MutexLock(pClient->lock_generic);
1300-
countdown(&pClient->ping_timer, pClient->options.keep_alive_interval);
1301-
HAL_MutexUnlock(pClient->lock_generic);
1302-
13031300
switch (*packet_type) {
13041301
case CONNACK:
13051302
break;
@@ -1326,10 +1323,8 @@ int cycle_for_read(Qcloud_IoT_Client *pClient, Timer *timer, uint8_t *packet_typ
13261323
}
13271324
case PUBCOMP:
13281325
break;
1329-
case PINGRESP: {
1330-
_handle_pingresp_packet(pClient);
1326+
case PINGRESP:
13311327
break;
1332-
}
13331328
default: {
13341329
/* Either unknown packet type or Failure occurred
13351330
* Should not happen */
@@ -1339,6 +1334,23 @@ int cycle_for_read(Qcloud_IoT_Client *pClient, Timer *timer, uint8_t *packet_typ
13391334
}
13401335
}
13411336

1337+
/* Receiving below msgs are all considered as PING OK */
1338+
switch (*packet_type) {
1339+
case PUBACK:
1340+
case SUBACK:
1341+
case UNSUBACK:
1342+
case PINGRESP: {
1343+
_handle_pingresp_packet(pClient);
1344+
break;
1345+
}
1346+
case PUBLISH: {
1347+
HAL_MutexLock(pClient->lock_generic);
1348+
pClient->is_ping_outstanding = 0;
1349+
HAL_MutexUnlock(pClient->lock_generic);
1350+
break;
1351+
}
1352+
}
1353+
13421354
IOT_FUNC_EXIT_RC(rc);
13431355
}
13441356

src/mqtt/src/mqtt_client_unsubscribe.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,7 @@ int qcloud_iot_mqtt_unsubscribe(Qcloud_IoT_Client *pClient, char *topicFilter) {
126126

127127
if (suber_exists == false) {
128128
Log_e("subscription does not exists: %s", topicFilter);
129-
IOT_FUNC_EXIT_RC(QCLOUD_ERR_FAILURE);
129+
IOT_FUNC_EXIT_RC(QCLOUD_ERR_MQTT_UNSUB_FAIL);
130130
}
131131

132132
if (!get_client_conn_state(pClient)) {

src/mqtt/src/mqtt_client_yield.c

Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,10 @@ static int _handle_reconnect(Qcloud_IoT_Client *pClient) {
130130
* @param pClient
131131
* @return
132132
*/
133-
static int _mqtt_keep_alive(Qcloud_IoT_Client *pClient) {
133+
static int _mqtt_keep_alive(Qcloud_IoT_Client *pClient)
134+
{
135+
#define MQTT_PING_TIMES_IN_KEEPALIVE_INTERVAL 2
136+
134137
IOT_FUNC_ENTRY;
135138

136139
int rc;
@@ -145,14 +148,14 @@ static int _mqtt_keep_alive(Qcloud_IoT_Client *pClient) {
145148
IOT_FUNC_EXIT_RC(QCLOUD_ERR_SUCCESS);
146149
}
147150

148-
if (pClient->is_ping_outstanding) {
151+
if (pClient->is_ping_outstanding >= MQTT_PING_TIMES_IN_KEEPALIVE_INTERVAL) {
152+
//Reaching here means we haven't received any MQTT packet for a long time (keep_alive_interval)
153+
Log_e("Fail to recv MQTT msg. Something wrong with the connection.");
149154
rc = _handle_disconnect(pClient);
150155
IOT_FUNC_EXIT_RC(rc);
151156
}
152157

153-
/* there is no ping outstanding - send one */
154-
InitTimer(&timer);
155-
countdown_ms(&timer, pClient->command_timeout_ms);
158+
/* there is no ping outstanding - send one */
156159
HAL_MutexLock(pClient->lock_write_buf);
157160
rc = serialize_packet_with_zero_payload(pClient->write_buf, pClient->write_buf_size, PINGREQ, &serialized_len);
158161
if (QCLOUD_ERR_SUCCESS != rc) {
@@ -161,10 +164,15 @@ static int _mqtt_keep_alive(Qcloud_IoT_Client *pClient) {
161164
}
162165

163166
/* send the ping packet */
164-
rc = send_mqtt_packet(pClient, serialized_len, &timer);
167+
int i = 0;
168+
InitTimer(&timer);
169+
do {
170+
countdown_ms(&timer, pClient->command_timeout_ms);
171+
rc = send_mqtt_packet(pClient, serialized_len, &timer);
172+
} while (QCLOUD_ERR_SUCCESS != rc && (i++ < 3));
173+
165174
if (QCLOUD_ERR_SUCCESS != rc) {
166-
HAL_MutexUnlock(pClient->lock_write_buf);
167-
//Reaching here means we haven't received any MQTT packet for a long time (keep_alive_interval)
175+
HAL_MutexUnlock(pClient->lock_write_buf);
168176
//If sending a PING fails, propably the connection is not OK and we decide to disconnect and begin reconnection attempts
169177
Log_e("Fail to send PING request. Something wrong with the connection.");
170178
rc = _handle_disconnect(pClient);
@@ -173,10 +181,11 @@ static int _mqtt_keep_alive(Qcloud_IoT_Client *pClient) {
173181
HAL_MutexUnlock(pClient->lock_write_buf);
174182

175183
HAL_MutexLock(pClient->lock_generic);
176-
pClient->is_ping_outstanding = 1;
184+
pClient->is_ping_outstanding++;
177185
/* start a timer to wait for PINGRESP from server */
178-
countdown(&pClient->ping_timer, pClient->options.keep_alive_interval/2);
186+
countdown(&pClient->ping_timer, pClient->options.keep_alive_interval/MQTT_PING_TIMES_IN_KEEPALIVE_INTERVAL);
179187
HAL_MutexUnlock(pClient->lock_generic);
188+
Log_d("PING request %u has been sent...", pClient->is_ping_outstanding);
180189

181190
IOT_FUNC_EXIT_RC(QCLOUD_ERR_SUCCESS);
182191
}

src/sdk-impl/exports/qcloud_iot_export_error.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ typedef enum {
6363
QCLOUD_ERR_RX_MESSAGE_INVAL = -118, // 表示收到的消息无效
6464
QCLOUD_ERR_BUF_TOO_SHORT = -119, // 表示消息接收缓冲区的长度小于消息的长度
6565
QCLOUD_ERR_MQTT_QOS_NOT_SUPPORT = -120, // 表示该QOS级别不支持
66+
QCLOUD_ERR_MQTT_UNSUB_FAIL = -121, // 表示取消订阅主题失败,比如该主题不存在
6667

6768
QCLOUD_ERR_JSON_PARSE = -132, // 表示JSON解析错误
6869
QCLOUD_ERR_JSON_BUFFER_TRUNCATED = -133, // 表示JSON文档会被截断

src/sdk-impl/qcloud_iot_export.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ extern "C" {
2424
#define QCLOUD_IOT_DEVICE_SDK_APPID "21010406"
2525

2626
/* IoT C-SDK version info */
27-
#define QCLOUD_IOT_DEVICE_SDK_VERSION "2.3.2"
27+
#define QCLOUD_IOT_DEVICE_SDK_VERSION "2.3.3"
2828

2929
/* MQTT心跳消息发送周期, 单位:ms */
3030
#define QCLOUD_IOT_MQTT_KEEP_ALIVE_INTERNAL (240 * 1000)

0 commit comments

Comments
 (0)