7 #if !defined(MQTT_CLIENT_HPP)
8 #define MQTT_CLIENT_HPP
15 #include <type_traits>
20 #include <boost/lexical_cast.hpp>
21 #include <boost/asio.hpp>
33 namespace mi = boost::multi_index;
35 template <
typename Socket, std::
size_t PacketIdBytes = 2>
47 template<
typename ... Args>
49 :
client(std::forward<Args>(args)...)
59 friend std::shared_ptr<callable_overlay<client<tcp_endpoint<as::ip::tcp::socket, as::io_context::strand>>>>
69 friend std::shared_ptr<callable_overlay<client<tcp_endpoint<as::ip::tcp::socket, null_strand>>>>
72 #if defined(MQTT_USE_WS)
82 friend std::shared_ptr<callable_overlay<client<ws_endpoint<as::ip::tcp::socket, as::io_context::strand>>>>
83 make_client_ws(as::io_context& ioc, std::string host, std::string port, std::string path,
protocol_version version);
93 friend std::shared_ptr<callable_overlay<client<ws_endpoint<as::ip::tcp::socket, null_strand>>>>
94 make_client_no_strand_ws(as::io_context& ioc, std::string host, std::string port, std::string path,
protocol_version version);
97 #if defined(MQTT_USE_TLS)
105 friend std::shared_ptr<callable_overlay<client<tcp_endpoint<tls::stream<as::ip::tcp::socket>, as::io_context::strand>>>>
106 make_tls_client(as::io_context& ioc, std::string host, std::string port,
protocol_version version);
115 friend std::shared_ptr<callable_overlay<client<tcp_endpoint<tls::stream<as::ip::tcp::socket>,
null_strand>>>>
116 make_tls_client_no_strand(as::io_context& ioc, std::string host, std::string port,
protocol_version version);
118 #if defined(MQTT_USE_WS)
128 friend std::shared_ptr<callable_overlay<client<ws_endpoint<tls::stream<as::ip::tcp::socket>, as::io_context::strand>>>>
129 make_tls_client_ws(as::io_context& ioc, std::string host, std::string port, std::string path,
protocol_version version);
139 friend std::shared_ptr<callable_overlay<client<ws_endpoint<tls::stream<as::ip::tcp::socket>,
null_strand>>>>
140 make_tls_client_no_strand_ws(as::io_context& ioc, std::string host, std::string port, std::string path,
protocol_version version);
151 friend std::shared_ptr<callable_overlay<client<tcp_endpoint<as::ip::tcp::socket, as::io_context::strand>, 4>>>
161 friend std::shared_ptr<callable_overlay<client<tcp_endpoint<as::ip::tcp::socket, null_strand>, 4>>>
164 #if defined(MQTT_USE_WS)
174 friend std::shared_ptr<callable_overlay<client<ws_endpoint<as::ip::tcp::socket, as::io_context::strand>, 4>>>
175 make_client_ws_32(as::io_context& ioc, std::string host, std::string port, std::string path,
protocol_version version);
185 friend std::shared_ptr<callable_overlay<client<ws_endpoint<as::ip::tcp::socket, null_strand>, 4>>>
186 make_client_no_strand_ws_32(as::io_context& ioc, std::string host, std::string port, std::string path,
protocol_version version);
189 #if defined(MQTT_USE_TLS)
197 friend std::shared_ptr<callable_overlay<client<tcp_endpoint<tls::stream<as::ip::tcp::socket>, as::io_context::strand>, 4>>>
198 make_tls_client_32(as::io_context& ioc, std::string host, std::string port,
protocol_version version);
207 friend std::shared_ptr<callable_overlay<client<tcp_endpoint<tls::stream<as::ip::tcp::socket>,
null_strand>, 4>>>
208 make_tls_client_no_strand_32(as::io_context& ioc, std::string host, std::string port,
protocol_version version);
210 #if defined(MQTT_USE_WS)
220 friend std::shared_ptr<callable_overlay<client<ws_endpoint<tls::stream<as::ip::tcp::socket>, as::io_context::strand>, 4>>>
221 make_tls_client_ws_32(as::io_context& ioc, std::string host, std::string port, std::string path,
protocol_version version);
231 friend std::shared_ptr<callable_overlay<client<ws_endpoint<tls::stream<as::ip::tcp::socket>,
null_strand>, 4>>>
232 make_tls_client_no_strand_ws_32(as::io_context& ioc, std::string host, std::string port, std::string path,
protocol_version version);
337 template<
typename ... Args>
339 will_.emplace(std::forward<Args>(args)...);
342 #if defined(MQTT_USE_TLS)
348 tls::context& get_ssl_context() {
349 static_assert(has_tls<std::decay_t<decltype(*
this)>>::value,
"Client is required to support TLS.");
357 tls::context
const& get_ssl_context()
const {
358 static_assert(has_tls<std::decay_t<decltype(*
this)>>::value,
"Client is required to support TLS.");
381 if ((ping_duration_ != std::chrono::steady_clock::duration::zero()) &&
base::connected() && (ping == std::chrono::steady_clock::duration::zero())) {
384 keep_alive_sec_ = keep_alive_sec;
426 void connect(any session_life_keeper = any()) {
449 setup_socket(socket_);
465 any session_life_keeper = any()) {
466 setup_socket(socket_);
477 void connect(std::shared_ptr<Socket>&&
socket, any session_life_keeper = any()) {
490 std::shared_ptr<Socket>&&
socket,
492 any session_life_keeper = any()) {
507 std::shared_ptr<Socket>&&
socket,
509 any session_life_keeper = any()) {
527 std::shared_ptr<Socket>&&
socket,
530 any session_life_keeper = any()) {
549 template <
typename T>
552 !std::is_convertible<T, async_handler_t>::value
585 template <
typename T>
588 !std::is_convertible<T, async_handler_t>::value
616 setup_socket(socket_);
637 template <
typename T>
640 !std::is_convertible<T, async_handler_t>::value
692 template <
typename T>
695 !std::is_convertible<T, async_handler_t>::value
749 std::chrono::steady_clock::duration timeout,
753 if (ping_duration_ != std::chrono::steady_clock::duration::zero()) tim_ping_.cancel();
755 std::weak_ptr<this_type> wp(std::static_pointer_cast<this_type>(this->shared_from_this()));
756 tim_close_.expires_after(
force_move(timeout));
757 tim_close_.async_wait(
759 if (
auto sp = wp.lock()) {
763 sp->force_disconnect();
793 if (ping_duration_ != std::chrono::steady_clock::duration::zero()) tim_ping_.cancel();
794 if (base::connected()) {
795 base::disconnect(reason_code,
force_move(props));
809 std::chrono::steady_clock::duration timeout,
811 if (ping_duration_ != std::chrono::steady_clock::duration::zero()) tim_ping_.cancel();
812 if (base::connected()) {
813 std::weak_ptr<this_type> wp(std::static_pointer_cast<this_type>(this->shared_from_this()));
814 tim_close_.expires_after(
force_move(timeout));
815 tim_close_.async_wait(
817 if (
auto sp = wp.lock()) {
821 sp->force_disconnect();
850 std::chrono::steady_clock::duration timeout,
854 if (ping_duration_ != std::chrono::steady_clock::duration::zero()) tim_ping_.cancel();
855 if (base::connected()) {
856 std::weak_ptr<this_type> wp(std::static_pointer_cast<this_type>(this->shared_from_this()));
857 tim_close_.expires_after(
force_move(timeout));
858 tim_close_.async_wait(
860 if (
auto sp = wp.lock()) {
864 sp->force_disconnect();
885 if (ping_duration_ != std::chrono::steady_clock::duration::zero()) tim_ping_.cancel();
886 if (base::connected()) {
911 if (ping_duration_ != std::chrono::steady_clock::duration::zero()) tim_ping_.cancel();
912 if (base::connected()) {
923 if (ping_duration_ != std::chrono::steady_clock::duration::zero()) tim_ping_.cancel();
925 base::force_disconnect();
936 std::shared_ptr<Socket>
const&
socket()
const {
948 #
if defined(MQTT_USE_WS)
950 std::string path =
"/"
954 bool async_store_send =
false
956 :
base(ioc, version, async_store_send),
962 #if defined(MQTT_USE_WS)
967 #if defined(MQTT_USE_TLS)
968 ctx_.set_verify_mode(tls::verify_peer);
973 template <
typename Strand>
975 socket = std::make_shared<Socket>(ioc_);
976 base::socket_sp_ref() =
socket;
979 #if defined(MQTT_USE_WS)
980 template <
typename Strand>
981 void setup_socket(std::shared_ptr<ws_endpoint<as::ip::tcp::socket, Strand>>& socket) {
982 socket = std::make_shared<Socket>(ioc_);
983 base::socket_sp_ref() = socket;
987 #if defined(MQTT_USE_TLS)
988 template <
typename Strand>
989 void setup_socket(std::shared_ptr<tcp_endpoint<tls::stream<as::ip::tcp::socket>, Strand>>& socket) {
990 socket = std::make_shared<Socket>(ioc_, ctx_);
991 base::socket_sp_ref() = socket;
994 #if defined(MQTT_USE_WS)
995 template <
typename Strand>
996 void setup_socket(std::shared_ptr<ws_endpoint<tls::stream<as::ip::tcp::socket>, Strand>>& socket) {
997 socket = std::make_shared<Socket>(ioc_, ctx_);
998 base::socket_sp_ref() = socket;
1004 void start_session(
v5::properties props, any session_life_keeper) {
1005 base::async_read_control_packet_type(
force_move(session_life_keeper));
1011 : optional<buffer>() ),
1013 : optional<buffer>() ),
1020 void async_start_session(
v5::properties props, any session_life_keeper, async_handler_t func) {
1021 base::async_read_control_packet_type(
force_move(session_life_keeper));
1024 base::async_connect(
1027 : optional<buffer>() ),
1029 : optional<buffer>() ),
1037 template <
typename Strand>
1038 void handshake_socket(
1039 tcp_endpoint<as::ip::tcp::socket, Strand>&,
1041 any session_life_keeper) {
1045 template <
typename Strand>
1046 void handshake_socket(
1047 tcp_endpoint<as::ip::tcp::socket, Strand>&,
1049 any session_life_keeper,
1052 ec = boost::system::errc::make_error_code(boost::system::errc::success);
1055 #if defined(MQTT_USE_WS)
1057 template <
typename Strand>
1058 void handshake_socket(
1059 ws_endpoint<as::ip::tcp::socket, Strand>& socket,
1061 any session_life_keeper) {
1062 socket.handshake(host_, path_);
1066 template <
typename Strand>
1067 void handshake_socket(
1068 ws_endpoint<as::ip::tcp::socket, Strand>& socket,
1070 any session_life_keeper,
1072 socket.handshake(host_, path_, ec);
1079 #if defined(MQTT_USE_TLS)
1081 template <
typename Strand>
1082 void handshake_socket(
1083 tcp_endpoint<tls::stream<as::ip::tcp::socket>, Strand>& socket,
1085 any session_life_keeper) {
1086 socket.handshake(tls::stream_base::client);
1090 template <
typename Strand>
1091 void handshake_socket(
1092 tcp_endpoint<tls::stream<as::ip::tcp::socket>, Strand>& socket,
1094 any session_life_keeper,
1096 socket.handshake(tls::stream_base::client, ec);
1101 #if defined(MQTT_USE_WS)
1103 template <
typename Strand>
1104 void handshake_socket(
1105 ws_endpoint<tls::stream<as::ip::tcp::socket>, Strand>& socket,
1107 any session_life_keeper) {
1108 socket.next_layer().handshake(tls::stream_base::client);
1109 socket.handshake(host_, path_);
1113 template <
typename Strand>
1114 void handshake_socket(
1115 ws_endpoint<tls::stream<as::ip::tcp::socket>, Strand>& socket,
1117 any session_life_keeper,
1119 socket.next_layer().handshake(tls::stream_base::client, ec);
1121 socket.handshake(host_, path_, ec);
1130 template <
typename Strand>
1131 void async_handshake_socket(
1132 tcp_endpoint<as::ip::tcp::socket, Strand>&,
1134 any session_life_keeper,
1135 async_handler_t func) {
1139 #if defined(MQTT_USE_WS)
1140 template <
typename Strand>
1141 void async_handshake_socket(
1142 ws_endpoint<as::ip::tcp::socket, Strand>& socket,
1144 any session_life_keeper,
1145 async_handler_t func) {
1146 socket.async_handshake(
1151 self = this->shared_from_this(),
1152 session_life_keeper =
force_move(session_life_keeper),
1166 #if defined(MQTT_USE_TLS)
1168 template <
typename Strand>
1169 void async_handshake_socket(
1170 tcp_endpoint<tls::stream<as::ip::tcp::socket>, Strand>& socket,
1172 any session_life_keeper,
1173 async_handler_t func) {
1174 socket.async_handshake(
1175 tls::stream_base::client,
1178 self = this->shared_from_this(),
1179 session_life_keeper =
force_move(session_life_keeper),
1192 #if defined(MQTT_USE_WS)
1193 template <
typename Strand>
1194 void async_handshake_socket(
1195 ws_endpoint<tls::stream<as::ip::tcp::socket>, Strand>& socket,
1197 any session_life_keeper,
1198 async_handler_t func) {
1199 socket.next_layer().async_handshake(
1200 tls::stream_base::client,
1203 self = this->shared_from_this(),
1204 session_life_keeper =
force_move(session_life_keeper),
1214 socket.async_handshake(
1220 session_life_keeper =
force_move(session_life_keeper),
1239 any session_life_keeper) {
1240 as::ip::tcp::resolver r(ioc_);
1241 auto eps = r.resolve(host_, port_);
1242 as::connect(socket_->lowest_layer(), eps.begin(), eps.end());
1243 base::set_connect();
1244 if (ping_duration_ != std::chrono::steady_clock::duration::zero()) {
1252 any session_life_keeper,
1254 as::ip::tcp::resolver r(ioc_);
1255 auto eps = r.resolve(host_, port_, ec);
1257 as::connect(socket_->lowest_layer(), eps.begin(), eps.end(), ec);
1259 base::set_connect();
1260 if (ping_duration_ != std::chrono::steady_clock::duration::zero()) {
1266 void async_connect_impl(
1268 any session_life_keeper,
1269 async_handler_t func) {
1270 auto r = std::make_shared<as::ip::tcp::resolver>(ioc_);
1277 self = this->shared_from_this(),
1279 session_life_keeper =
force_move(session_life_keeper),
1285 as::ip::tcp::resolver::results_type eps
1292 socket_->lowest_layer(), eps.begin(), eps.end(),
1297 session_life_keeper =
force_move(session_life_keeper),
1305 base::set_connect();
1306 if (ping_duration_ != std::chrono::steady_clock::duration::zero()) {
1318 if (ping_duration_ != std::chrono::steady_clock::duration::zero()) {
1326 if (async_pingreq_) {
1327 base::async_pingreq();
1336 tim_ping_.expires_after(ping_duration_);
1337 std::weak_ptr<this_type> wp(std::static_pointer_cast<this_type>(this->shared_from_this()));
1338 tim_ping_.async_wait(
1340 if (
auto sp = wp.lock()) {
1341 sp->handle_timer(ec);
1347 void reset_timer() {
1354 if (ping_duration_ != std::chrono::steady_clock::duration::zero()) tim_ping_.cancel();
1359 if (ping_duration_ != std::chrono::steady_clock::duration::zero()) tim_ping_.cancel();
1371 #if defined(MQTT_USE_TLS)
1373 template <
typename T>
1374 struct has_tls : std::false_type {
1377 template <
typename U>
1378 struct has_tls<
client<
tcp_endpoint<tls::stream<as::ip::tcp::socket>, U>>> : std::true_type {
1381 #if defined(MQTT_USE_WS)
1383 template <
typename U>
1384 struct has_tls<client<ws_endpoint<tls::stream<as::ip::tcp::socket>, U>>> : std::true_type {
1391 std::shared_ptr<Socket> socket_;
1392 as::io_context& ioc_;
1393 as::steady_timer tim_ping_;
1394 as::steady_timer tim_close_;
1397 std::uint16_t keep_alive_sec_{0};
1398 std::chrono::steady_clock::duration ping_duration_{std::chrono::steady_clock::duration::zero()};
1399 std::string client_id_;
1400 optional<will> will_;
1401 optional<std::string> user_name_;
1402 optional<std::string> password_;
1403 bool async_pingreq_ =
false;
1404 #if defined(MQTT_USE_TLS)
1405 tls::context ctx_{tls::context::tlsv12};
1407 #if defined(MQTT_USE_WS)
1412 inline std::shared_ptr<callable_overlay<client<tcp_endpoint<as::ip::tcp::socket, as::io_context::strand>>>>
1415 return std::make_shared<callable_overlay<client_t>>(
1416 client_t::constructor_access(),
1420 #if defined(MQTT_USE_WS)
1427 inline std::shared_ptr<callable_overlay<client<tcp_endpoint<as::ip::tcp::socket, as::io_context::strand>>>>
1432 std::to_string(port),
1437 inline std::shared_ptr<callable_overlay<client<tcp_endpoint<as::ip::tcp::socket, null_strand>>>>
1440 return std::make_shared<callable_overlay<client_t>>(
1441 client_t::constructor_access(),
1445 #if defined(MQTT_USE_WS)
1452 inline std::shared_ptr<callable_overlay<client<tcp_endpoint<as::ip::tcp::socket, null_strand>>>>
1457 std::to_string(port),
1462 #if defined(MQTT_USE_WS)
1464 inline std::shared_ptr<callable_overlay<client<ws_endpoint<as::ip::tcp::socket, as::io_context::strand>>>>
1465 make_client_ws(as::io_context& ioc, std::string host, std::string port, std::string path =
"/",
protocol_version version = protocol_version::v3_1_1) {
1466 using client_t = client<ws_endpoint<as::ip::tcp::socket, as::io_context::strand>>;
1467 return std::make_shared<callable_overlay<client_t>>(
1468 client_t::constructor_access(),
1477 inline std::shared_ptr<callable_overlay<client<ws_endpoint<as::ip::tcp::socket, as::io_context::strand>>>>
1478 make_client_ws(as::io_context& ioc, std::string host, std::uint16_t port, std::string path =
"/",
protocol_version version = protocol_version::v3_1_1) {
1479 return make_client_ws(
1482 std::to_string(port),
1488 inline std::shared_ptr<callable_overlay<client<ws_endpoint<as::ip::tcp::socket, null_strand>>>>
1489 make_client_no_strand_ws(as::io_context& ioc, std::string host, std::string port, std::string path =
"/",
protocol_version version = protocol_version::v3_1_1) {
1490 using client_t = client<ws_endpoint<as::ip::tcp::socket, null_strand>>;
1491 return std::make_shared<callable_overlay<client_t>>(
1492 client_t::constructor_access(),
1501 inline std::shared_ptr<callable_overlay<client<ws_endpoint<as::ip::tcp::socket, null_strand>>>>
1502 make_client_no_strand_ws(as::io_context& ioc, std::string host, std::uint16_t port, std::string path =
"/",
protocol_version version = protocol_version::v3_1_1) {
1503 return make_client_no_strand_ws(
1506 std::to_string(port),
1514 #if defined(MQTT_USE_TLS)
1516 inline std::shared_ptr<callable_overlay<client<tcp_endpoint<tls::stream<as::ip::tcp::socket>, as::io_context::strand>>>>
1517 make_tls_client(as::io_context& ioc, std::string host, std::string port,
protocol_version version = protocol_version::v3_1_1) {
1518 using client_t = client<tcp_endpoint<tls::stream<as::ip::tcp::socket>, as::io_context::strand>>;
1519 return std::make_shared<callable_overlay<client_t>>(
1520 client_t::constructor_access(),
1524 #if defined(MQTT_USE_WS)
1531 inline std::shared_ptr<callable_overlay<client<tcp_endpoint<tls::stream<as::ip::tcp::socket>, as::io_context::strand>>>>
1532 make_tls_client(as::io_context& ioc, std::string host, std::uint16_t port,
protocol_version version = protocol_version::v3_1_1) {
1533 return make_tls_client(
1536 std::to_string(port),
1541 inline std::shared_ptr<callable_overlay<client<tcp_endpoint<tls::stream<as::ip::tcp::socket>, null_strand>>>>
1542 make_tls_client_no_strand(as::io_context& ioc, std::string host, std::string port,
protocol_version version = protocol_version::v3_1_1) {
1543 using client_t = client<tcp_endpoint<tls::stream<as::ip::tcp::socket>, null_strand>>;
1544 return std::make_shared<callable_overlay<client_t>>(
1545 client_t::constructor_access(),
1549 #if defined(MQTT_USE_WS)
1556 inline std::shared_ptr<callable_overlay<client<tcp_endpoint<tls::stream<as::ip::tcp::socket>, null_strand>>>>
1557 make_tls_client_no_strand(as::io_context& ioc, std::string host, std::uint16_t port,
protocol_version version = protocol_version::v3_1_1) {
1558 return make_tls_client_no_strand(
1561 std::to_string(port),
1566 #if defined(MQTT_USE_WS)
1568 inline std::shared_ptr<callable_overlay<client<ws_endpoint<tls::stream<as::ip::tcp::socket>, as::io_context::strand>>>>
1569 make_tls_client_ws(as::io_context& ioc, std::string host, std::string port, std::string path =
"/",
protocol_version version = protocol_version::v3_1_1) {
1570 using client_t = client<ws_endpoint<tls::stream<as::ip::tcp::socket>, as::io_context::strand>>;
1571 return std::make_shared<callable_overlay<client_t>>(
1572 client_t::constructor_access(),
1581 inline std::shared_ptr<callable_overlay<client<ws_endpoint<tls::stream<as::ip::tcp::socket>, as::io_context::strand>>>>
1582 make_tls_client_ws(as::io_context& ioc, std::string host, std::uint16_t port, std::string path =
"/",
protocol_version version = protocol_version::v3_1_1) {
1583 return make_tls_client_ws(
1586 std::to_string(port),
1592 inline std::shared_ptr<callable_overlay<client<ws_endpoint<tls::stream<as::ip::tcp::socket>, null_strand>>>>
1593 make_tls_client_no_strand_ws(as::io_context& ioc, std::string host, std::string port, std::string path =
"/",
protocol_version version = protocol_version::v3_1_1) {
1594 using client_t = client<ws_endpoint<tls::stream<as::ip::tcp::socket>, null_strand>>;
1595 return std::make_shared<callable_overlay<client_t>>(
1596 client_t::constructor_access(),
1605 inline std::shared_ptr<callable_overlay<client<ws_endpoint<tls::stream<as::ip::tcp::socket>, null_strand>>>>
1606 make_tls_client_no_strand_ws(as::io_context& ioc, std::string host, std::uint16_t port, std::string path =
"/",
protocol_version version = protocol_version::v3_1_1) {
1607 return make_tls_client_no_strand_ws(
1610 std::to_string(port),
1623 inline std::shared_ptr<callable_overlay<client<tcp_endpoint<as::ip::tcp::socket, as::io_context::strand>, 4>>>
1626 return std::make_shared<callable_overlay<client_t>>(
1627 client_t::constructor_access(),
1631 #if defined(MQTT_USE_WS)
1638 inline std::shared_ptr<callable_overlay<client<tcp_endpoint<as::ip::tcp::socket, as::io_context::strand>, 4>>>
1643 std::to_string(port),
1648 inline std::shared_ptr<callable_overlay<client<tcp_endpoint<as::ip::tcp::socket, null_strand>, 4>>>
1651 return std::make_shared<callable_overlay<client_t>>(
1652 client_t::constructor_access(),
1656 #if defined(MQTT_USE_WS)
1663 inline std::shared_ptr<callable_overlay<client<tcp_endpoint<as::ip::tcp::socket, null_strand>, 4>>>
1668 std::to_string(port),
1673 #if defined(MQTT_USE_WS)
1675 inline std::shared_ptr<callable_overlay<client<ws_endpoint<as::ip::tcp::socket, as::io_context::strand>, 4>>>
1676 make_client_ws_32(as::io_context& ioc, std::string host, std::string port, std::string path =
"/",
protocol_version version = protocol_version::v3_1_1) {
1677 using client_t = client<ws_endpoint<as::ip::tcp::socket, as::io_context::strand>, 4>;
1678 return std::make_shared<callable_overlay<client_t>>(
1679 client_t::constructor_access(),
1688 inline std::shared_ptr<callable_overlay<client<ws_endpoint<as::ip::tcp::socket, as::io_context::strand>, 4>>>
1689 make_client_ws_32(as::io_context& ioc, std::string host, std::uint16_t port, std::string path =
"/",
protocol_version version = protocol_version::v3_1_1) {
1690 return make_client_ws_32(
1693 std::to_string(port),
1699 inline std::shared_ptr<callable_overlay<client<ws_endpoint<as::ip::tcp::socket, null_strand>, 4>>>
1700 make_client_no_strand_ws_32(as::io_context& ioc, std::string host, std::string port, std::string path =
"/",
protocol_version version = protocol_version::v3_1_1) {
1701 using client_t = client<ws_endpoint<as::ip::tcp::socket, null_strand>, 4>;
1702 return std::make_shared<callable_overlay<client_t>>(
1703 client_t::constructor_access(),
1712 inline std::shared_ptr<callable_overlay<client<ws_endpoint<as::ip::tcp::socket, null_strand>, 4>>>
1713 make_client_no_strand_ws_32(as::io_context& ioc, std::string host, std::uint16_t port, std::string path =
"/",
protocol_version version = protocol_version::v3_1_1) {
1714 return make_client_no_strand_ws_32(
1717 std::to_string(port),
1725 #if defined(MQTT_USE_TLS)
1727 inline std::shared_ptr<callable_overlay<client<tcp_endpoint<tls::stream<as::ip::tcp::socket>, as::io_context::strand>, 4>>>
1728 make_tls_client_32(as::io_context& ioc, std::string host, std::string port,
protocol_version version = protocol_version::v3_1_1) {
1729 using client_t = client<tcp_endpoint<tls::stream<as::ip::tcp::socket>, as::io_context::strand>, 4>;
1730 return std::make_shared<callable_overlay<client_t>>(
1731 client_t::constructor_access(),
1735 #if defined(MQTT_USE_WS)
1742 inline std::shared_ptr<callable_overlay<client<tcp_endpoint<tls::stream<as::ip::tcp::socket>, as::io_context::strand>, 4>>>
1743 make_tls_client_32(as::io_context& ioc, std::string host, std::uint16_t port,
protocol_version version = protocol_version::v3_1_1) {
1744 return make_tls_client_32(
1747 std::to_string(port),
1752 inline std::shared_ptr<callable_overlay<client<tcp_endpoint<tls::stream<as::ip::tcp::socket>, null_strand>, 4>>>
1753 make_tls_client_no_strand_32(as::io_context& ioc, std::string host, std::string port,
protocol_version version = protocol_version::v3_1_1) {
1754 using client_t = client<tcp_endpoint<tls::stream<as::ip::tcp::socket>, null_strand>, 4>;
1755 return std::make_shared<callable_overlay<client_t>>(
1756 client_t::constructor_access(),
1760 #if defined(MQTT_USE_WS)
1767 inline std::shared_ptr<callable_overlay<client<tcp_endpoint<tls::stream<as::ip::tcp::socket>, null_strand>, 4>>>
1768 make_tls_client_no_strand_32(as::io_context& ioc, std::string host, std::uint16_t port,
protocol_version version = protocol_version::v3_1_1) {
1769 return make_tls_client_no_strand_32(
1772 std::to_string(port),
1777 #if defined(MQTT_USE_WS)
1779 inline std::shared_ptr<callable_overlay<client<ws_endpoint<tls::stream<as::ip::tcp::socket>, as::io_context::strand>, 4>>>
1780 make_tls_client_ws_32(as::io_context& ioc, std::string host, std::string port, std::string path =
"/",
protocol_version version = protocol_version::v3_1_1) {
1781 using client_t = client<ws_endpoint<tls::stream<as::ip::tcp::socket>, as::io_context::strand>, 4>;
1782 return std::make_shared<callable_overlay<client_t>>(
1783 client_t::constructor_access(),
1792 inline std::shared_ptr<callable_overlay<client<ws_endpoint<tls::stream<as::ip::tcp::socket>, as::io_context::strand>, 4>>>
1793 make_tls_client_ws_32(as::io_context& ioc, std::string host, std::uint16_t port, std::string path =
"/",
protocol_version version = protocol_version::v3_1_1) {
1794 return make_tls_client_ws_32(
1797 std::to_string(port),
1803 inline std::shared_ptr<callable_overlay<client<ws_endpoint<tls::stream<as::ip::tcp::socket>, null_strand>, 4>>>
1804 make_tls_client_no_strand_ws_32(as::io_context& ioc, std::string host, std::string port, std::string path =
"/",
protocol_version version = protocol_version::v3_1_1) {
1805 using client_t = client<ws_endpoint<tls::stream<as::ip::tcp::socket>, null_strand>, 4>;
1806 return std::make_shared<callable_overlay<client_t>>(
1807 client_t::constructor_access(),
1816 inline std::shared_ptr<callable_overlay<client<ws_endpoint<tls::stream<as::ip::tcp::socket>, null_strand>, 4>>>
1817 make_tls_client_no_strand_ws_32(as::io_context& ioc, std::string host, std::uint16_t port, std::string path =
"/",
protocol_version version = protocol_version::v3_1_1) {
1818 return make_tls_client_no_strand_ws_32(
1821 std::to_string(port),
Definition: client.hpp:36
std::shared_ptr< Socket > const & socket() const
Definition: client.hpp:936
void connect(v5::properties props, any session_life_keeper=any())
Connect to a broker Before calling connect(), call set_xxx member functions to configure the connecti...
Definition: client.hpp:448
std::enable_if_t< !std::is_convertible< T, async_handler_t >::value > async_connect(std::shared_ptr< Socket > &&socket, v5::properties props, T session_life_keeper)
Connect to a broker Before calling connect(), call set_xxx member functions to configure the connecti...
Definition: client.hpp:697
void set_password(std::string password)
Set password.
Definition: client.hpp:322
std::string const & get_client_id() const
Get the client id.
Definition: client.hpp:272
void connect(v5::properties props, boost::system::error_code &ec, any session_life_keeper=any())
Connect to a broker Before calling connect(), call set_xxx member functions to configure the connecti...
Definition: client.hpp:462
friend std::shared_ptr< callable_overlay< client< tcp_endpoint< as::ip::tcp::socket, as::io_context::strand > > > > make_client(as::io_context &ioc, std::string host, std::string port, protocol_version version)
Create no tls client with strand.
Definition: client.hpp:1413
void async_connect(any session_life_keeper, async_handler_t func)
Connect to a broker Before calling connect(), call set_xxx member functions to configure the connecti...
Definition: client.hpp:573
friend std::shared_ptr< callable_overlay< client< tcp_endpoint< as::ip::tcp::socket, null_strand >, 4 > > > make_client_no_strand_32(as::io_context &ioc, std::string host, std::string port, protocol_version version)
Create no tls client without strand.
Definition: client.hpp:1649
client(as::io_context &ioc, std::string host, std::string port, protocol_version version=protocol_version::v3_1_1, bool async_store_send=false)
Definition: client.hpp:945
void async_disconnect(std::chrono::steady_clock::duration timeout, v5::disconnect_reason_code reason_code, v5::properties props, async_handler_t func=async_handler_t())
Disconnect Send a disconnect packet to the connected broker. It is a clean disconnecting sequence....
Definition: client.hpp:849
void async_connect(v5::properties props, any session_life_keeper, async_handler_t func)
Connect to a broker Before calling connect(), call set_xxx member functions to configure the connecti...
Definition: client.hpp:615
void set_will(Args &&... args)
Definition: client.hpp:338
void async_connect(std::shared_ptr< Socket > &&socket)
Connect to a broker Before calling connect(), call set_xxx member functions to configure the connecti...
Definition: client.hpp:626
void set_async_pingreq(bool b)
Set pingreq message sending mode.
Definition: client.hpp:932
std::shared_ptr< Socket > & socket()
Definition: client.hpp:940
friend std::shared_ptr< callable_overlay< client< tcp_endpoint< as::ip::tcp::socket, as::io_context::strand >, 4 > > > make_client_32(as::io_context &ioc, std::string host, std::string port, protocol_version version)
Create no tls client with strand.
Definition: client.hpp:1624
void async_connect(std::shared_ptr< Socket > &&socket, async_handler_t func)
Connect to a broker Before calling connect(), call set_xxx member functions to configure the connecti...
Definition: client.hpp:653
void set_clean_start(bool cs)
Set clean start.
Definition: client.hpp:298
void on_error(error_code ec) noexcept override
Error handler.
Definition: client.hpp:1357
void connect(std::shared_ptr< Socket > &&socket, v5::properties props, any session_life_keeper=any())
Connect to a broker Before calling connect(), call set_xxx member functions to configure the connecti...
Definition: client.hpp:506
void async_connect(std::shared_ptr< Socket > &&socket, any session_life_keeper, async_handler_t func)
Connect to a broker Before calling connect(), call set_xxx member functions to configure the connecti...
Definition: client.hpp:665
void disconnect(std::chrono::steady_clock::duration timeout, v5::disconnect_reason_code reason_code=v5::disconnect_reason_code::normal_disconnection, v5::properties props={})
Disconnect Send a disconnect packet to the connected broker. It is a clean disconnecting sequence....
Definition: client.hpp:748
void async_connect()
Connect to a broker Before calling connect(), call set_xxx member functions to configure the connecti...
Definition: client.hpp:540
void on_close() noexcept override
Close handler.
Definition: client.hpp:1353
void connect(std::shared_ptr< Socket > &&socket, boost::system::error_code &ec, any session_life_keeper=any())
Connect to a broker Before calling connect(), call set_xxx member functions to configure the connecti...
Definition: client.hpp:489
void async_connect(async_handler_t func)
Connect to a broker Before calling connect(), call set_xxx member functions to configure the connecti...
Definition: client.hpp:563
void set_client_id(std::string id)
Set client id.
Definition: client.hpp:264
void set_keep_alive_sec_ping_ms(std::uint16_t keep_alive_sec, std::size_t ping_ms)
Set a keep alive second and a ping milli seconds.
Definition: client.hpp:403
void async_connect(std::shared_ptr< Socket > &&socket, v5::properties props, any session_life_keeper, async_handler_t func)
Connect to a broker Before calling connect(), call set_xxx member functions to configure the connecti...
Definition: client.hpp:726
void set_port(std::string port)
Set port.
Definition: client.hpp:252
void set_host(std::string host)
Set host.
Definition: client.hpp:242
void on_pre_send() noexcept override
Pre-send handler This handler is called when any mqtt control packet is decided to send.
Definition: client.hpp:1317
std::enable_if_t< !std::is_convertible< T, async_handler_t >::value > async_connect(v5::properties props, T session_life_keeper)
Connect to a broker Before calling connect(), call set_xxx member functions to configure the connecti...
Definition: client.hpp:590
void async_connect(std::shared_ptr< Socket > &&socket, v5::properties props)
Connect to a broker Before calling connect(), call set_xxx member functions to configure the connecti...
Definition: client.hpp:678
typename base::async_handler_t async_handler_t
Definition: client.hpp:42
void async_disconnect(v5::disconnect_reason_code reason_code, v5::properties props, async_handler_t func=async_handler_t())
Disconnect Send a disconnect packet to the connected broker. It is a clean disconnecting sequence....
Definition: client.hpp:907
void force_disconnect()
Disconnect by endpoint Force disconnect. It is not a clean disconnect sequence. When the endpoint di...
Definition: client.hpp:922
void set_keep_alive_sec(std::uint16_t keep_alive_sec, std::chrono::steady_clock::duration ping)
Set a keep alive second and a ping duration.
Definition: client.hpp:380
void async_connect(std::shared_ptr< Socket > &&socket, v5::properties props, async_handler_t func)
Connect to a broker Before calling connect(), call set_xxx member functions to configure the connecti...
Definition: client.hpp:711
client(constructor_access, Args &&... args)
Definition: client.hpp:48
void set_will(will w)
Set will.
Definition: client.hpp:333
void async_disconnect(async_handler_t func=async_handler_t())
Disconnect Send a disconnect packet to the connected broker. It is a clean disconnecting sequence....
Definition: client.hpp:883
void async_connect(v5::properties props, async_handler_t func)
Connect to a broker Before calling connect(), call set_xxx member functions to configure the connecti...
Definition: client.hpp:602
void set_clean_session(bool cs)
Set clean session.
Definition: client.hpp:285
friend std::shared_ptr< callable_overlay< client< tcp_endpoint< as::ip::tcp::socket, null_strand > > > > make_client_no_strand(as::io_context &ioc, std::string host, std::string port, protocol_version version)
Create no tls client without strand.
Definition: client.hpp:1438
void set_user_name(std::string name)
Set username.
Definition: client.hpp:310
std::enable_if_t< !std::is_convertible< T, async_handler_t >::value > async_connect(T session_life_keeper)
Connect to a broker Before calling connect(), call set_xxx member functions to configure the connecti...
Definition: client.hpp:554
void connect(any session_life_keeper=any())
Connect to a broker Before calling connect(), call set_xxx member functions to configure the connecti...
Definition: client.hpp:426
void connect(std::shared_ptr< Socket > &&socket, v5::properties props, boost::system::error_code &ec, any session_life_keeper=any())
Connect to a broker Before calling connect(), call set_xxx member functions to configure the connecti...
Definition: client.hpp:526
void connect(std::shared_ptr< Socket > &&socket, any session_life_keeper=any())
Connect to a broker Before calling connect(), call set_xxx member functions to configure the connecti...
Definition: client.hpp:477
std::enable_if_t< !std::is_convertible< T, async_handler_t >::value > async_connect(std::shared_ptr< Socket > &&socket, T session_life_keeper)
Connect to a broker Before calling connect(), call set_xxx member functions to configure the connecti...
Definition: client.hpp:642
void connect(boost::system::error_code &ec, any session_life_keeper=any())
Connect to a broker Before calling connect(), call set_xxx member functions to configure the connecti...
Definition: client.hpp:436
void async_disconnect(std::chrono::steady_clock::duration timeout, async_handler_t func=async_handler_t())
Disconnect Send a disconnect packet to the connected broker. It is a clean disconnecting sequence....
Definition: client.hpp:808
void disconnect(v5::disconnect_reason_code reason_code=v5::disconnect_reason_code::normal_disconnection, v5::properties props={})
Disconnect Send a disconnect packet to the connected broker. It is a clean disconnecting sequence....
Definition: client.hpp:789
void set_keep_alive_sec(std::uint16_t keep_alive_sec)
Set a keep alive second and a ping milli seconds.
Definition: client.hpp:416
Definition: endpoint.hpp:171
bool connected() const
Check connection status.
Definition: endpoint.hpp:4710
std::function< void(error_code ec)> async_handler_t
Definition: endpoint.hpp:176
void disconnect(v5::disconnect_reason_code reason=v5::disconnect_reason_code::normal_disconnection, v5::properties props={})
Disconnect Send a disconnect packet to the connected broker. It is a clean disconnecting sequence....
Definition: endpoint.hpp:1014
std::shared_ptr< MQTT_NS::socket > & socket_sp_ref()
Get shared_ptr of socket.
Definition: endpoint.hpp:4799
bool clean_start_
Definition: endpoint.hpp:10334
Definition: type_erased_socket.hpp:22
Definition: tcp_endpoint.hpp:23
std::vector< property_variant > properties
Definition: property_variant.hpp:51
disconnect_reason_code
Definition: reason_code.hpp:114
std::shared_ptr< callable_overlay< client< tcp_endpoint< as::ip::tcp::socket, as::io_context::strand >, 4 > > > make_client_32(as::io_context &ioc, std::string host, std::uint16_t port, protocol_version version=protocol_version::v3_1_1)
Definition: client.hpp:1639
boost::string_ref string_view
Definition: string_view.hpp:64
std::shared_ptr< callable_overlay< client< tcp_endpoint< as::ip::tcp::socket, null_strand >, 4 > > > make_client_no_strand_32(as::io_context &ioc, std::string host, std::uint16_t port, protocol_version version=protocol_version::v3_1_1)
Definition: client.hpp:1664
std::shared_ptr< callable_overlay< client< tcp_endpoint< as::ip::tcp::socket, as::io_context::strand > > > > make_client(as::io_context &ioc, std::string host, std::uint16_t port, protocol_version version=protocol_version::v3_1_1)
Definition: client.hpp:1428
boost::system::error_code error_code
Definition: error_code.hpp:16
constexpr std::remove_reference_t< T > && force_move(T &&t)
Definition: move.hpp:20
protocol_version
Definition: protocol_version.hpp:17
std::shared_ptr< callable_overlay< client< tcp_endpoint< as::ip::tcp::socket, null_strand > > > > make_client_no_strand(as::io_context &ioc, std::string host, std::uint16_t port, protocol_version version=protocol_version::v3_1_1)
Definition: client.hpp:1453
Definition: buffer.hpp:242
const_buffer buffer(MQTT_NS::buffer const &data)
create boost::asio::const_buffer from the MQTT_NS::buffer boost::asio::const_buffer is a kind of view...
Definition: buffer.hpp:253
Definition: client.hpp:40
Definition: null_strand.hpp:20