Comments (2)
I tried to fix the problem, but I was very confused about the construction of the deliver_publish function.
After some debugging and guesswork I found a working solution (at least for me):
void deliver_publish(mqtt_client *client, uint8_t *message, int length)
{
mqtt_event_data_t event_data;
int len_read = 0, total_mqtt_len = 0, mqtt_len = 0, mqtt_offset = 0;
do
{
do{
event_data.topic_length = length;
event_data.topic = mqtt_get_publish_topic(message+mqtt_offset, &event_data.topic_length);
event_data.data_length = length;
event_data.data = mqtt_get_publish_data(message+mqtt_offset, &event_data.data_length);
//mqtt_info("message_length: %d bytes message_length_read: %d bytes, data_length: %d bytes ", client->mqtt_state.message_length, client->mqtt_state.message_length_read, event_data.data_length);
total_mqtt_len = client->mqtt_state.message_length_read;
mqtt_len = event_data.data_length + event_data.topic_length + 4;
event_data.data_total_length = total_mqtt_len;
event_data.data_offset = mqtt_offset;
if(client->settings->data_cb) {
client->settings->data_cb(client, &event_data);
}
mqtt_offset += mqtt_len;
}while (mqtt_offset < total_mqtt_len);
if (client->mqtt_state.message_length_read >= client->mqtt_state.message_length)
break;
len_read = client->settings->read_cb(client, client->mqtt_state.in_buffer, CONFIG_MQTT_BUFFER_SIZE_BYTE, 0);
if(len_read < 0) {
mqtt_info("Read error: %d", errno);
break;
}
client->mqtt_state.message_length_read += len_read;
} while (1);
}
It works for me now, except if the messages are getting larger than the buffer size. But as I use only small messages, this is not a problem for me.
I'm not sure if the function now works as intended, perhaps someone can have a look at it.
from esp32-mqtt.
I think this is due to a fundamental misunderstanding of TCP in the design. The code treats the TCP connection as a packet source, making an assumption that each "read" will return one and only one message. However in reality TCP is a stream and there may be multiple packets waiting, including fragmented ones. My own testing found that this library often drops messages in both directions, especially when many are sent at once (e.g. reconnection followed by receipt of retained messages), because only the first message is processed. The rest are thrown away.
I spent some time looking over the code and I think it can be fixed, I just don't have the time or motivation at the moment to do so myself, unfortunately.
from esp32-mqtt.
Related Issues (20)
- What does t MQTT_MSG_TYPE_PINGRESP mean? HOT 9
- Connection refuse, Unknown reason HOT 1
- Samsung's ARTIK Cloud connect error HOT 1
- not able to connect to hive mqtt HOT 3
- example fails compile, when fixed fails to execute properly HOT 1
- Compilation error - undeclared struct field "reconnect_cb" HOT 5
- Pending Subscription message is overwritten by Publish => subscribe callback is missed HOT 1
- esp32 as server broker
- MQTT cjson HOT 8
- mqtt_stop() sometimes does not work.
- Documentation for MQTT Library by tuanpmt HOT 1
- Client socket being destroyed after unsuccessful read from mqtt_start_receive_schedule() function HOT 2
- Offline Buffer issues trigger watchdog timer error
- Installing library in Eclipse environment
- Connection refused, not authorized HOT 9
- Mqtt_1 HOT 1
- lwt message is blank
- last will
- esp32 constant reboot HOT 1
- Link to new version is 404
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 esp32-mqtt.