core.hpp 59 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359
  1. /*
  2. * Copyright (c) 2017-2023 zhllxt
  3. *
  4. * author : zhllxt
  5. * email : 37792738@qq.com
  6. *
  7. * https://github.com/mcxiaoke/mqtt
  8. * https://github.com/eclipse/paho.mqtt.c
  9. * https://github.com/eclipse/paho.mqtt.cpp
  10. *
  11. * https://github.com/mqtt/mqtt.org/wiki/libraries
  12. *
  13. * Distributed under the Boost Software License, Version 1.0. (See accompanying
  14. * file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  15. */
  16. #ifndef __ASIO2_MQTT_CORE_HPP__
  17. #define __ASIO2_MQTT_CORE_HPP__
  18. #if defined(_MSC_VER) && (_MSC_VER >= 1200)
  19. #pragma once
  20. #endif // defined(_MSC_VER) && (_MSC_VER >= 1200)
  21. #include <iosfwd>
  22. #include <cstdint>
  23. #include <cstddef>
  24. #include <memory>
  25. #include <chrono>
  26. #include <functional>
  27. #include <atomic>
  28. #include <string>
  29. #include <string_view>
  30. #include <vector>
  31. #include <optional>
  32. #include <array>
  33. #include <variant>
  34. #include <tuple>
  35. #include <type_traits>
  36. #include <asio2/external/predef.h>
  37. #ifdef ASIO2_HEADER_ONLY
  38. #include <asio2/bho/beast/websocket/detail/utf8_checker.hpp>
  39. #else
  40. #include <boost/beast/websocket/detail/utf8_checker.hpp>
  41. #endif
  42. #include <asio2/base/detail/util.hpp>
  43. #include <asio2/mqtt/detail/mqtt_topic_util.hpp>
  44. #include <asio2/mqtt/error.hpp>
  45. /*
  46. * Server Broker Port Websocket
  47. * ------------------------------------------------------------------
  48. * iot.eclipse.org Mosquitto 1883/8883 n/a
  49. * broker.hivemq.com HiveMQ 1883 8000 **
  50. * test.mosquitto.org Mosquitto 1883/8883/8884 8080/8081
  51. * test.mosca.io mosca 1883 80
  52. * broker.mqttdashboard.com HiveMQ 1883
  53. *
  54. */
  55. //namespace asio2::detail
  56. //{
  57. // template <class> class mqtt_handler_t;
  58. // template <class> class mqtt_invoker_t;
  59. // template <class> class mqtt_aop_connect;
  60. // template <class> class mqtt_aop_publish;
  61. //}
  62. namespace asio2::mqtt
  63. {
  64. static constexpr unsigned int max_payload = 268435455u;
  65. enum class version : std::uint8_t
  66. {
  67. // mqtt version 3.1 , The protocol version of the mqtt 3.1 CONNECT message is 0x03
  68. v3 = 3,
  69. // mqtt version 3.1.1, The protocol version of the mqtt 3.1.1 CONNECT message is 0x04
  70. v4 = 4,
  71. // mqtt version 5.0 , The protocol version of the mqtt 5.0 CONNECT message is 0x05
  72. v5 = 5
  73. };
  74. /**
  75. * MQTT Control Packet type
  76. *
  77. * http://docs.oasis-open.org/mqtt/mqtt/v3.1.1/os/mqtt-v3.1.1-os.html#_Toc398718021
  78. * https://docs.oasis-open.org/mqtt/mqtt/v5.0/os/mqtt-v5.0-os.html#_Toc3901022
  79. */
  80. enum class control_packet_type : std::uint8_t
  81. {
  82. // [Forbidden]
  83. // Reserved
  84. reserved = 0,
  85. // [Client to Server]
  86. // Client request to connect to Server
  87. connect = 1,
  88. // [Server to Client]
  89. // Connect acknowledgment
  90. connack = 2,
  91. // [Client to Server] or [Server to Client]
  92. // Publish message
  93. publish = 3,
  94. // [Client to Server] or [Server to Client]
  95. // Publish acknowledgment
  96. puback = 4,
  97. // [Client to Server] or [Server to Client]
  98. // Publish received (assured delivery part 1)
  99. pubrec = 5,
  100. // [Client to Server] or [Server to Client]
  101. // Publish release (assured delivery part 2)
  102. pubrel = 6,
  103. // [Client to Server] or [Server to Client]
  104. // Publish complete (assured delivery part 3)
  105. pubcomp = 7,
  106. // [Client to Server]
  107. // Client subscribe request
  108. subscribe = 8,
  109. // [Server to Client]
  110. // Subscribe acknowledgment
  111. suback = 9,
  112. // [Client to Server]
  113. // Unsubscribe request
  114. unsubscribe = 10,
  115. // [Server to Client]
  116. // Unsubscribe acknowledgment
  117. unsuback = 11,
  118. // [Client to Server]
  119. // PING request
  120. pingreq = 12,
  121. // [Server to Client]
  122. // PING response
  123. pingresp = 13,
  124. // [Client to Server]
  125. // Client is disconnecting
  126. disconnect = 14,
  127. // [Client to Server] or [Server to Client]
  128. // Authentication exchange
  129. // Only valid in mqtt 5.0
  130. // https://docs.oasis-open.org/mqtt/mqtt/v5.0/os/mqtt-v5.0-os.html#_Toc3901256
  131. auth = 15,
  132. };
  133. /**
  134. * Data representation
  135. *
  136. * https://docs.oasis-open.org/mqtt/mqtt/v5.0/os/mqtt-v5.0-os.html#_Toc3901006
  137. */
  138. enum class representation_type : std::uint8_t
  139. {
  140. one_byte_integer,
  141. two_byte_integer,
  142. four_byte_integer,
  143. variable_byte_integer,
  144. binary_data,
  145. utf8_string,
  146. utf8_string_pair
  147. };
  148. /**
  149. * This field indicates the level of assurance for delivery of an Application Message.
  150. * The QoS levels are shown below.
  151. *
  152. * https://docs.oasis-open.org/mqtt/mqtt/v5.0/os/mqtt-v5.0-os.html#_Toc3901103
  153. */
  154. enum class qos_type : std::uint8_t
  155. {
  156. at_most_once = 0, // At most once delivery
  157. at_least_once = 1, // At least once delivery
  158. exactly_once = 2, // Exactly once delivery
  159. };
  160. static constexpr std::uint8_t qos_min_value = static_cast<std::uint8_t>(qos_type::at_most_once);
  161. static constexpr std::uint8_t qos_max_value = static_cast<std::uint8_t>(qos_type::exactly_once);
  162. template<class QosOrInt>
  163. typename std::enable_if_t<
  164. std::is_same_v<asio2::detail::remove_cvref_t<QosOrInt>, mqtt::qos_type> ||
  165. std::is_integral_v<asio2::detail::remove_cvref_t<QosOrInt>>, bool>
  166. inline is_valid_qos(QosOrInt q)
  167. {
  168. return (static_cast<std::uint8_t>(q) >= qos_min_value && static_cast<std::uint8_t>(q) <= qos_max_value);
  169. }
  170. /**
  171. * Bits 4 and 5 of the Subscription Options represent the Retain Handling option.
  172. * This option specifies whether retained messages are sent when the subscription is established.
  173. * This does not affect the sending of retained messages at any point after the subscribe.
  174. * If there are no retained messages matching the Topic Filter, all of these values act the same.
  175. * The values are:
  176. *
  177. * https://docs.oasis-open.org/mqtt/mqtt/v5.0/os/mqtt-v5.0-os.html#_Toc3901169
  178. */
  179. enum class retain_handling_type : std::uint8_t
  180. {
  181. // Send retained messages at the time of the subscribe
  182. send = 0,
  183. // Send retained messages at subscribe only if the subscription does not currently exist
  184. send_only_new_subscription = 1,
  185. // Do not send retained messages at the time of the subscribe
  186. not_send = 2,
  187. };
  188. /*
  189. * The algorithm for encoding a non-negative integer (X) into the Variable Byte Integer encoding scheme is as follows:
  190. * https://docs.oasis-open.org/mqtt/mqtt/v5.0/os/mqtt-v5.0-os.html#_Toc3901011
  191. */
  192. template<class Integer>
  193. std::array<std::uint8_t, 4> encode_variable_byte_integer(Integer X)
  194. {
  195. std::int32_t i = 0;
  196. std::array<std::uint8_t, 4> value{};
  197. if (static_cast<std::size_t>(X) > static_cast<std::size_t>(mqtt::max_payload))
  198. {
  199. asio2::set_last_error(asio::error::invalid_argument);
  200. return value;
  201. }
  202. asio2::clear_last_error();
  203. do
  204. {
  205. std::uint8_t encodedByte = X % 128;
  206. X = X / 128;
  207. // if there are more data to encode, set the top bit of this byte
  208. if (X > 0)
  209. {
  210. encodedByte = encodedByte | 128;
  211. }
  212. // 'output' encodedByte
  213. value[i] = encodedByte;
  214. ++i;
  215. } while (X > 0);
  216. return value;
  217. }
  218. template<class Integer>
  219. inline bool check_size(Integer size)
  220. {
  221. return (std::size_t(size) <= std::size_t(65535));
  222. }
  223. template<class String>
  224. inline bool check_utf8(String& str)
  225. {
  226. #ifdef ASIO2_HEADER_ONLY
  227. namespace beast = ::bho::beast;
  228. #else
  229. namespace beast = ::boost::beast;
  230. #endif
  231. #if defined(ASIO2_CHECK_UTF8)
  232. return beast::websocket::detail::check_utf8(str.data(), str.size());
  233. #else
  234. asio2::detail::ignore_unused(str);
  235. return true;
  236. #endif
  237. }
  238. /*
  239. * @return pair.first - the integer value, pair.second - number of bytes
  240. *
  241. * The algorithm for decoding a Variable Byte Integer type is as follows:
  242. * https://docs.oasis-open.org/mqtt/mqtt/v5.0/os/mqtt-v5.0-os.html#_Toc3901011
  243. */
  244. template<class BufferContainer>
  245. std::pair<std::int32_t, std::int32_t> decode_variable_byte_integer(BufferContainer X)
  246. {
  247. std::int32_t i = 0;
  248. std::int32_t multiplier = 1;
  249. std::int32_t value = 0;
  250. std::uint8_t encodedByte;
  251. do
  252. {
  253. if (X.size() < std::size_t(i + 1))
  254. {
  255. i = 0;
  256. value = 0;
  257. asio2::set_last_error(asio::error::no_buffer_space);
  258. return { value, i };
  259. }
  260. encodedByte = static_cast<std::uint8_t>(X[i]); // 'next byte from stream'
  261. ++i;
  262. value += (encodedByte & 127) * multiplier;
  263. if (multiplier > 128 * 128 * 128)
  264. {
  265. i = 0;
  266. value = 0;
  267. asio2::set_last_error(asio::error::invalid_argument);
  268. return { value, i };
  269. }
  270. multiplier *= 128;
  271. } while ((encodedByte & 128) != 0);
  272. asio2::clear_last_error();
  273. // When this algorithm terminates, value contains the Variable Byte Integer value.
  274. return { value, i };
  275. }
  276. /**
  277. * Bits in a byte are labelled 7 to 0. Bit number 7 is the most significant bit,
  278. * the least significant bit is assigned bit number 0.
  279. *
  280. * https://docs.oasis-open.org/mqtt/mqtt/v5.0/os/mqtt-v5.0-os.html#_Toc3901007
  281. */
  282. class one_byte_integer
  283. {
  284. public:
  285. using value_type = std::uint8_t;
  286. one_byte_integer() = default;
  287. explicit one_byte_integer(std::uint8_t v) : value_(v)
  288. {
  289. }
  290. inline std::uint8_t value() const noexcept
  291. {
  292. return value_;
  293. }
  294. inline operator std::uint8_t() const noexcept { return value(); }
  295. inline one_byte_integer& operator=(std::uint8_t v)
  296. {
  297. value_ = v;
  298. return (*this);
  299. }
  300. inline constexpr std::size_t required_size() const noexcept
  301. {
  302. return (sizeof(std::uint8_t));
  303. }
  304. inline constexpr std::size_t size() const noexcept
  305. {
  306. return (sizeof(std::uint8_t));
  307. }
  308. inline one_byte_integer& serialize(std::vector<asio::const_buffer>& buffers)
  309. {
  310. buffers.emplace_back(std::addressof(value_), required_size());
  311. return (*this);
  312. }
  313. /*
  314. * The Container is usually a std::string, std::vector<char>, ...
  315. */
  316. template<class Container>
  317. inline one_byte_integer& serialize(Container& buffer)
  318. {
  319. static_assert(sizeof(typename Container::value_type) == std::size_t(1));
  320. auto* p = reinterpret_cast<typename Container::const_pointer>(std::addressof(value_));
  321. buffer.insert(buffer.end(), p, p + required_size());
  322. return (*this);
  323. }
  324. inline one_byte_integer& deserialize(std::string_view& data)
  325. {
  326. if (data.size() < required_size())
  327. {
  328. set_last_error(mqtt::make_error_code(mqtt::error::malformed_packet));
  329. return (*this);
  330. }
  331. asio2::clear_last_error();
  332. value_ = data[0];
  333. data.remove_prefix(required_size());
  334. return (*this);
  335. }
  336. protected:
  337. std::uint8_t value_{ 0 };
  338. };
  339. /**
  340. * Two Byte Integer data values are 16-bit unsigned integers in big-endian order: the high order
  341. * byte precedes the lower order byte. This means that a 16-bit word is presented on the network
  342. * as Most Significant Byte (MSB), followed by Least Significant Byte (LSB).
  343. *
  344. * https://docs.oasis-open.org/mqtt/mqtt/v5.0/os/mqtt-v5.0-os.html#_Toc3901008
  345. */
  346. class two_byte_integer
  347. {
  348. public:
  349. using value_type = std::uint16_t;
  350. two_byte_integer() = default;
  351. explicit two_byte_integer(std::uint16_t v)
  352. {
  353. *this = v;
  354. }
  355. inline std::uint16_t value() const noexcept
  356. {
  357. #if ASIO2_ENDIAN_BIG_BYTE
  358. return value_;
  359. #else
  360. return (((value_ >> 8) & 0x00ff) | ((value_ << 8) & 0xff00));
  361. #endif
  362. }
  363. inline operator std::uint16_t() const noexcept { return value(); }
  364. inline two_byte_integer& operator=(std::uint16_t v)
  365. {
  366. #if ASIO2_ENDIAN_BIG_BYTE
  367. value_ = v;
  368. #else
  369. value_ = ((v >> 8) & 0x00ff) | ((v << 8) & 0xff00);
  370. #endif
  371. return (*this);
  372. }
  373. inline constexpr std::size_t required_size() const noexcept
  374. {
  375. return (sizeof(std::uint16_t));
  376. }
  377. inline constexpr std::size_t size() const noexcept
  378. {
  379. return (sizeof(std::uint16_t));
  380. }
  381. inline two_byte_integer& serialize(std::vector<asio::const_buffer>& buffers)
  382. {
  383. buffers.emplace_back(std::addressof(value_), required_size());
  384. return (*this);
  385. }
  386. /*
  387. * The Container is usually a std::string, std::vector<char>, ...
  388. */
  389. template<class Container>
  390. inline two_byte_integer& serialize(Container& buffer)
  391. {
  392. static_assert(sizeof(typename Container::value_type) == std::size_t(1));
  393. auto* p = reinterpret_cast<typename Container::const_pointer>(std::addressof(value_));
  394. buffer.insert(buffer.end(), p, p + required_size());
  395. return (*this);
  396. }
  397. inline two_byte_integer& deserialize(std::string_view& data)
  398. {
  399. if (data.size() < required_size())
  400. {
  401. set_last_error(mqtt::make_error_code(mqtt::error::malformed_packet));
  402. return (*this);
  403. }
  404. asio2::clear_last_error();
  405. value_ = ((data[1] << 8) & 0xff00) | ((data[0] << 0) & 0x00ff);
  406. data.remove_prefix(required_size());
  407. return (*this);
  408. }
  409. protected:
  410. std::uint16_t value_{ 0 };
  411. };
  412. /**
  413. * Four Byte Integer data values are 32-bit unsigned integers in big-endian order: the high order
  414. * byte precedes the successively lower order bytes. This means that a 32-bit word is presented on
  415. * the network as Most Significant Byte (MSB), followed by the next most Significant Byte (MSB),
  416. * followed by the next most Significant Byte (MSB), followed by Least Significant Byte (LSB).
  417. *
  418. * https://docs.oasis-open.org/mqtt/mqtt/v5.0/os/mqtt-v5.0-os.html#_Toc3901009
  419. */
  420. class four_byte_integer
  421. {
  422. public:
  423. using value_type = std::uint32_t;
  424. four_byte_integer() = default;
  425. explicit four_byte_integer(std::uint32_t v)
  426. {
  427. *this = v;
  428. }
  429. inline std::uint32_t value() const noexcept
  430. {
  431. #if ASIO2_ENDIAN_BIG_BYTE
  432. return value_;
  433. #else
  434. return (
  435. ((value_ >> 24) & 0x000000ff) |
  436. ((value_ << 24) & 0xff000000) |
  437. ((value_ >> 8) & 0x0000ff00) |
  438. ((value_ << 8) & 0x00ff0000));
  439. #endif
  440. }
  441. inline operator std::uint32_t() const noexcept { return value(); }
  442. inline four_byte_integer& operator=(std::uint32_t v)
  443. {
  444. #if ASIO2_ENDIAN_BIG_BYTE
  445. value_ = v;
  446. #else
  447. value_ = ((v >> 24) & 0x000000ff) | ((v << 24) & 0xff000000) | ((v >> 8) & 0x0000ff00) | ((v << 8) & 0x00ff0000);
  448. #endif
  449. return (*this);
  450. }
  451. inline constexpr std::size_t required_size() const noexcept
  452. {
  453. return (sizeof(std::uint32_t));
  454. }
  455. inline constexpr std::size_t size() const noexcept
  456. {
  457. return (sizeof(std::uint32_t));
  458. }
  459. inline four_byte_integer& serialize(std::vector<asio::const_buffer>& buffers)
  460. {
  461. buffers.emplace_back(std::addressof(value_), required_size());
  462. return (*this);
  463. }
  464. /*
  465. * The Container is usually a std::string, std::vector<char>, ...
  466. */
  467. template<class Container>
  468. inline four_byte_integer& serialize(Container& buffer)
  469. {
  470. static_assert(sizeof(typename Container::value_type) == std::size_t(1));
  471. auto* p = reinterpret_cast<typename Container::const_pointer>(std::addressof(value_));
  472. buffer.insert(buffer.end(), p, p + required_size());
  473. return (*this);
  474. }
  475. inline four_byte_integer& deserialize(std::string_view& data)
  476. {
  477. if (data.size() < required_size())
  478. {
  479. set_last_error(mqtt::make_error_code(mqtt::error::malformed_packet));
  480. return (*this);
  481. }
  482. asio2::clear_last_error();
  483. value_ =
  484. ((data[3] << 24) & 0xff000000) |
  485. ((data[2] << 16) & 0x00ff0000) |
  486. ((data[1] << 8) & 0x0000ff00) |
  487. ((data[0] << 0) & 0x000000ff);
  488. data.remove_prefix(required_size());
  489. return (*this);
  490. }
  491. protected:
  492. std::uint32_t value_{ 0 };
  493. };
  494. /**
  495. * The Variable Byte Integer is encoded using an encoding scheme which uses a single byte for values up to 127.
  496. * Larger values are handled as follows. The least significant seven bits of each byte encode the data, and
  497. * the most significant bit is used to indicate whether there are bytes following in the representation.
  498. * Thus, each byte encodes 128 values and a "continuation bit".
  499. * The maximum number of bytes in the Variable Byte Integer field is four.
  500. * The encoded value MUST use the minimum number of bytes necessary to represent the value [MQTT-1.5.5-1].
  501. * This is shown in Table 1?1 Size of Variable Byte Integer.
  502. *
  503. * Table 1-1 Size of Variable Byte Integer
  504. * +---------+-------------------------------------+---------------------------------------+
  505. * | Digits | From | To |
  506. * +---------+-------------------------------------+---------------------------------------+
  507. * | 1 | 0 (0x00) | 127 (0x7F) |
  508. * +---------+-------------------------------------+---------------------------------------+
  509. * | 2 | 128 (0x80, 0x01) | 16,383 (0xFF, 0x7F) |
  510. * +---------+-------------------------------------+---------------------------------------+
  511. * | 3 | 16,384 (0x80, 0x80, 0x01) | 2,097,151 (0xFF, 0xFF, 0x7F) |
  512. * +---------+-------------------------------------+---------------------------------------+
  513. * | 4 | 2,097,152 (0x80, 0x80, 0x80, 0x01) | 268,435,455 (0xFF, 0xFF, 0xFF, 0x7F) |
  514. * +---------+-------------------------------------+---------------------------------------+
  515. *
  516. * https://docs.oasis-open.org/mqtt/mqtt/v5.0/os/mqtt-v5.0-os.html#_Toc3901011
  517. */
  518. class variable_byte_integer
  519. {
  520. public:
  521. using value_type = std::int32_t;
  522. variable_byte_integer() = default;
  523. explicit variable_byte_integer(std::int32_t v)
  524. {
  525. *this = v;
  526. }
  527. inline std::int32_t value() const
  528. {
  529. return std::get<0>(decode_variable_byte_integer(value_));
  530. }
  531. inline operator std::int32_t() const { return value(); }
  532. inline variable_byte_integer& operator=(std::int32_t v)
  533. {
  534. value_ = encode_variable_byte_integer(v);
  535. return (*this);
  536. }
  537. inline std::size_t required_size() const
  538. {
  539. return std::get<1>(decode_variable_byte_integer(value_));
  540. }
  541. inline std::size_t size() const
  542. {
  543. return std::get<1>(decode_variable_byte_integer(value_));
  544. }
  545. inline variable_byte_integer& serialize(std::vector<asio::const_buffer>& buffers)
  546. {
  547. buffers.emplace_back(value_.data(), required_size());
  548. return (*this);
  549. }
  550. /*
  551. * The Container is usually a std::string, std::vector<char>, ...
  552. */
  553. template<class Container>
  554. inline variable_byte_integer& serialize(Container& buffer)
  555. {
  556. static_assert(sizeof(typename Container::value_type) == std::size_t(1));
  557. auto* p = reinterpret_cast<typename Container::const_pointer>(value_.data());
  558. buffer.insert(buffer.end(), p, p + required_size());
  559. return (*this);
  560. }
  561. inline variable_byte_integer& deserialize(std::string_view& data)
  562. {
  563. auto[value, bytes] = decode_variable_byte_integer(data);
  564. if (asio2::get_last_error())
  565. return (*this);
  566. asio2::detail::ignore_unused(value);
  567. std::memcpy((void*)value_.data(), (const void*)data.data(), bytes);
  568. data.remove_prefix(bytes);
  569. return (*this);
  570. }
  571. protected:
  572. // The Variable Byte Integer is encoded using an encoding scheme which uses a single byte for values
  573. // up to 127. Larger values are handled as follows. The least significant seven bits of each byte
  574. // encode the data, and the most significant bit is used to indicate whether there are bytes following
  575. // in the representation. Thus, each byte encodes 128 values and a "continuation bit". The maximum
  576. // number of bytes in the Variable Byte Integer field is four. The encoded value MUST use the minimum
  577. // number of bytes necessary to represent the value [MQTT-1.5.5-1].
  578. std::array<std::uint8_t, 4> value_{};
  579. };
  580. /**
  581. * UTF-8 Encoded String
  582. * https://docs.oasis-open.org/mqtt/mqtt/v5.0/os/mqtt-v5.0-os.html#_Toc3901010
  583. */
  584. class utf8_string
  585. {
  586. public:
  587. using value_type = std::string;
  588. using view_type = std::string_view;
  589. utf8_string() = default;
  590. utf8_string(utf8_string const& o) : length_(o.length_), data_(o.data_)
  591. {
  592. //asio2::detail::disable_sso(data_);
  593. }
  594. utf8_string& operator=(utf8_string const& o)
  595. {
  596. length_ = o.length_;
  597. data_ = o.data_;
  598. //asio2::detail::disable_sso(data_);
  599. return (*this);
  600. }
  601. explicit utf8_string(const char* const s)
  602. {
  603. *this = std::string{ s };
  604. }
  605. explicit utf8_string(std::string s)
  606. {
  607. *this = std::move(s);
  608. }
  609. explicit utf8_string(std::string_view s)
  610. {
  611. *this = std::move(s);
  612. }
  613. inline utf8_string& operator=(std::string s)
  614. {
  615. if (!check_size(s.size()))
  616. {
  617. asio2::set_last_error(asio::error::invalid_argument);
  618. return (*this);
  619. }
  620. if (!check_utf8(s))
  621. {
  622. asio2::set_last_error(asio::error::invalid_argument);
  623. return (*this);
  624. }
  625. asio2::clear_last_error();
  626. data_ = std::move(s);
  627. length_ = static_cast<std::uint16_t>(data_.size());
  628. //asio2::detail::disable_sso(data_);
  629. return (*this);
  630. }
  631. inline utf8_string& operator=(std::string_view s)
  632. {
  633. *this = std::string{ s };
  634. return (*this);
  635. }
  636. inline utf8_string& operator=(const char* const s)
  637. {
  638. *this = std::string{ s };
  639. return (*this);
  640. }
  641. inline bool operator==(const std::string & s) noexcept { return (data_ == s); }
  642. inline bool operator==(const std::string_view & s) noexcept { return (data_ == s); }
  643. inline bool operator!=(const std::string & s) noexcept { return (data_ != s); }
  644. inline bool operator!=(const std::string_view & s) noexcept { return (data_ != s); }
  645. inline utf8_string& operator+=(const std::string & s)
  646. {
  647. if (!check_size(data_.size() + s.size()))
  648. {
  649. asio2::set_last_error(asio::error::invalid_argument);
  650. return (*this);
  651. }
  652. if (!check_utf8(s))
  653. {
  654. asio2::set_last_error(asio::error::invalid_argument);
  655. return (*this);
  656. }
  657. asio2::clear_last_error();
  658. data_ += s;
  659. length_ = static_cast<std::uint16_t>(data_.size());
  660. //asio2::detail::disable_sso(data_);
  661. return (*this);
  662. }
  663. inline utf8_string& operator+=(const std::string_view & s)
  664. {
  665. if (!check_size(data_.size() + s.size()))
  666. {
  667. asio2::set_last_error(asio::error::invalid_argument);
  668. return (*this);
  669. }
  670. if (!check_utf8(s))
  671. {
  672. asio2::set_last_error(asio::error::invalid_argument);
  673. return (*this);
  674. }
  675. asio2::clear_last_error();
  676. data_ += s;
  677. length_ = static_cast<std::uint16_t>(data_.size());
  678. //asio2::detail::disable_sso(data_);
  679. return (*this);
  680. }
  681. inline utf8_string& operator+=(const char* const s)
  682. {
  683. this->operator+=(std::string_view{ s });
  684. return (*this);
  685. }
  686. // https://stackoverflow.com/questions/56833000/c20-with-u8-char8-t-and-stdstring
  687. #if defined(__cpp_lib_char8_t)
  688. explicit utf8_string(const char8_t* const s)
  689. {
  690. *this = std::u8string_view{ s };
  691. }
  692. explicit utf8_string(const std::u8string& s)
  693. {
  694. *this = s;
  695. }
  696. explicit utf8_string(const std::u8string_view& s)
  697. {
  698. *this = s;
  699. }
  700. inline utf8_string& operator=(const std::u8string& s)
  701. {
  702. *this = std::string{ s.begin(), s.end() };
  703. return (*this);
  704. }
  705. inline utf8_string& operator=(const std::u8string_view& s)
  706. {
  707. *this = std::string{ s.begin(), s.end() };
  708. return (*this);
  709. }
  710. inline utf8_string& operator=(const char8_t* const s)
  711. {
  712. *this = std::u8string_view{ s };
  713. return (*this);
  714. }
  715. inline bool operator==(const std::u8string & s) noexcept { return (data_ == std::string{s.begin(), s.end()}); }
  716. inline bool operator==(const std::u8string_view & s) noexcept { return (data_ == std::string{s.begin(), s.end()}); }
  717. inline bool operator!=(const std::u8string & s) noexcept { return (data_ != std::string{s.begin(), s.end()}); }
  718. inline bool operator!=(const std::u8string_view & s) noexcept { return (data_ != std::string{s.begin(), s.end()}); }
  719. inline utf8_string& operator+=(const std::u8string & s)
  720. {
  721. *this += std::string{ s.begin(), s.end() };
  722. return (*this);
  723. }
  724. inline utf8_string& operator+=(const std::u8string_view & s)
  725. {
  726. *this += std::string{ s.begin(), s.end() };
  727. return (*this);
  728. }
  729. inline utf8_string& operator+=(const char8_t* const s)
  730. {
  731. *this += std::u8string_view{ s };
  732. return (*this);
  733. }
  734. #endif
  735. inline std::size_t required_size() const noexcept
  736. {
  737. return (length_.required_size() + data_.size());
  738. }
  739. inline std::size_t size() const noexcept
  740. {
  741. return (data_.size());
  742. }
  743. inline bool empty() const noexcept { return data_.empty(); }
  744. inline std::string data () const { return data_; }
  745. inline std::string_view data_view() const { return data_; }
  746. inline operator std::string () const { return data_; }
  747. inline operator std::string_view () const { return data_; }
  748. inline utf8_string& serialize(std::vector<asio::const_buffer>& buffers)
  749. {
  750. length_.serialize(buffers);
  751. buffers.emplace_back(asio::buffer(data_));
  752. return (*this);
  753. }
  754. /*
  755. * The Container is usually a std::string, std::vector<char>, ...
  756. */
  757. template<class Container>
  758. inline utf8_string& serialize(Container& buffer)
  759. {
  760. length_.serialize(buffer);
  761. buffer.insert(buffer.end(), data_.begin(), data_.end());
  762. return (*this);
  763. }
  764. inline utf8_string& deserialize(std::string_view& data)
  765. {
  766. length_.deserialize(data);
  767. if (asio2::get_last_error())
  768. return (*this);
  769. if (data.size() < length_.value())
  770. {
  771. set_last_error(mqtt::make_error_code(mqtt::error::malformed_packet));
  772. return (*this);
  773. }
  774. asio2::clear_last_error();
  775. data_ = data.substr(0, length_.value());
  776. data.remove_prefix(data_.size());
  777. //asio2::detail::disable_sso(data_);
  778. return (*this);
  779. }
  780. protected:
  781. // Each of these strings is prefixed with a Two Byte Integer length field that gives the number of
  782. // bytes in a UTF-8 encoded string itself, as illustrated in Figure 1.1 Structure of UTF-8 Encoded
  783. // Strings below. Consequently, the maximum size of a UTF-8 Encoded String is 65,535 bytes.
  784. two_byte_integer length_{};
  785. // UTF-8 encoded character data, if length > 0.
  786. std::string data_ {};
  787. };
  788. /**
  789. * Binary Data
  790. * https://docs.oasis-open.org/mqtt/mqtt/v5.0/os/mqtt-v5.0-os.html#_Toc3901012
  791. */
  792. class binary_data
  793. {
  794. public:
  795. using value_type = std::string;
  796. using view_type = std::string_view;
  797. binary_data() = default;
  798. binary_data(binary_data const& o) : length_(o.length_), data_(o.data_)
  799. {
  800. //asio2::detail::disable_sso(data_);
  801. }
  802. binary_data& operator=(binary_data const& o)
  803. {
  804. length_ = o.length_;
  805. data_ = o.data_;
  806. //asio2::detail::disable_sso(data_);
  807. return (*this);
  808. }
  809. explicit binary_data(const char* const s)
  810. {
  811. *this = std::string{ s };
  812. }
  813. explicit binary_data(std::string s)
  814. {
  815. *this = std::move(s);
  816. }
  817. explicit binary_data(std::string_view s)
  818. {
  819. *this = std::move(s);
  820. }
  821. inline binary_data& operator=(std::string s)
  822. {
  823. if (!check_size(s.size()))
  824. {
  825. asio2::set_last_error(asio::error::invalid_argument);
  826. return (*this);
  827. }
  828. asio2::clear_last_error();
  829. data_ = std::move(s);
  830. length_ = static_cast<std::uint16_t>(data_.size());
  831. //asio2::detail::disable_sso(data_);
  832. return (*this);
  833. }
  834. inline binary_data& operator=(std::string_view s)
  835. {
  836. *this = std::string{ s };
  837. return (*this);
  838. }
  839. inline binary_data& operator=(const char* const s)
  840. {
  841. *this = std::string{ s };
  842. return (*this);
  843. }
  844. inline bool operator==(const std::string & s) noexcept { return (data_ == s); }
  845. inline bool operator==(const std::string_view & s) noexcept { return (data_ == s); }
  846. inline bool operator!=(const std::string & s) noexcept { return (data_ != s); }
  847. inline bool operator!=(const std::string_view & s) noexcept { return (data_ != s); }
  848. inline binary_data& operator+=(const std::string & s)
  849. {
  850. if (!check_size(data_.size() + s.size()))
  851. {
  852. asio2::set_last_error(asio::error::invalid_argument);
  853. return (*this);
  854. }
  855. asio2::clear_last_error();
  856. data_ += s;
  857. length_ = static_cast<std::uint16_t>(data_.size());
  858. //asio2::detail::disable_sso(data_);
  859. return (*this);
  860. }
  861. inline binary_data& operator+=(const std::string_view & s)
  862. {
  863. if (!check_size(data_.size() + s.size()))
  864. {
  865. asio2::set_last_error(asio::error::invalid_argument);
  866. return (*this);
  867. }
  868. asio2::clear_last_error();
  869. data_ += s;
  870. length_ = static_cast<std::uint16_t>(data_.size());
  871. //asio2::detail::disable_sso(data_);
  872. return (*this);
  873. }
  874. inline binary_data& operator+=(const char* const s)
  875. {
  876. this->operator+=(std::string_view{ s });
  877. return (*this);
  878. }
  879. #if defined(__cpp_lib_char8_t)
  880. explicit binary_data(const char8_t* const s)
  881. {
  882. *this = std::u8string{ s };
  883. }
  884. explicit binary_data(const std::u8string& s)
  885. {
  886. *this = s;
  887. }
  888. explicit binary_data(const std::u8string_view& s)
  889. {
  890. *this = s;
  891. }
  892. inline binary_data& operator=(const std::u8string& s)
  893. {
  894. *this = std::string{ s.begin(), s.end() };
  895. return (*this);
  896. }
  897. inline binary_data& operator=(const std::u8string_view& s)
  898. {
  899. *this = std::string{ s.begin(), s.end() };
  900. return (*this);
  901. }
  902. inline binary_data& operator=(const char8_t* const s)
  903. {
  904. *this = std::u8string_view{ s };
  905. return (*this);
  906. }
  907. inline bool operator==(const std::u8string & s) noexcept { return (data_ == std::string(s.begin(), s.end())); }
  908. inline bool operator==(const std::u8string_view & s) noexcept { return (data_ == std::string(s.begin(), s.end())); }
  909. inline bool operator!=(const std::u8string & s) noexcept { return (data_ != std::string(s.begin(), s.end())); }
  910. inline bool operator!=(const std::u8string_view & s) noexcept { return (data_ != std::string(s.begin(), s.end())); }
  911. inline binary_data& operator+=(const std::u8string & s)
  912. {
  913. *this += std::string{ s.begin(), s.end() };
  914. return (*this);
  915. }
  916. inline binary_data& operator+=(const std::u8string_view & s)
  917. {
  918. *this += std::string{ s.begin(), s.end() };
  919. return (*this);
  920. }
  921. inline binary_data& operator+=(const char8_t* const s)
  922. {
  923. *this += std::u8string_view{ s };
  924. return (*this);
  925. }
  926. #endif
  927. inline std::size_t required_size() const noexcept
  928. {
  929. return (length_.required_size() + data_.size());
  930. }
  931. inline std::size_t size() const noexcept
  932. {
  933. return (data_.size());
  934. }
  935. inline bool empty() const noexcept { return data_.empty(); }
  936. inline std::string data () const { return data_; }
  937. inline std::string_view data_view() const { return data_; }
  938. inline operator std::string () const { return data_; }
  939. inline operator std::string_view () const { return data_; }
  940. inline binary_data& serialize(std::vector<asio::const_buffer>& buffers)
  941. {
  942. length_.serialize(buffers);
  943. buffers.emplace_back(asio::buffer(data_));
  944. return (*this);
  945. }
  946. /*
  947. * The Container is usually a std::string, std::vector<char>, ...
  948. */
  949. template<class Container>
  950. inline binary_data& serialize(Container& buffer)
  951. {
  952. length_.serialize(buffer);
  953. buffer.insert(buffer.end(), data_.begin(), data_.end());
  954. return (*this);
  955. }
  956. inline binary_data& deserialize(std::string_view& data)
  957. {
  958. length_.deserialize(data);
  959. if (asio2::get_last_error())
  960. return (*this);
  961. if (data.size() < length_.value())
  962. {
  963. set_last_error(mqtt::make_error_code(mqtt::error::malformed_packet));
  964. return (*this);
  965. }
  966. asio2::clear_last_error();
  967. data_ = data.substr(0, length_.value());
  968. data.remove_prefix(data_.size());
  969. //asio2::detail::disable_sso(data_);
  970. return (*this);
  971. }
  972. protected:
  973. // Binary Data is represented by a Two Byte Integer length which indicates the number of data bytes,
  974. // followed by that number of bytes. Thus, the length of Binary Data is limited to the range of 0 to
  975. // 65,535 Bytes.
  976. two_byte_integer length_{};
  977. // number of bytes
  978. std::string data_ {};
  979. };
  980. /**
  981. * A UTF-8 String Pair consists of two UTF-8 Encoded Strings.
  982. * This data type is used to hold name-value pairs.
  983. * The first string serves as the name, and the second string contains the value.
  984. *
  985. * https://docs.oasis-open.org/mqtt/mqtt/v5.0/os/mqtt-v5.0-os.html#_Toc3901013
  986. */
  987. class utf8_string_pair
  988. {
  989. public:
  990. using value_type = std::pair<std::string, std::string>;
  991. using view_type = std::pair<std::string_view, std::string_view>;
  992. utf8_string_pair() = default;
  993. explicit utf8_string_pair(const char* const key, const char* const val)
  994. : key_(std::move(key)), val_(std::move(val))
  995. {
  996. }
  997. explicit utf8_string_pair(std::string key, std::string val)
  998. : key_(std::move(key)), val_(std::move(val))
  999. {
  1000. }
  1001. explicit utf8_string_pair(std::string_view key, std::string_view val)
  1002. : key_(std::move(key)), val_(std::move(val))
  1003. {
  1004. }
  1005. inline utf8_string_pair& operator=(std::tuple<std::string, std::string> kv)
  1006. {
  1007. key_ = std::move(std::get<0>(kv));
  1008. val_ = std::move(std::get<1>(kv));
  1009. return (*this);
  1010. }
  1011. inline utf8_string_pair& operator=(std::pair<std::string, std::string> kv)
  1012. {
  1013. key_ = std::move(std::get<0>(kv));
  1014. val_ = std::move(std::get<1>(kv));
  1015. return (*this);
  1016. }
  1017. inline utf8_string_pair& operator=(std::tuple<std::string_view, std::string_view> kv)
  1018. {
  1019. key_ = std::move(std::get<0>(kv));
  1020. val_ = std::move(std::get<1>(kv));
  1021. return (*this);
  1022. }
  1023. inline utf8_string_pair& operator=(std::pair<std::string_view, std::string_view> kv)
  1024. {
  1025. key_ = std::move(std::get<0>(kv));
  1026. val_ = std::move(std::get<1>(kv));
  1027. return (*this);
  1028. }
  1029. #if defined(__cpp_lib_char8_t)
  1030. explicit utf8_string_pair(const char8_t* const key, const char8_t* const val)
  1031. : key_(std::move(key)), val_(std::move(val))
  1032. {
  1033. }
  1034. explicit utf8_string_pair(const std::u8string& key, const std::u8string& val)
  1035. : key_(key), val_(val)
  1036. {
  1037. }
  1038. explicit utf8_string_pair(const std::u8string_view& key, const std::u8string_view& val)
  1039. : key_(key), val_(val)
  1040. {
  1041. }
  1042. inline utf8_string_pair& operator=(std::tuple<std::u8string, std::u8string> kv)
  1043. {
  1044. key_ = std::move(std::get<0>(kv));
  1045. val_ = std::move(std::get<1>(kv));
  1046. return (*this);
  1047. }
  1048. inline utf8_string_pair& operator=(std::pair<std::u8string, std::u8string> kv)
  1049. {
  1050. key_ = std::move(std::get<0>(kv));
  1051. val_ = std::move(std::get<1>(kv));
  1052. return (*this);
  1053. }
  1054. inline utf8_string_pair& operator=(std::tuple<std::u8string_view, std::u8string_view> kv)
  1055. {
  1056. key_ = std::move(std::get<0>(kv));
  1057. val_ = std::move(std::get<1>(kv));
  1058. return (*this);
  1059. }
  1060. inline utf8_string_pair& operator=(std::pair<std::u8string_view, std::u8string_view> kv)
  1061. {
  1062. key_ = std::move(std::get<0>(kv));
  1063. val_ = std::move(std::get<1>(kv));
  1064. return (*this);
  1065. }
  1066. #endif
  1067. inline std::size_t required_size() const noexcept
  1068. {
  1069. return (key_.required_size() + val_.required_size());
  1070. }
  1071. inline std::size_t size() const noexcept
  1072. {
  1073. return (key_.size() + val_.size());
  1074. }
  1075. inline std::string key() const { return key_.data(); }
  1076. inline std::string val() const { return val_.data(); }
  1077. inline std::string_view key_view() const { return key_.data_view(); }
  1078. inline std::string_view val_view() const { return val_.data_view(); }
  1079. /*
  1080. * The Container is usually a std::string, std::vector<char>, ...
  1081. */
  1082. template<class Container>
  1083. inline utf8_string_pair& serialize(Container& buffer)
  1084. {
  1085. key_.serialize(buffer);
  1086. val_.serialize(buffer);
  1087. return (*this);
  1088. }
  1089. inline utf8_string_pair& deserialize(std::string_view& data)
  1090. {
  1091. key_.deserialize(data);
  1092. if (asio2::get_last_error())
  1093. return (*this);
  1094. val_.deserialize(data);
  1095. return (*this);
  1096. }
  1097. protected:
  1098. utf8_string key_{};
  1099. utf8_string val_{};
  1100. };
  1101. /**
  1102. * The Payload contains the Application Message that is being published.
  1103. * The content and format of the data is application specific.
  1104. * The length of the Payload can be calculated by subtracting the length of the Variable Header
  1105. * from the Remaining Length field that is in the Fixed Header.
  1106. * It is valid for a PUBLISH packet to contain a zero length Payload.
  1107. *
  1108. * https://docs.oasis-open.org/mqtt/mqtt/v5.0/os/mqtt-v5.0-os.html#_Toc3901119
  1109. */
  1110. class application_message
  1111. {
  1112. public:
  1113. using value_type = std::string;
  1114. using view_type = std::string_view;
  1115. application_message() = default;
  1116. application_message(application_message const& o) : data_(o.data_)
  1117. {
  1118. //asio2::detail::disable_sso(data_);
  1119. }
  1120. application_message& operator=(application_message const& o)
  1121. {
  1122. data_ = o.data_;
  1123. //asio2::detail::disable_sso(data_);
  1124. return (*this);
  1125. }
  1126. explicit application_message(const char* const s)
  1127. {
  1128. *this = std::string{ s };
  1129. }
  1130. explicit application_message(std::string s)
  1131. {
  1132. *this = std::move(s);
  1133. }
  1134. explicit application_message(std::string_view s)
  1135. {
  1136. *this = std::move(s);
  1137. }
  1138. inline application_message& operator=(std::string s)
  1139. {
  1140. if (s.size() > std::size_t(max_payload))
  1141. {
  1142. set_last_error(asio::error::invalid_argument);
  1143. return (*this);
  1144. }
  1145. asio2::clear_last_error();
  1146. data_ = std::move(s);
  1147. //asio2::detail::disable_sso(data_);
  1148. return (*this);
  1149. }
  1150. inline application_message& operator=(std::string_view s)
  1151. {
  1152. *this = std::string{ s };
  1153. return (*this);
  1154. }
  1155. inline application_message& operator=(const char* const s)
  1156. {
  1157. *this = std::string{ s };
  1158. return (*this);
  1159. }
  1160. inline bool operator==(const std::string & s) noexcept { return (data_ == s); }
  1161. inline bool operator==(const std::string_view & s) noexcept { return (data_ == s); }
  1162. inline bool operator!=(const std::string & s) noexcept { return (data_ != s); }
  1163. inline bool operator!=(const std::string_view & s) noexcept { return (data_ != s); }
  1164. inline application_message& operator+=(const std::string & s)
  1165. {
  1166. if (data_.size() + s.size() > std::size_t(max_payload))
  1167. {
  1168. set_last_error(asio::error::invalid_argument);
  1169. return (*this);
  1170. }
  1171. asio2::clear_last_error();
  1172. data_ += s;
  1173. //asio2::detail::disable_sso(data_);
  1174. return (*this);
  1175. }
  1176. inline application_message& operator+=(const std::string_view & s)
  1177. {
  1178. if (data_.size() + s.size() > std::size_t(max_payload))
  1179. {
  1180. set_last_error(asio::error::invalid_argument);
  1181. return (*this);
  1182. }
  1183. asio2::clear_last_error();
  1184. data_ += s;
  1185. //asio2::detail::disable_sso(data_);
  1186. return (*this);
  1187. }
  1188. inline application_message& operator+=(const char* const s)
  1189. {
  1190. this->operator+=(std::string_view{ s });
  1191. return (*this);
  1192. }
  1193. #if defined(__cpp_lib_char8_t)
  1194. explicit application_message(const char8_t* const s)
  1195. {
  1196. *this = std::u8string{ s };
  1197. }
  1198. explicit application_message(const std::u8string& s)
  1199. {
  1200. *this = s;
  1201. }
  1202. explicit application_message(const std::u8string_view& s)
  1203. {
  1204. *this = s;
  1205. }
  1206. inline application_message& operator=(const std::u8string& s)
  1207. {
  1208. *this = std::string(s.begin(), s.end());
  1209. return (*this);
  1210. }
  1211. inline application_message& operator=(const std::u8string_view& s)
  1212. {
  1213. *this = std::string(s.begin(), s.end());
  1214. return (*this);
  1215. }
  1216. inline application_message& operator=(const char8_t* const s)
  1217. {
  1218. *this = std::u8string_view{ s };
  1219. return (*this);
  1220. }
  1221. inline bool operator==(const std::u8string & s) noexcept { return (data_ == std::string(s.begin(), s.end())); }
  1222. inline bool operator==(const std::u8string_view & s) noexcept { return (data_ == std::string(s.begin(), s.end())); }
  1223. inline bool operator!=(const std::u8string & s) noexcept { return (data_ != std::string(s.begin(), s.end())); }
  1224. inline bool operator!=(const std::u8string_view & s) noexcept { return (data_ != std::string(s.begin(), s.end())); }
  1225. inline application_message& operator+=(const std::u8string & s)
  1226. {
  1227. *this += std::string{ s.begin(), s.end() };
  1228. return (*this);
  1229. }
  1230. inline application_message& operator+=(const std::u8string_view & s)
  1231. {
  1232. *this += std::string{ s.begin(), s.end() };
  1233. return (*this);
  1234. }
  1235. inline application_message& operator+=(const char8_t* const s)
  1236. {
  1237. *this += std::u8string_view{ s };
  1238. return (*this);
  1239. }
  1240. #endif
  1241. inline std::size_t required_size() const noexcept
  1242. {
  1243. return (data_.size());
  1244. }
  1245. inline std::size_t size() const noexcept
  1246. {
  1247. return (data_.size());
  1248. }
  1249. inline bool empty() const noexcept { return data_.empty(); }
  1250. inline std::string data () const { return data_; }
  1251. inline std::string_view data_view() const { return data_; }
  1252. inline operator std::string () const { return data_; }
  1253. inline operator std::string_view () const { return data_; }
  1254. inline application_message& serialize(std::vector<asio::const_buffer>& buffers)
  1255. {
  1256. buffers.emplace_back(asio::buffer(data_));
  1257. return (*this);
  1258. }
  1259. /*
  1260. * The Container is usually a std::string, std::vector<char>, ...
  1261. */
  1262. template<class Container>
  1263. inline application_message& serialize(Container& buffer)
  1264. {
  1265. buffer.insert(buffer.end(), data_.begin(), data_.end());
  1266. return (*this);
  1267. }
  1268. inline application_message& deserialize(std::string_view& data)
  1269. {
  1270. data_ = data;
  1271. data.remove_prefix(data_.size());
  1272. //asio2::detail::disable_sso(data_);
  1273. return (*this);
  1274. }
  1275. protected:
  1276. std::string data_{};
  1277. };
  1278. /**
  1279. * this class is just used for user application, it is not the part of mqtt protocol.
  1280. */
  1281. template<class derived_t>
  1282. class user_message_attr
  1283. {
  1284. //template <class> friend class asio2::detail::mqtt_handler_t;
  1285. //template <class> friend class asio2::detail::mqtt_invoker_t;
  1286. //template <class> friend class asio2::detail::mqtt_aop_connect;
  1287. //template <class> friend class asio2::detail::mqtt_aop_publish;
  1288. public:
  1289. user_message_attr() = default;
  1290. ~user_message_attr() = default;
  1291. //protected:
  1292. inline derived_t& set_send_flag(bool v) { send_flag_ = v; return (static_cast<derived_t&>(*this)); }
  1293. inline bool get_send_flag( ) const { return send_flag_; }
  1294. protected:
  1295. bool send_flag_ = true;
  1296. };
  1297. /**
  1298. * Fixed header format
  1299. * Each MQTT Control Packet contains a Fixed Header as shown below.
  1300. *
  1301. * http://docs.oasis-open.org/mqtt/mqtt/v3.1.1/os/mqtt-v3.1.1-os.html#_Toc398718020
  1302. * https://docs.oasis-open.org/mqtt/mqtt/v5.0/os/mqtt-v5.0-os.html#_Toc3901021
  1303. */
  1304. template<std::uint8_t Version>
  1305. class fixed_header : public user_message_attr<fixed_header<Version>>
  1306. {
  1307. public:
  1308. union type_and_flags
  1309. {
  1310. one_byte_integer byte{ 0 }; // the whole byte
  1311. #if ASIO2_ENDIAN_BIG_BYTE
  1312. struct
  1313. {
  1314. std::uint8_t type : 4; // MQTT Control Packet type
  1315. bool dup : 1; // Duplicate delivery of a PUBLISH Control Packet
  1316. std::uint8_t qos : 2; // PUBLISH Quality of Service, 0, 1 or 2
  1317. bool retain : 1; // PUBLISH Retain flag
  1318. } bits;
  1319. struct
  1320. {
  1321. std::uint8_t type : 4; // MQTT Control Packet type
  1322. std::uint8_t bit3 : 1;
  1323. std::uint8_t bit2 : 1;
  1324. std::uint8_t bit1 : 1;
  1325. std::uint8_t bit0 : 1;
  1326. } reserved;
  1327. #else
  1328. struct
  1329. {
  1330. bool retain : 1; // PUBLISH Retain flag
  1331. std::uint8_t qos : 2; // PUBLISH Quality of Service, 0, 1 or 2
  1332. bool dup : 1; // Duplicate delivery of a PUBLISH Control Packet
  1333. std::uint8_t type : 4; // MQTT Control Packet type
  1334. } bits;
  1335. struct
  1336. {
  1337. std::uint8_t bit0 : 1;
  1338. std::uint8_t bit1 : 1;
  1339. std::uint8_t bit2 : 1;
  1340. std::uint8_t bit3 : 1;
  1341. std::uint8_t type : 4; // MQTT Control Packet type
  1342. } reserved;
  1343. #endif
  1344. };
  1345. public:
  1346. fixed_header()
  1347. {
  1348. }
  1349. explicit fixed_header(control_packet_type type)
  1350. {
  1351. type_and_flags_.bits.type = asio2::detail::to_underlying(type);
  1352. }
  1353. constexpr std::uint8_t version() const noexcept { return Version; }
  1354. inline std::size_t required_size() const
  1355. {
  1356. return (type_and_flags_.byte.required_size() + remain_length_.required_size());
  1357. }
  1358. /*
  1359. * The Container is usually a std::string, std::vector<char>, ...
  1360. */
  1361. template<class Container>
  1362. inline fixed_header& serialize(Container& buffer)
  1363. {
  1364. type_and_flags_.byte.serialize(buffer);
  1365. remain_length_ .serialize(buffer);
  1366. return (*this);
  1367. }
  1368. inline fixed_header& deserialize(std::string_view& data)
  1369. {
  1370. type_and_flags_.byte.deserialize(data);
  1371. if (asio2::get_last_error())
  1372. return (*this);
  1373. remain_length_ .deserialize(data);
  1374. return (*this);
  1375. }
  1376. inline control_packet_type packet_type () const { return static_cast<control_packet_type>(type_and_flags_.bits.type ); }
  1377. inline control_packet_type message_type () const { return packet_type(); }
  1378. inline std::int32_t remain_length() const { return remain_length_.value(); }
  1379. protected:
  1380. type_and_flags type_and_flags_{ };
  1381. // The Remaining Length is a Variable Byte Integer that represents the number of bytes remaining
  1382. // within the current Control Packet, including data in the Variable Header and the Payload.
  1383. // The Remaining Length does not include the bytes used to encode the Remaining Length.
  1384. // The packet size is the total number of bytes in an MQTT Control Packet, this is equal to the
  1385. // length of the Fixed Header plus the Remaining Length.
  1386. variable_byte_integer remain_length_ { 0 };
  1387. };
  1388. /**
  1389. * Used to init a variant mqtt::message to empty.
  1390. */
  1391. class nullmsg : public fixed_header<asio2::detail::to_underlying(mqtt::version::v4)>
  1392. {
  1393. public:
  1394. nullmsg() : fixed_header(control_packet_type::reserved) {}
  1395. inline nullmsg& update_remain_length() { return (*this); }
  1396. };
  1397. /**
  1398. * https://docs.oasis-open.org/mqtt/mqtt/v5.0/os/mqtt-v5.0-os.html#_Toc3901168
  1399. * https://docs.oasis-open.org/mqtt/mqtt/v5.0/os/mqtt-v5.0-os.html#_Toc3901248
  1400. */
  1401. class subscription
  1402. {
  1403. public:
  1404. subscription() = default;
  1405. /*
  1406. * the "no_local,rap,retain_handling" are only valid in mqtt 5.0
  1407. */
  1408. template<class String, class QosOrInt>
  1409. explicit subscription(String&& topic_filter,
  1410. QosOrInt qos, bool no_local = false, bool rap = false,
  1411. retain_handling_type retain_handling = retain_handling_type::send
  1412. )
  1413. : topic_filter_(std::forward<String>(topic_filter))
  1414. {
  1415. option_.bits.qos = static_cast<std::uint8_t>(qos);
  1416. option_.bits.nl = no_local;
  1417. option_.bits.rap = rap;
  1418. option_.bits.retain_handling = asio2::detail::to_underlying(retain_handling);
  1419. }
  1420. inline std::size_t required_size() const
  1421. {
  1422. return (topic_filter_.required_size() + option_.byte.required_size());
  1423. }
  1424. /*
  1425. * The Container is usually a std::string, std::vector<char>, ...
  1426. */
  1427. template<class Container>
  1428. inline subscription& serialize(Container& buffer)
  1429. {
  1430. topic_filter_.serialize(buffer);
  1431. option_.byte .serialize(buffer);
  1432. return (*this);
  1433. }
  1434. inline subscription& deserialize(std::string_view& data)
  1435. {
  1436. topic_filter_.deserialize(data);
  1437. if (asio2::get_last_error())
  1438. return (*this);
  1439. option_.byte .deserialize(data);
  1440. return (*this);
  1441. }
  1442. inline qos_type qos () const { return static_cast<qos_type>(option_.bits.qos); }
  1443. inline bool no_local () const { return option_.bits.nl ; }
  1444. inline bool rap () const { return option_.bits.rap ; }
  1445. inline retain_handling_type retain_handling() const { return static_cast<retain_handling_type>(option_.bits.retain_handling); }
  1446. inline std::string_view share_name () const
  1447. {
  1448. auto[name, filter] = parse_topic_filter(topic_filter_.data_view());
  1449. std::ignore = filter;
  1450. return name;
  1451. }
  1452. inline std::string_view topic_filter () const
  1453. {
  1454. auto[name, filter] = parse_topic_filter(topic_filter_.data_view());
  1455. std::ignore = name;
  1456. return filter;
  1457. }
  1458. template<class QosOrInt>
  1459. inline subscription& qos (QosOrInt v) { option_.bits.qos = static_cast<std::uint8_t>(v); return (*this); }
  1460. inline subscription& no_local (bool v) { option_.bits.nl = v; return (*this); }
  1461. inline subscription& rap (bool v) { option_.bits.rap = v; return (*this); }
  1462. inline subscription& retain_handling(std::uint8_t v) { option_.bits.retain_handling = v; return (*this); }
  1463. template<class String>
  1464. inline subscription& topic_filter (String&& v) { topic_filter_ = std::forward<String>(v) ; return (*this); }
  1465. inline subscription& retain_handling(retain_handling_type v) { option_.bits.retain_handling = asio2::detail::to_underlying(v); return (*this); }
  1466. protected:
  1467. // The Payload of a SUBSCRIBE packet contains a list of Topic Filters indicating the Topics to
  1468. // which the Client wants to subscribe. The Topic Filters MUST be a UTF-8 Encoded String
  1469. utf8_string topic_filter_{};
  1470. // Each Topic Filter is followed by a Subscription Options byte.
  1471. union
  1472. {
  1473. one_byte_integer byte{ 0 }; // the whole byte
  1474. #if ASIO2_ENDIAN_BIG_BYTE
  1475. struct
  1476. {
  1477. std::uint8_t reserved : 2; // reserved for future use
  1478. std::uint8_t retain_handling : 2; // Retain Handling option, [Only valid for mqtt 5.0]
  1479. bool rap : 1; // Retain As Published option, [Only valid for mqtt 5.0]
  1480. bool nl : 1; // No Local option, [Only valid for mqtt 5.0]
  1481. std::uint8_t qos : 2; // PUBLISH Quality of Service, 0, 1 or 2
  1482. } bits;
  1483. #else
  1484. struct
  1485. {
  1486. std::uint8_t qos : 2; // PUBLISH Quality of Service, 0, 1 or 2
  1487. bool nl : 1; // No Local option, [Only valid for mqtt 5.0]
  1488. bool rap : 1; // Retain As Published option, [Only valid for mqtt 5.0]
  1489. std::uint8_t retain_handling : 2; // Retain Handling option, [Only valid for mqtt 5.0]
  1490. std::uint8_t reserved : 2; // reserved for future use
  1491. } bits;
  1492. #endif
  1493. } option_{ };
  1494. };
  1495. /**
  1496. * https://docs.oasis-open.org/mqtt/mqtt/v5.0/os/mqtt-v5.0-os.html#_Toc3901168
  1497. */
  1498. class subscriptions_set
  1499. {
  1500. protected:
  1501. template<class... Args>
  1502. static constexpr bool _is_subscription() noexcept
  1503. {
  1504. if constexpr (sizeof...(Args) == std::size_t(0))
  1505. return true;
  1506. else
  1507. return ((std::is_same_v<asio2::detail::remove_cvref_t<Args>, mqtt::subscription>) && ...);
  1508. }
  1509. public:
  1510. subscriptions_set()
  1511. {
  1512. }
  1513. template<class... Subscriptions, std::enable_if_t<_is_subscription<Subscriptions...>(), int> = 0>
  1514. explicit subscriptions_set(Subscriptions&&... Subscripts)
  1515. {
  1516. set(std::forward<Subscriptions>(Subscripts)...);
  1517. }
  1518. subscriptions_set(subscriptions_set&&) noexcept = default;
  1519. subscriptions_set(subscriptions_set const&) = default;
  1520. subscriptions_set& operator=(subscriptions_set&&) noexcept = default;
  1521. subscriptions_set& operator=(subscriptions_set const&) = default;
  1522. template<class... Subscriptions, std::enable_if_t<_is_subscription<Subscriptions...>(), int> = 0>
  1523. inline subscriptions_set& set(Subscriptions&&... Subscripts)
  1524. {
  1525. data_.clear();
  1526. (data_.emplace_back(std::forward<Subscriptions>(Subscripts)), ...);
  1527. return (*this);
  1528. }
  1529. template<class... Subscriptions, std::enable_if_t<_is_subscription<Subscriptions...>(), int> = 0>
  1530. inline subscriptions_set& add(Subscriptions&&... Subscripts)
  1531. {
  1532. (data_.emplace_back(std::forward<Subscriptions>(Subscripts)), ...);
  1533. return (*this);
  1534. }
  1535. inline subscriptions_set& erase(std::string_view topic_filter)
  1536. {
  1537. for (auto it = data_.begin(); it != data_.end();)
  1538. {
  1539. if (it->topic_filter() == topic_filter)
  1540. it = data_.erase(it);
  1541. else
  1542. ++it;
  1543. }
  1544. return (*this);
  1545. }
  1546. inline subscriptions_set& clear()
  1547. {
  1548. data_.clear();
  1549. return (*this);
  1550. }
  1551. inline std::size_t required_size() const
  1552. {
  1553. std::size_t r = 0;
  1554. for (auto& v : data_)
  1555. {
  1556. r += v.required_size();
  1557. }
  1558. return r;
  1559. }
  1560. inline std::size_t count() const noexcept
  1561. {
  1562. return data_.size();
  1563. }
  1564. /*
  1565. * The Container is usually a std::string, std::vector<char>, ...
  1566. */
  1567. template<class Container>
  1568. inline subscriptions_set& serialize(Container& buffer)
  1569. {
  1570. for (auto& v : data_)
  1571. {
  1572. v.serialize(buffer);
  1573. }
  1574. return (*this);
  1575. }
  1576. inline subscriptions_set& deserialize(std::string_view& data)
  1577. {
  1578. while (!data.empty())
  1579. {
  1580. subscription s{};
  1581. s.deserialize(data);
  1582. if (asio2::get_last_error())
  1583. return (*this);
  1584. data_.emplace_back(std::move(s));
  1585. }
  1586. return (*this);
  1587. }
  1588. inline std::vector<subscription>& data() { return data_; }
  1589. inline std::vector<subscription> const& data() const { return data_; }
  1590. protected:
  1591. std::vector<subscription> data_{};
  1592. };
  1593. class one_byte_integer_set
  1594. {
  1595. protected:
  1596. template<class... Args>
  1597. static constexpr bool _is_integral() noexcept
  1598. {
  1599. if constexpr (sizeof...(Args) == std::size_t(0))
  1600. return true;
  1601. else
  1602. return ((std::is_integral_v<asio2::detail::remove_cvref_t<Args>>) && ...);
  1603. }
  1604. public:
  1605. one_byte_integer_set()
  1606. {
  1607. }
  1608. template<class... Integers, std::enable_if_t<_is_integral<Integers...>(), int> = 0>
  1609. explicit one_byte_integer_set(Integers... Ints)
  1610. {
  1611. set(Ints...);
  1612. }
  1613. one_byte_integer_set(one_byte_integer_set&&) noexcept = default;
  1614. one_byte_integer_set(one_byte_integer_set const&) = default;
  1615. one_byte_integer_set& operator=(one_byte_integer_set&&) noexcept = default;
  1616. one_byte_integer_set& operator=(one_byte_integer_set const&) = default;
  1617. template<class... Integers, std::enable_if_t<_is_integral<Integers...>(), int> = 0>
  1618. inline one_byte_integer_set& set(Integers... Ints)
  1619. {
  1620. data_.clear();
  1621. (data_.emplace_back(static_cast<one_byte_integer::value_type>(Ints)), ...);
  1622. return (*this);
  1623. }
  1624. template<class... Integers, std::enable_if_t<_is_integral<Integers...>(), int> = 0>
  1625. inline one_byte_integer_set& add(Integers... Ints)
  1626. {
  1627. (data_.emplace_back(static_cast<one_byte_integer::value_type>(Ints)), ...);
  1628. return (*this);
  1629. }
  1630. inline one_byte_integer_set& erase(std::size_t index)
  1631. {
  1632. if (index < data_.size())
  1633. data_.erase(std::next(data_.begin(), index));
  1634. return (*this);
  1635. }
  1636. inline one_byte_integer_set& clear()
  1637. {
  1638. data_.clear();
  1639. return (*this);
  1640. }
  1641. inline std::size_t required_size() const
  1642. {
  1643. return (data_.empty() ? 0 : data_.size() * data_.front().required_size());
  1644. }
  1645. inline std::size_t count() const noexcept
  1646. {
  1647. return data_.size();
  1648. }
  1649. /*
  1650. * The Container is usually a std::string, std::vector<char>, ...
  1651. */
  1652. template<class Container>
  1653. inline one_byte_integer_set& serialize(Container& buffer)
  1654. {
  1655. for (auto& v : data_)
  1656. {
  1657. v.serialize(buffer);
  1658. }
  1659. return (*this);
  1660. }
  1661. inline one_byte_integer_set& deserialize(std::string_view& data)
  1662. {
  1663. while (!data.empty())
  1664. {
  1665. one_byte_integer v{};
  1666. v.deserialize(data);
  1667. if (asio2::get_last_error())
  1668. return (*this);
  1669. data_.emplace_back(std::move(v));
  1670. }
  1671. return (*this);
  1672. }
  1673. inline std::vector<one_byte_integer>& data() { return data_; }
  1674. inline std::vector<one_byte_integer> const& data() const { return data_; }
  1675. inline one_byte_integer::value_type at(std::size_t i) const
  1676. {
  1677. return static_cast<one_byte_integer::value_type>(data_.size() > i ? data_[i].value() : -1);
  1678. }
  1679. protected:
  1680. std::vector<one_byte_integer> data_{};
  1681. };
  1682. class utf8_string_set
  1683. {
  1684. public:
  1685. utf8_string_set()
  1686. {
  1687. }
  1688. template<class... Strings>
  1689. explicit utf8_string_set(Strings&&... Strs)
  1690. {
  1691. set(std::forward<Strings>(Strs)...);
  1692. }
  1693. utf8_string_set(utf8_string_set&&) noexcept = default;
  1694. utf8_string_set(utf8_string_set const&) = default;
  1695. utf8_string_set& operator=(utf8_string_set&&) noexcept = default;
  1696. utf8_string_set& operator=(utf8_string_set const&) = default;
  1697. template<class... Strings>
  1698. inline utf8_string_set& set(Strings&&... Strs)
  1699. {
  1700. data_.clear();
  1701. (data_.emplace_back(std::forward<Strings>(Strs)), ...);
  1702. return (*this);
  1703. }
  1704. template<class... Strings>
  1705. inline utf8_string_set& add(Strings... Strs)
  1706. {
  1707. (data_.emplace_back(std::forward<Strings>(Strs)), ...);
  1708. return (*this);
  1709. }
  1710. inline utf8_string_set& erase(std::string_view s)
  1711. {
  1712. for (auto it = data_.begin(); it != data_.end();)
  1713. {
  1714. if (it->data_view() == s)
  1715. it = data_.erase(it);
  1716. else
  1717. ++it;
  1718. }
  1719. return (*this);
  1720. }
  1721. inline utf8_string_set& clear()
  1722. {
  1723. data_.clear();
  1724. return (*this);
  1725. }
  1726. inline std::size_t required_size() const
  1727. {
  1728. std::size_t r = 0;
  1729. for (auto& v : data_)
  1730. {
  1731. r += v.required_size();
  1732. }
  1733. return r;
  1734. }
  1735. inline std::size_t count() const noexcept
  1736. {
  1737. return data_.size();
  1738. }
  1739. /*
  1740. * The Container is usually a std::string, std::vector<char>, ...
  1741. */
  1742. template<class Container>
  1743. inline utf8_string_set& serialize(Container& buffer)
  1744. {
  1745. for (auto& v : data_)
  1746. {
  1747. v.serialize(buffer);
  1748. }
  1749. return (*this);
  1750. }
  1751. inline utf8_string_set& deserialize(std::string_view& data)
  1752. {
  1753. while (!data.empty())
  1754. {
  1755. utf8_string s{};
  1756. s.deserialize(data);
  1757. if (asio2::get_last_error())
  1758. return (*this);
  1759. data_.emplace_back(std::move(s));
  1760. }
  1761. return (*this);
  1762. }
  1763. inline std::vector<utf8_string>& data() { return data_; }
  1764. inline std::vector<utf8_string> const& data() const { return data_; }
  1765. /**
  1766. * function signature : void(mqtt::utf8_string& str)
  1767. */
  1768. template<class Function>
  1769. inline utf8_string_set& for_each(Function&& f)
  1770. {
  1771. for (auto& v : data_)
  1772. {
  1773. f(v);
  1774. }
  1775. return (*this);
  1776. }
  1777. protected:
  1778. std::vector<utf8_string> data_{};
  1779. };
  1780. namespace
  1781. {
  1782. using iterator = asio::buffers_iterator<asio::streambuf::const_buffers_type>;
  1783. using diff_type = typename iterator::difference_type;
  1784. std::pair<iterator, bool> mqtt_match_role(iterator begin, iterator end)
  1785. {
  1786. for (iterator p = begin; p < end;)
  1787. {
  1788. // Fixed header : at least 2 bytes
  1789. if (end - p < static_cast<diff_type>(2))
  1790. break;
  1791. p += 1;
  1792. // Remaining Length
  1793. std::int32_t remain_length = 0, remain_bytes = 0;
  1794. auto[value, bytes] = decode_variable_byte_integer(std::string_view{ reinterpret_cast<
  1795. std::string_view::const_pointer>(p.operator->()), static_cast<std::size_t>(end - p) });
  1796. if (error_code ec = asio2::get_last_error(); ec)
  1797. {
  1798. // need more data
  1799. if (ec == asio::error::no_buffer_space)
  1800. {
  1801. return std::pair(begin, false);
  1802. }
  1803. // illegal packet
  1804. else
  1805. {
  1806. return std::pair(begin, true);
  1807. }
  1808. }
  1809. remain_length = value;
  1810. remain_bytes = bytes;
  1811. p += remain_bytes;
  1812. // Variable Header, Payload
  1813. if (end - p < static_cast<diff_type>(remain_length))
  1814. break;
  1815. return std::pair(p + static_cast<diff_type>(remain_length), true);
  1816. }
  1817. return std::pair(begin, false);
  1818. }
  1819. }
  1820. // Write the text for a one_byte_integer variable to an output stream.
  1821. inline std::ostream& operator<<(std::ostream& os, one_byte_integer v)
  1822. {
  1823. std::string s;
  1824. v.serialize(s);
  1825. return os << std::move(s);
  1826. }
  1827. // Write the text for a two_byte_integer variable to an output stream.
  1828. inline std::ostream& operator<<(std::ostream& os, two_byte_integer v)
  1829. {
  1830. std::string s;
  1831. v.serialize(s);
  1832. return os << std::move(s);
  1833. }
  1834. // Write the text for a four_byte_integer variable to an output stream.
  1835. inline std::ostream& operator<<(std::ostream& os, four_byte_integer v)
  1836. {
  1837. std::string s;
  1838. v.serialize(s);
  1839. return os << std::move(s);
  1840. }
  1841. // Write the text for a variable_byte_integer variable to an output stream.
  1842. inline std::ostream& operator<<(std::ostream& os, variable_byte_integer v)
  1843. {
  1844. std::string s;
  1845. v.serialize(s);
  1846. return os << std::move(s);
  1847. }
  1848. // Write the text for a utf8_string variable to an output stream.
  1849. inline std::ostream& operator<<(std::ostream& os, utf8_string& v)
  1850. {
  1851. std::string s;
  1852. v.serialize(s);
  1853. return os << std::move(s);
  1854. }
  1855. // Write the text for a binary_data variable to an output stream.
  1856. inline std::ostream& operator<<(std::ostream& os, binary_data& v)
  1857. {
  1858. std::string s;
  1859. v.serialize(s);
  1860. return os << std::move(s);
  1861. }
  1862. // Write the text for a utf8_string_pair variable to an output stream.
  1863. inline std::ostream& operator<<(std::ostream& os, utf8_string_pair& v)
  1864. {
  1865. std::string s;
  1866. v.serialize(s);
  1867. return os << std::move(s);
  1868. }
  1869. // Write the text for a application_message variable to an output stream.
  1870. inline std::ostream& operator<<(std::ostream& os, application_message& v)
  1871. {
  1872. return os << v.data_view();
  1873. }
  1874. }
  1875. #endif // !__ASIO2_MQTT_CORE_HPP__