protocol_v5.hpp 110 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246
  1. /*
  2. * Copyright (c) 2017-2023 zhllxt
  3. *
  4. * author : zhllxt
  5. * email : 37792738@qq.com
  6. *
  7. * chinese : http://mqtt.p2hp.com/mqtt-5-0
  8. * english : https://docs.oasis-open.org/mqtt/mqtt/v5.0/os/mqtt-v5.0-os.html
  9. *
  10. * Distributed under the Boost Software License, Version 1.0. (See accompanying
  11. * file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  12. */
  13. #ifndef __ASIO2_MQTT_PROTOCOL_V5_HPP__
  14. #define __ASIO2_MQTT_PROTOCOL_V5_HPP__
  15. #if defined(_MSC_VER) && (_MSC_VER >= 1200)
  16. #pragma once
  17. #endif // defined(_MSC_VER) && (_MSC_VER >= 1200)
  18. #include <asio2/mqtt/core.hpp>
  19. namespace asio2::mqtt::v5
  20. {
  21. static constexpr std::uint8_t version_number = asio2::detail::to_underlying(mqtt::version::v5);
  22. /**
  23. * Property
  24. *
  25. * A Property consists of an Identifier which defines its usage and data type, followed by a value.
  26. * The Identifier is encoded as a Variable Byte Integer. A Control Packet which contains an Identifier
  27. * which is not valid for its packet type, or contains a value not of the specified data type, is a
  28. * Malformed Packet. If received, use a CONNACK or DISCONNECT packet with Reason Code 0x81 (Malformed Packet)
  29. * as described in section 4.13 Handling errors. There is no significance in the order of Properties with
  30. * different Identifiers.
  31. *
  32. * Although the Property Identifier is defined as a Variable Byte Integer, in this version of the
  33. * specification all of the Property Identifiers are one byte long.
  34. *
  35. * https://docs.oasis-open.org/mqtt/mqtt/v5.0/os/mqtt-v5.0-os.html#_Toc3901027
  36. */
  37. enum class property_type : std::int32_t
  38. {
  39. payload_format_indicator = 1, // Byte
  40. message_expiry_interval = 2, // Four Byte Integer
  41. content_type = 3, // UTF-8 Encoded String
  42. response_topic = 8, // UTF-8 Encoded String
  43. correlation_data = 9, // Binary Data
  44. subscription_identifier = 11, // Variable Byte Integer
  45. session_expiry_interval = 17, // Four Byte Integer
  46. assigned_client_identifier = 18, // UTF-8 Encoded String
  47. server_keep_alive = 19, // Two Byte Integer
  48. authentication_method = 21, // UTF-8 Encoded String
  49. authentication_data = 22, // Binary Data
  50. request_problem_information = 23, // Byte
  51. will_delay_interval = 24, // Four Byte Integer
  52. request_response_information = 25, // Byte
  53. response_information = 26, // UTF-8 Encoded String
  54. server_reference = 28, // UTF-8 Encoded String
  55. reason_string = 31, // UTF-8 Encoded String
  56. receive_maximum = 33, // Two Byte Integer
  57. topic_alias_maximum = 34, // Two Byte Integer
  58. topic_alias = 35, // Two Byte Integer
  59. maximum_qos = 36, // Byte
  60. retain_available = 37, // Byte
  61. user_property = 38, // UTF-8 String Pair
  62. maximum_packet_size = 39, // Four Byte Integer
  63. wildcard_subscription_available = 40, // Byte
  64. subscription_identifier_available = 41, // Byte
  65. shared_subscription_available = 42, // Byte
  66. };
  67. }
  68. namespace asio2::mqtt
  69. {
  70. template<typename = void>
  71. inline constexpr std::string_view to_string(v5::property_type type)
  72. {
  73. using namespace std::string_view_literals;
  74. switch (type)
  75. {
  76. case v5::property_type::payload_format_indicator : return "payload_format_indicator"sv;
  77. case v5::property_type::message_expiry_interval : return "message_expiry_interval"sv;
  78. case v5::property_type::content_type : return "content_type"sv;
  79. case v5::property_type::response_topic : return "response_topic"sv;
  80. case v5::property_type::correlation_data : return "correlation_data"sv;
  81. case v5::property_type::subscription_identifier : return "subscription_identifier"sv;
  82. case v5::property_type::session_expiry_interval : return "session_expiry_interval"sv;
  83. case v5::property_type::assigned_client_identifier : return "assigned_client_identifier"sv;
  84. case v5::property_type::server_keep_alive : return "server_keep_alive"sv;
  85. case v5::property_type::authentication_method : return "authentication_method"sv;
  86. case v5::property_type::authentication_data : return "authentication_data"sv;
  87. case v5::property_type::request_problem_information : return "request_problem_information"sv;
  88. case v5::property_type::will_delay_interval : return "will_delay_interval"sv;
  89. case v5::property_type::request_response_information : return "request_response_information"sv;
  90. case v5::property_type::response_information : return "response_information"sv;
  91. case v5::property_type::server_reference : return "server_reference"sv;
  92. case v5::property_type::reason_string : return "reason_string"sv;
  93. case v5::property_type::receive_maximum : return "receive_maximum"sv;
  94. case v5::property_type::topic_alias_maximum : return "topic_alias_maximum"sv;
  95. case v5::property_type::topic_alias : return "topic_alias"sv;
  96. case v5::property_type::maximum_qos : return "maximum_qos"sv;
  97. case v5::property_type::retain_available : return "retain_available"sv;
  98. case v5::property_type::user_property : return "user_property"sv;
  99. case v5::property_type::maximum_packet_size : return "maximum_packet_size"sv;
  100. case v5::property_type::wildcard_subscription_available : return "wildcard_subscription_available"sv;
  101. case v5::property_type::subscription_identifier_available : return "subscription_identifier_available"sv;
  102. case v5::property_type::shared_subscription_available : return "shared_subscription_available"sv;
  103. }
  104. return ""sv;
  105. };
  106. }
  107. namespace asio2::mqtt::v5
  108. {
  109. // forward declared
  110. template<class> struct property_ops;
  111. // forward declared
  112. class properties_set;
  113. /**
  114. * A Property consists of an Identifier which defines its usage and data type, followed by a value.
  115. * The Identifier is encoded as a Variable Byte Integer.
  116. * A Control Packet which contains an Identifier which is not valid for its packet type, or contains
  117. * a value not of the specified data type, is a Malformed Packet. If received, use a CONNACK or
  118. * DISCONNECT packet with Reason Code 0x81 (Malformed Packet) as described in section 4.13 Handling
  119. * errors. There is no significance in the order of Properties with different Identifiers.
  120. *
  121. * https://docs.oasis-open.org/mqtt/mqtt/v5.0/os/mqtt-v5.0-os.html#_Toc3901029
  122. */
  123. template<property_type T>
  124. struct basic_property
  125. {
  126. template<class> friend struct property_ops; friend class properties_set;
  127. inline variable_byte_integer::value_type id() const { return id_.value(); }
  128. inline constexpr property_type type() const { return T; }
  129. inline constexpr std::string_view name() const { return mqtt::to_string(T); }
  130. protected:
  131. // Property Identifier
  132. // Although the Property Identifier is defined as a Variable Byte Integer, in this version of the
  133. // specification all of the Property Identifiers are one byte long.
  134. variable_byte_integer id_{ asio2::detail::to_underlying(T) };
  135. };
  136. /**
  137. * Payload Format Indicator
  138. * https://docs.oasis-open.org/mqtt/mqtt/v5.0/os/mqtt-v5.0-os.html#_Toc3901063
  139. */
  140. template<class derived_t>
  141. struct property_ops
  142. {
  143. /*
  144. * The Container is usually a std::string, std::vector<char>, ...
  145. */
  146. template<class Container>
  147. inline derived_t& serialize(Container& buffer)
  148. {
  149. derived_t& derive = static_cast<derived_t&>(*this);
  150. derive.id_ .serialize(buffer);
  151. derive.value_.serialize(buffer);
  152. return derive;
  153. }
  154. inline derived_t& deserialize(std::string_view& data)
  155. {
  156. derived_t& derive = static_cast<derived_t&>(*this);
  157. derive.id_ .deserialize(data);
  158. derive.value_.deserialize(data);
  159. return derive;
  160. }
  161. inline std::size_t required_size()
  162. {
  163. derived_t& derive = static_cast<derived_t&>(*this);
  164. return (derive.id_.required_size() + derive.value_.required_size());
  165. }
  166. inline std::size_t required_size() const
  167. {
  168. derived_t const& derive = static_cast<derived_t const&>(*this);
  169. return (derive.id_.required_size() + derive.value_.required_size());
  170. }
  171. };
  172. /////////////////////////////////////////////////////////////////////////////////////////////////////////
  173. // Properties
  174. // https://docs.oasis-open.org/mqtt/mqtt/v5.0/os/mqtt-v5.0-os.html#_Toc3901027
  175. /////////////////////////////////////////////////////////////////////////////////////////////////////////
  176. /**
  177. * Payload Format Indicator
  178. * https://docs.oasis-open.org/mqtt/mqtt/v5.0/os/mqtt-v5.0-os.html#_Toc3901063
  179. */
  180. struct payload_format_indicator
  181. : public basic_property<property_type::payload_format_indicator>
  182. , public property_ops<payload_format_indicator>
  183. {
  184. // template<class> friend struct property_ops; friend class properties_set;
  185. // can't use the code previous line, it will cause compile error under gcc 8.2.0.
  186. friend struct property_ops<payload_format_indicator>; friend class properties_set;
  187. enum class format : std::uint8_t
  188. {
  189. binary,
  190. string
  191. };
  192. payload_format_indicator() = default;
  193. payload_format_indicator(format v) : value_(asio2::detail::to_underlying(v)) {}
  194. /**
  195. * 0 (0x00) Indicates that the Will Message is unspecified bytes.
  196. * 1 (0x01) Indicates that the Will Message is UTF-8 Encoded Character Data.
  197. */
  198. payload_format_indicator(std::uint8_t v) : value_(v) {}
  199. inline one_byte_integer::value_type value() const { return value_.value(); }
  200. protected:
  201. // Followed by the value of the Payload Format Indicator, either of: 0 or 1
  202. one_byte_integer value_{};
  203. };
  204. /**
  205. * Message Expiry Interval
  206. * https://docs.oasis-open.org/mqtt/mqtt/v5.0/os/mqtt-v5.0-os.html#_Toc3901112
  207. */
  208. struct message_expiry_interval
  209. : public basic_property<property_type::message_expiry_interval>
  210. , public property_ops<message_expiry_interval>
  211. {
  212. friend struct property_ops<message_expiry_interval>; friend class properties_set;
  213. message_expiry_interval() = default;
  214. message_expiry_interval(std::uint32_t v) : value_(v) {}
  215. inline four_byte_integer::value_type value() const { return value_.value(); }
  216. protected:
  217. // The Four Byte value is the lifetime of the Application Message in seconds.
  218. four_byte_integer value_{};
  219. };
  220. /**
  221. * Content Type
  222. * https://docs.oasis-open.org/mqtt/mqtt/v5.0/os/mqtt-v5.0-os.html#_Toc3901118
  223. */
  224. struct content_type
  225. : public basic_property<property_type::content_type>
  226. , public property_ops<content_type>
  227. {
  228. friend struct property_ops<content_type>; friend class properties_set;
  229. content_type() = default;
  230. template<class String, std::enable_if_t<asio2::detail::is_character_string_v<String>, int> = 0>
  231. explicit content_type(String&& v) : value_(std::forward<String>(v)) {}
  232. inline utf8_string::view_type value() const { return value_.data_view(); }
  233. protected:
  234. // Followed by a UTF-8 Encoded String describing the content of the Application Message.
  235. // The Content Type MUST be a UTF-8 Encoded String as defined in section 1.5.4 [MQTT-3.3.2-19].
  236. // It is a Protocol Error to include the Content Type more than once.The value of the Content Type
  237. // is defined by the sending and receiving application.
  238. utf8_string value_{};
  239. };
  240. /**
  241. * Response Topic
  242. * https://docs.oasis-open.org/mqtt/mqtt/v5.0/os/mqtt-v5.0-os.html#_Toc3901114
  243. */
  244. struct response_topic
  245. : public basic_property<property_type::response_topic>
  246. , public property_ops<response_topic>
  247. {
  248. friend struct property_ops<response_topic>; friend class properties_set;
  249. response_topic() = default;
  250. template<class String, std::enable_if_t<asio2::detail::is_character_string_v<String>, int> = 0>
  251. response_topic(String&& v) : value_(std::forward<String>(v)) {}
  252. inline utf8_string::view_type value() const { return value_.data_view(); }
  253. protected:
  254. // Followed by a UTF-8 Encoded String which is used as the Topic Name for a response message.
  255. // The Response Topic MUST be a UTF-8 Encoded String as defined in section 1.5.4 [MQTT-3.3.2-13].
  256. // The Response Topic MUST NOT contain wildcard characters [MQTT-3.3.2-14].
  257. // It is a Protocol Error to include the Response Topic more than once.
  258. // The presence of a Response Topic identifies the Message as a Request.
  259. utf8_string value_{};
  260. };
  261. /**
  262. * Correlation Data
  263. * https://docs.oasis-open.org/mqtt/mqtt/v5.0/os/mqtt-v5.0-os.html#_Toc3901115
  264. */
  265. struct correlation_data
  266. : public basic_property<property_type::correlation_data>
  267. , public property_ops<correlation_data>
  268. {
  269. friend struct property_ops<correlation_data>; friend class properties_set;
  270. correlation_data() = default;
  271. template<class String, std::enable_if_t<asio2::detail::is_character_string_v<String>, int> = 0>
  272. correlation_data(String&& v) : value_(std::forward<String>(v)) {}
  273. inline binary_data::view_type value() const { return value_.data_view(); }
  274. protected:
  275. // Followed by Binary Data.
  276. // The Correlation Data is used by the sender of the Request Message to identify which request the
  277. // Response Message is for when it is received.
  278. // It is a Protocol Error to include Correlation Data more than once.
  279. // If the Correlation Data is not present, the Requester does not require any correlation data.
  280. // The Server MUST send the Correlation Data unaltered to all subscribers receiving the Application
  281. // Message[MQTT - 3.3.2 - 16].
  282. // The value of the Correlation Data only has meaning to the sender of the Request Message and
  283. // receiver of the Response Message.
  284. binary_data value_{};
  285. };
  286. /**
  287. * Subscription Identifier
  288. * https://docs.oasis-open.org/mqtt/mqtt/v5.0/os/mqtt-v5.0-os.html#_Toc3901166
  289. */
  290. struct subscription_identifier
  291. : public basic_property<property_type::subscription_identifier>
  292. , public property_ops<subscription_identifier>
  293. {
  294. friend struct property_ops<subscription_identifier>; friend class properties_set;
  295. subscription_identifier() = default;
  296. subscription_identifier(std::int32_t v) : value_(v) {}
  297. inline variable_byte_integer::value_type value() const { return value_.value(); }
  298. protected:
  299. // Followed by a Variable Byte Integer representing the identifier of the subscription.
  300. // The Subscription Identifier can have the value of 1 to 268,435,455.
  301. // It is a Protocol Error if the Subscription Identifier has a value of 0.
  302. // It is a Protocol Error to include the Subscription Identifier more than once.
  303. variable_byte_integer value_{};
  304. };
  305. /**
  306. * Session Expiry Interval
  307. * https://docs.oasis-open.org/mqtt/mqtt/v5.0/os/mqtt-v5.0-os.html#_Toc3901048
  308. */
  309. struct session_expiry_interval
  310. : public basic_property<property_type::session_expiry_interval>
  311. , public property_ops<session_expiry_interval>
  312. {
  313. friend struct property_ops<session_expiry_interval>; friend class properties_set;
  314. session_expiry_interval() = default;
  315. session_expiry_interval(std::uint32_t v) : value_(v) {}
  316. inline four_byte_integer::value_type value() const { return value_.value(); }
  317. protected:
  318. // Followed by the Four Byte Integer representing the Session Expiry Interval in seconds.
  319. // It is a Protocol Error to include the Session Expiry Interval more than once.
  320. four_byte_integer value_{};
  321. };
  322. /**
  323. * Assigned Client Identifier
  324. * https://docs.oasis-open.org/mqtt/mqtt/v5.0/os/mqtt-v5.0-os.html#_Toc3901087
  325. */
  326. struct assigned_client_identifier
  327. : public basic_property<property_type::assigned_client_identifier>
  328. , public property_ops<assigned_client_identifier>
  329. {
  330. friend struct property_ops<assigned_client_identifier>; friend class properties_set;
  331. assigned_client_identifier() = default;
  332. template<class String, std::enable_if_t<asio2::detail::is_character_string_v<String>, int> = 0>
  333. assigned_client_identifier(String&& v) : value_(std::forward<String>(v)) {}
  334. inline utf8_string::view_type value() const { return value_.data_view(); }
  335. protected:
  336. // Followed by the UTF-8 string which is the Assigned Client Identifier.
  337. // It is a Protocol Error to include the Assigned Client Identifier more than once.
  338. utf8_string value_{};
  339. };
  340. /**
  341. * Server Keep Alive
  342. * https://docs.oasis-open.org/mqtt/mqtt/v5.0/os/mqtt-v5.0-os.html#_Toc3901094
  343. */
  344. struct server_keep_alive
  345. : public basic_property<property_type::server_keep_alive>
  346. , public property_ops<server_keep_alive>
  347. {
  348. friend struct property_ops<server_keep_alive>; friend class properties_set;
  349. server_keep_alive() = default;
  350. server_keep_alive(std::uint16_t v) : value_(v) {}
  351. inline two_byte_integer::value_type value() const { return value_.value(); }
  352. protected:
  353. // Followed by a Two Byte Integer with the Keep Alive time assigned by the Server.
  354. // If the Server sends a Server Keep Alive on the CONNACK packet, the Client MUST use this value
  355. // instead of the Keep Alive value the Client sent on CONNECT [MQTT-3.2.2-21].
  356. // If the Server does not send the Server Keep Alive, the Server MUST use the Keep Alive value
  357. // set by the Client on CONNECT [MQTT-3.2.2-22].
  358. // It is a Protocol Error to include the Server Keep Alive more than once.
  359. two_byte_integer value_{};
  360. };
  361. /**
  362. * Authentication Method
  363. * https://docs.oasis-open.org/mqtt/mqtt/v5.0/os/mqtt-v5.0-os.html#_Toc3901055
  364. */
  365. struct authentication_method
  366. : public basic_property<property_type::authentication_method>
  367. , public property_ops<authentication_method>
  368. {
  369. friend struct property_ops<authentication_method>; friend class properties_set;
  370. authentication_method() = default;
  371. template<class String, std::enable_if_t<asio2::detail::is_character_string_v<String>, int> = 0>
  372. authentication_method(String&& v) : value_(std::forward<String>(v)) {}
  373. inline utf8_string::view_type value() const { return value_.data_view(); }
  374. protected:
  375. // Followed by a UTF-8 Encoded String containing the name of the authentication method used
  376. // for extended authentication .
  377. // It is a Protocol Error to include Authentication Method more than once.
  378. // If Authentication Method is absent, extended authentication is not performed.Refer to section 4.12.
  379. utf8_string value_{};
  380. };
  381. /**
  382. * Authentication Data
  383. * https://docs.oasis-open.org/mqtt/mqtt/v5.0/os/mqtt-v5.0-os.html#_Toc3901056
  384. */
  385. struct authentication_data
  386. : public basic_property<property_type::authentication_data>
  387. , public property_ops<authentication_data>
  388. {
  389. friend struct property_ops<authentication_data>; friend class properties_set;
  390. authentication_data() = default;
  391. template<class String, std::enable_if_t<asio2::detail::is_character_string_v<String>, int> = 0>
  392. authentication_data(String&& v) : value_(std::forward<String>(v)) {}
  393. inline binary_data::view_type value() const { return value_.data_view(); }
  394. protected:
  395. // Followed by Binary Data containing authentication data.
  396. // It is a Protocol Error to include Authentication Data if there is no Authentication Method.
  397. // It is a Protocol Error to include Authentication Data more than once.
  398. binary_data value_{};
  399. };
  400. /**
  401. * Request Problem Information
  402. * https://docs.oasis-open.org/mqtt/mqtt/v5.0/os/mqtt-v5.0-os.html#_Toc3901053
  403. */
  404. struct request_problem_information
  405. : public basic_property<property_type::request_problem_information>
  406. , public property_ops<request_problem_information>
  407. {
  408. friend struct property_ops<request_problem_information>; friend class properties_set;
  409. request_problem_information() = default;
  410. request_problem_information(std::uint8_t v) : value_(v) {}
  411. inline one_byte_integer::value_type value() const { return value_.value(); }
  412. protected:
  413. // Followed by a Byte with a value of either 0 or 1.
  414. // It is a Protocol Error to include Request Problem Information more than once, or to have a
  415. // value other than 0 or 1. If the Request Problem Information is absent, the value of 1 is used.
  416. one_byte_integer value_{};
  417. };
  418. /**
  419. * Will Delay Interval
  420. * https://docs.oasis-open.org/mqtt/mqtt/v5.0/os/mqtt-v5.0-os.html#_Toc3901062
  421. */
  422. struct will_delay_interval
  423. : public basic_property<property_type::will_delay_interval>
  424. , public property_ops<will_delay_interval>
  425. {
  426. friend struct property_ops<will_delay_interval>; friend class properties_set;
  427. will_delay_interval() = default;
  428. will_delay_interval(std::uint32_t v) : value_(v) {}
  429. inline four_byte_integer::value_type value() const { return value_.value(); }
  430. protected:
  431. // Followed by the Four Byte Integer representing the Will Delay Interval in seconds.
  432. // It is a Protocol Error to include the Will Delay Interval more than once.
  433. // If the Will Delay Interval is absent, the default value is 0 and there is no delay before the
  434. // Will Message is published.
  435. four_byte_integer value_{};
  436. };
  437. /**
  438. * Request Response Information
  439. * https://docs.oasis-open.org/mqtt/mqtt/v5.0/os/mqtt-v5.0-os.html#_Toc3901052
  440. */
  441. struct request_response_information
  442. : public basic_property<property_type::request_response_information>
  443. , public property_ops<request_response_information>
  444. {
  445. friend struct property_ops<request_response_information>; friend class properties_set;
  446. request_response_information() = default;
  447. request_response_information(std::uint8_t v) : value_(v) {}
  448. inline one_byte_integer::value_type value() const { return value_.value(); }
  449. protected:
  450. // Followed by a Byte with a value of either 0 or 1.
  451. // It is Protocol Error to include the Request Response Information more than once, or to have a
  452. // value other than 0 or 1. If the Request Response Information is absent, the value of 0 is used.
  453. one_byte_integer value_{};
  454. };
  455. /**
  456. * Response Information
  457. * https://docs.oasis-open.org/mqtt/mqtt/v5.0/os/mqtt-v5.0-os.html#_Toc3901095
  458. */
  459. struct response_information
  460. : public basic_property<property_type::response_information>
  461. , public property_ops<response_information>
  462. {
  463. friend struct property_ops<response_information>; friend class properties_set;
  464. response_information() = default;
  465. template<class String, std::enable_if_t<asio2::detail::is_character_string_v<String>, int> = 0>
  466. response_information(String&& v) : value_(std::forward<String>(v)) {}
  467. inline utf8_string::view_type value() const { return value_.data_view(); }
  468. protected:
  469. // Followed by a UTF-8 Encoded String which is used as the basis for creating a Response Topic.
  470. // The way in which the Client creates a Response Topic from the Response Information is not
  471. // defined by this specification. It is a Protocol Error to include the Response Information
  472. // more than once.
  473. utf8_string value_{};
  474. };
  475. /**
  476. * Server Reference
  477. * https://docs.oasis-open.org/mqtt/mqtt/v5.0/os/mqtt-v5.0-os.html#_Toc3901214
  478. */
  479. struct server_reference
  480. : public basic_property<property_type::server_reference>
  481. , public property_ops<server_reference>
  482. {
  483. friend struct property_ops<server_reference>; friend class properties_set;
  484. server_reference() = default;
  485. template<class String, std::enable_if_t<asio2::detail::is_character_string_v<String>, int> = 0>
  486. server_reference(String&& v) : value_(std::forward<String>(v)) {}
  487. inline utf8_string::view_type value() const { return value_.data_view(); }
  488. protected:
  489. // Followed by a UTF-8 Encoded String which can be used by the Client to identify another Server to use.
  490. // It is a Protocol Error to include the Server Reference more than once.
  491. utf8_string value_{};
  492. };
  493. /**
  494. * Reason String
  495. * https://docs.oasis-open.org/mqtt/mqtt/v5.0/os/mqtt-v5.0-os.html#_Toc3901089
  496. */
  497. struct reason_string
  498. : public basic_property<property_type::reason_string>
  499. , public property_ops<reason_string>
  500. {
  501. friend struct property_ops<reason_string>; friend class properties_set;
  502. reason_string() = default;
  503. template<class String, std::enable_if_t<asio2::detail::is_character_string_v<String>, int> = 0>
  504. reason_string(String&& v) : value_(std::forward<String>(v)) {}
  505. inline utf8_string::view_type value() const { return value_.data_view(); }
  506. protected:
  507. // Followed by the UTF-8 Encoded String representing the reason associated with this response.
  508. // This Reason String is a human readable string designed for diagnostics and SHOULD NOT be
  509. // parsed by the Client.
  510. utf8_string value_{};
  511. };
  512. /**
  513. * Receive Maximum
  514. * https://docs.oasis-open.org/mqtt/mqtt/v5.0/os/mqtt-v5.0-os.html#_Toc3901049
  515. */
  516. struct receive_maximum
  517. : public basic_property<property_type::receive_maximum>
  518. , public property_ops<receive_maximum>
  519. {
  520. friend struct property_ops<receive_maximum>; friend class properties_set;
  521. receive_maximum() = default;
  522. receive_maximum(std::uint16_t v) : value_(v) {}
  523. inline two_byte_integer::value_type value() const { return value_.value(); }
  524. protected:
  525. // Followed by the Two Byte Integer representing the Receive Maximum value.
  526. // It is a Protocol Error to include the Receive Maximum value more than once or for it to have the value 0.
  527. two_byte_integer value_{};
  528. };
  529. /**
  530. * https://docs.oasis-open.org/mqtt/mqtt/v5.0/os/mqtt-v5.0-os.html#_Toc3901051
  531. */
  532. struct topic_alias_maximum
  533. : public basic_property<property_type::topic_alias_maximum>
  534. , public property_ops<topic_alias_maximum>
  535. {
  536. friend struct property_ops<topic_alias_maximum>; friend class properties_set;
  537. topic_alias_maximum() = default;
  538. topic_alias_maximum(std::uint16_t v) : value_(v) {}
  539. inline two_byte_integer::value_type value() const { return value_.value(); }
  540. protected:
  541. // Followed by the Two Byte Integer representing the Topic Alias Maximum value.
  542. // It is a Protocol Error to include the Topic Alias Maximum value more than once.
  543. // If the Topic Alias Maximum property is absent, the default value is 0.
  544. two_byte_integer value_{};
  545. };
  546. /**
  547. * https://docs.oasis-open.org/mqtt/mqtt/v5.0/os/mqtt-v5.0-os.html#_Toc3901113
  548. */
  549. struct topic_alias
  550. : public basic_property<property_type::topic_alias>
  551. , public property_ops<topic_alias>
  552. {
  553. friend struct property_ops<topic_alias>; friend class properties_set;
  554. topic_alias() = default;
  555. topic_alias(std::uint16_t v) : value_(v) {}
  556. inline two_byte_integer::value_type value() const { return value_.value(); }
  557. protected:
  558. // Followed by the Two Byte integer representing the Topic Alias value.
  559. // It is a Protocol Error to include the Topic Alias value more than once.
  560. two_byte_integer value_{};
  561. };
  562. /**
  563. * https://docs.oasis-open.org/mqtt/mqtt/v5.0/os/mqtt-v5.0-os.html#_Toc3901084
  564. */
  565. struct maximum_qos
  566. : public basic_property<property_type::maximum_qos>
  567. , public property_ops<maximum_qos>
  568. {
  569. friend struct property_ops<maximum_qos>; friend class properties_set;
  570. maximum_qos() = default;
  571. maximum_qos(std::uint8_t v) : value_(v) {}
  572. inline one_byte_integer::value_type value() const { return value_.value(); }
  573. protected:
  574. // Followed by a Byte with a value of either 0 or 1.
  575. // It is a Protocol Error to include Maximum QoS more than once, or to have a value other than 0 or 1.
  576. // If the Maximum QoS is absent, the Client uses a Maximum QoS of 2.
  577. one_byte_integer value_{};
  578. };
  579. /**
  580. * https://docs.oasis-open.org/mqtt/mqtt/v5.0/os/mqtt-v5.0-os.html#_Toc3901085
  581. */
  582. struct retain_available
  583. : public basic_property<property_type::retain_available>
  584. , public property_ops<retain_available>
  585. {
  586. friend struct property_ops<retain_available>; friend class properties_set;
  587. retain_available() = default;
  588. retain_available(std::uint8_t v) : value_(v) {}
  589. inline one_byte_integer::value_type value() const { return value_.value(); }
  590. protected:
  591. // Followed by a Byte field.
  592. // If present, this byte declares whether the Server supports retained messages.
  593. // A value of 0 means that retained messages are not supported.
  594. // A value of 1 means retained messages are supported.
  595. // If not present, then retained messages are supported.
  596. // It is a Protocol Error to include Retain Available more than once or to use a value other than 0 or 1.
  597. one_byte_integer value_{};
  598. };
  599. /**
  600. * https://docs.oasis-open.org/mqtt/mqtt/v5.0/os/mqtt-v5.0-os.html#_Toc3901054
  601. */
  602. struct user_property
  603. : public basic_property<property_type::user_property>
  604. , public property_ops<user_property>
  605. {
  606. friend struct property_ops<user_property>; friend class properties_set;
  607. user_property() = default;
  608. template<class String1, class String2,
  609. std::enable_if_t<
  610. asio2::detail::is_character_string_v<String1>&&
  611. asio2::detail::is_character_string_v<String2>, int> = 0>
  612. user_property(String1&& k, String2&& v) : value_(std::forward<String1>(k), std::forward<String2>(v)) {}
  613. inline utf8_string_pair::view_type value() const { return { value_.key_view(), value_.val_view() }; }
  614. protected:
  615. // Followed by a UTF-8 String Pair.
  616. utf8_string_pair value_{};
  617. };
  618. /**
  619. * https://docs.oasis-open.org/mqtt/mqtt/v5.0/os/mqtt-v5.0-os.html#_Toc3901050
  620. */
  621. struct maximum_packet_size
  622. : public basic_property<property_type::maximum_packet_size>
  623. , public property_ops<maximum_packet_size>
  624. {
  625. friend struct property_ops<maximum_packet_size>; friend class properties_set;
  626. maximum_packet_size() = default;
  627. maximum_packet_size(std::uint32_t v) : value_(v) {}
  628. inline four_byte_integer::value_type value() const { return value_.value(); }
  629. protected:
  630. // Followed by a Four Byte Integer representing the Maximum Packet Size the Client is willing to accept.
  631. // If the Maximum Packet Size is not present, no limit on the packet size is imposed beyond the
  632. // limitations in the protocol as a result of the remaining length encoding and the protocol header sizes.
  633. four_byte_integer value_{};
  634. };
  635. /**
  636. * https://docs.oasis-open.org/mqtt/mqtt/v5.0/os/mqtt-v5.0-os.html#_Toc3901091
  637. */
  638. struct wildcard_subscription_available
  639. : public basic_property<property_type::wildcard_subscription_available>
  640. , public property_ops<wildcard_subscription_available>
  641. {
  642. friend struct property_ops<wildcard_subscription_available>; friend class properties_set;
  643. wildcard_subscription_available() = default;
  644. wildcard_subscription_available(std::uint8_t v) : value_(v) {}
  645. inline one_byte_integer::value_type value() const { return value_.value(); }
  646. protected:
  647. // Followed by a Byte field.
  648. // If present, this byte declares whether the Server supports Wildcard Subscriptions.
  649. // A value is 0 means that Wildcard Subscriptions are not supported.
  650. // A value of 1 means Wildcard Subscriptions are supported.
  651. // If not present, then Wildcard Subscriptions are supported.
  652. // It is a Protocol Error to include the Wildcard Subscription Available more than once or to
  653. // send a value other than 0 or 1.
  654. one_byte_integer value_{};
  655. };
  656. /**
  657. * https://docs.oasis-open.org/mqtt/mqtt/v5.0/os/mqtt-v5.0-os.html#_Toc3901092
  658. */
  659. struct subscription_identifier_available
  660. : public basic_property<property_type::subscription_identifier_available>
  661. , public property_ops<subscription_identifier_available>
  662. {
  663. friend struct property_ops<subscription_identifier_available>; friend class properties_set;
  664. subscription_identifier_available() = default;
  665. subscription_identifier_available(std::uint8_t v) : value_(v) {}
  666. inline one_byte_integer::value_type value() const { return value_.value(); }
  667. protected:
  668. // Followed by a Byte field.
  669. // If present, this byte declares whether the Server supports Subscription Identifiers.
  670. // A value is 0 means that Subscription Identifiers are not supported.
  671. // A value of 1 means Subscription Identifiers are supported.
  672. // If not present, then Subscription Identifiers are supported.
  673. // It is a Protocol Error to include the Subscription Identifier Available more than once, or to
  674. // send a value other than 0 or 1.
  675. one_byte_integer value_{};
  676. };
  677. /**
  678. * https://docs.oasis-open.org/mqtt/mqtt/v5.0/os/mqtt-v5.0-os.html#_Toc3901093
  679. */
  680. struct shared_subscription_available
  681. : public basic_property<property_type::shared_subscription_available>
  682. , public property_ops<shared_subscription_available>
  683. {
  684. friend struct property_ops<shared_subscription_available>; friend class properties_set;
  685. shared_subscription_available() = default;
  686. shared_subscription_available(std::uint8_t v) : value_(v) {}
  687. inline one_byte_integer::value_type value() const { return value_.value(); }
  688. protected:
  689. // Followed by a Byte field.
  690. // If present, this byte declares whether the Server supports Shared Subscriptions.
  691. // A value is 0 means that Shared Subscriptions are not supported.
  692. // A value of 1 means Shared Subscriptions are supported.
  693. // If not present, then Shared Subscriptions are supported.
  694. // It is a Protocol Error to include the Shared Subscription Available more than once or to
  695. // send a value other than 0 or 1.
  696. one_byte_integer value_{};
  697. };
  698. using property_variant = std::variant<
  699. v5::payload_format_indicator ,
  700. v5::message_expiry_interval ,
  701. v5::content_type ,
  702. v5::response_topic ,
  703. v5::correlation_data ,
  704. v5::subscription_identifier ,
  705. v5::session_expiry_interval ,
  706. v5::assigned_client_identifier ,
  707. v5::server_keep_alive ,
  708. v5::authentication_method ,
  709. v5::authentication_data ,
  710. v5::request_problem_information ,
  711. v5::will_delay_interval ,
  712. v5::request_response_information ,
  713. v5::response_information ,
  714. v5::server_reference ,
  715. v5::reason_string ,
  716. v5::receive_maximum ,
  717. v5::topic_alias_maximum ,
  718. v5::topic_alias ,
  719. v5::maximum_qos ,
  720. v5::retain_available ,
  721. v5::user_property ,
  722. v5::maximum_packet_size ,
  723. v5::wildcard_subscription_available ,
  724. v5::subscription_identifier_available ,
  725. v5::shared_subscription_available
  726. >;
  727. template<typename T>
  728. inline constexpr bool is_v5_property() noexcept
  729. {
  730. using type = asio2::detail::remove_cvref_t<T>;
  731. if constexpr (
  732. std::is_same_v<type, v5::payload_format_indicator > ||
  733. std::is_same_v<type, v5::message_expiry_interval > ||
  734. std::is_same_v<type, v5::content_type > ||
  735. std::is_same_v<type, v5::response_topic > ||
  736. std::is_same_v<type, v5::correlation_data > ||
  737. std::is_same_v<type, v5::subscription_identifier > ||
  738. std::is_same_v<type, v5::session_expiry_interval > ||
  739. std::is_same_v<type, v5::assigned_client_identifier > ||
  740. std::is_same_v<type, v5::server_keep_alive > ||
  741. std::is_same_v<type, v5::authentication_method > ||
  742. std::is_same_v<type, v5::authentication_data > ||
  743. std::is_same_v<type, v5::request_problem_information > ||
  744. std::is_same_v<type, v5::will_delay_interval > ||
  745. std::is_same_v<type, v5::request_response_information > ||
  746. std::is_same_v<type, v5::response_information > ||
  747. std::is_same_v<type, v5::server_reference > ||
  748. std::is_same_v<type, v5::reason_string > ||
  749. std::is_same_v<type, v5::receive_maximum > ||
  750. std::is_same_v<type, v5::topic_alias_maximum > ||
  751. std::is_same_v<type, v5::topic_alias > ||
  752. std::is_same_v<type, v5::maximum_qos > ||
  753. std::is_same_v<type, v5::retain_available > ||
  754. std::is_same_v<type, v5::user_property > ||
  755. std::is_same_v<type, v5::maximum_packet_size > ||
  756. std::is_same_v<type, v5::wildcard_subscription_available > ||
  757. std::is_same_v<type, v5::subscription_identifier_available > ||
  758. std::is_same_v<type, v5::shared_subscription_available >
  759. )
  760. {
  761. return true;
  762. }
  763. else
  764. {
  765. return false;
  766. }
  767. }
  768. class property : public property_variant
  769. {
  770. public:
  771. using super = property_variant;
  772. property()
  773. {
  774. }
  775. template<class T, std::enable_if_t<is_v5_property<T>(), int> = 0>
  776. property(T&& v) : property_variant(std::forward<T>(v))
  777. {
  778. }
  779. template<class T, std::enable_if_t<is_v5_property<T>(), int> = 0>
  780. property& operator=(T&& v)
  781. {
  782. this->base() = std::forward<T>(v);
  783. return (*this);
  784. }
  785. property(property&&) noexcept = default;
  786. property(property const&) = default;
  787. property& operator=(property&&) noexcept = default;
  788. property& operator=(property const&) = default;
  789. template<class T, std::enable_if_t<is_v5_property<T>(), int> = 0>
  790. operator T&()
  791. {
  792. return std::get<T>(this->base());
  793. }
  794. template<class T, std::enable_if_t<is_v5_property<T>(), int> = 0>
  795. operator const T&()
  796. {
  797. return std::get<T>(this->base());
  798. }
  799. template<class T, std::enable_if_t<is_v5_property<T>(), int> = 0>
  800. operator const T&() const
  801. {
  802. return std::get<T>(this->base());
  803. }
  804. template<class T, std::enable_if_t<is_v5_property<T>(), int> = 0>
  805. operator T*() noexcept
  806. {
  807. return std::get_if<T>(std::addressof(this->base()));
  808. }
  809. template<class T, std::enable_if_t<is_v5_property<T>(), int> = 0>
  810. operator const T*() noexcept
  811. {
  812. return std::get_if<T>(std::addressof(this->base()));
  813. }
  814. template<class T, std::enable_if_t<is_v5_property<T>(), int> = 0>
  815. operator const T*() const noexcept
  816. {
  817. return std::get_if<T>(std::addressof(this->base()));
  818. }
  819. // this overload will cause compile error on gcc, see mqtt/message.hpp operator T()
  820. //template<class T, std::enable_if_t<is_v5_property<T>(), int> = 0>
  821. //operator T()
  822. //{
  823. // return std::get<T>(this->base());
  824. //}
  825. inline variable_byte_integer::value_type id() const
  826. {
  827. variable_byte_integer::value_type r = 0;
  828. if (this->base().index() != std::variant_npos)
  829. {
  830. asio2::clear_last_error();
  831. r = std::visit([](auto& prop) mutable { return prop.id(); }, this->base());
  832. }
  833. else
  834. {
  835. asio2::set_last_error(asio::error::no_data);
  836. }
  837. return r;
  838. }
  839. inline property_type type() const
  840. {
  841. property_type r = static_cast<property_type>(0);
  842. if (this->base().index() != std::variant_npos)
  843. {
  844. asio2::clear_last_error();
  845. r = std::visit([](auto& prop) mutable { return prop.type(); }, this->base());
  846. }
  847. else
  848. {
  849. asio2::set_last_error(asio::error::no_data);
  850. }
  851. return r;
  852. }
  853. inline std::string_view name() const
  854. {
  855. std::string_view r{};
  856. if (this->base().index() != std::variant_npos)
  857. {
  858. asio2::clear_last_error();
  859. r = std::visit([](auto& prop) mutable { return prop.name(); }, this->base());
  860. }
  861. else
  862. {
  863. asio2::set_last_error(asio::error::no_data);
  864. }
  865. return r;
  866. }
  867. /// Returns the base variant of the message
  868. inline super const& base() const noexcept { return *this; }
  869. /// Returns the base variant of the message
  870. inline super& base() noexcept { return *this; }
  871. /// Returns the base variant of the message
  872. inline super const& variant() const noexcept { return *this; }
  873. /// Returns the base variant of the message
  874. inline super& variant() noexcept { return *this; }
  875. /**
  876. * @brief Checks if the variant holds anyone of the alternative Types...
  877. */
  878. template<class... Types>
  879. inline bool has() const noexcept
  880. {
  881. return (std::holds_alternative<Types>(this->base()) || ...);
  882. }
  883. /**
  884. * @brief Checks if the variant holds anyone of the alternative Types...
  885. */
  886. template<class... Types>
  887. inline bool holds() const noexcept
  888. {
  889. return (std::holds_alternative<Types>(this->base()) || ...);
  890. }
  891. /**
  892. * @brief If this holds the alternative T, returns a pointer to the value stored in the variant.
  893. * Otherwise, returns a null pointer value.
  894. */
  895. template<class T>
  896. inline std::add_pointer_t<T> get_if() noexcept
  897. {
  898. return std::get_if<T>(std::addressof(this->base()));
  899. }
  900. /**
  901. * @brief If this holds the alternative T, returns a pointer to the value stored in the variant.
  902. * Otherwise, returns a null pointer value.
  903. */
  904. template<class T>
  905. inline std::add_pointer_t<std::add_const_t<T>> get_if() const noexcept
  906. {
  907. return std::get_if<T>(std::addressof(this->base()));
  908. }
  909. /**
  910. * @brief If this holds the alternative T, returns a reference to the value stored in the variant.
  911. * Otherwise, throws std::bad_variant_access.
  912. */
  913. template<class T>
  914. inline T& get()
  915. {
  916. return std::get<T>(this->base());
  917. }
  918. /**
  919. * @brief If this holds the alternative T, returns a reference to the value stored in the variant.
  920. * Otherwise, throws std::bad_variant_access.
  921. */
  922. template<class T>
  923. inline T const& get() const
  924. {
  925. return std::get<T>(this->base());
  926. }
  927. protected:
  928. };
  929. template<class... Args>
  930. static constexpr bool is_property() noexcept
  931. {
  932. if constexpr (sizeof...(Args) == std::size_t(0))
  933. return false;
  934. else
  935. return ((
  936. std::is_same_v<asio2::detail::remove_cvref_t<Args>, v5::property> ||
  937. is_v5_property<asio2::detail::remove_cvref_t<Args>>()) && ...);
  938. }
  939. /**
  940. * The set of Properties is composed of a Property Length followed by the Properties.
  941. *
  942. * https://docs.oasis-open.org/mqtt/mqtt/v5.0/os/mqtt-v5.0-os.html#_Toc3901027
  943. */
  944. class properties_set
  945. {
  946. public:
  947. properties_set() = default;
  948. template<class... Properties, std::enable_if_t<is_property<Properties...>(), int> = 0>
  949. explicit properties_set(Properties&&... Props)
  950. {
  951. set(std::forward<Properties>(Props)...);
  952. }
  953. properties_set(properties_set&&) noexcept = default;
  954. properties_set(properties_set const&) = default;
  955. properties_set& operator=(properties_set&&) noexcept = default;
  956. properties_set& operator=(properties_set const&) = default;
  957. template<class... Properties, std::enable_if_t<is_property<Properties...>(), int> = 0>
  958. inline properties_set& set(Properties&&... Props)
  959. {
  960. data_.clear();
  961. (data_.emplace_back(std::forward<Properties>(Props)), ...);
  962. update_length();
  963. return (*this);
  964. }
  965. template<class... Properties, std::enable_if_t<is_property<Properties...>(), int> = 0>
  966. inline properties_set& add(Properties&&... Props)
  967. {
  968. (data_.emplace_back(std::forward<Properties>(Props)), ...);
  969. update_length();
  970. return (*this);
  971. }
  972. template<class Propertie, std::enable_if_t<is_property<Propertie>(), int> = 0>
  973. inline properties_set& erase(Propertie&& Prop)
  974. {
  975. for (auto it = data_.begin(); it != data_.end();)
  976. {
  977. std::visit([this, &it, &Prop](auto&& prop) mutable
  978. {
  979. asio2::detail::ignore_unused(this, it, Prop);
  980. using T1 = std::decay_t<decltype(Prop)>;
  981. using T2 = std::decay_t<decltype(prop)>;
  982. if constexpr (std::is_same_v<T1, T2>)
  983. it = data_.erase(it);
  984. else
  985. ++it;
  986. }, (*it).base());
  987. }
  988. update_length();
  989. return (*this);
  990. }
  991. template<class Propertie, std::enable_if_t<is_property<Propertie>(), int> = 0>
  992. inline properties_set& erase()
  993. {
  994. for (auto it = data_.begin(); it != data_.end();)
  995. {
  996. std::visit([this, &it](auto&& prop) mutable
  997. {
  998. using T1 = std::decay_t<Propertie>;
  999. using T2 = std::decay_t<decltype(prop)>;
  1000. if constexpr (std::is_same_v<T1, T2>)
  1001. it = data_.erase(it);
  1002. else
  1003. ++it;
  1004. }, (*it).base());
  1005. }
  1006. update_length();
  1007. return (*this);
  1008. }
  1009. inline properties_set& clear() noexcept
  1010. {
  1011. data_.clear();
  1012. update_length();
  1013. return (*this);
  1014. }
  1015. inline std::size_t required_size() const
  1016. {
  1017. return (length_.required_size() + length_.value());
  1018. }
  1019. inline std::size_t count() const noexcept
  1020. {
  1021. return data_.size();
  1022. }
  1023. /**
  1024. * @brief Checks if the properties holds the alternative property T.
  1025. */
  1026. template<class T>
  1027. inline bool has() const noexcept
  1028. {
  1029. for (auto& v : data_)
  1030. {
  1031. if (std::holds_alternative<T>(v))
  1032. return true;
  1033. }
  1034. return false;
  1035. }
  1036. template<class T>
  1037. inline std::add_pointer_t<T> get_if() noexcept
  1038. {
  1039. for (auto& v : data_)
  1040. {
  1041. if (auto pval = std::get_if<T>(std::addressof(v)))
  1042. return pval;
  1043. }
  1044. return nullptr;
  1045. }
  1046. template<class T>
  1047. inline std::add_pointer_t<std::add_const_t<T>> get_if() const noexcept
  1048. {
  1049. for (auto& v : data_)
  1050. {
  1051. if (auto pval = std::get_if<T>(std::addressof(v)))
  1052. return pval;
  1053. }
  1054. return nullptr;
  1055. }
  1056. /*
  1057. * The Container is usually a std::string, std::vector<char>, ...
  1058. */
  1059. template<class Container>
  1060. inline properties_set& serialize(Container& buffer)
  1061. {
  1062. update_length();
  1063. length_.serialize(buffer);
  1064. for (auto& v : data_)
  1065. {
  1066. std::visit([&buffer](auto& prop) mutable { prop.serialize(buffer); }, v.base());
  1067. }
  1068. return (*this);
  1069. }
  1070. inline properties_set& deserialize(std::string_view& data)
  1071. {
  1072. asio2::clear_last_error();
  1073. length_.deserialize(data);
  1074. if (asio2::get_last_error())
  1075. {
  1076. asio2::set_last_error(mqtt::make_error_code(mqtt::error::malformed_packet));
  1077. return (*this);
  1078. }
  1079. std::int32_t length = length_.value();
  1080. std::string_view props_data = data.substr(0, length);
  1081. data.remove_prefix(length);
  1082. while (!props_data.empty())
  1083. {
  1084. variable_byte_integer id{};
  1085. id.deserialize(props_data);
  1086. if (asio2::get_last_error())
  1087. {
  1088. asio2::set_last_error(mqtt::make_error_code(mqtt::error::malformed_packet));
  1089. return (*this);
  1090. }
  1091. // It is a Protocol Error to include the Session Expiry Interval more than once.
  1092. for (auto& prop : data_)
  1093. {
  1094. if (prop.id() == id)
  1095. {
  1096. asio2::set_last_error(mqtt::make_error_code(mqtt::error::protocol_error));
  1097. break;
  1098. }
  1099. }
  1100. switch (static_cast<property_type>(id.value()))
  1101. {
  1102. case property_type::payload_format_indicator : data_.emplace_back(payload_format_indicator {}); break;
  1103. case property_type::message_expiry_interval : data_.emplace_back(message_expiry_interval {}); break;
  1104. case property_type::content_type : data_.emplace_back(content_type {}); break;
  1105. case property_type::response_topic : data_.emplace_back(response_topic {}); break;
  1106. case property_type::correlation_data : data_.emplace_back(correlation_data {}); break;
  1107. case property_type::subscription_identifier : data_.emplace_back(subscription_identifier {}); break;
  1108. case property_type::session_expiry_interval : data_.emplace_back(session_expiry_interval {}); break;
  1109. case property_type::assigned_client_identifier : data_.emplace_back(assigned_client_identifier {}); break;
  1110. case property_type::server_keep_alive : data_.emplace_back(server_keep_alive {}); break;
  1111. case property_type::authentication_method : data_.emplace_back(authentication_method {}); break;
  1112. case property_type::authentication_data : data_.emplace_back(authentication_data {}); break;
  1113. case property_type::request_problem_information : data_.emplace_back(request_problem_information {}); break;
  1114. case property_type::will_delay_interval : data_.emplace_back(will_delay_interval {}); break;
  1115. case property_type::request_response_information : data_.emplace_back(request_response_information {}); break;
  1116. case property_type::response_information : data_.emplace_back(response_information {}); break;
  1117. case property_type::server_reference : data_.emplace_back(server_reference {}); break;
  1118. case property_type::reason_string : data_.emplace_back(reason_string {}); break;
  1119. case property_type::receive_maximum : data_.emplace_back(receive_maximum {}); break;
  1120. case property_type::topic_alias_maximum : data_.emplace_back(topic_alias_maximum {}); break;
  1121. case property_type::topic_alias : data_.emplace_back(topic_alias {}); break;
  1122. case property_type::maximum_qos : data_.emplace_back(maximum_qos {}); break;
  1123. case property_type::retain_available : data_.emplace_back(retain_available {}); break;
  1124. case property_type::user_property : data_.emplace_back(user_property {}); break;
  1125. case property_type::maximum_packet_size : data_.emplace_back(maximum_packet_size {}); break;
  1126. case property_type::wildcard_subscription_available : data_.emplace_back(wildcard_subscription_available {}); break;
  1127. case property_type::subscription_identifier_available : data_.emplace_back(subscription_identifier_available{}); break;
  1128. case property_type::shared_subscription_available : data_.emplace_back(shared_subscription_available {}); break;
  1129. default:
  1130. // A Control Packet which contains an Identifier which is not valid for its packet type,
  1131. // or contains a value not of the specified data type, is a Malformed Packet. If received,
  1132. // use a CONNACK or DISCONNECT packet with Reason Code 0x81 (Malformed Packet) as described
  1133. // in section 4.13 Handling errors.
  1134. asio2::set_last_error(mqtt::make_error_code(mqtt::error::malformed_packet));
  1135. return (*this);
  1136. }
  1137. std::visit([&props_data](auto& prop) mutable
  1138. {
  1139. prop.value_.deserialize(props_data);
  1140. }, data_.back().base());
  1141. // aboved deserialize maybe failed.
  1142. if (asio2::get_last_error())
  1143. {
  1144. asio2::set_last_error(mqtt::make_error_code(mqtt::error::malformed_packet));
  1145. return (*this);
  1146. }
  1147. }
  1148. return (*this);
  1149. }
  1150. inline std::vector<property>& data() { return data_; }
  1151. inline std::vector<property> const& data() const { return data_; }
  1152. inline properties_set& update_length()
  1153. {
  1154. std::size_t size = 0;
  1155. for (auto& v : data_)
  1156. {
  1157. size += std::visit([](auto& prop) mutable { return prop.required_size(); }, v.base());
  1158. }
  1159. length_ = static_cast<std::int32_t>(size);
  1160. return (*this);
  1161. }
  1162. /**
  1163. * function signature : void(auto& prop)
  1164. */
  1165. template<class Function>
  1166. inline properties_set& for_each(Function&& f)
  1167. {
  1168. for (auto& v : data_)
  1169. {
  1170. std::visit([&f](auto& prop) mutable { f(prop); }, v.base());
  1171. }
  1172. return (*this);
  1173. }
  1174. protected:
  1175. // The Property Length is encoded as a Variable Byte Integer.
  1176. // The Property Length does not include the bytes used to encode itself, but includes the
  1177. // length of the Properties.
  1178. // If there are no properties, this MUST be indicated by including a Property Length of zero [MQTT-2.2.2-1].
  1179. variable_byte_integer length_{};
  1180. // propertie list
  1181. std::vector<property> data_ {};
  1182. };
  1183. namespace detail
  1184. {
  1185. template<class derived_t>
  1186. struct reason_string_ops
  1187. {
  1188. inline bool has_reason_string() const
  1189. {
  1190. derived_t& derive = static_cast<derived_t&>(*this);
  1191. return derive.properties().template has<v5::reason_string>();
  1192. }
  1193. inline std::string_view reason_string() const
  1194. {
  1195. derived_t& derive = static_cast<derived_t&>(*this);
  1196. v5::reason_string* prs = derive.properties().template get_if<v5::reason_string>();
  1197. return (prs ? prs->value() : std::string_view{});
  1198. }
  1199. };
  1200. }
  1201. /**
  1202. * CONNECT - Connection Request
  1203. *
  1204. * After a Network Connection is established by a Client to a Server, the first packet sent from the
  1205. * Client to the Server MUST be a CONNECT packet [MQTT-3.1.0-1].
  1206. *
  1207. * A Client can only send the CONNECT packet once over a Network Connection. The Server MUST process
  1208. * a second CONNECT packet sent from a Client as a Protocol Error and close the Network Connection [MQTT-3.1.0-2].
  1209. *
  1210. * https://docs.oasis-open.org/mqtt/mqtt/v5.0/os/mqtt-v5.0-os.html#_Toc3901033
  1211. */
  1212. class connect : public fixed_header<version_number>
  1213. {
  1214. public:
  1215. connect() : fixed_header(control_packet_type::connect)
  1216. {
  1217. update_remain_length();
  1218. }
  1219. connect(utf8_string::value_type clientid) : fixed_header(control_packet_type::connect)
  1220. {
  1221. client_id(std::move(clientid));
  1222. update_remain_length();
  1223. }
  1224. inline std::size_t required_size() const
  1225. {
  1226. return (fixed_header::required_size() + fixed_header::remain_length());
  1227. }
  1228. /*
  1229. * The Container is usually a std::string, std::vector<char>, ...
  1230. */
  1231. template<class Container>
  1232. inline connect& serialize(Container& buffer)
  1233. {
  1234. update_remain_length();
  1235. fixed_header::serialize(buffer);
  1236. protocol_name_ .serialize(buffer);
  1237. protocol_version_ .serialize(buffer);
  1238. connect_flags_.byte .serialize(buffer);
  1239. keep_alive_ .serialize(buffer);
  1240. properties_ .serialize(buffer);
  1241. client_id_ .serialize(buffer);
  1242. if (will_props_ ) will_props_ ->serialize(buffer);
  1243. if (will_topic_ ) will_topic_ ->serialize(buffer);
  1244. if (will_payload_) will_payload_->serialize(buffer);
  1245. if (username_ ) username_ ->serialize(buffer);
  1246. if (password_ ) password_ ->serialize(buffer);
  1247. return (*this);
  1248. }
  1249. inline connect& deserialize(std::string_view& data)
  1250. {
  1251. fixed_header::deserialize(data);
  1252. protocol_name_ .deserialize(data);
  1253. protocol_version_ .deserialize(data);
  1254. connect_flags_.byte .deserialize(data);
  1255. keep_alive_ .deserialize(data);
  1256. properties_ .deserialize(data);
  1257. client_id_ .deserialize(data);
  1258. if (has_will())
  1259. {
  1260. properties_set will_props{};
  1261. will_props.deserialize(data);
  1262. will_props_ = std::move(will_props);
  1263. utf8_string will_topic{};
  1264. will_topic.deserialize(data);
  1265. will_topic_ = std::move(will_topic);
  1266. binary_data will_payload{};
  1267. will_payload.deserialize(data);
  1268. will_payload_ = std::move(will_payload);
  1269. }
  1270. if (has_username())
  1271. {
  1272. utf8_string username{};
  1273. username.deserialize(data);
  1274. username_ = std::move(username);
  1275. }
  1276. if (has_password())
  1277. {
  1278. binary_data password{};
  1279. password.deserialize(data);
  1280. password_ = std::move(password);
  1281. }
  1282. update_remain_length();
  1283. return (*this);
  1284. }
  1285. inline std::uint8_t protocol_version() const { return (protocol_version_ ); }
  1286. inline bool clean_start () const { return (connect_flags_.bits.clean_start ); }
  1287. inline bool clean_session () const { return (connect_flags_.bits.clean_start ); }
  1288. inline bool has_will () const { return (connect_flags_.bits.will_flag ); }
  1289. inline qos_type will_qos () const { return static_cast<qos_type>(connect_flags_.bits.will_qos ); }
  1290. inline bool will_retain () const { return (connect_flags_.bits.will_retain ); }
  1291. inline bool has_password () const { return (connect_flags_.bits.password_flag); }
  1292. inline bool has_username () const { return (connect_flags_.bits.username_flag); }
  1293. inline connect& clean_start (bool v) { connect_flags_.bits.clean_start = v; return (*this); }
  1294. inline connect& clean_session (bool v) { connect_flags_.bits.clean_start = v; return (*this); }
  1295. inline two_byte_integer::value_type keep_alive () const { return keep_alive_ .value() ; }
  1296. inline properties_set& properties () { return properties_ ; }
  1297. inline properties_set const& properties () const { return properties_ ; }
  1298. inline utf8_string::view_type client_id () const { return client_id_ .data_view(); }
  1299. inline properties_set& will_properties () { return will_props_ .value() ; }
  1300. inline properties_set const& will_properties () const { return will_props_ .value() ; }
  1301. inline utf8_string::view_type will_topic () const { return will_topic_ ? will_topic_ ->data_view() : ""; }
  1302. inline binary_data::view_type will_payload () const { return will_payload_ ? will_payload_->data_view() : ""; }
  1303. inline utf8_string::view_type username () const { return username_ ? username_ ->data_view() : ""; }
  1304. inline binary_data::view_type password () const { return password_ ? password_ ->data_view() : ""; }
  1305. inline connect& keep_alive(two_byte_integer::value_type v)
  1306. {
  1307. keep_alive_ = std::move(v);
  1308. return (*this);
  1309. }
  1310. template<class String>
  1311. inline connect& client_id(String&& v)
  1312. {
  1313. client_id_ = std::forward<String>(v);
  1314. update_remain_length();
  1315. return (*this);
  1316. }
  1317. template<class String>
  1318. inline connect& username(String&& v)
  1319. {
  1320. username_ = std::forward<String>(v);
  1321. connect_flags_.bits.username_flag = true;
  1322. update_remain_length();
  1323. return (*this);
  1324. }
  1325. inline connect& password(binary_data::value_type v)
  1326. {
  1327. password_ = std::move(v);
  1328. connect_flags_.bits.password_flag = true;
  1329. update_remain_length();
  1330. return (*this);
  1331. }
  1332. template<class... Properties>
  1333. inline connect& properties(Properties&&... Props)
  1334. {
  1335. properties_.set(std::forward<Properties>(Props)...);
  1336. update_remain_length();
  1337. return (*this);
  1338. }
  1339. template<class String1, class String2, class QosOrInt, class... Properties>
  1340. inline connect& will_attributes(String1&& topic, String2&& payload, QosOrInt qos,
  1341. bool retain, Properties&&... Props)
  1342. {
  1343. will_props_ = properties_set{ std::forward<Properties>(Props)... };
  1344. will_topic_ = std::forward<String1>(topic);
  1345. will_payload_ = std::forward<String2>(payload);
  1346. connect_flags_.bits.will_flag = true;
  1347. connect_flags_.bits.will_qos = static_cast<std::uint8_t>(qos);
  1348. connect_flags_.bits.will_retain = retain;
  1349. update_remain_length();
  1350. return (*this);
  1351. }
  1352. inline bool will_topic_has_value() const noexcept { return will_props_.has_value(); }
  1353. inline bool username_has_value () const noexcept { return username_ .has_value(); }
  1354. inline bool password_has_value () const noexcept { return password_ .has_value(); }
  1355. inline connect& update_remain_length()
  1356. {
  1357. remain_length_ = static_cast<std::int32_t>(0
  1358. + protocol_name_ .required_size()
  1359. + protocol_version_ .required_size()
  1360. + connect_flags_.byte .required_size()
  1361. + keep_alive_ .required_size()
  1362. + properties_ .required_size()
  1363. + client_id_ .required_size()
  1364. + (will_props_ ? will_props_ ->required_size() : 0)
  1365. + (will_topic_ ? will_topic_ ->required_size() : 0)
  1366. + (will_payload_ ? will_payload_->required_size() : 0)
  1367. + (username_ ? username_ ->required_size() : 0)
  1368. + (password_ ? password_ ->required_size() : 0)
  1369. );
  1370. return (*this);
  1371. }
  1372. protected:
  1373. // The Protocol Name is a UTF-8 Encoded String that represents the protocol name "MQTT".
  1374. // The string, its offset and length will not be changed by future versions of the MQTT specification.
  1375. utf8_string protocol_name_{ "MQTT" };
  1376. // The one byte unsigned value that represents the revision level of the protocol used by the Client.
  1377. // The value of the Protocol Version field for version 5.0 of the protocol is 5 (0x05).
  1378. one_byte_integer protocol_version_{ 0x05 };
  1379. union
  1380. {
  1381. one_byte_integer byte{ 0 }; // all connect flags
  1382. #if ASIO2_ENDIAN_BIG_BYTE
  1383. struct
  1384. {
  1385. bool username_flag : 1; // User Name Flag
  1386. bool password_flag : 1; // Password Flag
  1387. bool will_retain : 1; // will retain setting
  1388. std::uint8_t will_qos : 2; // will QoS value
  1389. bool will_flag : 1; // will flag
  1390. bool clean_start : 1; // Clean Start flag
  1391. std::uint8_t reserved : 1; // unused
  1392. } bits;
  1393. #else
  1394. struct
  1395. {
  1396. std::uint8_t reserved : 1; // unused
  1397. bool clean_start : 1; // Clean Start flag
  1398. bool will_flag : 1; // will flag
  1399. std::uint8_t will_qos : 2; // will QoS value
  1400. bool will_retain : 1; // will retain setting
  1401. bool password_flag : 1; // Password Flag
  1402. bool username_flag : 1; // User Name Flag
  1403. } bits;
  1404. #endif
  1405. } connect_flags_{}; // connect flags byte
  1406. // The Keep Alive is a Two Byte Integer which is a time interval measured in seconds.
  1407. // Default to 60 seconds
  1408. two_byte_integer keep_alive_ { 60 };
  1409. // CONNECT Properties
  1410. properties_set properties_ {};
  1411. // The Client Identifier (ClientID) identifies the Client to the Server.
  1412. // Each Client connecting to the Server has a unique ClientID.
  1413. utf8_string client_id_ {};
  1414. // If the Will Flag is set to 1, the Will Properties is the next field in the Payload.
  1415. std::optional<properties_set> will_props_ {};
  1416. // If the Will Flag is set to 1, the Will Topic is the next field in the Payload.
  1417. std::optional<utf8_string> will_topic_ {};
  1418. // If the Will Flag is set to 1 the Will Payload is the next field in the Payload.
  1419. std::optional<binary_data> will_payload_{};
  1420. // If the User Name Flag is set to 1, the User Name is the next field in the Payload.
  1421. std::optional<utf8_string> username_ {};
  1422. // If the Password Flag is set to 1, the Password is the next field in the Payload.
  1423. std::optional<binary_data> password_ {};
  1424. };
  1425. /**
  1426. * CONNACK - Connect acknowledgement
  1427. *
  1428. * The CONNACK packet is the packet sent by the Server in response to a CONNECT packet received from a Client.
  1429. * The Server MUST send a CONNACK with a 0x00 (Success) Reason Code before sending any Packet other than AUTH [MQTT-3.2.0-1].
  1430. * The Server MUST NOT send more than one CONNACK in a Network Connection [MQTT-3.2.0-2].
  1431. * If the Client does not receive a CONNACK packet from the Server within a reasonable amount of time,
  1432. * the Client SHOULD close the Network Connection.
  1433. * A "reasonable" amount of time depends on the type of application and the communications infrastructure.
  1434. *
  1435. * https://docs.oasis-open.org/mqtt/mqtt/v5.0/os/mqtt-v5.0-os.html#_Toc3901074
  1436. */
  1437. class connack : public fixed_header<version_number>, public detail::reason_string_ops<connack>
  1438. {
  1439. public:
  1440. connack() : fixed_header(control_packet_type::connack)
  1441. {
  1442. update_remain_length();
  1443. }
  1444. template<class... Properties, std::enable_if_t<is_property<Properties...>(), int> = 0>
  1445. explicit connack(bool session_present, std::uint8_t reason_code, Properties&&... Props)
  1446. : fixed_header(control_packet_type::connack)
  1447. , reason_code_(reason_code)
  1448. , properties_(std::forward<Properties>(Props)...)
  1449. {
  1450. connack_flags_.bits.session_present = session_present;
  1451. update_remain_length();
  1452. }
  1453. inline std::size_t required_size() const
  1454. {
  1455. return (fixed_header::required_size() + fixed_header::remain_length());
  1456. }
  1457. /*
  1458. * The Container is usually a std::string, std::vector<char>, ...
  1459. */
  1460. template<class Container>
  1461. inline connack& serialize(Container& buffer)
  1462. {
  1463. update_remain_length();
  1464. fixed_header::serialize(buffer);
  1465. connack_flags_.byte .serialize(buffer);
  1466. reason_code_ .serialize(buffer);
  1467. properties_ .serialize(buffer);
  1468. return (*this);
  1469. }
  1470. inline connack& deserialize(std::string_view& data)
  1471. {
  1472. fixed_header::deserialize(data);
  1473. connack_flags_.byte .deserialize(data);
  1474. reason_code_ .deserialize(data);
  1475. properties_ .deserialize(data);
  1476. update_remain_length();
  1477. return (*this);
  1478. }
  1479. inline bool session_present() const { return connack_flags_.bits.session_present; }
  1480. inline mqtt::error reason_code () const { return static_cast<mqtt::error>(reason_code_.value()); }
  1481. inline properties_set& properties () { return properties_ ; }
  1482. inline properties_set const& properties() const { return properties_ ; }
  1483. inline connack & session_present(bool v) { connack_flags_.bits.session_present = v; return (*this); }
  1484. inline connack & reason_code (std::uint8_t v) { reason_code_ = v; return (*this); }
  1485. inline connack & reason_code (mqtt::error v)
  1486. { reason_code_ = asio2::detail::to_underlying(v); return (*this); }
  1487. template<class... Properties>
  1488. inline connack & properties(Properties&&... Props)
  1489. {
  1490. properties_.set(std::forward<Properties>(Props)...);
  1491. update_remain_length();
  1492. return (*this);
  1493. }
  1494. inline connack& update_remain_length()
  1495. {
  1496. remain_length_ = static_cast<std::int32_t>(0
  1497. + connack_flags_.byte .required_size()
  1498. + reason_code_ .required_size()
  1499. + properties_ .required_size()
  1500. );
  1501. return (*this);
  1502. }
  1503. protected:
  1504. union
  1505. {
  1506. one_byte_integer byte{ 0 }; // all connack flags
  1507. #if ASIO2_ENDIAN_BIG_BYTE
  1508. struct
  1509. {
  1510. std::uint8_t reserved : 7;
  1511. bool session_present : 1; // session found on the server?
  1512. } bits;
  1513. #else
  1514. struct
  1515. {
  1516. bool session_present : 1; // session found on the server?
  1517. std::uint8_t reserved : 7;
  1518. } bits;
  1519. #endif
  1520. } connack_flags_{}; // connack flags
  1521. // Byte 2 in the Variable Header is the Connect Reason Code.
  1522. one_byte_integer reason_code_{ 0 };
  1523. // CONNACK Properties
  1524. properties_set properties_ { };
  1525. };
  1526. /**
  1527. * PUBLISH - Publish message
  1528. *
  1529. * A PUBLISH packet is sent from a Client to a Server or from a Server to a Client to transport an Application Message.
  1530. *
  1531. * https://docs.oasis-open.org/mqtt/mqtt/v5.0/os/mqtt-v5.0-os.html#_Toc3901100
  1532. */
  1533. class publish : public fixed_header<version_number>
  1534. {
  1535. public:
  1536. publish() : fixed_header(control_packet_type::publish)
  1537. {
  1538. update_remain_length();
  1539. }
  1540. template<class String1, class String2, class QosOrInt, std::enable_if_t<
  1541. asio2::detail::is_character_string_v<String1>, int> = 0>
  1542. explicit publish(String1&& topic_name, String2&& payload, QosOrInt qos,
  1543. bool dup = false, bool retain = false)
  1544. : fixed_header(control_packet_type::publish)
  1545. , topic_name_ (std::forward<String1>(topic_name))
  1546. , payload_ (std::forward<String2>(payload ))
  1547. {
  1548. type_and_flags_.bits.dup = dup;
  1549. type_and_flags_.bits.qos = static_cast<std::uint8_t>(qos);
  1550. type_and_flags_.bits.retain = retain;
  1551. update_remain_length();
  1552. }
  1553. template<class String1, class String2, class QosOrInt, std::enable_if_t<
  1554. asio2::detail::is_character_string_v<String1>, int> = 0>
  1555. explicit publish(std::uint16_t pid, String1&& topic_name, String2&& payload, QosOrInt qos,
  1556. bool dup = false, bool retain = false)
  1557. : fixed_header(control_packet_type::publish)
  1558. , topic_name_ (std::forward<String1>(topic_name))
  1559. , packet_id_ (pid)
  1560. , payload_ (std::forward<String2>(payload ))
  1561. {
  1562. type_and_flags_.bits.dup = dup;
  1563. type_and_flags_.bits.qos = static_cast<std::uint8_t>(qos);
  1564. type_and_flags_.bits.retain = retain;
  1565. ASIO2_ASSERT(type_and_flags_.bits.qos > std::uint8_t(0));
  1566. update_remain_length();
  1567. }
  1568. inline std::size_t required_size() const
  1569. {
  1570. return (fixed_header::required_size() + fixed_header::remain_length());
  1571. }
  1572. /*
  1573. * The Container is usually a std::string, std::vector<char>, ...
  1574. */
  1575. template<class Container>
  1576. inline publish& serialize(Container& buffer)
  1577. {
  1578. update_remain_length();
  1579. fixed_header::serialize(buffer);
  1580. // The Packet Identifier field is only present in PUBLISH packets where the QoS level is 1 or 2.
  1581. // A PUBLISH packet MUST NOT contain a Packet Identifier if its QoS value is set to 0
  1582. if ((type_and_flags_.bits.qos == std::uint8_t(0) && packet_id_.has_value()) ||
  1583. (type_and_flags_.bits.qos > std::uint8_t(0) && !packet_id_.has_value()))
  1584. {
  1585. ASIO2_ASSERT(false);
  1586. asio2::set_last_error(mqtt::make_error_code(mqtt::error::malformed_packet));
  1587. }
  1588. topic_name_.serialize(buffer);
  1589. if (type_and_flags_.bits.qos > std::uint8_t(0) && packet_id_.has_value())
  1590. {
  1591. packet_id_->serialize(buffer);
  1592. }
  1593. properties_.serialize(buffer);
  1594. payload_ .serialize(buffer);
  1595. return (*this);
  1596. }
  1597. inline publish& deserialize(std::string_view& data)
  1598. {
  1599. fixed_header::deserialize(data);
  1600. topic_name_.deserialize(data);
  1601. if (type_and_flags_.bits.qos == 1 || type_and_flags_.bits.qos == 2)
  1602. {
  1603. two_byte_integer packet_id{};
  1604. packet_id.deserialize(data);
  1605. packet_id_ = packet_id;
  1606. }
  1607. properties_.deserialize(data);
  1608. payload_ .deserialize(data);
  1609. update_remain_length();
  1610. return (*this);
  1611. }
  1612. inline bool dup () const { return (type_and_flags_.bits.dup ); }
  1613. inline qos_type qos () const { return static_cast<qos_type>(type_and_flags_.bits.qos ); }
  1614. inline bool retain() const { return (type_and_flags_.bits.retain); }
  1615. inline publish & dup (bool v) { type_and_flags_.bits.dup = v; return (*this); }
  1616. template<class QosOrInt>
  1617. inline publish & qos (QosOrInt v) { type_and_flags_.bits.qos = static_cast<std::uint8_t>(v); return (*this); }
  1618. inline publish & retain(bool v) { type_and_flags_.bits.retain = v; return (*this); }
  1619. inline utf8_string::view_type topic_name() const { return topic_name_.data_view(); }
  1620. inline two_byte_integer::value_type packet_id () const { return packet_id_ ? packet_id_->value() : 0; }
  1621. inline properties_set& properties() { return properties_ ; }
  1622. inline properties_set const& properties() const { return properties_ ; }
  1623. inline application_message::view_type payload () const { return payload_.data_view() ; }
  1624. inline publish & packet_id (std::uint16_t v) { packet_id_ = v ; update_remain_length(); return (*this); }
  1625. template<class String>
  1626. inline publish & topic_name(String&& v) { topic_name_ = std::forward<String>(v); update_remain_length(); return (*this); }
  1627. template<class String>
  1628. inline publish & payload (String&& v) { payload_ = std::forward<String>(v); update_remain_length(); return (*this); }
  1629. template<class... Properties>
  1630. inline publish & properties(Properties&&... Props)
  1631. {
  1632. properties_.set(std::forward<Properties>(Props)...);
  1633. update_remain_length();
  1634. return (*this);
  1635. }
  1636. inline bool has_packet_id() const noexcept { return packet_id_.has_value(); }
  1637. inline publish& update_remain_length()
  1638. {
  1639. remain_length_ = static_cast<std::int32_t>(0
  1640. + topic_name_.required_size()
  1641. + (packet_id_ ? packet_id_->required_size() : 0)
  1642. + properties_.required_size()
  1643. + payload_ .required_size()
  1644. );
  1645. return (*this);
  1646. }
  1647. protected:
  1648. // The Topic Name identifies the information channel to which Payload data is published.
  1649. utf8_string topic_name_{};
  1650. // The Packet Identifier field is only present in PUBLISH packets where the QoS level is 1 or 2.
  1651. // a Two Byte Integer Packet Identifier.
  1652. std::optional<two_byte_integer> packet_id_ {};
  1653. // PUBLISH Properties
  1654. properties_set properties_{};
  1655. // The Payload contains the Application Message that is being published.
  1656. // The content and format of the data is application specific.
  1657. // The length of the Payload can be calculated by subtracting the length of the Variable Header
  1658. // from the Remaining Length field that is in the Fixed Header.
  1659. // It is valid for a PUBLISH packet to contain a zero length Payload.
  1660. application_message payload_ {};
  1661. };
  1662. /**
  1663. * PUBACK - Publish acknowledgement
  1664. *
  1665. * A PUBACK packet is the response to a PUBLISH packet with QoS 1.
  1666. *
  1667. * https://docs.oasis-open.org/mqtt/mqtt/v5.0/os/mqtt-v5.0-os.html#_Toc3901121
  1668. */
  1669. class puback : public fixed_header<version_number>, public detail::reason_string_ops<puback>
  1670. {
  1671. public:
  1672. puback() : fixed_header(control_packet_type::puback)
  1673. {
  1674. update_remain_length();
  1675. }
  1676. template<class... Properties, std::enable_if_t<is_property<Properties...>(), int> = 0>
  1677. explicit puback(std::uint16_t packet_id, std::uint8_t reason_code, Properties&&... Props)
  1678. : fixed_header(control_packet_type::puback)
  1679. , packet_id_ (packet_id)
  1680. , reason_code_(reason_code)
  1681. , properties_ (std::forward<Properties>(Props)...)
  1682. {
  1683. update_remain_length();
  1684. }
  1685. inline std::size_t required_size() const
  1686. {
  1687. return (fixed_header::required_size() + fixed_header::remain_length());
  1688. }
  1689. /*
  1690. * The Container is usually a std::string, std::vector<char>, ...
  1691. */
  1692. template<class Container>
  1693. inline puback& serialize(Container& buffer)
  1694. {
  1695. update_remain_length();
  1696. fixed_header::serialize(buffer);
  1697. packet_id_ .serialize(buffer);
  1698. reason_code_.serialize(buffer);
  1699. properties_ .serialize(buffer);
  1700. return (*this);
  1701. }
  1702. inline puback& deserialize(std::string_view& data)
  1703. {
  1704. fixed_header::deserialize(data);
  1705. // The Reason Code and Property Length can be omitted if the Reason Code is 0x00 (Success)
  1706. // and there are no Properties. In this case the PUBACK has a Remaining Length of 2.
  1707. packet_id_ .deserialize(data);
  1708. if (!data.empty())
  1709. {
  1710. reason_code_.deserialize(data);
  1711. // If the Remaining Length is less than 4 there is no Property Length and the value of 0 is used.
  1712. if (!data.empty())
  1713. properties_.deserialize(data);
  1714. }
  1715. update_remain_length();
  1716. return (*this);
  1717. }
  1718. inline two_byte_integer::value_type packet_id () const { return packet_id_ .value() ; }
  1719. inline std::uint8_t reason_code() const { return reason_code_.value() ; }
  1720. inline properties_set& properties () { return properties_ ; }
  1721. inline properties_set const& properties () const { return properties_ ; }
  1722. inline puback & packet_id (std::uint16_t v) { packet_id_ = v; return (*this); }
  1723. inline puback & reason_code(std::uint8_t v) { reason_code_ = v; return (*this); }
  1724. template<class... Properties>
  1725. inline puback & properties (Properties&&... Props)
  1726. {
  1727. properties_.set(std::forward<Properties>(Props)...);
  1728. update_remain_length();
  1729. return (*this);
  1730. }
  1731. inline puback& update_remain_length()
  1732. {
  1733. remain_length_ = static_cast<std::int32_t>(0
  1734. + packet_id_ .required_size()
  1735. + reason_code_.required_size()
  1736. + properties_ .required_size()
  1737. );
  1738. return (*this);
  1739. }
  1740. protected:
  1741. // The Variable Header of the PUBACK Packet contains the following fields in the order:
  1742. // Packet Identifier from the PUBLISH packet that is being acknowledged, PUBACK Reason Code,
  1743. // Property Length, and the Properties. The rules for encoding Properties are described in section 2.2.2.
  1744. two_byte_integer packet_id_ {};
  1745. // Byte 3 in the Variable Header is the PUBACK Reason Code.
  1746. // If the Remaining Length is 2, then there is no Reason Code and the value of 0x00 (Success) is used.
  1747. one_byte_integer reason_code_{ 0 };
  1748. // PUBACK Properties
  1749. properties_set properties_{};
  1750. };
  1751. /**
  1752. * PUBREC - Publish received (QoS 2 delivery part 1)
  1753. *
  1754. * A PUBREC packet is the response to a PUBLISH packet with QoS 2.
  1755. * It is the second packet of the QoS 2 protocol exchange.
  1756. *
  1757. * https://docs.oasis-open.org/mqtt/mqtt/v5.0/os/mqtt-v5.0-os.html#_Toc3901131
  1758. */
  1759. class pubrec : public fixed_header<version_number>, public detail::reason_string_ops<pubrec>
  1760. {
  1761. public:
  1762. pubrec() : fixed_header(control_packet_type::pubrec)
  1763. {
  1764. update_remain_length();
  1765. }
  1766. template<class... Properties, std::enable_if_t<is_property<Properties...>(), int> = 0>
  1767. explicit pubrec(std::uint16_t packet_id, std::uint8_t reason_code, Properties&&... Props)
  1768. : fixed_header(control_packet_type::pubrec)
  1769. , packet_id_ (packet_id)
  1770. , reason_code_(reason_code)
  1771. , properties_ (std::forward<Properties>(Props)...)
  1772. {
  1773. update_remain_length();
  1774. }
  1775. inline std::size_t required_size() const
  1776. {
  1777. return (fixed_header::required_size() + fixed_header::remain_length());
  1778. }
  1779. /*
  1780. * The Container is usually a std::string, std::vector<char>, ...
  1781. */
  1782. template<class Container>
  1783. inline pubrec& serialize(Container& buffer)
  1784. {
  1785. update_remain_length();
  1786. fixed_header::serialize(buffer);
  1787. packet_id_ .serialize(buffer);
  1788. reason_code_.serialize(buffer);
  1789. properties_ .serialize(buffer);
  1790. return (*this);
  1791. }
  1792. inline pubrec& deserialize(std::string_view& data)
  1793. {
  1794. fixed_header::deserialize(data);
  1795. // The Reason Code and Property Length can be omitted if the Reason Code is 0x00 (Success)
  1796. // and there are no Properties. In this case the PUBREC has a Remaining Length of 2.
  1797. packet_id_ .deserialize(data);
  1798. if (!data.empty())
  1799. {
  1800. reason_code_.deserialize(data);
  1801. // If the Remaining Length is less than 4 there is no Property Length and the value of 0 is used.
  1802. if (!data.empty())
  1803. properties_.deserialize(data);
  1804. }
  1805. update_remain_length();
  1806. return (*this);
  1807. }
  1808. inline two_byte_integer::value_type packet_id () const { return packet_id_ .value() ; }
  1809. inline std::uint8_t reason_code() const { return reason_code_.value() ; }
  1810. inline properties_set& properties () { return properties_ ; }
  1811. inline properties_set const& properties () const { return properties_ ; }
  1812. inline pubrec & packet_id (std::uint16_t v) { packet_id_ = v; return (*this); }
  1813. inline pubrec & reason_code(std::uint8_t v) { reason_code_ = v; return (*this); }
  1814. template<class... Properties>
  1815. inline pubrec & properties (Properties&&... Props)
  1816. {
  1817. properties_.set(std::forward<Properties>(Props)...);
  1818. update_remain_length();
  1819. return (*this);
  1820. }
  1821. inline pubrec& update_remain_length()
  1822. {
  1823. remain_length_ = static_cast<std::int32_t>(0
  1824. + packet_id_ .required_size()
  1825. + reason_code_.required_size()
  1826. + properties_ .required_size()
  1827. );
  1828. return (*this);
  1829. }
  1830. protected:
  1831. // The Variable Header of the PUBREC Packet consists of the following fields in the order:
  1832. // the Packet Identifier from the PUBLISH packet that is being acknowledged, PUBREC Reason Code,
  1833. // and Properties. The rules for encoding Properties are described in section 2.2.2.
  1834. two_byte_integer packet_id_ {};
  1835. // Byte 3 in the Variable Header is the PUBREC Reason Code.
  1836. // If the Remaining Length is 2, then the Publish Reason Code has the value 0x00 (Success).
  1837. one_byte_integer reason_code_{ 0 };
  1838. // PUBREC Properties
  1839. properties_set properties_{};
  1840. };
  1841. /**
  1842. * PUBREL - Publish release (QoS 2 delivery part 2)
  1843. *
  1844. * A PUBREL packet is the response to a PUBREC packet.
  1845. * It is the third packet of the QoS 2 protocol exchange.
  1846. *
  1847. * https://docs.oasis-open.org/mqtt/mqtt/v5.0/os/mqtt-v5.0-os.html#_Toc3901141
  1848. */
  1849. class pubrel : public fixed_header<version_number>, public detail::reason_string_ops<pubrel>
  1850. {
  1851. public:
  1852. pubrel() : fixed_header(control_packet_type::pubrel)
  1853. {
  1854. // Bits 3,2,1 and 0 of the Fixed Header in the PUBREL packet are reserved and MUST be
  1855. // set to 0,0,1 and 0 respectively.
  1856. // The Server MUST treat any other value as malformed and close the Network Connection [MQTT-3.6.1-1].
  1857. type_and_flags_.reserved.bit1 = 1;
  1858. update_remain_length();
  1859. }
  1860. template<class... Properties, std::enable_if_t<is_property<Properties...>(), int> = 0>
  1861. explicit pubrel(std::uint16_t packet_id, std::uint8_t reason_code, Properties&&... Props)
  1862. : fixed_header(control_packet_type::pubrel)
  1863. , packet_id_ (packet_id)
  1864. , reason_code_(reason_code)
  1865. , properties_ (std::forward<Properties>(Props)...)
  1866. {
  1867. type_and_flags_.reserved.bit1 = 1;
  1868. update_remain_length();
  1869. }
  1870. inline std::size_t required_size() const
  1871. {
  1872. return (fixed_header::required_size() + fixed_header::remain_length());
  1873. }
  1874. /*
  1875. * The Container is usually a std::string, std::vector<char>, ...
  1876. */
  1877. template<class Container>
  1878. inline pubrel& serialize(Container& buffer)
  1879. {
  1880. update_remain_length();
  1881. fixed_header::serialize(buffer);
  1882. packet_id_ .serialize(buffer);
  1883. reason_code_.serialize(buffer);
  1884. properties_ .serialize(buffer);
  1885. return (*this);
  1886. }
  1887. inline pubrel& deserialize(std::string_view& data)
  1888. {
  1889. fixed_header::deserialize(data);
  1890. // The Reason Code and Property Length can be omitted if the Reason Code is 0x00 (Success)
  1891. // and there are no Properties. In this case the PUBREL has a Remaining Length of 2.
  1892. packet_id_ .deserialize(data);
  1893. if (!data.empty())
  1894. {
  1895. reason_code_.deserialize(data);
  1896. // If the Remaining Length is less than 4 there is no Property Length and the value of 0 is used.
  1897. if (!data.empty())
  1898. properties_.deserialize(data);
  1899. }
  1900. update_remain_length();
  1901. return (*this);
  1902. }
  1903. inline two_byte_integer::value_type packet_id () const { return packet_id_ .value() ; }
  1904. inline std::uint8_t reason_code() const { return reason_code_.value() ; }
  1905. inline properties_set& properties () { return properties_ ; }
  1906. inline properties_set const& properties () const { return properties_ ; }
  1907. inline pubrel & packet_id (std::uint16_t v) { packet_id_ = v; return (*this); }
  1908. inline pubrel & reason_code(std::uint8_t v) { reason_code_ = v; return (*this); }
  1909. template<class... Properties>
  1910. inline pubrel & properties (Properties&&... Props)
  1911. {
  1912. properties_.set(std::forward<Properties>(Props)...);
  1913. update_remain_length();
  1914. return (*this);
  1915. }
  1916. inline pubrel& update_remain_length()
  1917. {
  1918. remain_length_ = static_cast<std::int32_t>(0
  1919. + packet_id_ .required_size()
  1920. + reason_code_.required_size()
  1921. + properties_ .required_size()
  1922. );
  1923. return (*this);
  1924. }
  1925. protected:
  1926. // The Variable Header of the PUBREL Packet contains the following fields in the order:
  1927. // the Packet Identifier from the PUBREC packet that is being acknowledged, PUBREL Reason Code,
  1928. // and Properties. The rules for encoding Properties are described in section 2.2.2.
  1929. two_byte_integer packet_id_ {};
  1930. // Byte 3 in the Variable Header is the PUBREL Reason Code.
  1931. // If the Remaining Length is 2, the value of 0x00 (Success) is used.
  1932. one_byte_integer reason_code_{ 0 };
  1933. // PUBREL Properties
  1934. properties_set properties_{};
  1935. };
  1936. /**
  1937. * PUBCOMP - Publish complete (QoS 2 delivery part 3)
  1938. *
  1939. * The PUBCOMP packet is the response to a PUBREL packet.
  1940. * It is the fourth and final packet of the QoS 2 protocol exchange.
  1941. *
  1942. * https://docs.oasis-open.org/mqtt/mqtt/v5.0/os/mqtt-v5.0-os.html#_Toc3901151
  1943. */
  1944. class pubcomp : public fixed_header<version_number>, public detail::reason_string_ops<pubcomp>
  1945. {
  1946. public:
  1947. pubcomp() : fixed_header(control_packet_type::pubcomp)
  1948. {
  1949. update_remain_length();
  1950. }
  1951. template<class... Properties, std::enable_if_t<is_property<Properties...>(), int> = 0>
  1952. explicit pubcomp(std::uint16_t packet_id, std::uint8_t reason_code, Properties&&... Props)
  1953. : fixed_header(control_packet_type::pubcomp)
  1954. , packet_id_ (packet_id)
  1955. , reason_code_(reason_code)
  1956. , properties_ (std::forward<Properties>(Props)...)
  1957. {
  1958. update_remain_length();
  1959. }
  1960. inline std::size_t required_size() const
  1961. {
  1962. return (fixed_header::required_size() + fixed_header::remain_length());
  1963. }
  1964. /*
  1965. * The Container is usually a std::string, std::vector<char>, ...
  1966. */
  1967. template<class Container>
  1968. inline pubcomp& serialize(Container& buffer)
  1969. {
  1970. update_remain_length();
  1971. fixed_header::serialize(buffer);
  1972. packet_id_ .serialize(buffer);
  1973. reason_code_.serialize(buffer);
  1974. properties_ .serialize(buffer);
  1975. return (*this);
  1976. }
  1977. inline pubcomp& deserialize(std::string_view& data)
  1978. {
  1979. fixed_header::deserialize(data);
  1980. // The Reason Code and Property Length can be omitted if the Reason Code is 0x00 (Success)
  1981. // and there are no Properties. In this case the PUBCOMP has a Remaining Length of 2.
  1982. packet_id_ .deserialize(data);
  1983. if (!data.empty())
  1984. {
  1985. reason_code_.deserialize(data);
  1986. // If the Remaining Length is less than 4 there is no Property Length and the value of 0 is used.
  1987. if (!data.empty())
  1988. properties_.deserialize(data);
  1989. }
  1990. update_remain_length();
  1991. return (*this);
  1992. }
  1993. inline two_byte_integer::value_type packet_id () const { return packet_id_ .value() ; }
  1994. inline std::uint8_t reason_code() const { return reason_code_.value() ; }
  1995. inline properties_set& properties () { return properties_ ; }
  1996. inline properties_set const& properties () const { return properties_ ; }
  1997. inline pubcomp & packet_id (std::uint16_t v) { packet_id_ = v; return (*this); }
  1998. inline pubcomp & reason_code(std::uint8_t v) { reason_code_ = v; return (*this); }
  1999. template<class... Properties>
  2000. inline pubcomp & properties (Properties&&... Props)
  2001. {
  2002. properties_.set(std::forward<Properties>(Props)...);
  2003. update_remain_length();
  2004. return (*this);
  2005. }
  2006. inline pubcomp& update_remain_length()
  2007. {
  2008. remain_length_ = static_cast<std::int32_t>(0
  2009. + packet_id_ .required_size()
  2010. + reason_code_.required_size()
  2011. + properties_ .required_size()
  2012. );
  2013. return (*this);
  2014. }
  2015. protected:
  2016. // The Variable Header of the PUBCOMP Packet contains the following fields in the order:
  2017. // Packet Identifier from the PUBREL packet that is being acknowledged, PUBCOMP Reason Code,
  2018. // and Properties. The rules for encoding Properties are described in section 2.2.2.
  2019. two_byte_integer packet_id_ {};
  2020. // Byte 3 in the Variable Header is the PUBCOMP Reason Code.
  2021. // If the Remaining Length is 2, then the value 0x00 (Success) is used.
  2022. one_byte_integer reason_code_{ 0 };
  2023. // PUBCOMP Properties
  2024. properties_set properties_{};
  2025. };
  2026. /**
  2027. * SUBSCRIBE - Subscribe request
  2028. *
  2029. * The SUBSCRIBE packet is sent from the Client to the Server to create one or more Subscriptions.
  2030. * Each Subscription registers a Client's interest in one or more Topics.
  2031. * The Server sends PUBLISH packets to the Client to forward Application Messages that were published
  2032. * to Topics that match these Subscriptions.
  2033. * The SUBSCRIBE packet also specifies (for each Subscription) the maximum QoS with which the Server
  2034. * can send Application Messages to the Client.
  2035. *
  2036. * https://docs.oasis-open.org/mqtt/mqtt/v5.0/os/mqtt-v5.0-os.html#_Toc3901161
  2037. */
  2038. class subscribe : public fixed_header<version_number>
  2039. {
  2040. public:
  2041. subscribe() : fixed_header(control_packet_type::subscribe)
  2042. {
  2043. // Bits 3,2,1 and 0 of the Fixed Header of the SUBSCRIBE packet are reserved and MUST be
  2044. // set to 0,0,1 and 0 respectively. The Server MUST treat any other value as malformed and
  2045. // close the Network Connection [MQTT-3.8.1-1].
  2046. type_and_flags_.reserved.bit1 = 1;
  2047. update_remain_length();
  2048. }
  2049. template<class... Properties, std::enable_if_t<is_property<Properties...>(), int> = 0>
  2050. explicit subscribe(std::uint16_t packet_id, Properties&&... Props)
  2051. : fixed_header(control_packet_type::subscribe)
  2052. , packet_id_ (packet_id)
  2053. , properties_ (std::forward<Properties>(Props)...)
  2054. {
  2055. type_and_flags_.reserved.bit1 = 1;
  2056. update_remain_length();
  2057. }
  2058. inline std::size_t required_size() const
  2059. {
  2060. return (fixed_header::required_size() + fixed_header::remain_length());
  2061. }
  2062. /*
  2063. * The Container is usually a std::string, std::vector<char>, ...
  2064. */
  2065. template<class Container>
  2066. inline subscribe& serialize(Container& buffer)
  2067. {
  2068. update_remain_length();
  2069. fixed_header::serialize(buffer);
  2070. packet_id_ .serialize(buffer);
  2071. properties_ .serialize(buffer);
  2072. subscriptions_.serialize(buffer);
  2073. return (*this);
  2074. }
  2075. inline subscribe& deserialize(std::string_view& data)
  2076. {
  2077. fixed_header::deserialize(data);
  2078. packet_id_ .deserialize(data);
  2079. properties_ .deserialize(data);
  2080. subscriptions_.deserialize(data);
  2081. update_remain_length();
  2082. return (*this);
  2083. }
  2084. inline two_byte_integer::value_type packet_id () const { return packet_id_ .value() ; }
  2085. inline properties_set & properties () { return properties_ ; }
  2086. inline subscriptions_set& subscriptions() { return subscriptions_ ; }
  2087. inline properties_set const& properties () const { return properties_ ; }
  2088. inline subscriptions_set const& subscriptions() const { return subscriptions_ ; }
  2089. inline subscribe & packet_id (std::uint16_t v) { packet_id_ = v; return (*this); }
  2090. template<class... Properties>
  2091. inline subscribe & properties (Properties&&... Props)
  2092. {
  2093. properties_.set(std::forward<Properties>(Props)...);
  2094. update_remain_length();
  2095. return (*this);
  2096. }
  2097. template<class... Subscriptions>
  2098. inline subscribe& add_subscriptions(Subscriptions&&... Subscripts)
  2099. {
  2100. subscriptions_.add(std::forward<Subscriptions>(Subscripts)...);
  2101. update_remain_length();
  2102. return (*this);
  2103. }
  2104. inline subscribe& erase_subscription(std::string_view topic_filter)
  2105. {
  2106. subscriptions_.erase(topic_filter);
  2107. update_remain_length();
  2108. return (*this);
  2109. }
  2110. inline subscribe& update_remain_length()
  2111. {
  2112. remain_length_ = static_cast<std::int32_t>(0
  2113. + packet_id_ .required_size()
  2114. + properties_ .required_size()
  2115. + subscriptions_.required_size()
  2116. );
  2117. return (*this);
  2118. }
  2119. protected:
  2120. // The Variable Header of the SUBSCRIBE Packet contains the following fields in the order:
  2121. // Packet Identifier, and Properties.
  2122. two_byte_integer packet_id_ {};
  2123. // SUBSCRIBE Properties
  2124. properties_set properties_ {};
  2125. // The Payload of a SUBSCRIBE packet contains a list of Topic Filters indicating the Topics
  2126. // to which the Client wants to subscribe. The Topic Filters MUST be a UTF-8 Encoded String
  2127. // [MQTT-3.8.3-1]. Each Topic Filter is followed by a Subscription Options byte.
  2128. subscriptions_set subscriptions_{};
  2129. };
  2130. /**
  2131. * SUBACK - Subscribe acknowledgement
  2132. *
  2133. * A SUBACK packet is sent by the Server to the Client to confirm receipt and processing of a SUBSCRIBE packet.
  2134. *
  2135. * https://docs.oasis-open.org/mqtt/mqtt/v5.0/os/mqtt-v5.0-os.html#_Toc3901171
  2136. */
  2137. class suback : public fixed_header<version_number>
  2138. {
  2139. public:
  2140. suback() : fixed_header(control_packet_type::suback)
  2141. {
  2142. update_remain_length();
  2143. }
  2144. template<class... Properties, std::enable_if_t<is_property<Properties...>(), int> = 0>
  2145. explicit suback(std::uint16_t packet_id, Properties&&... Props)
  2146. : fixed_header(control_packet_type::suback)
  2147. , packet_id_ (packet_id)
  2148. , properties_ (std::forward<Properties>(Props)...)
  2149. {
  2150. update_remain_length();
  2151. }
  2152. inline std::size_t required_size() const
  2153. {
  2154. return (fixed_header::required_size() + fixed_header::remain_length());
  2155. }
  2156. /*
  2157. * The Container is usually a std::string, std::vector<char>, ...
  2158. */
  2159. template<class Container>
  2160. inline suback& serialize(Container& buffer)
  2161. {
  2162. update_remain_length();
  2163. fixed_header::serialize(buffer);
  2164. packet_id_ .serialize(buffer);
  2165. properties_ .serialize(buffer);
  2166. reason_codes_ .serialize(buffer);
  2167. return (*this);
  2168. }
  2169. inline suback& deserialize(std::string_view& data)
  2170. {
  2171. fixed_header::deserialize(data);
  2172. packet_id_ .deserialize(data);
  2173. properties_ .deserialize(data);
  2174. reason_codes_ .deserialize(data);
  2175. update_remain_length();
  2176. return (*this);
  2177. }
  2178. inline two_byte_integer::value_type packet_id () const { return packet_id_ .value() ; }
  2179. inline properties_set & properties () { return properties_ ; }
  2180. inline one_byte_integer_set & reason_codes () { return reason_codes_ ; }
  2181. inline properties_set const& properties () const { return properties_ ; }
  2182. inline one_byte_integer_set const& reason_codes () const { return reason_codes_ ; }
  2183. inline suback & packet_id (std::uint16_t v) { packet_id_ = v; return (*this); }
  2184. template<class... Properties>
  2185. inline suback & properties (Properties&&... Props)
  2186. {
  2187. properties_.set(std::forward<Properties>(Props)...);
  2188. update_remain_length();
  2189. return (*this);
  2190. }
  2191. template<class... Integers>
  2192. inline suback& add_reason_codes(Integers... Ints)
  2193. {
  2194. reason_codes_.add(static_cast<one_byte_integer::value_type>(Ints)...);
  2195. update_remain_length();
  2196. return (*this);
  2197. }
  2198. inline suback& erase_reason_code(std::size_t index)
  2199. {
  2200. reason_codes_.erase(index);
  2201. update_remain_length();
  2202. return (*this);
  2203. }
  2204. inline suback& update_remain_length()
  2205. {
  2206. remain_length_ = static_cast<std::int32_t>(0
  2207. + packet_id_ .required_size()
  2208. + properties_ .required_size()
  2209. + reason_codes_ .required_size()
  2210. );
  2211. return (*this);
  2212. }
  2213. protected:
  2214. // The Variable Header of the SUBACK Packet contains the following fields in the order:
  2215. // the Packet Identifier from the SUBSCRIBE Packet that is being acknowledged, and Properties.
  2216. two_byte_integer packet_id_ {};
  2217. // SUBACK Properties
  2218. properties_set properties_ {};
  2219. // The Payload contains a list of Reason Codes. Each Reason Code corresponds to a Topic Filter
  2220. // in the SUBSCRIBE packet being acknowledged. The order of Reason Codes in the SUBACK packet
  2221. // MUST match the order of Topic Filters in the SUBSCRIBE packet [MQTT-3.9.3-1].
  2222. one_byte_integer_set reason_codes_{};
  2223. };
  2224. /**
  2225. * UNSUBSCRIBE - Unsubscribe request
  2226. *
  2227. * An UNSUBSCRIBE packet is sent by the Client to the Server, to unsubscribe from topics.
  2228. *
  2229. * https://docs.oasis-open.org/mqtt/mqtt/v5.0/os/mqtt-v5.0-os.html#_Toc3901179
  2230. */
  2231. class unsubscribe : public fixed_header<version_number>
  2232. {
  2233. public:
  2234. unsubscribe() : fixed_header(control_packet_type::unsubscribe)
  2235. {
  2236. // Bits 3,2,1 and 0 of the Fixed Header of the UNSUBSCRIBE packet are reserved and MUST
  2237. // be set to 0,0,1 and 0 respectively. The Server MUST treat any other value as malformed
  2238. // and close the Network Connection [MQTT-3.10.1-1].
  2239. type_and_flags_.reserved.bit1 = 1;
  2240. update_remain_length();
  2241. }
  2242. template<class... Strings>
  2243. explicit unsubscribe(std::uint16_t packet_id, Strings&&... topic_filters)
  2244. : fixed_header (control_packet_type::unsubscribe)
  2245. , packet_id_ (packet_id)
  2246. , topic_filters_(std::forward<Strings>(topic_filters)...)
  2247. {
  2248. type_and_flags_.reserved.bit1 = 1;
  2249. update_remain_length();
  2250. }
  2251. template<class... Properties, std::enable_if_t<is_property<Properties...>(), int> = 0>
  2252. explicit unsubscribe(std::uint16_t packet_id, Properties&&... Props)
  2253. : fixed_header(control_packet_type::unsubscribe)
  2254. , packet_id_ (packet_id)
  2255. , properties_ (std::forward<Properties>(Props)...)
  2256. {
  2257. type_and_flags_.reserved.bit1 = 1;
  2258. update_remain_length();
  2259. }
  2260. inline std::size_t required_size() const
  2261. {
  2262. return (fixed_header::required_size() + fixed_header::remain_length());
  2263. }
  2264. /*
  2265. * The Container is usually a std::string, std::vector<char>, ...
  2266. */
  2267. template<class Container>
  2268. inline unsubscribe& serialize(Container& buffer)
  2269. {
  2270. update_remain_length();
  2271. fixed_header::serialize(buffer);
  2272. packet_id_ .serialize(buffer);
  2273. properties_ .serialize(buffer);
  2274. topic_filters_.serialize(buffer);
  2275. return (*this);
  2276. }
  2277. inline unsubscribe& deserialize(std::string_view& data)
  2278. {
  2279. fixed_header::deserialize(data);
  2280. packet_id_ .deserialize(data);
  2281. properties_ .deserialize(data);
  2282. topic_filters_.deserialize(data);
  2283. update_remain_length();
  2284. return (*this);
  2285. }
  2286. inline two_byte_integer::value_type packet_id () const { return packet_id_ .value() ; }
  2287. inline properties_set & properties () { return properties_ ; }
  2288. inline utf8_string_set & topic_filters() { return topic_filters_ ; }
  2289. inline properties_set const& properties () const { return properties_ ; }
  2290. inline utf8_string_set const& topic_filters() const { return topic_filters_ ; }
  2291. inline unsubscribe & packet_id (std::uint16_t v) { packet_id_ = v; return (*this); }
  2292. template<class... Properties>
  2293. inline unsubscribe & properties (Properties&&... Props)
  2294. {
  2295. properties_.set(std::forward<Properties>(Props)...);
  2296. update_remain_length();
  2297. return (*this);
  2298. }
  2299. template<class... Strings>
  2300. inline unsubscribe& add_topic_filters(Strings&&... Strs)
  2301. {
  2302. topic_filters_.add(std::forward<Strings>(Strs)...);
  2303. update_remain_length();
  2304. return (*this);
  2305. }
  2306. inline unsubscribe& erase_topic_filter(std::string_view topic_filter)
  2307. {
  2308. topic_filters_.erase(topic_filter);
  2309. update_remain_length();
  2310. return (*this);
  2311. }
  2312. inline unsubscribe& update_remain_length()
  2313. {
  2314. remain_length_ = static_cast<std::int32_t>(0
  2315. + packet_id_ .required_size()
  2316. + properties_ .required_size()
  2317. + topic_filters_.required_size()
  2318. );
  2319. return (*this);
  2320. }
  2321. protected:
  2322. // The Variable Header of the UNSUBSCRIBE Packet contains the following fields in the order:
  2323. // Packet Identifier, and Properties.
  2324. two_byte_integer packet_id_ {};
  2325. // UNSUBSCRIBE Properties
  2326. properties_set properties_ {};
  2327. // The Payload for the UNSUBSCRIBE packet contains the list of Topic Filters that the Client
  2328. // wishes to unsubscribe from. The Topic Filters in an UNSUBSCRIBE packet MUST be UTF-8
  2329. // Encoded Strings [MQTT-3.10.3-1] as defined in section 1.5.4, packed contiguously.
  2330. // The Payload of an UNSUBSCRIBE packet MUST contain at least one Topic Filter[MQTT - 3.10.3 - 2].
  2331. // An UNSUBSCRIBE packet with no Payload is a Protocol Error.Refer to section 4.13 for
  2332. // information about handling errors.
  2333. utf8_string_set topic_filters_{};
  2334. };
  2335. /**
  2336. * UNSUBACK - Unsubscribe acknowledgement
  2337. *
  2338. * The UNSUBACK packet is sent by the Server to the Client to confirm receipt of an UNSUBSCRIBE packet.
  2339. *
  2340. * https://docs.oasis-open.org/mqtt/mqtt/v5.0/os/mqtt-v5.0-os.html#_Toc3901187
  2341. */
  2342. class unsuback : public fixed_header<version_number>
  2343. {
  2344. public:
  2345. unsuback() : fixed_header(control_packet_type::unsuback)
  2346. {
  2347. update_remain_length();
  2348. }
  2349. template<class... Properties, std::enable_if_t<is_property<Properties...>(), int> = 0>
  2350. explicit unsuback(std::uint16_t packet_id, Properties&&... Props)
  2351. : fixed_header(control_packet_type::unsuback)
  2352. , packet_id_ (packet_id)
  2353. , properties_ (std::forward<Properties>(Props)...)
  2354. {
  2355. update_remain_length();
  2356. }
  2357. inline std::size_t required_size() const
  2358. {
  2359. return (fixed_header::required_size() + fixed_header::remain_length());
  2360. }
  2361. /*
  2362. * The Container is usually a std::string, std::vector<char>, ...
  2363. */
  2364. template<class Container>
  2365. inline unsuback& serialize(Container& buffer)
  2366. {
  2367. update_remain_length();
  2368. fixed_header::serialize(buffer);
  2369. packet_id_ .serialize(buffer);
  2370. properties_ .serialize(buffer);
  2371. reason_codes_ .serialize(buffer);
  2372. return (*this);
  2373. }
  2374. inline unsuback& deserialize(std::string_view& data)
  2375. {
  2376. fixed_header::deserialize(data);
  2377. packet_id_ .deserialize(data);
  2378. properties_ .deserialize(data);
  2379. reason_codes_ .deserialize(data);
  2380. update_remain_length();
  2381. return (*this);
  2382. }
  2383. inline two_byte_integer::value_type packet_id () const { return packet_id_ .value() ; }
  2384. inline properties_set & properties () { return properties_ ; }
  2385. inline one_byte_integer_set & reason_codes () { return reason_codes_ ; }
  2386. inline properties_set const& properties () const { return properties_ ; }
  2387. inline one_byte_integer_set const& reason_codes () const { return reason_codes_ ; }
  2388. inline unsuback & packet_id (std::uint16_t v) { packet_id_ = v; return (*this); }
  2389. template<class... Properties>
  2390. inline unsuback & properties (Properties&&... Props)
  2391. {
  2392. properties_.set(std::forward<Properties>(Props)...);
  2393. update_remain_length();
  2394. return (*this);
  2395. }
  2396. template<class... Integers>
  2397. inline unsuback& add_reason_codes(Integers... Ints)
  2398. {
  2399. reason_codes_.add(static_cast<one_byte_integer::value_type>(Ints)...);
  2400. update_remain_length();
  2401. return (*this);
  2402. }
  2403. inline unsuback& erase_reason_code(std::size_t index)
  2404. {
  2405. reason_codes_.erase(index);
  2406. update_remain_length();
  2407. return (*this);
  2408. }
  2409. inline unsuback& update_remain_length()
  2410. {
  2411. remain_length_ = static_cast<std::int32_t>(0
  2412. + packet_id_ .required_size()
  2413. + properties_ .required_size()
  2414. + reason_codes_ .required_size()
  2415. );
  2416. return (*this);
  2417. }
  2418. protected:
  2419. // The Variable Header of the UNSUBACK Packet the following fields in the order:
  2420. // the Packet Identifier from the UNSUBSCRIBE Packet that is being acknowledged, and Properties.
  2421. two_byte_integer packet_id_ {};
  2422. // UNSUBACK Properties
  2423. properties_set properties_ {};
  2424. // The Payload contains a list of Reason Codes.
  2425. // Each Reason Code corresponds to a Topic Filter in the UNSUBSCRIBE packet being acknowledged.
  2426. one_byte_integer_set reason_codes_{};
  2427. };
  2428. /**
  2429. * PINGREQ - PING request
  2430. *
  2431. * The PINGREQ packet is sent from a Client to the Server.
  2432. *
  2433. * https://docs.oasis-open.org/mqtt/mqtt/v5.0/os/mqtt-v5.0-os.html#_Toc3901195
  2434. */
  2435. class pingreq : public fixed_header<version_number>
  2436. {
  2437. public:
  2438. pingreq() : fixed_header(control_packet_type::pingreq)
  2439. {
  2440. update_remain_length();
  2441. }
  2442. inline std::size_t required_size() const
  2443. {
  2444. return (fixed_header::required_size() + fixed_header::remain_length());
  2445. }
  2446. /*
  2447. * The Container is usually a std::string, std::vector<char>, ...
  2448. */
  2449. template<class Container>
  2450. inline pingreq& serialize(Container& buffer)
  2451. {
  2452. fixed_header::serialize(buffer);
  2453. return (*this);
  2454. }
  2455. inline pingreq& deserialize(std::string_view& data)
  2456. {
  2457. fixed_header::deserialize(data);
  2458. update_remain_length();
  2459. return (*this);
  2460. }
  2461. inline pingreq& update_remain_length()
  2462. {
  2463. remain_length_ = static_cast<std::int32_t>(0
  2464. );
  2465. return (*this);
  2466. }
  2467. protected:
  2468. // The PINGREQ packet has no Variable Header.
  2469. // The PINGREQ packet has no Payload.
  2470. };
  2471. /**
  2472. * PINGRESP - PING response
  2473. *
  2474. * A PINGRESP Packet is sent by the Server to the Client in response to a PINGREQ packet.
  2475. * It indicates that the Server is alive.
  2476. *
  2477. * https://docs.oasis-open.org/mqtt/mqtt/v5.0/os/mqtt-v5.0-os.html#_Toc3901200
  2478. */
  2479. class pingresp : public fixed_header<version_number>
  2480. {
  2481. public:
  2482. pingresp() : fixed_header(control_packet_type::pingresp)
  2483. {
  2484. update_remain_length();
  2485. }
  2486. inline std::size_t required_size() const
  2487. {
  2488. return (fixed_header::required_size() + fixed_header::remain_length());
  2489. }
  2490. /*
  2491. * The Container is usually a std::string, std::vector<char>, ...
  2492. */
  2493. template<class Container>
  2494. inline pingresp& serialize(Container& buffer)
  2495. {
  2496. fixed_header::serialize(buffer);
  2497. return (*this);
  2498. }
  2499. inline pingresp& deserialize(std::string_view& data)
  2500. {
  2501. fixed_header::deserialize(data);
  2502. update_remain_length();
  2503. return (*this);
  2504. }
  2505. inline pingresp& update_remain_length()
  2506. {
  2507. remain_length_ = static_cast<std::int32_t>(0
  2508. );
  2509. return (*this);
  2510. }
  2511. protected:
  2512. // The PINGRESP packet has no Variable Header.
  2513. // The PINGRESP packet has no Payload.
  2514. };
  2515. /**
  2516. * DISCONNECT - Disconnect notification
  2517. *
  2518. * The DISCONNECT packet is the final MQTT Control Packet sent from the Client or the Server.
  2519. * It indicates the reason why the Network Connection is being closed.
  2520. * The Client or Server MAY send a DISCONNECT packet before closing the Network Connection.
  2521. * If the Network Connection is closed without the Client first sending a DISCONNECT packet
  2522. * with Reason Code 0x00 (Normal disconnection) and the Connection has a Will Message, the
  2523. * Will Message is published. Refer to section 3.1.2.5 for further details.
  2524. * A Server MUST NOT send a DISCONNECT until after it has sent a CONNACK with Reason Code of
  2525. * less than 0x80 [MQTT-3.14.0-1].
  2526. *
  2527. * https://docs.oasis-open.org/mqtt/mqtt/v5.0/os/mqtt-v5.0-os.html#_Toc3901205
  2528. */
  2529. class disconnect : public fixed_header<version_number>, public detail::reason_string_ops<disconnect>
  2530. {
  2531. public:
  2532. disconnect() : fixed_header(control_packet_type::disconnect)
  2533. {
  2534. update_remain_length();
  2535. }
  2536. template<class... Properties, std::enable_if_t<is_property<Properties...>(), int> = 0>
  2537. explicit disconnect(std::uint8_t reason_code, Properties&&... Props)
  2538. : fixed_header(control_packet_type::disconnect)
  2539. , reason_code_(reason_code)
  2540. , properties_(std::forward<Properties>(Props)...)
  2541. {
  2542. update_remain_length();
  2543. }
  2544. inline std::size_t required_size() const
  2545. {
  2546. return (fixed_header::required_size() + fixed_header::remain_length());
  2547. }
  2548. /*
  2549. * The Container is usually a std::string, std::vector<char>, ...
  2550. */
  2551. template<class Container>
  2552. inline disconnect& serialize(Container& buffer)
  2553. {
  2554. update_remain_length();
  2555. fixed_header::serialize(buffer);
  2556. reason_code_ .serialize(buffer);
  2557. properties_ .serialize(buffer);
  2558. return (*this);
  2559. }
  2560. inline disconnect& deserialize(std::string_view& data)
  2561. {
  2562. fixed_header::deserialize(data);
  2563. // If the Remaining Length is less than 1 the value of 0x00 (Normal disconnection) is used.
  2564. if (!data.empty())
  2565. {
  2566. reason_code_ .deserialize(data);
  2567. properties_ .deserialize(data);
  2568. }
  2569. update_remain_length();
  2570. return (*this);
  2571. }
  2572. inline std::uint8_t reason_code () const { return reason_code_.value() ; }
  2573. inline properties_set& properties () { return properties_ ; }
  2574. inline properties_set const& properties() const { return properties_ ; }
  2575. inline disconnect & reason_code (std::uint8_t v) { reason_code_ = v; return (*this); }
  2576. template<class... Properties>
  2577. inline disconnect & properties(Properties&&... Props)
  2578. {
  2579. properties_.set(std::forward<Properties>(Props)...);
  2580. update_remain_length();
  2581. return (*this);
  2582. }
  2583. inline disconnect& update_remain_length()
  2584. {
  2585. remain_length_ = static_cast<std::int32_t>(0
  2586. + reason_code_ .required_size()
  2587. + properties_ .required_size()
  2588. );
  2589. return (*this);
  2590. }
  2591. protected:
  2592. // The Variable Header of the DISCONNECT Packet contains the following fields in the order:
  2593. // Disconnect Reason Code, and Properties.
  2594. // Byte 1 in the Variable Header is the Disconnect Reason Code.
  2595. // If the Remaining Length is less than 1 the value of 0x00 (Normal disconnection) is used.
  2596. one_byte_integer reason_code_{ 0 };
  2597. // DISCONNECT Properties
  2598. properties_set properties_ { };
  2599. // The DISCONNECT packet has no Payload.
  2600. };
  2601. /**
  2602. * AUTH - Authentication exchange
  2603. *
  2604. * An AUTH packet is sent from Client to Server or Server to Client as part of an extended
  2605. * authentication exchange, such as challenge / response authentication.
  2606. * It is a Protocol Error for the Client or Server to send an AUTH packet if the CONNECT packet
  2607. * did not contain the same Authentication Method.
  2608. *
  2609. * https://docs.oasis-open.org/mqtt/mqtt/v5.0/os/mqtt-v5.0-os.html#_Toc3901217
  2610. */
  2611. class auth : public fixed_header<version_number>, public detail::reason_string_ops<auth>
  2612. {
  2613. public:
  2614. auth() : fixed_header(control_packet_type::auth)
  2615. {
  2616. update_remain_length();
  2617. }
  2618. template<class... Properties, std::enable_if_t<is_property<Properties...>(), int> = 0>
  2619. explicit auth(std::uint8_t reason_code, Properties&&... Props)
  2620. : fixed_header(control_packet_type::auth)
  2621. , reason_code_(reason_code)
  2622. , properties_(std::forward<Properties>(Props)...)
  2623. {
  2624. update_remain_length();
  2625. }
  2626. inline std::size_t required_size() const
  2627. {
  2628. return (fixed_header::required_size() + fixed_header::remain_length());
  2629. }
  2630. /*
  2631. * The Container is usually a std::string, std::vector<char>, ...
  2632. */
  2633. template<class Container>
  2634. inline auth& serialize(Container& buffer)
  2635. {
  2636. update_remain_length();
  2637. fixed_header::serialize(buffer);
  2638. reason_code_ .serialize(buffer);
  2639. properties_ .serialize(buffer);
  2640. return (*this);
  2641. }
  2642. inline auth& deserialize(std::string_view& data)
  2643. {
  2644. fixed_header::deserialize(data);
  2645. // The Reason Code and Property Length can be omitted if the Reason Code is 0x00 (Success)
  2646. // and there are no Properties. In this case the AUTH has a Remaining Length of 0.
  2647. if (!data.empty())
  2648. {
  2649. reason_code_ .deserialize(data);
  2650. properties_ .deserialize(data);
  2651. }
  2652. update_remain_length();
  2653. return (*this);
  2654. }
  2655. inline std::uint8_t reason_code () const { return reason_code_.value() ; }
  2656. inline properties_set& properties () { return properties_ ; }
  2657. inline properties_set const& properties() const { return properties_ ; }
  2658. inline auth & reason_code (std::uint8_t v) { reason_code_ = v; return (*this); }
  2659. template<class... Properties>
  2660. inline auth & properties(Properties&&... Props)
  2661. {
  2662. properties_.set(std::forward<Properties>(Props)...);
  2663. update_remain_length();
  2664. return (*this);
  2665. }
  2666. inline auth& update_remain_length()
  2667. {
  2668. remain_length_ = static_cast<std::int32_t>(0
  2669. + reason_code_ .required_size()
  2670. + properties_ .required_size()
  2671. );
  2672. return (*this);
  2673. }
  2674. protected:
  2675. // The Variable Header of the AUTH Packet contains the following fields in the order:
  2676. // Authenticate Reason Code, and Properties.
  2677. // Byte 0 in the Variable Header is the Authenticate Reason Code.
  2678. // The values for the one byte unsigned Authenticate Reason Code field are shown below.
  2679. // The sender of the AUTH Packet MUST use one of the Authenticate Reason Codes [MQTT-3.15.2-1].
  2680. // 0-Success 24-Continue authentication 25-Re-authenticate
  2681. // The Reason Code and Property Length can be omitted if the Reason Code is 0x00 (Success)
  2682. // and there are no Properties. In this case the AUTH has a Remaining Length of 0.
  2683. one_byte_integer reason_code_{ 0 };
  2684. // AUTH Properties
  2685. properties_set properties_ { };
  2686. // The AUTH packet has no Payload.
  2687. };
  2688. }
  2689. namespace asio2::mqtt
  2690. {
  2691. template<typename message_type>
  2692. inline constexpr bool is_v5_message()
  2693. {
  2694. using type = asio2::detail::remove_cvref_t<message_type>;
  2695. if constexpr (
  2696. std::is_same_v<type, mqtt::v5::connect > ||
  2697. std::is_same_v<type, mqtt::v5::connack > ||
  2698. std::is_same_v<type, mqtt::v5::publish > ||
  2699. std::is_same_v<type, mqtt::v5::puback > ||
  2700. std::is_same_v<type, mqtt::v5::pubrec > ||
  2701. std::is_same_v<type, mqtt::v5::pubrel > ||
  2702. std::is_same_v<type, mqtt::v5::pubcomp > ||
  2703. std::is_same_v<type, mqtt::v5::subscribe > ||
  2704. std::is_same_v<type, mqtt::v5::suback > ||
  2705. std::is_same_v<type, mqtt::v5::unsubscribe > ||
  2706. std::is_same_v<type, mqtt::v5::unsuback > ||
  2707. std::is_same_v<type, mqtt::v5::pingreq > ||
  2708. std::is_same_v<type, mqtt::v5::pingresp > ||
  2709. std::is_same_v<type, mqtt::v5::disconnect > ||
  2710. std::is_same_v<type, mqtt::v5::auth > )
  2711. {
  2712. return true;
  2713. }
  2714. else
  2715. {
  2716. return false;
  2717. }
  2718. }
  2719. }
  2720. #endif // !__ASIO2_MQTT_PROTOCOL_V5_HPP__