-
Notifications
You must be signed in to change notification settings - Fork 2
Description
With the current design, allocating a packet ID requires a mutable reference to the Connection, so it is difficult to generate MQTT packets from multiple threads (or async contexts) and send them over a channel to the thread running the MQTT client event loop (especially if the Connection object is local to the MQTT client thread). Some solutions I can think of:
- Pass my own message struct over the channel, and generate Publish/Subscribe/Unsubscribe packets on the MQTT client thread.
- Pass the packet builder over the channel, and call
builder.packet_id(foo).build()on the MQTT client thread. - Add an option for the MQTT client to allocate and overwrite the packet ID when serializing the packet inside
checked_send. - Allow
Connectionto use an externalPacketIdManagertrait object, so I can wrap thePacketIdManagerin a mutex and access it directly from the other threads.
Do you have any suggestions?
One reason I want to pass the pre-constructed packets over the channel, beyond having them pre-validated, is that they own the topic string/payload data, so they can be sent by value without additional boxing. But if I send my own message struct over the channel, at least the topic name will be copied again when constructing the MqttString. Would it make sense to change the builder::topic_name() methods to take an Into<MqttString> (like payloads take IntoPayload), so the channel sender can pre-create the MqttString?