diff --git a/src/TinyMqtt.cpp b/src/TinyMqtt.cpp index 3f11eed..83f112f 100644 --- a/src/TinyMqtt.cpp +++ b/src/TinyMqtt.cpp @@ -37,6 +37,7 @@ MqttBroker::~MqttBroker() #ifdef EPOXY_DUINO instances--; #endif + closeRemoteBroker(); while(clients.size()) { auto client = clients[0]; @@ -155,9 +156,19 @@ void MqttBroker::addClient(MqttClient* client) clients.push_back(client); } +void MqttBroker::closeRemoteBroker() +{ + if (remote_broker) + { + delete remote_broker; + remote_broker = nullptr; + } +} + void MqttBroker::connect(const string& host, uint16_t port) { debug("MqttBroker::connect"); + closeRemoteBroker(); if (remote_broker == nullptr) remote_broker = new MqttClient; remote_broker->connect(host, port); remote_broker->local_broker = this; // Because connect removed the link diff --git a/src/TinyMqtt.h b/src/TinyMqtt.h index 911d20a..6fb99a7 100644 --- a/src/TinyMqtt.h +++ b/src/TinyMqtt.h @@ -209,8 +209,8 @@ class MqttClient ~MqttClient(); void connect(MqttBroker* local_broker); - void connect(string broker, uint16_t port, uint16_t keep_alive = 10); - void connect(const IPAddress& ip, uint16_t port, uint16_t keep_alive = 10) + void connect(string broker, uint16_t port = 1883, uint16_t keep_alive = 10); + void connect(const IPAddress& ip, uint16_t port = 1883, uint16_t keep_alive = 10) { connect(ip.toString().c_str(), port, keep_alive); } // TODO it seems that connected returns true in tcp mode even if @@ -387,6 +387,8 @@ class MqttBroker const char* auth_password = "guest"; MqttClient* remote_broker = nullptr; + void closeRemoteBroker(); + void retain(const Topic& topic, const MqttMessage& msg); void retainDrop();