1
0
mirror of https://github.com/eclipse/paho.mqtt.cpp.git synced 2025-05-09 11:21:24 +08:00

#523 Additional ways to create and publish messages with v5 properties

This commit is contained in:
fpagliughi 2025-01-03 15:31:44 -05:00
parent fa88c2f67b
commit 9bb127986d
10 changed files with 56 additions and 44 deletions

View File

@ -141,8 +141,7 @@ int main(int argc, char* argv[])
signal(SIGINT, ctrlc_handler);
// Sync clock to start of delta period
while (timestamp() % DELTA_MS != 0)
;
while (timestamp() % DELTA_MS != 0);
uint64_t t = timestamp(), tlast = t, tstart = t;
top.publish(to_string(t));

View File

@ -207,8 +207,7 @@ int main(int argc, char* argv[])
// Just block till user tells us to quit.
while (std::tolower(std::cin.get()) != 'q')
;
while (std::tolower(std::cin.get()) != 'q');
// Disconnect

View File

@ -541,7 +541,8 @@ public:
* token will be passed to callback methods if set.
*/
delivery_token_ptr publish(
string_ref topic, const void* payload, size_t n, int qos, bool retained
string_ref topic, const void* payload, size_t n, int qos, bool retained,
const properties &props=properties()
) override;
/**
* Publishes a message to a topic on the server
@ -567,7 +568,9 @@ public:
* @return token used to track and wait for the publish to complete. The
* token will be passed to callback methods if set.
*/
delivery_token_ptr publish(string_ref topic, binary_ref payload, int qos, bool retained)
delivery_token_ptr publish(string_ref topic, binary_ref payload, int qos, bool retained,
const properties &props=properties()
)
override;
/**
* Publishes a message to a topic on the server

View File

@ -207,7 +207,8 @@ public:
* token will be passed to callback methods if set.
*/
virtual delivery_token_ptr publish(
string_ref topic, const void* payload, size_t n, int qos, bool retained
string_ref topic, const void* payload, size_t n, int qos, bool retained,
const properties &props=properties()
) = 0;
/**
* Publishes a message to a topic on the server
@ -249,7 +250,8 @@ public:
* token will be passed to callback methods if set.
*/
virtual delivery_token_ptr publish(
string_ref topic, binary_ref payload, int qos, bool retained
string_ref topic, binary_ref payload, int qos, bool retained,
const properties &props=properties()
) = 0;
/**
* Publishes a message to a topic on the server.

View File

@ -160,8 +160,7 @@ public:
~message() {}
/**
* Constructs a message with the specified array as a payload, and all
* other values set to defaults.
* Constructs a message with the specified values.
* @param topic The message topic
* @param payload the bytes to use as the message payload
* @param len the number of bytes in the payload
@ -383,9 +382,15 @@ using const_message_ptr = message::const_ptr_t;
* @param topic The message topic
* @param payload the bytes to use as the message payload
* @param len the number of bytes in the payload
* @param qos The quality of service for the message.
* @param retained Whether the message should be retained by the broker.
* @param props The MQTT v5 properties for the message.
*/
inline message_ptr make_message(string_ref topic, const void* payload, size_t len) {
return mqtt::message::create(std::move(topic), payload, len);
inline message_ptr make_message(
string_ref topic, const void* payload, size_t len, int qos, bool retained,
const properties& props = properties()
) {
return mqtt::message::create(std::move(topic), payload, len, qos, retained, props);
}
/**
@ -394,23 +399,9 @@ inline message_ptr make_message(string_ref topic, const void* payload, size_t le
* @param topic The message topic
* @param payload the bytes to use as the message payload
* @param len the number of bytes in the payload
* @param qos The quality of service for the message.
* @param retained Whether the message should be retained by the broker.
*/
inline message_ptr make_message(
string_ref topic, const void* payload, size_t len, int qos, bool retained
) {
return mqtt::message::create(std::move(topic), payload, len, qos, retained);
}
/**
* Constructs a message with the specified buffer as a payload, and
* all other values set to defaults.
* @param topic The message topic
* @param payload A string to use as the message payload.
*/
inline message_ptr make_message(string_ref topic, binary_ref payload) {
return mqtt::message::create(std::move(topic), std::move(payload));
inline message_ptr make_message(string_ref topic, const void* payload, size_t len) {
return mqtt::message::create(std::move(topic), payload, len);
}
/**
@ -421,9 +412,20 @@ inline message_ptr make_message(string_ref topic, binary_ref payload) {
* @param retained Whether the message should be retained by the broker.
*/
inline message_ptr make_message(
string_ref topic, binary_ref payload, int qos, bool retained
string_ref topic, binary_ref payload, int qos, bool retained,
const properties& props = properties()
) {
return mqtt::message::create(std::move(topic), std::move(payload), qos, retained);
return mqtt::message::create(std::move(topic), std::move(payload), qos, retained, props);
}
/**
* Constructs a message with the specified buffer as a payload, and
* all other values set to defaults.
* @param topic The message topic
* @param payload A string to use as the message payload.
*/
inline message_ptr make_message(string_ref topic, binary_ref payload) {
return mqtt::message::create(std::move(topic), std::move(payload));
}
/////////////////////////////////////////////////////////////////////////////

View File

@ -557,18 +557,20 @@ std::vector<delivery_token_ptr> async_client::get_pending_delivery_tokens() cons
// Publish
delivery_token_ptr async_client::publish(
string_ref topic, const void* payload, size_t n, int qos, bool retained
string_ref topic, const void* payload, size_t n, int qos, bool retained,
const properties& props /*=properties()*/
)
{
auto msg = message::create(std::move(topic), payload, n, qos, retained);
auto msg = message::create(std::move(topic), payload, n, qos, retained, props);
return publish(std::move(msg));
}
delivery_token_ptr async_client::publish(
string_ref topic, binary_ref payload, int qos, bool retained
string_ref topic, binary_ref payload, int qos, bool retained,
const properties& props /*=properties()*/
)
{
auto msg = message::create(std::move(topic), std::move(payload), qos, retained);
auto msg = message::create(std::move(topic), std::move(payload), qos, retained, props);
return publish(std::move(msg));
}

View File

@ -262,8 +262,9 @@ properties& properties::operator=(properties&& rhs)
property properties::get(property::code propid, size_t idx /*=0*/) const
{
MQTTProperty* prop =
MQTTProperties_getPropertyAt(&props_, MQTTPropertyCodes(propid), int(idx));
MQTTProperty* prop = MQTTProperties_getPropertyAt(
const_cast<MQTTProperties*>(&props_), MQTTPropertyCodes(propid), int(idx)
);
if (!prop)
throw bad_cast();

View File

@ -116,7 +116,8 @@ public:
bool is_connected() const override { return true; };
mqtt::delivery_token_ptr publish(
string_ref topic, const void* payload, size_t n, int qos, bool retained
string_ref topic, const void* payload, size_t n, int qos, bool retained,
const properties &props=properties()
) override
{
auto msg = mqtt::message::create(topic, payload, n, qos, retained);
@ -130,7 +131,8 @@ public:
};
mqtt::delivery_token_ptr publish(
string_ref topic, binary_ref payload, int qos, bool retained
string_ref topic, binary_ref payload, int qos, bool retained,
const properties &props=properties()
) override
{
auto msg = mqtt::message::create(topic, payload, qos, retained);

View File

@ -576,7 +576,8 @@ TEST_CASE("async_client publish 5 args", "[client]")
const void* payload{PAYLOAD.data()};
const size_t payload_size{PAYLOAD.size()};
delivery_token_ptr token_pub{cli.publish(TOPIC, payload, payload_size, GOOD_QOS, RETAINED)
delivery_token_ptr token_pub{
cli.publish(TOPIC, payload, payload_size, GOOD_QOS, RETAINED)
};
REQUIRE(token_pub);
token_pub->wait_for(TIMEOUT);
@ -735,8 +736,8 @@ TEST_CASE("async_client subscribe many topics 2 args_single", "[client]")
cli.connect()->wait();
REQUIRE(cli.is_connected());
mqtt::const_string_collection_ptr TOPIC_1_COLL{mqtt::string_collection::create({"TOPIC0"})
};
mqtt::const_string_collection_ptr TOPIC_1_COLL{mqtt::string_collection::create({"TOPIC0"}
)};
iasync_client::qos_collection GOOD_QOS_1_COLL{0};
try {
cli.subscribe(TOPIC_1_COLL, GOOD_QOS_1_COLL)->wait_for(TIMEOUT);

View File

@ -534,8 +534,8 @@ public:
// NOTE: Only tokens for messages with QOS=1 and QOS=2 are kept. That's
// why the vector's size does not account for QOS=0 message tokens
std::vector<mqtt::delivery_token_ptr> tokens_pending{cli.get_pending_delivery_tokens()
};
std::vector<mqtt::delivery_token_ptr> tokens_pending{cli.get_pending_delivery_tokens(
)};
CPPUNIT_ASSERT_EQUAL(2, static_cast<int>(tokens_pending.size()));
mqtt::token_ptr token_disconn{cli.disconnect()};
@ -831,7 +831,8 @@ public:
CPPUNIT_ASSERT(cli.is_connected());
mqtt::test::dummy_action_listener listener;
mqtt::token_ptr token_sub{cli.subscribe(TOPIC_COLL, GOOD_QOS_COLL, &CONTEXT, listener)
mqtt::token_ptr token_sub{
cli.subscribe(TOPIC_COLL, GOOD_QOS_COLL, &CONTEXT, listener)
};
CPPUNIT_ASSERT(token_sub);
token_sub->wait_for(TIMEOUT);