Comments (4)
@aiot-embedded Thanks for your suggested fix! However I think resetting the timer is too extreme, starting a new countdown after sending the ping packet seems to work flawlessly for me.
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)
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
{
TimerCountdownMS(&c->last_received, 3000); // ADDED NEW
c->ping_outstanding = 1;
}
}
}
exit:
return rc;
}
Still a weird decision by the pahoMQTT team. I don't know why they thought that PINGRESP would be available on the next run of the cycle() function, it might take up to a few seconds for the PINGRESP to arrive to the client when the network is slow (up to 2 seconds in my case)
from paho.mqtt.embedded-c.
@alireza-tabatabaee
Your code is so nice! It works fine for me too.
from paho.mqtt.embedded-c.
The C version of the code was done by another contributor. It should match the C++ version, and it's definitely strange to expect a response within 2 seconds. I assume that was done for a particular application, and then no further thought was made. Anyway, I'll put a fix in which makes the C processing match the C++ code.
from paho.mqtt.embedded-c.
it's definitely strange to expect a response within 2 seconds. I assume that was done for a particular application, and then no further thought was made.
Actually, the 3 second delay in the code I posted above was added by me as a makeshift patch. The way the original pahoMQTT C library works is that it'll send the PINGREQ packet, then on the next iteration of the cycle() / MQTTYield() function it expects to have the PINGRESP response from the broker. If it doesn't, it'll declare the connection as broken.
So, in use cases such as mine where the network was slow and PINGRESP packets might've taken a while (up to 2 seconds) to arrive at the device, the pahoMQTT library would just report the connection as broken on every ping event. Adding a little bit of leniency on the packet arrival time and not expecting a response right afterwards would fix it.
Nevertheless, thanks for your time and looking forward to your patch 🙏
from paho.mqtt.embedded-c.
Related Issues (20)
- MQTT auto disconnected after KeepAlive time HOT 4
- arduino version have a non atomic Timer implementation and block publish forever sometimes
- Arduino keeps reconnecting in loop HOT 2
- Messages are not retained even if the flag is set to true HOT 2
- Suspected bug with MQTTYield() / cycle()
- Performance problem related to cycle() and MQTT_TASK HOT 2
- Is there a limit on the amount of bytes the payload must be in the publish message? HOT 6
- Are there any plans to migrate to exact-width integer types? HOT 13
- isTopicMatched() does not behave correctly for filter that terminates with wildcard /# HOT 2
- Why use `enddata - curdata` instead of `mylen` in `MQTTDeserialize_ack()`?
- MQTTPacket_decode() doesn't present errors when it should HOT 1
- Does this library support mqtt v5? HOT 6
- Memory corruptions when casting enum to `int *` HOT 1
- Client never closes socket HOT 2
- Move includes to a subdirectory HOT 3
- Potential OOB Writes in eclipse/paho.mqtt.embedded-c
- PlatformIO's last release outdate HOT 2
- Compile error when compiling MQTTV5 HOT 1
- Library linking issues HOT 2
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from paho.mqtt.embedded-c.