acl/lib_acl_cpp/samples/mqtt/mqtt_pub/main.cpp

166 lines
3.4 KiB
C++
Raw Normal View History

2021-03-07 10:34:59 +08:00
#include <stdio.h>
#include <stdlib.h>
#include <getopt.h>
2022-07-20 16:25:21 +08:00
#include "lib_acl.h"
2021-03-07 10:34:59 +08:00
#include "acl_cpp/lib_acl.hpp"
2022-07-20 16:25:21 +08:00
static bool __payload_binary = false;
2021-03-07 10:34:59 +08:00
static bool handle_connack(const acl::mqtt_message& message) {
const acl::mqtt_connack& connack = (const acl::mqtt_connack&) message;
printf("%s: connect code=%d\r\n", __FUNCTION__, connack.get_connack_code());
return true;
}
static bool handle_puback(acl::mqtt_client&, const acl::mqtt_message&) {
return true;
}
2022-07-13 09:38:26 +08:00
static bool test_publish(acl::mqtt_client& conn, unsigned short id, int length) {
2021-03-07 10:34:59 +08:00
acl::mqtt_publish publish;
2021-03-08 19:57:24 +08:00
publish.get_header().set_qos(acl::MQTT_QOS1);
2021-03-07 10:34:59 +08:00
publish.set_pkt_id(id);
const char* topic = "test/topic";
publish.set_topic(topic);
acl::string payload;
2022-07-13 09:38:26 +08:00
payload.format("payload-%d:", id);
2022-07-20 16:25:21 +08:00
char ch;
if (__payload_binary) {
ch = 0;
} else {
ch = 'c';
}
2022-07-13 09:38:26 +08:00
for (; payload.size() < (size_t) length;) {
2022-07-20 16:25:21 +08:00
payload.append(&ch, 1);
2022-07-13 09:38:26 +08:00
}
2021-03-07 10:34:59 +08:00
publish.set_payload((unsigned) payload.size(), payload);
if (!conn.send(publish)) {
printf("send publish error\r\n");
return false;
}
2022-07-20 16:25:21 +08:00
ACL_VSTREAM* vs = conn.sock_stream()->get_vstream();
assert(vs);
printf("payload->size=%zd, total sent size=%lld\n",
payload.size(), vs->total_write_cnt);
2021-03-08 19:57:24 +08:00
if (publish.get_header().get_qos() == acl::MQTT_QOS0) {
2021-03-07 10:34:59 +08:00
return true;
}
acl::mqtt_message* res = conn.get_message();
if (res == NULL) {
printf("get puback error\r\n");
return false;
}
2021-03-08 15:03:32 +08:00
if (res->get_header().get_type() != acl::MQTT_PUBACK) {
printf("not puback type, type=%d\r\n",
(int) res->get_header().get_type());
2021-03-07 10:34:59 +08:00
delete res;
return false;
}
if (!handle_puback(conn, *res)) {
printf("handle_puback error\r\n");
delete res;
return false;
}
2021-03-12 11:07:29 +08:00
acl::mqtt_suback* suback = (acl::mqtt_suback*) res;
printf("%s => puback ok, pkt id=%d\r\n",
__FUNCTION__, suback->get_pkt_id());
2021-03-07 10:34:59 +08:00
delete res;
return true;
}
static void usage(const char* procname) {
2022-07-20 16:25:21 +08:00
printf("usage: %s -h [help] -s addr -n max -c payload_length -B [if payload is binary data]\r\n", procname);
2021-03-07 10:34:59 +08:00
}
int main(int argc, char* argv[]) {
char ch;
2022-07-13 09:38:26 +08:00
int max = 1, length = 32;
2021-03-11 19:03:22 +08:00
acl::string addr("127.0.0.1|1883");
2021-03-07 10:34:59 +08:00
2022-07-20 16:25:21 +08:00
while ((ch = getopt(argc, argv, "hs:n:c:B")) > 0) {
2021-03-07 10:34:59 +08:00
switch (ch) {
case 'h':
usage(argv[0]);
return 0;
case 's':
addr = optarg;
break;
case 'n':
max = atoi(optarg);
break;
2022-07-13 09:38:26 +08:00
case 'c':
length = atoi(optarg);
break;
2022-07-20 16:25:21 +08:00
case 'B':
__payload_binary = true;
break;
2021-03-07 10:34:59 +08:00
default:
break;
}
}
acl::log::stdout_open(true);
acl::mqtt_connect message;
message.set_cid("client-id-zsx-pub");
message.set_username("user-zsx");
//message.set_passwd("pass");
#if 0
message.set_will_qos(acl::MQTT_QOS0);
message.set_will_topic("test/topic");
message.set_will_msg("msg-hello");
#endif
printf("-----------------------------------------------\r\n");
acl::mqtt_client conn(addr, 10, 0);
if (!conn.send(message)) {
printf("send message error\r\n");
return 1;
}
printf("send connect message ok\r\n");
acl::mqtt_message* res = conn.get_message();
if (res == NULL) {
printf("read CONNACK error\r\n");
return 1;
}
2021-03-08 15:03:32 +08:00
acl::mqtt_type_t type = res->get_header().get_type();
2021-03-07 10:34:59 +08:00
if (type != acl::MQTT_CONNACK) {
printf("invalid message type=%d\r\n", (int) type);
delete res;
return 1;
}
if (!handle_connack(*res)) {
delete res;
return 1;
}
2021-03-12 11:07:29 +08:00
unsigned short id = 1;
for (int i = 1; i <= max; i++) {
2022-07-13 09:38:26 +08:00
if (!test_publish(conn, id++, length)) {
2021-03-07 10:34:59 +08:00
break;
}
2021-03-12 11:07:29 +08:00
// id must be more than 0
if (id == 0) {
id = 1;
}
2021-03-07 10:34:59 +08:00
}
return 0;
}