BugID:17646749:[mqtt] retry 8 times with 2 seconds interval if CONNACK timeout
@@ -2719,13 +2719,18 @@ static int MQTTPubInfoProc(iotx_mc_client_t *pClient)
/* connect */
int iotx_mc_connect(iotx_mc_client_t *pClient)
{
+#define RETRY_TIME_LIMIT (8+1)
+#define RETRY_INTV_PERIOD (2000)
+
int rc = FAIL_RETURN;
+ int try_count = 1;
if (NULL == pClient) {
return NULL_VALUE_ERROR;
}
/* Establish TCP or TLS connection */
+ do {
rc = pClient->ipstack->connect(pClient->ipstack);
if (SUCCESS_RETURN != rc) {
pClient->ipstack->disconnect(pClient->ipstack);
@@ -2739,23 +2744,30 @@ int iotx_mc_connect(iotx_mc_client_t *pClient)
}
}
- /* remove */
- /*mqtt_debug("start MQTT connection with parameters: clientid=%s, username=%s, password=%s",
- pClient->connect_data.clientID.cstring,
- pClient->connect_data.username.cstring,
- pClient->connect_data.password.cstring);*/
-
rc = MQTTConnect(pClient);
+
if (rc != SUCCESS_RETURN) {
pClient->ipstack->disconnect(pClient->ipstack);
- mqtt_err("send connect packet failed");
+ mqtt_err("send connect packet failed, rc = %d", rc);
return rc;
}
if (SUCCESS_RETURN != iotx_mc_wait_CONNACK(pClient)) {
+ mqtt_err("wait connect ACK timeout, or receive a ACK indicating error!");
+ mqtt_warning("tried [%d/%d] times CONN, waiting for %d ms...", try_count, RETRY_TIME_LIMIT - 1, RETRY_INTV_PERIOD);
+
+ HAL_SleepMs(RETRY_INTV_PERIOD);
+
(void)MQTTDisconnect(pClient);
pClient->ipstack->disconnect(pClient->ipstack);
- mqtt_err("wait connect ACK timeout, or receive a ACK indicating error!");
+ continue;
+ } else {
+ break;
+ }
+
+ } while (++try_count < RETRY_TIME_LIMIT);
+
+ if (try_count == RETRY_TIME_LIMIT) {
return MQTT_CONNECT_ERROR;
}
