tcp_client is now a unique_ptr

This commit is contained in:
hsaturn
2023-01-02 00:10:40 +01:00
parent 8162b4c35b
commit b8022f58a4
2 changed files with 6 additions and 8 deletions

View File

@@ -38,12 +38,12 @@ MqttClient::MqttClient(MqttBroker* local_broker, TcpClient* new_client)
connect(local_broker); connect(local_broker);
debug("MqttClient private with broker"); debug("MqttClient private with broker");
#ifdef TINY_MQTT_ASYNC #ifdef TINY_MQTT_ASYNC
tcp_client = new_client; tcp_client.reset(new_client);
tcp_client->onData(onData, this); tcp_client->onData(onData, this);
// client->onConnect() TODO // client->onConnect() TODO
// client->onDisconnect() TODO // client->onDisconnect() TODO
#else #else
tcp_client = new WiFiClient(*new_client); tcp_client.reset(new WiFiClient(*new_client));
#endif #endif
alive = millis()+5000; alive = millis()+5000;
} }
@@ -59,7 +59,6 @@ MqttClient::MqttClient(MqttBroker* local_broker, const std::string& id)
MqttClient::~MqttClient() MqttClient::~MqttClient()
{ {
close(); close();
delete tcp_client;
debug("*** MqttClient delete()"); debug("*** MqttClient delete()");
} }
@@ -99,8 +98,7 @@ void MqttClient::connect(std::string broker, uint16_t port, uint16_t ka)
debug("MqttClient::connect_to_host " << broker << ':' << port); debug("MqttClient::connect_to_host " << broker << ':' << port);
keep_alive = ka; keep_alive = ka;
close(); close();
if (tcp_client) delete tcp_client; tcp_client.reset(new TcpClient);
tcp_client = new TcpClient;
#ifdef TINY_MQTT_ASYNC #ifdef TINY_MQTT_ASYNC
tcp_client->onData(onData, this); tcp_client->onData(onData, this);
@@ -110,7 +108,7 @@ void MqttClient::connect(std::string broker, uint16_t port, uint16_t ka)
if (tcp_client->connect(broker.c_str(), port)) if (tcp_client->connect(broker.c_str(), port))
{ {
debug("link established"); debug("link established");
onConnect(this, tcp_client); onConnect(this, tcp_client.get());
} }
else else
{ {
@@ -580,7 +578,7 @@ void MqttClient::processMessage(MqttMessage* mesg)
case MqttMessage::Type::Publish: case MqttMessage::Type::Publish:
#if TINY_MQTT_DEBUG #if TINY_MQTT_DEBUG
Console << "publish " << (mqtt_flags & FlagConnected) << '/' << (long) tcp_client << endl; Console << "publish " << (mqtt_flags & FlagConnected) << '/' << (long) tcp_client.get() << endl;
#endif #endif
if ((mqtt_flags & FlagConnected) or tcp_client == nullptr) if ((mqtt_flags & FlagConnected) or tcp_client == nullptr)
{ {

View File

@@ -303,7 +303,7 @@ class MqttClient
// when MqttBroker uses MqttClient for each external connexion // when MqttBroker uses MqttClient for each external connexion
MqttBroker* local_broker=nullptr; MqttBroker* local_broker=nullptr;
TcpClient* tcp_client=nullptr; // connection to remote broker std::unique_ptr<TcpClient> tcp_client; // connection to remote broker
std::set<Topic> subscriptions; std::set<Topic> subscriptions;
std::string clientId; std::string clientId;
CallBack callback = nullptr; CallBack callback = nullptr;