Version 0.2
This commit is contained in:
@@ -11,11 +11,14 @@ class Topic : public IndexedString
|
||||
public:
|
||||
Topic(const char* s, uint8_t len) : IndexedString(s,len){}
|
||||
Topic(const char* s) : Topic(s, strlen(s)) {}
|
||||
Topic(const std::string s) : Topic(s.c_str(), s.length()){};
|
||||
|
||||
const char* c_str() const { return str().c_str(); }
|
||||
|
||||
bool matches(const Topic&) const;
|
||||
};
|
||||
|
||||
class MqttCnx;
|
||||
class MqttClient;
|
||||
class MqttMessage
|
||||
{
|
||||
public:
|
||||
@@ -45,6 +48,9 @@ class MqttMessage
|
||||
MqttMessage(Type t) { create(t); }
|
||||
void incoming(char byte);
|
||||
void add(char byte) { incoming(byte); }
|
||||
void add(const char* p, size_t len);
|
||||
void add(const std::string& s) { add(s.c_str(), s.length()); }
|
||||
void add(const Topic& t) { add(t.str()); }
|
||||
char* getVHeader() const { return vheader; }
|
||||
char* end() const { return curr; }
|
||||
uint16_t length() const { return curr-buffer; }
|
||||
@@ -69,7 +75,7 @@ class MqttMessage
|
||||
size=0;
|
||||
state=Create;
|
||||
}
|
||||
void sendTo(MqttCnx*);
|
||||
void sendTo(MqttClient*);
|
||||
void hexdump(const char* prefix=nullptr) const;
|
||||
|
||||
private:
|
||||
@@ -83,8 +89,9 @@ class MqttMessage
|
||||
};
|
||||
|
||||
class MqttBroker;
|
||||
class MqttCnx
|
||||
class MqttClient
|
||||
{
|
||||
using CallBack = void (*)(const Topic& topic, const char* payload, size_t payload_length);
|
||||
enum Flags
|
||||
{
|
||||
FlagUserName = 128,
|
||||
@@ -96,11 +103,11 @@ class MqttCnx
|
||||
FlagReserved = 1
|
||||
};
|
||||
public:
|
||||
MqttCnx(MqttBroker* parent, WiFiClient& client);
|
||||
MqttClient(MqttBroker*);
|
||||
|
||||
~MqttCnx();
|
||||
~MqttClient();
|
||||
|
||||
bool connected() { return client && client->connected(); }
|
||||
bool connected() { return client==nullptr || client->connected(); }
|
||||
void write(const char* buf, size_t length)
|
||||
{ if (client) client->write(buf, length); }
|
||||
|
||||
@@ -108,9 +115,22 @@ class MqttCnx
|
||||
|
||||
void loop();
|
||||
void close();
|
||||
void publish(const Topic& topic, MqttMessage& msg);
|
||||
void setCallback(CallBack fun) {callback=fun; };
|
||||
|
||||
// Publish from client to the world
|
||||
void publish(const Topic&, const char* payload, size_t pay_length);
|
||||
void publish(const Topic& t, const std::string& s) { publish(t,s.c_str(),s.length());}
|
||||
void publish(const Topic& t) { publish(t, nullptr, 0);};
|
||||
|
||||
void subscribe(Topic topic) { subscriptions.insert(topic); }
|
||||
void unsubscribe(Topic& topic);
|
||||
|
||||
private:
|
||||
friend class MqttBroker;
|
||||
MqttClient(MqttBroker* parent, WiFiClient& client);
|
||||
// republish a received publish if topic matches any in subscriptions
|
||||
void publish(const Topic& topic, MqttMessage& msg);
|
||||
|
||||
void clientAlive();
|
||||
void processMessage();
|
||||
|
||||
@@ -118,20 +138,12 @@ class MqttCnx
|
||||
uint32_t keep_alive;
|
||||
uint32_t alive;
|
||||
bool mqtt_connected;
|
||||
WiFiClient* client;
|
||||
WiFiClient* client; // nullptr if this client is local
|
||||
MqttMessage message;
|
||||
MqttBroker* parent;
|
||||
std::set<Topic> subscriptions;
|
||||
std::string clientId;
|
||||
};
|
||||
|
||||
class MqttClient
|
||||
{
|
||||
public:
|
||||
MqttClient(IPAddress broker) : broker_ip(broker) {}
|
||||
|
||||
protected:
|
||||
IPAddress broker_ip;
|
||||
CallBack callback;
|
||||
};
|
||||
|
||||
class MqttBroker
|
||||
@@ -142,17 +154,26 @@ class MqttBroker
|
||||
void begin() { server.begin(); }
|
||||
void loop();
|
||||
|
||||
uint8_t port() const { return server.port(); }
|
||||
|
||||
private:
|
||||
friend class MqttClient;
|
||||
|
||||
bool checkUser(const char* user, uint8_t len) const
|
||||
{ return compareString(auth_user, user, len); }
|
||||
|
||||
bool checkPassword(const char* password, uint8_t len) const
|
||||
{ return compareString(auth_password, password, len); }
|
||||
|
||||
|
||||
void publish(const Topic& topic, MqttMessage& msg);
|
||||
|
||||
private:
|
||||
// For clients that are added not by the broker itself
|
||||
void addClient(MqttClient* client);
|
||||
void removeClient(MqttClient* client);
|
||||
|
||||
bool compareString(const char* good, const char* str, uint8_t str_len) const;
|
||||
std::vector<MqttCnx*> clients;
|
||||
std::vector<MqttClient*> clients;
|
||||
WiFiServer server;
|
||||
|
||||
const char* auth_user = "guest";
|
||||
|
||||
Reference in New Issue
Block a user