diff --git a/include/mqtt/async_client.h b/include/mqtt/async_client.h index 99f065db..8a9d210d 100644 --- a/include/mqtt/async_client.h +++ b/include/mqtt/async_client.h @@ -499,6 +499,11 @@ class async_client : public virtual iasync_client * @return delivery_token[] */ std::vector get_pending_delivery_tokens() const override; + /** + * Returns the current number of buffered (publish) messages for this client. + * @return number of buffered messages + */ + virtual int get_buffered_messages_num() const override; /** * Returns the client ID used by this client. * @return The client ID used by this client. diff --git a/include/mqtt/iasync_client.h b/include/mqtt/iasync_client.h index acff640c..188da937 100644 --- a/include/mqtt/iasync_client.h +++ b/include/mqtt/iasync_client.h @@ -181,6 +181,11 @@ class iasync_client * @return delivery_token[] */ virtual std::vector get_pending_delivery_tokens() const = 0; + /** + * Returns the current number of buffered (publish) messages for this client. + * @return number of buffered messages + */ + virtual int get_buffered_messages_num() const = 0; /** * Returns the client ID used by this client. * @return string diff --git a/src/async_client.cpp b/src/async_client.cpp index b11f00f2..8d1007ce 100644 --- a/src/async_client.cpp +++ b/src/async_client.cpp @@ -31,6 +31,12 @@ #include "mqtt/response_options.h" #include "mqtt/token.h" +// Include MQTTAsyncUtils, but need a workaround for the error +// ""redeclaration of C++ built-in type ‘bool’" inside this header" +#define bool c_bool +#include +#undef bool + #define UNUSED(x) (void)(x) namespace mqtt { @@ -559,8 +565,22 @@ std::vector async_client::get_pending_delivery_tokens() cons } } return toks; + +} + +int async_client::get_buffered_messages_num() const +{ + // Gets number of all buffered messages (QOS0-2) + // I'd like to have used MQTTAsync_getNoBufferedMessages() from MQTTAsyncUtils.h + // directly, but I haven't gotten it to compile correctly + // I hope the C++ lock_ also serves the same functionality as the C MQTTAsync mutex + + guard g(lock_); + return ((MQTTAsyncs*)cli_)->noBufferedMessages; } + + // -------------------------------------------------------------------------- // Publish diff --git a/test/unit/mock_async_client.h b/test/unit/mock_async_client.h index 080c8ee1..2ca67748 100644 --- a/test/unit/mock_async_client.h +++ b/test/unit/mock_async_client.h @@ -109,6 +109,11 @@ class mock_async_client : public virtual mqtt::iasync_client return std::vector{}; }; + int get_buffered_messages_num() const override + { + return 0; + } + std::string get_client_id() const override { return std::string{}; }; std::string get_server_uri() const override { return std::string{}; };