Fix crash on MqttClient timeout when not linked to a broker
This commit is contained in:
@@ -28,10 +28,11 @@ MqttBroker::~MqttBroker()
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// private constructor used by broker only
|
||||||
MqttClient::MqttClient(MqttBroker* parent, WiFiClient& new_client)
|
MqttClient::MqttClient(MqttBroker* parent, WiFiClient& new_client)
|
||||||
: parent(parent)
|
: parent(parent)
|
||||||
{
|
{
|
||||||
client = new_client ? new WiFiClient(new_client) : nullptr;
|
client = new WiFiClient(new_client);
|
||||||
alive = millis()+5000; // client expires after 5s if no CONNECT msg
|
alive = millis()+5000; // client expires after 5s if no CONNECT msg
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -80,7 +81,7 @@ void MqttClient::connect(std::string broker, uint16_t port)
|
|||||||
message.add(0x4); // Mqtt protocol version 3.1.1
|
message.add(0x4); // Mqtt protocol version 3.1.1
|
||||||
message.add(0x0); // Connect flags TODO user / name
|
message.add(0x0); // Connect flags TODO user / name
|
||||||
|
|
||||||
keep_alive = 1;
|
keep_alive = 1; // TODO not configurable
|
||||||
message.add(0x00); // keep_alive
|
message.add(0x00); // keep_alive
|
||||||
message.add((char)keep_alive);
|
message.add((char)keep_alive);
|
||||||
message.add(clientId);
|
message.add(clientId);
|
||||||
@@ -127,7 +128,7 @@ void MqttBroker::loop()
|
|||||||
for(int i=0; i<clients.size(); i++)
|
for(int i=0; i<clients.size(); i++)
|
||||||
{
|
{
|
||||||
auto client = clients[i];
|
auto client = clients[i];
|
||||||
if(client->connected())
|
if (client->connected())
|
||||||
{
|
{
|
||||||
client->loop();
|
client->loop();
|
||||||
}
|
}
|
||||||
@@ -209,8 +210,9 @@ void MqttClient::loop()
|
|||||||
{
|
{
|
||||||
debug("timeout client");
|
debug("timeout client");
|
||||||
close();
|
close();
|
||||||
|
debug("closed");
|
||||||
}
|
}
|
||||||
else
|
else if (client && client->connected())
|
||||||
{
|
{
|
||||||
uint16_t pingreq = MqttMessage::Type::PingReq;
|
uint16_t pingreq = MqttMessage::Type::PingReq;
|
||||||
client->write((uint8_t*)(&pingreq), 2);
|
client->write((uint8_t*)(&pingreq), 2);
|
||||||
|
|||||||
@@ -114,7 +114,9 @@ class MqttClient
|
|||||||
void connect(MqttBroker* parent);
|
void connect(MqttBroker* parent);
|
||||||
void connect(std::string broker, uint16_t port);
|
void connect(std::string broker, uint16_t port);
|
||||||
|
|
||||||
bool connected() { return client==nullptr || client->connected(); }
|
bool connected() { return
|
||||||
|
(parent!=nullptr and client==nullptr) or
|
||||||
|
(client and client->connected()); }
|
||||||
void write(const char* buf, size_t length)
|
void write(const char* buf, size_t length)
|
||||||
{ if (client) client->write(buf, length); }
|
{ if (client) client->write(buf, length); }
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user