Fix some rare case crashes

This commit is contained in:
hsaturn
2021-03-22 00:27:23 +01:00
parent 9f28e7f92f
commit 52690ec7e7
2 changed files with 20 additions and 6 deletions

View File

@@ -20,6 +20,14 @@ MqttBroker::MqttBroker(uint16_t port) : server(port)
{ {
} }
MqttBroker::~MqttBroker()
{
while(clients.size())
{
delete clients[0];
}
}
MqttClient::MqttClient(MqttBroker* parent, WiFiClient& new_client) MqttClient::MqttClient(MqttBroker* parent, WiFiClient& new_client)
: parent(parent) : parent(parent)
{ {
@@ -32,7 +40,7 @@ MqttClient::MqttClient(MqttBroker* parent)
{ {
client = nullptr; client = nullptr;
parent->addClient(this); if (parent) parent->addClient(this);
} }
MqttClient::~MqttClient() MqttClient::~MqttClient()
@@ -61,9 +69,9 @@ void MqttClient::connect(std::string broker, uint16_t port)
{ {
debug("cnx: closing"); debug("cnx: closing");
close(); close();
debug("cnx: closed");
if (client) delete client; if (client) delete client;
client = new WiFiClient; client = new WiFiClient;
debug("Trying to connect to " << broker.c_str() << ':' << port);
if (client->connect(broker.c_str(), port)) if (client->connect(broker.c_str(), port))
{ {
debug("cnx: connecting"); debug("cnx: connecting");
@@ -467,9 +475,9 @@ void MqttMessage::incoming(char in_byte)
reset(); reset();
break; break;
} }
if (buffer.length() > 256) // TODO magic 256 ? if (buffer.length() > MaxBufferLength) // TODO magic 256 ?
{ {
debug("Too long"); debug("Too long " << state);
reset(); reset();
} }
} }

View File

@@ -4,7 +4,6 @@
#include <string> #include <string>
#include "StringIndexer.h" #include "StringIndexer.h"
#define MaxBufferLength 255
class Topic : public IndexedString class Topic : public IndexedString
{ {
@@ -21,6 +20,7 @@ class Topic : public IndexedString
class MqttClient; class MqttClient;
class MqttMessage class MqttMessage
{ {
const uint16_t MaxBufferLength = 255;
public: public:
enum Type enum Type
{ {
@@ -161,7 +161,12 @@ class MqttClient
uint32_t keep_alive; uint32_t keep_alive;
uint32_t alive; uint32_t alive;
MqttMessage message; MqttMessage message;
// TODO having a pointer on MqttBroker may produce larger binaries
// due to unecessary function linked if ever parent is not used
// (this is the case when MqttBroker isn't used except here)
MqttBroker* parent=nullptr; // connection to local broker MqttBroker* parent=nullptr; // connection to local broker
WiFiClient* client=nullptr; // connection to mqtt client or to remote broker WiFiClient* client=nullptr; // connection to mqtt client or to remote broker
std::set<Topic> subscriptions; std::set<Topic> subscriptions;
std::string clientId; std::string clientId;
@@ -179,6 +184,7 @@ class MqttBroker
public: public:
// TODO limit max number of clients // TODO limit max number of clients
MqttBroker(uint16_t port); MqttBroker(uint16_t port);
~MqttBroker();
void begin() { server.begin(); } void begin() { server.begin(); }
void loop(); void loop();
@@ -190,7 +196,7 @@ class MqttBroker
void dump() void dump()
{ {
Serial << "broker: " << clients.size() << " client/s" << endl; Serial << clients.size() << " client/s" << endl;
for(auto client: clients) for(auto client: clients)
{ {
Serial << " "; Serial << " ";