[MqttClient] Fix keep_alive decoding

This commit is contained in:
hsaturn
2022-12-03 20:23:15 +01:00
parent 2a4e84d827
commit 4726ff293c
6 changed files with 52 additions and 8 deletions

View File

@@ -106,7 +106,7 @@ void MqttClient::connect(std::string broker, uint16_t port, uint16_t ka)
#ifdef TINY_MQTT_ASYNC
client->onData(onData, this);
client->onConnect(onConnect, this);
client->connect(broker.c_str(), port);
client->connect(broker.c_str(), port, ka);
#else
if (client->connect(broker.c_str(), port))
{
@@ -259,7 +259,7 @@ bool MqttBroker::compareString(
void MqttMessage::getString(const char* &buff, uint16_t& len)
{
len = (buff[0]<<8)|(buff[1]);
len = getSize(buff);
buff+=2;
}
@@ -321,8 +321,8 @@ void MqttClient::onConnect(void *mqttclient_ptr, TcpClient*)
msg.add(0x4); // Mqtt protocol version 3.1.1
msg.add(0x0); // Connect flags TODO user / name
msg.add(0x00); // keep_alive
msg.add((char)mqtt->keep_alive);
msg.add((char)(mqtt->keep_alive >> 8)); // keep_alive
msg.add((char)(mqtt->keep_alive & 0xFF));
msg.add(mqtt->clientId);
debug("cnx: mqtt connecting");
msg.sendTo(mqtt);
@@ -443,7 +443,7 @@ void MqttClient::processMessage(MqttMessage* mesg)
}
payload = header+10;
mqtt_flags = header[7];
keep_alive = (header[8]<<8)|(header[9]);
keep_alive = MqttMessage::getSize(header+8);
if (strncmp("MQTT", header+2,4))
{
debug("bad mqtt header");

View File

@@ -104,6 +104,11 @@ class MqttMessage
Create=6
};
static inline uint32_t getSize(const char* buffer)
{
const unsigned char* bun = (const unsigned char*)buffer;
return (*bun << 8) | bun[1]; }
MqttMessage() { reset(); }
MqttMessage(Type t, uint8_t bits_d3_d0=0) { create(t); buffer[0] |= bits_d3_d0; }
void incoming(char byte);
@@ -250,6 +255,7 @@ class MqttClient
#ifdef EPOXY_DUINO
static std::map<MqttMessage::Type, int> counters; // Number of processed messages
#endif
uint32_t keepAlive() const { return keep_alive; }
private:
@@ -312,6 +318,8 @@ class MqttBroker
client->dump(indent);
}
const std::vector<MqttClient*> getClients() const { return clients; }
private:
friend class MqttClient;