-
Notifications
You must be signed in to change notification settings - Fork 111
/
Copy pathtls_client.cpp
143 lines (127 loc) · 4.75 KB
/
tls_client.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
// Copyright Takatoshi Kondo 2015
//
// Distributed under the Boost Software License, Version 1.0.
// (See accompanying file LICENSE_1_0.txt or copy at
// http://www.boost.org/LICENSE_1_0.txt)
#include <iostream>
#include <iomanip>
#include <map>
#include <mqtt_client_cpp.hpp>
int main(int argc, char** argv) {
if (argc != 4) {
std::cout << argv[0] << " host port cacert_file" << std::endl;
return -1;
}
MQTT_NS::setup_log();
boost::asio::io_context ioc;
std::string host = argv[1];
auto port = argv[2];
std::string cacert = argv[3];
std::uint16_t pid_sub1;
std::uint16_t pid_sub2;
int count = 0;
// Create TLS client
auto c = MQTT_NS::make_tls_sync_client(ioc, host, port);
using packet_id_t = typename std::remove_reference_t<decltype(*c)>::packet_id_t;
auto disconnect = [&] {
if (++count == 5) c->disconnect();
};
// Setup client
c->set_client_id("cid1");
c->set_clean_session(true);
c->get_ssl_context().load_verify_file(cacert);
#if OPENSSL_VERSION_NUMBER >= 0x10101000L
SSL_CTX_set_keylog_callback(
c->get_ssl_context().native_handle(),
[](SSL const*, char const* line) {
std::cout << line << std::endl;
}
);
#endif // OPENSSL_VERSION_NUMBER >= 0x10101000L
// Setup handlers
c->set_connack_handler(
[&c, &pid_sub1, &pid_sub2]
(bool sp, MQTT_NS::connect_return_code connack_return_code){
std::cout << "Connack handler called" << std::endl;
std::cout << "Session Present: " << std::boolalpha << sp << std::endl;
std::cout << "Connack Return Code: "
<< MQTT_NS::connect_return_code_to_str(connack_return_code) << std::endl;
if (connack_return_code == MQTT_NS::connect_return_code::accepted) {
pid_sub1 = c->subscribe("mqtt_client_cpp/topic1", MQTT_NS::qos::at_most_once);
pid_sub2 = c->subscribe(
std::vector<std::tuple<MQTT_NS::string_view, MQTT_NS::subscribe_options>>
{
{ "mqtt_client_cpp/topic2_1", MQTT_NS::subscribe_options(MQTT_NS::qos::at_least_once) },
{ "mqtt_client_cpp/topic2_2", MQTT_NS::subscribe_options(MQTT_NS::qos::exactly_once) }
}
);
}
return true;
});
c->set_close_handler(
[]
(){
std::cout << "closed." << std::endl;
});
c->set_error_handler(
[]
(MQTT_NS::error_code ec){
std::cout << "error: " << ec.message() << std::endl;
});
c->set_puback_handler(
[&]
(packet_id_t packet_id){
std::cout << "puback received. packet_id: " << packet_id << std::endl;
disconnect();
return true;
});
c->set_pubrec_handler(
[&]
(packet_id_t packet_id){
std::cout << "pubrec received. packet_id: " << packet_id << std::endl;
return true;
});
c->set_pubcomp_handler(
[&]
(packet_id_t packet_id){
std::cout << "pubcomp received. packet_id: " << packet_id << std::endl;
disconnect();
return true;
});
c->set_suback_handler(
[&]
(packet_id_t packet_id, std::vector<MQTT_NS::suback_return_code> results){
std::cout << "suback received. packet_id: " << packet_id << std::endl;
for (auto const& e : results) {
std::cout << "[client] subscribe result: " << e << std::endl;
}
if (packet_id == pid_sub1) {
c->publish("mqtt_client_cpp/topic1", "test1", MQTT_NS::qos::at_most_once);
}
else if (packet_id == pid_sub2) {
c->publish("mqtt_client_cpp/topic2_1", "test2_1", MQTT_NS::qos::at_least_once);
c->publish("mqtt_client_cpp/topic2_2", "test2_2", MQTT_NS::qos::exactly_once);
}
return true;
});
c->set_publish_handler(
[&]
(MQTT_NS::optional<packet_id_t> packet_id,
MQTT_NS::publish_options pubopts,
MQTT_NS::buffer topic_name,
MQTT_NS::buffer contents){
std::cout << "publish received."
<< " dup: " << pubopts.get_dup()
<< " qos: " << pubopts.get_qos()
<< " retain: " << pubopts.get_retain() << std::endl;
if (packet_id)
std::cout << "packet_id: " << *packet_id << std::endl;
std::cout << "topic_name: " << topic_name << std::endl;
std::cout << "contents: " << contents << std::endl;
disconnect();
return true;
});
// Connect
c->connect();
ioc.run();
}