123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820 |
- #ifndef __mqtt_async_client_h
- #define __mqtt_async_client_h
- #include "MQTTAsync.h"
- #include "mqtt/types.h"
- #include "mqtt/token.h"
- #include "mqtt/create_options.h"
- #include "mqtt/string_collection.h"
- #include "mqtt/delivery_token.h"
- #include "mqtt/iclient_persistence.h"
- #include "mqtt/iaction_listener.h"
- #include "mqtt/properties.h"
- #include "mqtt/exception.h"
- #include "mqtt/message.h"
- #include "mqtt/callback.h"
- #include "mqtt/thread_queue.h"
- #include "mqtt/iasync_client.h"
- #include <vector>
- #include <list>
- #include <memory>
- #include <tuple>
- #include <functional>
- #include <stdexcept>
- namespace mqtt {
- #if defined(PAHO_MQTTPP_VERSIONS)
-
- const uint32_t PAHO_MQTTPP_VERSION = 0x01040000;
-
- const string PAHO_MQTTPP_VERSION_STR("Paho MQTT C++ (mqttpp) v. 1.4.0");
-
- const string PAHO_MQTTPP_COPYRIGHT("Copyright (c) 2013-2024 Frank Pagliughi");
- #else
-
- const uint32_t VERSION = 0x01040000;
-
- const string VERSION_STR("Paho MQTT C++ (mqttpp) v. 1.4.0");
-
- const string COPYRIGHT("Copyright (c) 2013-2024 Frank Pagliughi");
- #endif
- class async_client : public virtual iasync_client
- {
- public:
-
- using ptr_t = std::shared_ptr<async_client>;
-
- using consumer_queue_type = std::unique_ptr<thread_queue<const_message_ptr>>;
-
- using message_handler = std::function<void(const_message_ptr)>;
-
- using connection_handler = std::function<void(const string& cause)>;
-
- using disconnected_handler = std::function<void(const properties&, ReasonCode)>;
-
- using update_connection_handler = std::function<bool(connect_data&)>;
- private:
-
- using guard = std::unique_lock<std::mutex>;
-
- using unique_lock = std::unique_lock<std::mutex>;
-
- mutable std::mutex lock_;
-
- MQTTAsync cli_;
-
- string serverURI_;
-
- string clientId_;
-
- int mqttVersion_;
-
- std::unique_ptr<MQTTClient_persistence> persist_;
-
- callback* userCallback_;
-
- connection_handler connHandler_;
-
- connection_handler connLostHandler_;
-
- disconnected_handler disconnectedHandler_;
-
- update_connection_handler updateConnectionHandler_;
-
- message_handler msgHandler_;
-
- connect_options connOpts_;
-
- token_ptr connTok_;
-
- std::list<token_ptr> pendingTokens_;
-
- std::list<delivery_token_ptr> pendingDeliveryTokens_;
-
- consumer_queue_type que_;
-
- static void on_connected(void* context, char* cause);
- static void on_connection_lost(void *context, char *cause);
- static void on_disconnected(void* context, MQTTProperties* cprops,
- MQTTReasonCodes reasonCode);
- static int on_message_arrived(void* context, char* topicName, int topicLen,
- MQTTAsync_message* msg);
- static void on_delivery_complete(void* context, MQTTAsync_token tok);
- static int on_update_connection(void* context, MQTTAsync_connectData* cdata);
-
- friend class token;
- virtual void add_token(token_ptr tok);
- virtual void add_token(delivery_token_ptr tok);
- virtual void remove_token(token* tok) override;
- virtual void remove_token(token_ptr tok) { remove_token(tok.get()); }
- void remove_token(delivery_token_ptr tok) { remove_token(tok.get()); }
-
- async_client() =delete;
- async_client(const async_client&) =delete;
- async_client& operator=(const async_client&) =delete;
-
- static void check_ret(int rc) {
- if (rc != MQTTASYNC_SUCCESS)
- throw exception(rc);
- }
- public:
-
- async_client(const string& serverURI, const string& clientId,
- const string& persistDir);
-
- async_client(const string& serverURI, const string& clientId,
- iclient_persistence* persistence=nullptr);
-
- async_client(const string& serverURI, const string& clientId,
- int maxBufferedMessages, const string& persistDir);
-
- async_client(const string& serverURI, const string& clientId,
- int maxBufferedMessages,
- iclient_persistence* persistence=nullptr);
-
- async_client(const string& serverURI, const string& clientId,
- const create_options& opts, const string& persistDir);
-
- async_client(const string& serverURI, const string& clientId,
- const create_options& opts,
- iclient_persistence* persistence=nullptr);
-
- ~async_client() override;
-
- void set_callback(callback& cb) override;
-
- void disable_callbacks() override;
-
- void set_connected_handler(connection_handler cb) ;
-
- void set_connection_lost_handler(connection_handler cb) ;
-
- void set_disconnected_handler(disconnected_handler cb) ;
-
- void set_message_callback(message_handler cb) ;
-
- void set_update_connection_handler(update_connection_handler cb);
-
- token_ptr connect() override;
-
- token_ptr connect(connect_options options) override;
-
- token_ptr connect(connect_options options, void* userContext,
- iaction_listener& cb) override;
-
- token_ptr connect(void* userContext, iaction_listener& cb) override {
- return connect(connect_options{}, userContext, cb);
- }
-
- token_ptr reconnect() override;
-
- token_ptr disconnect() override { return disconnect(disconnect_options()); }
-
- token_ptr disconnect(disconnect_options opts) override;
-
- token_ptr disconnect(int timeout) override {
- return disconnect(disconnect_options(timeout));
- }
-
- template <class Rep, class Period>
- token_ptr disconnect(const std::chrono::duration<Rep, Period>& timeout) {
-
- return disconnect((int) to_milliseconds_count(timeout));
- }
-
- token_ptr disconnect(int timeout, void* userContext,
- iaction_listener& cb) override;
-
- template <class Rep, class Period>
- token_ptr disconnect(const std::chrono::duration<Rep, Period>& timeout,
- void* userContext, iaction_listener& cb) {
-
- return disconnect((int) to_milliseconds_count(timeout), userContext, cb);
- }
-
- token_ptr disconnect(void* userContext, iaction_listener& cb) override {
- return disconnect(0L, userContext, cb);
- }
-
- delivery_token_ptr get_pending_delivery_token(int msgID) const override;
-
- std::vector<delivery_token_ptr> get_pending_delivery_tokens() const override;
-
- string get_client_id() const override { return clientId_; }
-
- string get_server_uri() const override { return serverURI_; }
-
- int mqtt_version() const noexcept { return mqttVersion_; }
-
- connect_options get_connect_options() const {
- guard g(lock_);
- return connOpts_;
- }
-
- bool is_connected() const override { return to_bool(MQTTAsync_isConnected(cli_)); }
-
- delivery_token_ptr publish(string_ref topic, const void* payload, size_t n,
- int qos, bool retained) override;
-
- delivery_token_ptr publish(string_ref topic, const void* payload, size_t n) override {
- return publish(std::move(topic), payload, n,
- message::DFLT_QOS, message::DFLT_RETAINED);
- }
-
- delivery_token_ptr publish(string_ref topic, binary_ref payload,
- int qos, bool retained) override;
-
- delivery_token_ptr publish(string_ref topic, binary_ref payload) override {
- return publish(std::move(topic), std::move(payload),
- message::DFLT_QOS, message::DFLT_RETAINED);
- }
-
- delivery_token_ptr publish(string_ref topic,
- const void* payload, size_t n,
- int qos, bool retained,
- void* userContext, iaction_listener& cb) override;
-
- delivery_token_ptr publish(const_message_ptr msg) override;
-
- delivery_token_ptr publish(const_message_ptr msg,
- void* userContext, iaction_listener& cb) override;
-
- token_ptr subscribe(const string& topicFilter, int qos,
- const subscribe_options& opts=subscribe_options(),
- const properties& props=properties()) override;
-
- token_ptr subscribe(const string& topicFilter, int qos,
- void* userContext, iaction_listener& cb,
- const subscribe_options& opts=subscribe_options(),
- const properties& props=properties()) override;
-
- token_ptr subscribe(const_string_collection_ptr topicFilters,
- const qos_collection& qos,
- const std::vector<subscribe_options>& opts=std::vector<subscribe_options>(),
- const properties& props=properties()) override;
-
- token_ptr subscribe(const_string_collection_ptr topicFilters,
- const qos_collection& qos,
- void* userContext, iaction_listener& cb,
- const std::vector<subscribe_options>& opts=std::vector<subscribe_options>(),
- const properties& props=properties()) override;
-
- token_ptr unsubscribe(const string& topicFilter,
- const properties& props=properties()) override;
-
- token_ptr unsubscribe(const_string_collection_ptr topicFilters,
- const properties& props=properties()) override;
-
- token_ptr unsubscribe(const_string_collection_ptr topicFilters,
- void* userContext, iaction_listener& cb,
- const properties& props=properties()) override;
-
- token_ptr unsubscribe(const string& topicFilter,
- void* userContext, iaction_listener& cb,
- const properties& props=properties()) override;
-
- void start_consuming() override;
-
- void stop_consuming() override;
-
- const_message_ptr consume_message() override { return que_->get(); }
-
- bool try_consume_message(const_message_ptr* msg) override {
- return que_->try_get(msg);
- }
-
- template <typename Rep, class Period>
- bool try_consume_message_for(const_message_ptr* msg,
- const std::chrono::duration<Rep, Period>& relTime) {
- return que_->try_get_for(msg, relTime);
- }
-
- template <typename Rep, class Period>
- const_message_ptr try_consume_message_for(const std::chrono::duration<Rep, Period>& relTime) {
- const_message_ptr msg;
- que_->try_get_for(&msg, relTime);
- return msg;
- }
-
- template <class Clock, class Duration>
- bool try_consume_message_until(const_message_ptr* msg,
- const std::chrono::time_point<Clock,Duration>& absTime) {
- return que_->try_get_until(msg, absTime);
- }
-
- template <class Clock, class Duration>
- const_message_ptr try_consume_message_until(const std::chrono::time_point<Clock,Duration>& absTime) {
- const_message_ptr msg;
- que_->try_get_until(&msg, absTime);
- return msg;
- }
- };
- using async_client_ptr = async_client::ptr_t;
- }
- #endif
|