int keepalive(MQTTClient *c) { int rc = SUCCESS; if (c->keepAliveInterval == 0) { goto exit; } if (TimerIsExpired(&c->last_sent) || TimerIsExpired(&c->last_received)) { if (c->ping_outstanding && TimerIsExpired(&c->ping_wait)) { rc = FAILURE; /* PINGRESP not received in keepalive interval */ } else { Timer timer; TimerInit(&timer); TimerCountdownMS(&timer, 1000); int len = MQTTSerialize_pingreq(c->buf, c->buf_size); if (len > 0 && (rc = sendPacket(c, len, &timer)) == SUCCESS) { // send the ping packet c->ping_outstanding = 1; TimerCountdownMS(&c->ping_wait, CONFIG_MQTT_PING_TIMEOUT); } } } exit: return rc; }
int keepalive (Client *c) { int rc = FAILURE; if (c->keepAliveInterval == 0) { rc = SUCCESS; goto exit; } if (expired(&c->ping_timer)) { if (!c->ping_outstanding) { Timer timer; InitTimer (&timer); countdown_ms (&timer, 1000); int len = MQTTSerialize_pingreq (c->buf, c->buf_size); if (len > 0 && (rc = sendPacket(c, len, &timer)) == SUCCESS) // send the ping packet c->ping_outstanding = 1; } } exit: return rc; }
int keepalive(Client* c) { int rc = FAILURE; if (!c->ping_outstanding) { // c->send_type = PING_SENDING; int len = MQTTSerialize_pingreq(c->buf, c->buf_size); log_printf(LOG_NOTICE, "[MqttTimeReach]send ping\n"); if (len > 0 && (rc = sendPacket_ev(c, len)) == SUCCESS) // send the ping packet c->ping_outstanding = 1; } else { //规定时间内,服务器没有回复ping包 //TODO:error handle log_printf(LOG_ERROR, "timeout, mqtt server not ping respond\n [ReConnectMqtt]"); MQTTReConnect(c); } // // //for test // MQTTMessage msg; // log_printf(LOG_NOTICE, "ping send\n"); //// MQTTPublish(c, "ping", &msg); return rc; }
static void mqttClient_pingExpiryHndlr(le_timer_Ref_t timer) { mqttClient_t* clientData = le_timer_GetContextPtr(timer); int32_t rc = LE_OK; LE_ASSERT(clientData); int len = MQTTSerialize_pingreq(clientData->session.tx.buf, sizeof(clientData->session.tx.buf)); if (len < 0) { LE_ERROR("MQTTSerialize_pingreq() failed(%d)", len); goto cleanup; } LE_DEBUG("<--- PING"); rc = mqttClient_write(clientData, len); if (rc) { LE_ERROR("mqttClient_write() failed(%d)", rc); goto cleanup; } cleanup: return; }
int keepalive(Client* c) { int rc = FAILURE; if (c->keepAliveInterval == 0) { return SUCCESS; } if (expired(&c->ping_timer)) { if (!c->ping_outstanding) { // there is not a ping outstanding - send one Timer timer; InitTimer(&timer); countdown_ms(&timer, 2000); //STM: modified the value from 1000 to 2000 int len = MQTTSerialize_pingreq(c->buf, c->buf_size); INFO("-->keepalive"); rc = sendPacket(c, len, &timer); // send the ping packet DeInitTimer(&timer); //STM: added this line if (len > 0 && rc == SUCCESS) { c->ping_outstanding = 1; // start a timer to wait for PINGRESP from server countdown(&c->ping_timer, c->keepAliveInterval / 2); } else { rc = FAILURE; } } else { // timer expired while waiting for a ping - decide we are disconnected MQTTDisconnect(c); if (c->disconnectHandler != NULL) { c->disconnectHandler(); } } } else { rc = SUCCESS; } return rc; }
static void *mqtt_ping_thread(void *param) { int *sockfd = (int*)param; unsigned char buf[2]; int len; DEBUG("ping start\n"); while (*sockfd >= 0) { SLEEP(KEEPALIVE_INTERVAL-1); len = MQTTSerialize_pingreq(buf, sizeof(buf)); send(*sockfd, buf, len, 0); } DEBUG("ping exit\n"); return 0; }
int keepalive(MQTTClient* c) { int rc = MQTT_FAILURE; if (c->keepAliveInterval == 0 || !c->isconnected) { rc = MQTT_SUCCESS; goto exit; } //printf("%s: %d, ping time left: %d, tick count = %u\n", __func__, __LINE__, TimerLeftMS(&c->ping_timer), xTaskGetTickCount()); if (platform_timer_isexpired(&c->ping_timer)) { if (!c->ping_outstanding) { Timer timer; platform_timer_init(&timer); platform_timer_countdown(&timer, 1000); int len = MQTTSerialize_pingreq(c->buf, c->buf_size); if (len > 0 && (rc = sendPacket(c, len, &timer)) == MQTT_SUCCESS) // send the ping packet { platform_timer_countdown(&c->pingresp_timer, c->command_timeout_ms); c->ping_outstanding = 1; platform_printf("sent ping request\n"); } if (len > 0 && rc != MQTT_SUCCESS) { platform_printf("%s: %d failed to send ping request, rc = %d\n", __func__, __LINE__, rc); } } } exit: return rc; }
int keepalive(MQTTClient* c) { int rc = SUCCESS; if (c->keepAliveInterval == 0) { rc = SUCCESS; goto exit; } if (TimerIsExpired(&c->ping_timer)) { printf("keep alive counter expired\n"); if (!c->ping_outstanding) { printf("keep alive counter reset\n"); //Set the pingreq send timeout Timer timer; TimerInit(&timer); TimerCountdownMS(&timer, 1000); int len = MQTTSerialize_pingreq(c->buf, c->buf_size); /* If the sendPacket success, the keep alive works with keepAliveInterval */ if (len > 0 && (rc = sendPacket(c, len, &timer)) == SUCCESS) {// send the ping packet c->ping_outstanding = 1; TimerCountdown(&c->ping_timer, c->keepAliveInterval); // record the fact that we have successfully sent the packet } else { rc = FAILURE; } } else { printf("keep alive counter failure\n"); rc = FAILURE; } } exit: return rc; }