diff --git a/MQTTConnection.cpp b/MQTTConnection.cpp index c102bf1..b8189c3 100644 --- a/MQTTConnection.cpp +++ b/MQTTConnection.cpp @@ -21,7 +21,12 @@ #include #include #include -#include + +#if defined(_WIN32) || defined(_WIN64) +#include +#else +#include +#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>& subs, unsigned int keepalive, MQTT_QOS qos) : m_host(host), @@ -52,7 +57,11 @@ CMQTTConnection::~CMQTTConnection() bool CMQTTConnection::open() { char name[50U]; - ::sprintf(name, "DMRGateway.%ld", ::time(nullptr)); +#if defined(_WIN32) || defined(_WIN64) + ::sprintf(name, "DMRGateway.%u", (unsigned)::_getpid()); +#else + ::sprintf(name, "DMRGateway.%u", (unsigned)::getpid()); +#endif ::fprintf(stdout, "DMRGateway (%s) connecting to MQTT as %s\n", m_name.c_str(), name); @@ -137,6 +146,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; }