@@ -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}
0 commit comments