Memory problem of sending multiple data at one time
Hello, I'm using version 2.0.14 of mosquitto. I want to send multiple types of data including text and pictures at one time. The memory of this text and picture is discontinuous.
but I see that the payload of publish is a whole block of memory. When I'm currently using it, I need to apply for an extra section of memory to store text + pictures.
Because the size of the picture is uncertain, we hope to save this memory. How can I save this memory?
After called mosquitto_publish, you can free the payload memory.
https://github.com/eclipse/mosquitto/blob/f1bf2938b047b80913c8a37f088b27e812e0f8b7/lib/actions.c#L148
I don't want to reapply for memory. I want to use the existing memory to send it at one time.(Too little memory)
I don't want to reapply for memory. I want to use the existing memory to send it at one time.(Too little memory)
You can reuse the existing memory, but you must wait mosquitto_publish finished.
你是说文本和图片分开publish吗?我想要一次发送文本和图片
你的问题不是不想每次 pub 的时候都 malloc 一段内存么?那就重用啊,但是重用需要注意得等 publish 做完才可以。至于文本和图片是分开还是一起发送都可以,看自己的需要。但是图片是 binary datas,mosquitto 是要验证是否符合 utf8 格式的(具体搜索 mosquitto_validate_utf8 的调用处),你可能需要转成 base64 编码,再搞个压缩也行,具体带来的时空复杂度需要自己衡量。
不太明白重用是要怎么处理,这个接口的发送是一片内存,我的文本和图片内存是分开的,需要的是分片发送,想问一下这个当前的mosquitto可不可以实现 return send__publish(mosq, local_mid, topic, (uint32_t)payloadlen, payload, (uint8_t)qos, retain, false, outgoing_properties, NULL, 0);
抱歉,我理解错你的问题了。
目前没有接口可以满足你的需求,你只能把两段内存合为一段连续的传给 mosquitto_publish
The answers above are correct, mosquitto_publish expects to allocate memory to store your message, which you can then dispose of. There is no mechanism to pass memory which should be adopted as the payload. If memory is really that tight, I would suggest making use of an embedded library.