mqtt_cpp
mqtt_cpp copied to clipboard
endpoint.hpp: allow custom packet id implementations
Currently endpoint uses a mutex and a monotonic increasing packet id.
There are other possible packet id strategies.
For example: An implementation with a known-ahead-of-time number of threads, might want break the packet id into two parts, a prefix (1 or more bits) and an id (the rest of the packet id's bits) and use thread-local-storage to track the increment of the id portion of the packetid. This would guarantee no two threads can ever generate the same packet id, with the benefit of not needing a mutex, but it would come at the cost of increased complexity and a smaller packet id space per thread.
Another example: A cluster implementation where the packetid to use is determined by another process on a separate computer.
A third example: The packet ID is queried from a database
I'd like to see the packet ID concept be improved in one of the two following ways:
- Remove endpoint.hpp's ability to generate it's own packet ids entirely, and require this functionality be handled by a derived class. (e.g. see #192, which talks about decomposing endpoint.hpp into an endpoint_base.hpp and endpoint.hpp)
- Require a new template argument (with default implementation) that supports the following functions 2.1 PacketId_t get_packet_id(void) 2.2 void release_packet_id(PacketId_t id) 2.3 bool reserve_packet_id(PacketId_t id)
I think that separating PacketId management mechanism is a good idea :)