Merge pull request #856 from MW0MWZ/fix/mqtt-client-id

Fix MQTT client ID collision on 32-bit ARM userland
This commit is contained in:
Jonathan Naylor 2026-03-08 15:16:08 +00:00 committed by GitHub
commit ed6c28c0c1
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -21,7 +21,12 @@
#include <cassert>
#include <cstdio>
#include <cstring>
#include <ctime>
#if defined(_WIN32) || defined(_WIN64)
#include <process.h>
#else
#include <unistd.h>
#endif
CMQTTConnection::CMQTTConnection(const std::string& host, unsigned short port, const std::string& name, const bool authEnabled, const std::string& username, const std::string& password, const std::vector<std::pair<std::string, void (*)(const unsigned char*, unsigned int)>>& subs, unsigned int keepalive, MQTT_QOS qos) :
@ -53,7 +58,11 @@ CMQTTConnection::~CMQTTConnection()
bool CMQTTConnection::open()
{
char name[50U];
::sprintf(name, "MMDVMHost.%ld", ::time(nullptr));
#if defined(_WIN32) || defined(_WIN64)
::sprintf(name, "MMDVMHost.%u", (unsigned)::_getpid());
#else
::sprintf(name, "MMDVMHost.%u", (unsigned)::getpid());
#endif
::fprintf(stdout, "MMDVMHost (%s) connecting to MQTT as %s\n", m_name.c_str(), name);
@ -138,6 +147,7 @@ void CMQTTConnection::close()
{
if (m_mosq != nullptr) {
::mosquitto_disconnect(m_mosq);
::mosquitto_loop_stop(m_mosq, true);
::mosquitto_destroy(m_mosq);
m_mosq = nullptr;
}