From ab0ae2777ab9c0d1c3fcc953bb685c3211c3b36c Mon Sep 17 00:00:00 2001 From: zhengshuxin Date: Wed, 13 Jul 2022 09:38:26 +0800 Subject: [PATCH] test mqtt --- lib_acl_cpp/samples/mqtt/mqtt_client/main.cpp | 5 +++-- lib_acl_cpp/samples/mqtt/mqtt_pub/main.cpp | 20 +++++++++++++------ 2 files changed, 17 insertions(+), 8 deletions(-) diff --git a/lib_acl_cpp/samples/mqtt/mqtt_client/main.cpp b/lib_acl_cpp/samples/mqtt/mqtt_client/main.cpp index 72c26e60b..788dabf43 100644 --- a/lib_acl_cpp/samples/mqtt/mqtt_client/main.cpp +++ b/lib_acl_cpp/samples/mqtt/mqtt_client/main.cpp @@ -55,9 +55,10 @@ static bool handle_publish(acl::mqtt_client& conn, const acl::mqtt_message& message) { const acl::mqtt_publish& publish = (const acl::mqtt_publish&) message; const acl::string& payload = publish.get_payload(); - printf("topic: %s, qos: %d, pkt_id: %d, payload len: %zd, payload: %s\r\n", + printf("payload: %s\r\n", payload.c_str()); + printf("topic: %s, qos: %d, pkt_id: %d, payload len: %zd\r\n", publish.get_topic(), (int) publish.get_header().get_qos(), - publish.get_pkt_id(), payload.size(), payload.c_str()); + publish.get_pkt_id(), payload.size()); if (publish.get_header().get_qos() == acl::MQTT_QOS0) { return true; diff --git a/lib_acl_cpp/samples/mqtt/mqtt_pub/main.cpp b/lib_acl_cpp/samples/mqtt/mqtt_pub/main.cpp index ad33b9dfe..78fc77236 100644 --- a/lib_acl_cpp/samples/mqtt/mqtt_pub/main.cpp +++ b/lib_acl_cpp/samples/mqtt/mqtt_pub/main.cpp @@ -14,7 +14,7 @@ static bool handle_puback(acl::mqtt_client&, const acl::mqtt_message&) { return true; } -static bool test_publish(acl::mqtt_client& conn, unsigned short id) { +static bool test_publish(acl::mqtt_client& conn, unsigned short id, int length) { acl::mqtt_publish publish; publish.get_header().set_qos(acl::MQTT_QOS1); @@ -24,7 +24,12 @@ static bool test_publish(acl::mqtt_client& conn, unsigned short id) { publish.set_topic(topic); acl::string payload; - payload.format("payload-%ddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddd", id); + payload.format("payload-%d:", id); + + for (; payload.size() < (size_t) length;) { + payload += 'c'; + } + publish.set_payload((unsigned) payload.size(), payload); if (!conn.send(publish)) { @@ -64,15 +69,15 @@ static bool test_publish(acl::mqtt_client& conn, unsigned short id) { } static void usage(const char* procname) { - printf("usage: %s -h [help] -s addr -n max\r\n", procname); + printf("usage: %s -h [help] -s addr -n max -c payload_length\r\n", procname); } int main(int argc, char* argv[]) { char ch; - int max = 1; + int max = 1, length = 32; acl::string addr("127.0.0.1|1883"); - while ((ch = getopt(argc, argv, "hs:n:")) > 0) { + while ((ch = getopt(argc, argv, "hs:n:c:")) > 0) { switch (ch) { case 'h': usage(argv[0]); @@ -83,6 +88,9 @@ int main(int argc, char* argv[]) { case 'n': max = atoi(optarg); break; + case 'c': + length = atoi(optarg); + break; default: break; } @@ -126,7 +134,7 @@ int main(int argc, char* argv[]) { unsigned short id = 1; for (int i = 1; i <= max; i++) { - if (!test_publish(conn, id++)) { + if (!test_publish(conn, id++, length)) { break; } // id must be more than 0