[TinyMqtt.h] rework code

This commit is contained in:
hsaturn
2022-12-01 08:07:25 +01:00
parent 9ef47fa6a4
commit 2a4e84d827

View File

@@ -164,7 +164,7 @@ class MqttClient
FlagReserved = 1 FlagReserved = 1
}; };
public: public:
/** Constructor. If broker is not null, this is the adress of a local broker. /** Constructor. Broker is the adress of a local broker if not null
If you want to connect elsewhere, leave broker null and use connect() **/ If you want to connect elsewhere, leave broker null and use connect() **/
MqttClient(MqttBroker* broker = nullptr, const std::string& id=""); MqttClient(MqttBroker* broker = nullptr, const std::string& id="");
MqttClient(const std::string& id) : MqttClient(nullptr, id){} MqttClient(const std::string& id) : MqttClient(nullptr, id){}
@@ -175,10 +175,12 @@ class MqttClient
void connect(std::string broker, uint16_t port, uint16_t keep_alive = 10); void connect(std::string broker, uint16_t port, uint16_t keep_alive = 10);
// TODO it seems that connected returns true in tcp mode even if // TODO it seems that connected returns true in tcp mode even if
// no negociation occured (only if tcp link is established) // no negociation occured
bool connected() { return bool connected()
(local_broker!=nullptr and client==nullptr) or {
(client and client->connected()); } return (local_broker!=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);
@@ -273,10 +275,9 @@ class MqttClient
uint32_t alive; uint32_t alive;
MqttMessage message; MqttMessage message;
// TODO having a pointer on MqttBroker may produce larger binaries // connection to local broker, or link to the parent
// due to unecessary function linked if ever local_broker is not used // when MqttBroker uses MqttClient for each external connexion
// (this is the case when MqttBroker isn't used except here) MqttBroker* local_broker=nullptr;
MqttBroker* local_broker=nullptr; // connection to local broker
TcpClient* client=nullptr; // connection to remote broker TcpClient* client=nullptr; // connection to remote broker
std::set<Topic> subscriptions; std::set<Topic> subscriptions;
@@ -326,17 +327,19 @@ class MqttBroker
MqttError subscribe(const Topic& topic, uint8_t qos); MqttError subscribe(const Topic& topic, uint8_t qos);
// For clients that are added not by the broker itself // For clients that are added not by the broker itself (local clients)
void addClient(MqttClient* client); void addClient(MqttClient* client);
void removeClient(MqttClient* client); void removeClient(MqttClient* client);
bool compareString(const char* good, const char* str, uint8_t str_len) const; bool compareString(const char* good, const char* str, uint8_t str_len) const;
std::vector<MqttClient*> clients; std::vector<MqttClient*> clients;
private:
TcpServer* server = nullptr; TcpServer* server = nullptr;
const char* auth_user = "guest"; const char* auth_user = "guest";
const char* auth_password = "guest"; const char* auth_password = "guest";
State state = Disconnected;
MqttClient* broker = nullptr; MqttClient* broker = nullptr;
State state = Disconnected;
}; };