v5_message.hpp 107 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939
  1. // Copyright Takatoshi Kondo 2018
  2. //
  3. // Distributed under the Boost Software License, Version 1.0.
  4. // (See accompanying file LICENSE_1_0.txt or copy at
  5. // http://www.boost.org/LICENSE_1_0.txt)
  6. #if !defined(MQTT_V5_MESSAGE_HPP)
  7. #define MQTT_V5_MESSAGE_HPP
  8. #include <string>
  9. #include <vector>
  10. #include <memory>
  11. #include <algorithm>
  12. #include <numeric>
  13. #include <boost/asio/buffer.hpp>
  14. #include <boost/container/static_vector.hpp>
  15. #include <boost/numeric/conversion/cast.hpp>
  16. #include <mqtt/namespace.hpp>
  17. #include <mqtt/two_byte_util.hpp>
  18. #include <mqtt/fixed_header.hpp>
  19. #include <mqtt/remaining_length.hpp>
  20. #include <mqtt/subscribe_options.hpp>
  21. #include <mqtt/const_buffer_util.hpp>
  22. #include <mqtt/will.hpp>
  23. #include <mqtt/connect_flags.hpp>
  24. #include <mqtt/publish.hpp>
  25. #include <mqtt/exception.hpp>
  26. #include <mqtt/utf8encoded_strings.hpp>
  27. #include <mqtt/string_check.hpp>
  28. #include <mqtt/property.hpp>
  29. #include <mqtt/property_variant.hpp>
  30. #include <mqtt/property_parse.hpp>
  31. #include <mqtt/reason_code.hpp>
  32. #include <mqtt/packet_id_type.hpp>
  33. #include <mqtt/move.hpp>
  34. #include <mqtt/variant_visit.hpp>
  35. #include <mqtt/optional.hpp>
  36. #if !defined(MQTT_ALWAYS_SEND_REASON_CODE)
  37. #define MQTT_ALWAYS_SEND_REASON_CODE false
  38. #endif // !defined(MQTT_ALWAYS_SEND_REASON_CODE)
  39. namespace MQTT_NS {
  40. namespace as = boost::asio;
  41. namespace v5 {
  42. namespace detail {
  43. class header_only_message {
  44. public:
  45. /**
  46. * @brief Create empty header_packet_id_message.
  47. */
  48. header_only_message(control_packet_type type, std::uint8_t flags)
  49. : message_ { static_cast<char>(make_fixed_header(type, flags)), 0 }
  50. {}
  51. /**
  52. * @brief Create const buffer sequence
  53. * it is for boost asio APIs
  54. * @return const buffer sequence
  55. */
  56. std::vector<as::const_buffer> const_buffer_sequence() const {
  57. return { as::buffer(message_.data(), message_.size()) };
  58. }
  59. /**
  60. * @brief Get whole size of sequence
  61. * @return whole size
  62. */
  63. std::size_t size() const {
  64. return message_.size();
  65. }
  66. /**
  67. * @brief Get number of element of const_buffer_sequence
  68. * @return number of element of const_buffer_sequence
  69. */
  70. static constexpr std::size_t num_of_const_buffer_sequence() {
  71. return 1;
  72. }
  73. /**
  74. * @brief Create one continuours buffer.
  75. * All sequence of buffers are concatinated.
  76. * It is useful to store to file/database.
  77. * @return continuous buffer
  78. */
  79. std::string continuous_buffer() const {
  80. return std::string(message_.data(), message_.size());
  81. }
  82. private:
  83. boost::container::static_vector<char, 2> message_;
  84. };
  85. } // namespace detail
  86. class connect_message {
  87. public:
  88. connect_message(
  89. std::uint16_t keep_alive_sec,
  90. buffer client_id,
  91. bool clean_start,
  92. optional<will> w,
  93. optional<buffer> user_name,
  94. optional<buffer> password,
  95. properties props
  96. )
  97. : fixed_header_(make_fixed_header(control_packet_type::connect, 0b0000)),
  98. connect_flags_(0),
  99. // protocol name length, protocol name, protocol level, connect flag, client id length, client id, keep alive
  100. remaining_length_(
  101. 2 + // protocol name length
  102. 4 + // protocol name
  103. 1 + // protocol level
  104. 1 + // connect flag
  105. 2 + // keep alive
  106. 2 + // client id length
  107. client_id.size() // client id
  108. ),
  109. protocol_name_and_level_ { 0x00, 0x04, 'M', 'Q', 'T', 'T', 0x05 },
  110. client_id_(force_move(client_id)),
  111. client_id_length_buf_{ num_to_2bytes(boost::numeric_cast<std::uint16_t>(client_id_.size())) },
  112. will_property_length_(
  113. w ?
  114. std::accumulate(
  115. w.value().props().begin(),
  116. w.value().props().end(),
  117. std::size_t(0U),
  118. [](std::size_t total, property_variant const& pv) {
  119. return total + v5::size(pv);
  120. }
  121. )
  122. : 0U
  123. ),
  124. will_props_(
  125. w ?
  126. force_move(w.value().props())
  127. : properties()
  128. ),
  129. keep_alive_buf_ ({ num_to_2bytes(keep_alive_sec ) }),
  130. property_length_(
  131. std::accumulate(
  132. props.begin(),
  133. props.end(),
  134. std::size_t(0U),
  135. [](std::size_t total, property_variant const& pv) {
  136. return total + v5::size(pv);
  137. }
  138. )
  139. ),
  140. props_(force_move(props)),
  141. num_of_const_buffer_sequence_(
  142. 1 + // fixed header
  143. 1 + // remaining length
  144. 1 + // protocol name and level
  145. 1 + // connect flags
  146. 1 + // keep alive
  147. 1 + // property length
  148. std::accumulate(
  149. props_.begin(),
  150. props_.end(),
  151. std::size_t(0U),
  152. [](std::size_t total, property_variant const& pv) {
  153. return total + v5::num_of_const_buffer_sequence(pv);
  154. }
  155. ) +
  156. 2 // client id length, client id
  157. )
  158. {
  159. auto pb = variable_bytes(property_length_);
  160. for (auto e : pb) {
  161. property_length_buf_.push_back(e);
  162. }
  163. remaining_length_ += property_length_buf_.size() + property_length_;
  164. utf8string_check(client_id_);
  165. if (clean_start) connect_flags_ |= connect_flags::clean_start;
  166. if (user_name) {
  167. utf8string_check(user_name.value());
  168. connect_flags_ |= connect_flags::user_name_flag;
  169. user_name_ = force_move(user_name.value());
  170. add_uint16_t_to_buf(user_name_length_buf_, boost::numeric_cast<std::uint16_t>(user_name_.size()));
  171. remaining_length_ += 2 + user_name_.size();
  172. num_of_const_buffer_sequence_ += 2; // user name length, user name
  173. }
  174. if (password) {
  175. connect_flags_ |= connect_flags::password_flag;
  176. password_ = force_move(password.value());
  177. add_uint16_t_to_buf(password_length_buf_, boost::numeric_cast<std::uint16_t>(password_.size()));
  178. remaining_length_ += 2 + password_.size();
  179. num_of_const_buffer_sequence_ += 2; // password length, password
  180. }
  181. if (w) {
  182. connect_flags_ |= connect_flags::will_flag;
  183. if (w.value().get_retain() == retain::yes) connect_flags_ |= connect_flags::will_retain;
  184. connect_flags::set_will_qos(connect_flags_, w.value().get_qos());
  185. auto wpb = variable_bytes(will_property_length_);
  186. for (auto e : wpb) {
  187. will_property_length_buf_.push_back(e);
  188. }
  189. utf8string_check(w.value().topic());
  190. will_topic_name_ = force_move(w.value().topic());
  191. add_uint16_t_to_buf(
  192. will_topic_name_length_buf_,
  193. boost::numeric_cast<std::uint16_t>(will_topic_name_.size())
  194. );
  195. if (w.value().message().size() > 0xffffL) throw will_message_length_error();
  196. will_message_ = force_move(w.value().message());
  197. add_uint16_t_to_buf(
  198. will_message_length_buf_,
  199. boost::numeric_cast<std::uint16_t>(will_message_.size()));
  200. remaining_length_ +=
  201. will_property_length_buf_.size() +
  202. will_property_length_ +
  203. 2 + will_topic_name_.size() + 2 + will_message_.size();
  204. num_of_const_buffer_sequence_ +=
  205. std::accumulate(
  206. will_props_.begin(),
  207. will_props_.end(),
  208. std::size_t(0U),
  209. [](std::size_t total, property_variant const& pv) {
  210. return total + v5::num_of_const_buffer_sequence(pv);
  211. }
  212. ) +
  213. 2 + // will topic name length, will topic name
  214. 2; // will message length, will message
  215. }
  216. auto rb = remaining_bytes(remaining_length_);
  217. for (auto e : rb) {
  218. remaining_length_buf_.push_back(e);
  219. }
  220. }
  221. /**
  222. * @brief Create const buffer sequence
  223. * it is for boost asio APIs
  224. * @return const buffer sequence
  225. */
  226. std::vector<as::const_buffer> const_buffer_sequence() const {
  227. std::vector<as::const_buffer> ret;
  228. ret.reserve(num_of_const_buffer_sequence());
  229. ret.emplace_back(as::buffer(&fixed_header_, 1));
  230. ret.emplace_back(as::buffer(remaining_length_buf_.data(), remaining_length_buf_.size()));
  231. ret.emplace_back(as::buffer(protocol_name_and_level_.data(), protocol_name_and_level_.size()));
  232. ret.emplace_back(as::buffer(&connect_flags_, 1));
  233. ret.emplace_back(as::buffer(keep_alive_buf_.data(), keep_alive_buf_.size()));
  234. ret.emplace_back(as::buffer(property_length_buf_.data(), property_length_buf_.size()));
  235. for (auto const& p : props_) {
  236. v5::add_const_buffer_sequence(ret, p);
  237. }
  238. ret.emplace_back(as::buffer(client_id_length_buf_.data(), client_id_length_buf_.size()));
  239. ret.emplace_back(as::buffer(client_id_));
  240. if (connect_flags::has_will_flag(connect_flags_)) {
  241. ret.emplace_back(as::buffer(will_property_length_buf_.data(), will_property_length_buf_.size()));
  242. for (auto const& p : will_props_) {
  243. v5::add_const_buffer_sequence(ret, p);
  244. }
  245. ret.emplace_back(as::buffer(will_topic_name_length_buf_.data(), will_topic_name_length_buf_.size()));
  246. ret.emplace_back(as::buffer(will_topic_name_));
  247. ret.emplace_back(as::buffer(will_message_length_buf_.data(), will_message_length_buf_.size()));
  248. ret.emplace_back(as::buffer(will_message_));
  249. }
  250. if (connect_flags::has_user_name_flag(connect_flags_)) {
  251. ret.emplace_back(as::buffer(user_name_length_buf_.data(), user_name_length_buf_.size()));
  252. ret.emplace_back(as::buffer(user_name_));
  253. }
  254. if (connect_flags::has_password_flag(connect_flags_)) {
  255. ret.emplace_back(as::buffer(password_length_buf_.data(), password_length_buf_.size()));
  256. ret.emplace_back(as::buffer(password_));
  257. }
  258. return ret;
  259. }
  260. /**
  261. * @brief Get whole size of sequence
  262. * @return whole size
  263. */
  264. std::size_t size() const {
  265. return
  266. 1 + // fixed header
  267. remaining_length_buf_.size() +
  268. remaining_length_;
  269. }
  270. /**
  271. * @brief Get number of element of const_buffer_sequence
  272. * @return number of element of const_buffer_sequence
  273. */
  274. constexpr std::size_t num_of_const_buffer_sequence() const {
  275. return num_of_const_buffer_sequence_;
  276. }
  277. /**
  278. * @brief Create one continuours buffer.
  279. * All sequence of buffers are concatinated.
  280. * It is useful to store to file/database.
  281. * @return continuous buffer
  282. */
  283. std::string continuous_buffer() const {
  284. std::string ret;
  285. ret.reserve(size());
  286. ret.push_back(static_cast<char>(fixed_header_));
  287. ret.append(remaining_length_buf_.data(), remaining_length_buf_.size());
  288. ret.append(protocol_name_and_level_.data(), protocol_name_and_level_.size());
  289. ret.push_back(connect_flags_);
  290. ret.append(keep_alive_buf_.data(), keep_alive_buf_.size());
  291. ret.append(property_length_buf_.data(), property_length_buf_.size());
  292. auto it = ret.end();
  293. ret.resize(ret.size() + property_length_);
  294. auto end = ret.end();
  295. for (auto const& p : props_) {
  296. v5::fill(p, it, end);
  297. it += static_cast<std::string::difference_type>(v5::size(p));
  298. }
  299. ret.append(client_id_length_buf_.data(), client_id_length_buf_.size());
  300. ret.append(client_id_.data(), client_id_.size());
  301. if (connect_flags::has_will_flag(connect_flags_)) {
  302. ret.append(will_property_length_buf_.data(), will_property_length_buf_.size());
  303. auto it = ret.end();
  304. ret.resize(ret.size() + will_property_length_);
  305. auto end = ret.end();
  306. for (auto const& p : will_props_) {
  307. v5::fill(p, it, end);
  308. it += static_cast<std::string::difference_type>(v5::size(p));
  309. }
  310. ret.append(will_topic_name_length_buf_.data(), will_topic_name_length_buf_.size());
  311. ret.append(will_topic_name_.data(), will_topic_name_.size());
  312. ret.append(will_message_length_buf_.data(), will_message_length_buf_.size());
  313. ret.append(will_message_.data(), will_message_.size());
  314. }
  315. if (connect_flags::has_user_name_flag(connect_flags_)) {
  316. ret.append(user_name_length_buf_.data(), user_name_length_buf_.size());
  317. ret.append(user_name_.data(), user_name_.size());
  318. }
  319. if (connect_flags::has_password_flag(connect_flags_)) {
  320. ret.append(password_length_buf_.data(), password_length_buf_.size());
  321. ret.append(password_.data(), password_.size());
  322. }
  323. return ret;
  324. }
  325. private:
  326. std::uint8_t fixed_header_;
  327. char connect_flags_;
  328. std::size_t remaining_length_;
  329. boost::container::static_vector<char, 4> remaining_length_buf_;
  330. boost::container::static_vector<char, 7> protocol_name_and_level_;
  331. buffer client_id_;
  332. boost::container::static_vector<char, 2> client_id_length_buf_;
  333. std::size_t will_property_length_;
  334. boost::container::static_vector<char, 4> will_property_length_buf_;
  335. properties will_props_;
  336. buffer will_topic_name_;
  337. boost::container::static_vector<char, 2> will_topic_name_length_buf_;
  338. buffer will_message_;
  339. boost::container::static_vector<char, 2> will_message_length_buf_;
  340. buffer user_name_;
  341. boost::container::static_vector<char, 2> user_name_length_buf_;
  342. buffer password_;
  343. boost::container::static_vector<char, 2> password_length_buf_;
  344. boost::container::static_vector<char, 2> keep_alive_buf_;
  345. std::size_t property_length_;
  346. boost::container::static_vector<char, 4> property_length_buf_;
  347. properties props_;
  348. std::size_t num_of_const_buffer_sequence_;
  349. };
  350. class connack_message {
  351. public:
  352. connack_message(
  353. bool session_present,
  354. connect_reason_code reason_code,
  355. properties props
  356. )
  357. : fixed_header_(make_fixed_header(control_packet_type::connack, 0b0000)),
  358. remaining_length_(
  359. 1 + // connect acknowledge flags
  360. 1 // reason code
  361. ),
  362. connect_acknowledge_flags_(session_present ? 1 : 0),
  363. reason_code_(reason_code),
  364. property_length_(
  365. std::accumulate(
  366. props.begin(),
  367. props.end(),
  368. std::size_t(0U),
  369. [](std::size_t total, property_variant const& pv) {
  370. return total + v5::size(pv);
  371. }
  372. )
  373. ),
  374. props_(force_move(props)),
  375. num_of_const_buffer_sequence_(
  376. 1 + // fixed header
  377. 1 + // remaining length
  378. 1 + // connect acknowledge flags
  379. 1 + // reason code
  380. 1 + // property length
  381. std::accumulate(
  382. props_.begin(),
  383. props_.end(),
  384. std::size_t(0U),
  385. [](std::size_t total, property_variant const& pv) {
  386. return total + v5::num_of_const_buffer_sequence(pv);
  387. }
  388. )
  389. )
  390. {
  391. auto pb = variable_bytes(property_length_);
  392. for (auto e : pb) {
  393. property_length_buf_.push_back(e);
  394. }
  395. remaining_length_ += property_length_buf_.size() + property_length_;
  396. auto rb = remaining_bytes(remaining_length_);
  397. for (auto e : rb) {
  398. remaining_length_buf_.push_back(e);
  399. }
  400. }
  401. /**
  402. * @brief Create const buffer sequence
  403. * it is for boost asio APIs
  404. * @return const buffer sequence
  405. */
  406. std::vector<as::const_buffer> const_buffer_sequence() const {
  407. std::vector<as::const_buffer> ret;
  408. ret.reserve(num_of_const_buffer_sequence());
  409. ret.emplace_back(as::buffer(&fixed_header_, 1));
  410. ret.emplace_back(as::buffer(remaining_length_buf_.data(), remaining_length_buf_.size()));
  411. ret.emplace_back(as::buffer(&connect_acknowledge_flags_, 1));
  412. ret.emplace_back(as::buffer(&reason_code_, 1));
  413. ret.emplace_back(as::buffer(property_length_buf_.data(), property_length_buf_.size()));
  414. for (auto const& p : props_) {
  415. v5::add_const_buffer_sequence(ret, p);
  416. }
  417. return ret;
  418. }
  419. /**
  420. * @brief Get whole size of sequence
  421. * @return whole size
  422. */
  423. std::size_t size() const {
  424. return
  425. 1 + // fixed header
  426. remaining_length_buf_.size() +
  427. remaining_length_;
  428. }
  429. /**
  430. * @brief Get number of element of const_buffer_sequence
  431. * @return number of element of const_buffer_sequence
  432. */
  433. constexpr std::size_t num_of_const_buffer_sequence() const {
  434. return num_of_const_buffer_sequence_;
  435. }
  436. /**
  437. * @brief Create one continuours buffer.
  438. * All sequence of buffers are concatinated.
  439. * It is useful to store to file/database.
  440. * @return continuous buffer
  441. */
  442. std::string continuous_buffer() const {
  443. std::string ret;
  444. ret.reserve(size());
  445. ret.push_back(static_cast<char>(fixed_header_));
  446. ret.append(remaining_length_buf_.data(), remaining_length_buf_.size());
  447. ret.push_back(static_cast<char>(connect_acknowledge_flags_));
  448. ret.push_back(static_cast<char>(reason_code_));
  449. auto it = ret.end();
  450. ret.resize(ret.size() + property_length_);
  451. auto end = ret.end();
  452. for (auto const& p : props_) {
  453. v5::fill(p, it, end);
  454. it += static_cast<std::string::difference_type>(v5::size(p));
  455. }
  456. return ret;
  457. }
  458. private:
  459. std::uint8_t fixed_header_;
  460. std::size_t remaining_length_;
  461. boost::container::static_vector<char, 4> remaining_length_buf_;
  462. std::uint8_t connect_acknowledge_flags_;
  463. connect_reason_code reason_code_;
  464. std::size_t property_length_;
  465. boost::container::static_vector<char, 4> property_length_buf_;
  466. properties props_;
  467. std::size_t num_of_const_buffer_sequence_;
  468. };
  469. template <std::size_t PacketIdBytes>
  470. class basic_publish_message {
  471. public:
  472. template <
  473. typename ConstBufferSequence,
  474. typename std::enable_if<
  475. as::is_const_buffer_sequence<ConstBufferSequence>::value,
  476. std::nullptr_t
  477. >::type = nullptr
  478. >
  479. basic_publish_message(
  480. typename packet_id_type<PacketIdBytes>::type packet_id,
  481. as::const_buffer topic_name,
  482. ConstBufferSequence payloads,
  483. publish_options pubopts,
  484. properties props
  485. )
  486. : fixed_header_(make_fixed_header(control_packet_type::publish, 0b0000) | pubopts.operator std::uint8_t()),
  487. topic_name_(topic_name),
  488. topic_name_length_buf_ { num_to_2bytes(boost::numeric_cast<std::uint16_t>(topic_name_.size())) },
  489. property_length_(
  490. std::accumulate(
  491. props.begin(),
  492. props.end(),
  493. std::size_t(0U),
  494. [](std::size_t total, property_variant const& pv) {
  495. return total + v5::size(pv);
  496. }
  497. )
  498. ),
  499. props_(force_move(props)),
  500. remaining_length_(
  501. 2 // topic name length
  502. + topic_name_.size() // topic name
  503. + ( (pubopts.get_qos() == qos::at_least_once || pubopts.get_qos() == qos::exactly_once)
  504. ? PacketIdBytes // packet_id
  505. : 0)
  506. ),
  507. num_of_const_buffer_sequence_(
  508. 1 + // fixed header
  509. 1 + // remaining length
  510. 1 + // topic name length
  511. 1 + // topic name
  512. ((pubopts.get_qos() == qos::at_most_once) ? 0U : 1U) + // packet id
  513. 1 + // property length
  514. std::accumulate(
  515. props_.begin(),
  516. props_.end(),
  517. std::size_t(0U),
  518. [](std::size_t total, property_variant const& pv) {
  519. return total + v5::num_of_const_buffer_sequence(pv);
  520. }
  521. )
  522. )
  523. {
  524. auto b = as::buffer_sequence_begin(payloads);
  525. auto e = as::buffer_sequence_end(payloads);
  526. auto num_of_payloads = static_cast<std::size_t>(std::distance(b, e));
  527. payloads_.reserve(num_of_payloads);
  528. for (; b != e; ++b) {
  529. auto const& payload = *b;
  530. remaining_length_ += payload.size();
  531. payloads_.push_back(payload);
  532. }
  533. num_of_const_buffer_sequence_ += num_of_payloads;
  534. utf8string_check(topic_name_);
  535. auto pb = variable_bytes(property_length_);
  536. for (auto e : pb) {
  537. property_length_buf_.push_back(e);
  538. }
  539. remaining_length_ += property_length_buf_.size() + property_length_;
  540. auto rb = remaining_bytes(remaining_length_);
  541. for (auto e : rb) {
  542. remaining_length_buf_.push_back(e);
  543. }
  544. if (pubopts.get_qos() == qos::at_least_once ||
  545. pubopts.get_qos() == qos::exactly_once) {
  546. packet_id_.reserve(PacketIdBytes);
  547. add_packet_id_to_buf<PacketIdBytes>::apply(packet_id_, packet_id);
  548. }
  549. }
  550. basic_publish_message(buffer buf) {
  551. if (buf.empty()) throw remaining_length_error();
  552. fixed_header_ = static_cast<std::uint8_t>(buf.front());
  553. qos qos_value = get_qos();
  554. buf.remove_prefix(1);
  555. if (buf.empty()) throw remaining_length_error();
  556. auto len_consumed = remaining_length(buf.begin(), buf.end());
  557. remaining_length_ = std::get<0>(len_consumed);
  558. auto consumed = std::get<1>(len_consumed);
  559. std::copy(
  560. buf.begin(),
  561. std::next(buf.begin(), static_cast<buffer::difference_type>(consumed)),
  562. std::back_inserter(remaining_length_buf_));
  563. buf.remove_prefix(consumed);
  564. if (buf.size() < 2) throw remaining_length_error();
  565. std::copy(buf.begin(), std::next(buf.begin(), 2), std::back_inserter(topic_name_length_buf_));
  566. auto topic_name_length = make_uint16_t(buf.begin(), std::next(buf.begin(), 2));
  567. buf.remove_prefix(2);
  568. if (buf.size() < topic_name_length) throw remaining_length_error();
  569. topic_name_ = as::buffer(buf.substr(0, topic_name_length));
  570. utf8string_check(topic_name_);
  571. buf.remove_prefix(topic_name_length);
  572. switch (qos_value) {
  573. case qos::at_most_once:
  574. break;
  575. case qos::at_least_once:
  576. case qos::exactly_once:
  577. if (buf.size() < PacketIdBytes) throw remaining_length_error();
  578. std::copy(buf.begin(), std::next(buf.begin(), PacketIdBytes), std::back_inserter(packet_id_));
  579. buf.remove_prefix(PacketIdBytes);
  580. break;
  581. default:
  582. throw protocol_error();
  583. break;
  584. };
  585. auto len_consume = variable_length(
  586. buf.begin(),
  587. buf.end()
  588. );
  589. property_length_ = std::get<0>(len_consume);
  590. auto consume = std::get<1>(len_consume);
  591. if (consume == 0) throw property_length_error();
  592. std::copy(
  593. buf.begin(),
  594. std::next(buf.begin(), static_cast<buffer::difference_type>(consume)),
  595. std::back_inserter(property_length_buf_)
  596. );
  597. buf.remove_prefix(consume);
  598. if (buf.size() < property_length_) throw property_length_error();
  599. props_ = property::parse(buf.substr(0, property_length_));
  600. buf.remove_prefix(property_length_);
  601. if (!buf.empty()) {
  602. payloads_.emplace_back(as::buffer(buf));
  603. }
  604. num_of_const_buffer_sequence_ =
  605. 1 + // fixed header
  606. 1 + // remaining length
  607. 1 + // topic name length
  608. 1 + // topic name
  609. ((qos_value == qos::at_most_once) ? 0U : 1U) + // packet id
  610. 1 + // property length
  611. std::accumulate(
  612. props_.begin(),
  613. props_.end(),
  614. std::size_t(0U),
  615. [](std::size_t total, property_variant const& pv) {
  616. return total + v5::num_of_const_buffer_sequence(pv);
  617. }
  618. ) +
  619. payloads_.size(); // payload
  620. }
  621. /**
  622. * @brief Create const buffer sequence
  623. * it is for boost asio APIs
  624. * @return const buffer sequence
  625. */
  626. std::vector<as::const_buffer> const_buffer_sequence() const {
  627. std::vector<as::const_buffer> ret;
  628. ret.reserve(num_of_const_buffer_sequence());
  629. ret.emplace_back(as::buffer(&fixed_header_, 1));
  630. ret.emplace_back(as::buffer(remaining_length_buf_.data(), remaining_length_buf_.size()));
  631. ret.emplace_back(topic_name_length_buf_.data(), topic_name_length_buf_.size());
  632. ret.emplace_back(as::buffer(topic_name_));
  633. if (!packet_id_.empty()) {
  634. ret.emplace_back(as::buffer(packet_id_.data(), packet_id_.size()));
  635. }
  636. ret.emplace_back(as::buffer(property_length_buf_.data(), property_length_buf_.size()));
  637. for (auto const& p : props_) {
  638. v5::add_const_buffer_sequence(ret, p);
  639. }
  640. std::copy(payloads_.begin(), payloads_.end(), std::back_inserter(ret));
  641. return ret;
  642. }
  643. /**
  644. * @brief Get whole size of sequence
  645. * @return whole size
  646. */
  647. std::size_t size() const {
  648. return
  649. 1 + // fixed header
  650. remaining_length_buf_.size() +
  651. remaining_length_;
  652. }
  653. /**
  654. * @brief Get number of element of const_buffer_sequence
  655. * @return number of element of const_buffer_sequence
  656. */
  657. constexpr std::size_t num_of_const_buffer_sequence() const {
  658. return num_of_const_buffer_sequence_;
  659. }
  660. /**
  661. * @brief Create one continuours buffer.
  662. * All sequence of buffers are concatinated.
  663. * It is useful to store to file/database.
  664. * @return continuous buffer
  665. */
  666. std::string continuous_buffer() const {
  667. std::string ret;
  668. ret.reserve(size());
  669. ret.push_back(static_cast<char>(fixed_header_));
  670. ret.append(remaining_length_buf_.data(), remaining_length_buf_.size());
  671. ret.append(topic_name_length_buf_.data(), topic_name_length_buf_.size());
  672. ret.append(get_pointer(topic_name_), get_size(topic_name_));
  673. ret.append(packet_id_.data(), packet_id_.size());
  674. ret.append(property_length_buf_.data(), property_length_buf_.size());
  675. auto it = ret.end();
  676. ret.resize(ret.size() + property_length_);
  677. auto end = ret.end();
  678. for (auto const& p : props_) {
  679. v5::fill(p, it, end);
  680. it += static_cast<std::string::difference_type>(v5::size(p));
  681. }
  682. for (auto const& payload : payloads_) {
  683. ret.append(get_pointer(payload), get_size(payload));
  684. }
  685. return ret;
  686. }
  687. /**
  688. * @brief Get packet id
  689. * @return packet_id
  690. */
  691. typename packet_id_type<PacketIdBytes>::type packet_id() const {
  692. return make_packet_id<PacketIdBytes>::apply(packet_id_.begin(), packet_id_.end());
  693. }
  694. /**
  695. * @brief Get publish_options
  696. * @return publish_options.
  697. */
  698. constexpr publish_options get_options() const {
  699. return publish_options(fixed_header_);
  700. }
  701. /**
  702. * @brief Get qos
  703. * @return qos
  704. */
  705. constexpr qos get_qos() const {
  706. return publish::get_qos(fixed_header_);
  707. }
  708. /**
  709. * @brief Check retain flag
  710. * @return true if retain, otherwise return false.
  711. */
  712. constexpr bool is_retain() const {
  713. return publish::is_retain(fixed_header_);
  714. }
  715. /**
  716. * @brief Check dup flag
  717. * @return true if dup, otherwise return false.
  718. */
  719. constexpr bool is_dup() const {
  720. return publish::is_dup(fixed_header_);
  721. }
  722. /**
  723. * @brief Get topic name
  724. * @return topic name
  725. */
  726. string_view topic() const {
  727. return string_view(get_pointer(topic_name_), get_size(topic_name_));
  728. }
  729. /**
  730. * @brief Get payload
  731. * @return payload
  732. */
  733. std::vector<string_view> payload() const {
  734. std::vector<string_view> ret;
  735. ret.reserve(payloads_.size());
  736. for (auto const& payload : payloads_) {
  737. ret.emplace_back(get_pointer(payload), get_size(payload));
  738. }
  739. return ret;
  740. }
  741. /**
  742. * @brief Get payload as single buffer
  743. * @return payload
  744. */
  745. buffer payload_as_buffer() const {
  746. auto size = std::accumulate(
  747. payloads_.begin(),
  748. payloads_.end(),
  749. std::size_t(0),
  750. [](std::size_t s, as::const_buffer const& payload) {
  751. return s += payload.size();
  752. }
  753. );
  754. if (size == 0) return buffer();
  755. auto spa = make_shared_ptr_array(size);
  756. auto ptr = spa.get();
  757. auto it = ptr;
  758. for (auto const& payload : payloads_) {
  759. auto b = get_pointer(payload);
  760. auto s = get_size(payload);
  761. auto e = b + s;
  762. std::copy(b, e, it);
  763. it += s;
  764. }
  765. return buffer(string_view(ptr, size), force_move(spa));
  766. }
  767. /**
  768. * @brief Get properties
  769. * @return properties
  770. */
  771. properties const& props() const {
  772. return props_;
  773. }
  774. /**
  775. * @brief Add property
  776. * @param p property to add
  777. */
  778. void add_prop(property_variant p) {
  779. auto add_size = v5::size(p);
  780. props_.push_back(force_move(p));
  781. property_length_ += add_size;
  782. property_length_buf_.clear();
  783. auto pb = variable_bytes(property_length_);
  784. for (auto e : pb) {
  785. property_length_buf_.push_back(e);
  786. }
  787. remaining_length_buf_.clear();
  788. remaining_length_ += add_size;
  789. auto rb = remaining_bytes(remaining_length_);
  790. for (auto e : rb) {
  791. remaining_length_buf_.push_back(e);
  792. }
  793. }
  794. /**
  795. * @brief Update property
  796. * Only fixed size property can be updated.
  797. * @param p property to update
  798. */
  799. template <typename Property>
  800. std::enable_if_t<
  801. std::is_base_of<property::detail::n_bytes_property<1>, Property>::value ||
  802. std::is_base_of<property::detail::n_bytes_property<2>, Property>::value ||
  803. std::is_base_of<property::detail::n_bytes_property<4>, Property>::value
  804. >
  805. update_prop(Property update_prop) {
  806. for (auto& p : props_) {
  807. MQTT_NS::visit(
  808. make_lambda_visitor(
  809. [&update_prop](Property& t) { t = std::forward<Property>(update_prop); },
  810. [](auto&) { }
  811. ),
  812. p
  813. );
  814. }
  815. }
  816. /**
  817. * @brief Remove property
  818. * @param id property::id to remove
  819. */
  820. void remove_prop(v5::property::id id) {
  821. std::size_t removed_size = 0;
  822. auto it = props_.begin();
  823. auto end = props_.begin();
  824. while (it != end) {
  825. if (v5::id(*it) == id) {
  826. removed_size += v5::size(*it);
  827. it = props_.erase(it);
  828. }
  829. else {
  830. ++it;
  831. }
  832. }
  833. property_length_ -= removed_size;
  834. property_length_buf_.clear();
  835. auto pb = variable_bytes(property_length_);
  836. for (auto e : pb) {
  837. property_length_buf_.push_back(e);
  838. }
  839. remaining_length_buf_.clear();
  840. remaining_length_ -= removed_size;
  841. auto rb = remaining_bytes(remaining_length_);
  842. for (auto e : rb) {
  843. remaining_length_buf_.push_back(e);
  844. }
  845. }
  846. /**
  847. * @brief Set dup flag
  848. * @param dup flag value to set
  849. */
  850. constexpr void set_dup(bool dup) {
  851. publish::set_dup(fixed_header_, dup);
  852. }
  853. /**
  854. * @brief Set topic name
  855. * @param topic_name value to set
  856. */
  857. void set_topic_name(as::const_buffer topic_name) {
  858. auto prev_topic_name_size = get_size(topic_name_);
  859. topic_name_ = force_move(topic_name);
  860. topic_name_length_buf_ = boost::container::static_vector<char, 2>{
  861. num_to_2bytes(boost::numeric_cast<std::uint16_t>(get_size(topic_name_)))
  862. };
  863. remaining_length_buf_.clear();
  864. remaining_length_ = remaining_length_ - prev_topic_name_size + get_size(topic_name_);
  865. auto rb = remaining_bytes(remaining_length_);
  866. for (auto e : rb) {
  867. remaining_length_buf_.push_back(e);
  868. }
  869. }
  870. private:
  871. std::uint8_t fixed_header_;
  872. as::const_buffer topic_name_;
  873. boost::container::static_vector<char, 2> topic_name_length_buf_;
  874. boost::container::static_vector<char, PacketIdBytes> packet_id_;
  875. std::size_t property_length_;
  876. boost::container::static_vector<char, 4> property_length_buf_;
  877. properties props_;
  878. std::vector<as::const_buffer> payloads_;
  879. std::size_t remaining_length_;
  880. boost::container::static_vector<char, 4> remaining_length_buf_;
  881. std::size_t num_of_const_buffer_sequence_;
  882. };
  883. using publish_message = basic_publish_message<2>;
  884. using publish_32_message = basic_publish_message<4>;
  885. template <std::size_t PacketIdBytes>
  886. struct basic_puback_message {
  887. basic_puback_message(
  888. typename packet_id_type<PacketIdBytes>::type packet_id,
  889. v5::puback_reason_code reason_code,
  890. properties props)
  891. : fixed_header_(make_fixed_header(control_packet_type::puback, 0b0000)),
  892. reason_code_(reason_code),
  893. property_length_(
  894. std::accumulate(
  895. props.begin(),
  896. props.end(),
  897. std::size_t(0U),
  898. [](std::size_t total, property_variant const& pv) {
  899. return total + v5::size(pv);
  900. }
  901. )
  902. ),
  903. props_(force_move(props)),
  904. num_of_const_buffer_sequence_(
  905. 1 + // fixed header
  906. 1 + // remaining length
  907. 1 + // packet id
  908. // TODO: This is wrong. The reason code MUST be provided
  909. // if there are properties. Not the other way around.
  910. // https://docs.oasis-open.org/mqtt/mqtt/v5.0/os/mqtt-v5.0-os.html#_Toc3901124
  911. // 3.4.2.1 PUBACK Reason Code
  912. // The Reason Code and Property Length can be omitted if
  913. // the Reason Code is 0x00 (Success) and there are no Properties.
  914. // In this case the PUBACK has a Remaining Length of 2.
  915. [&] () -> std::size_t {
  916. if ((reason_code_ != v5::puback_reason_code::success) || MQTT_ALWAYS_SEND_REASON_CODE) {
  917. // https://docs.oasis-open.org/mqtt/mqtt/v5.0/os/mqtt-v5.0-os.html#_Toc3901126
  918. // If the Remaining Length is less than 4 there is no Property Length and the value of 0 is used.
  919. if (props_.empty()) {
  920. return 1; // reason code
  921. }
  922. else {
  923. return
  924. 1 + // reason code
  925. 1 + // property length
  926. std::accumulate( // properties
  927. props_.begin(),
  928. props_.end(),
  929. std::size_t(0U),
  930. [](std::size_t total, property_variant const& pv) {
  931. return total + v5::num_of_const_buffer_sequence(pv);
  932. }
  933. );
  934. }
  935. }
  936. else {
  937. return 0;
  938. }
  939. } ()
  940. )
  941. {
  942. add_packet_id_to_buf<PacketIdBytes>::apply(packet_id_, packet_id);
  943. auto pb = variable_bytes(property_length_);
  944. for (auto e : pb) {
  945. property_length_buf_.push_back(e);
  946. }
  947. remaining_length_ =
  948. PacketIdBytes + // packet id
  949. // TODO: This is wrong. The reason code MUST be provided
  950. // if there are properties. Not the other way around.
  951. // https://docs.oasis-open.org/mqtt/mqtt/v5.0/os/mqtt-v5.0-os.html#_Toc3901124
  952. // 3.4.2.1 PUBACK Reason Code
  953. // The Reason Code and Property Length can be omitted if
  954. // the Reason Code is 0x00 (Success) and there are no Properties.
  955. // In this case the PUBACK has a Remaining Length of 2.
  956. [&] () -> std::size_t {
  957. if ((reason_code_ != v5::puback_reason_code::success) || MQTT_ALWAYS_SEND_REASON_CODE) {
  958. // https://docs.oasis-open.org/mqtt/mqtt/v5.0/os/mqtt-v5.0-os.html#_Toc3901126
  959. // If the Remaining Length is less than 4 there is no Property Length and the value of 0 is used.
  960. if (props_.empty()) {
  961. return 1; // reason code
  962. }
  963. else {
  964. return
  965. 1 + // reason code
  966. property_length_buf_.size() +
  967. property_length_;
  968. }
  969. }
  970. else {
  971. return 0;
  972. }
  973. } ();
  974. auto rb = remaining_bytes(remaining_length_);
  975. for (auto e : rb) {
  976. remaining_length_buf_.push_back(e);
  977. }
  978. }
  979. /**
  980. * @brief Create const buffer sequence
  981. * it is for boost asio APIs
  982. * @return const buffer sequence
  983. */
  984. std::vector<as::const_buffer> const_buffer_sequence() const {
  985. std::vector<as::const_buffer> ret;
  986. ret.reserve(num_of_const_buffer_sequence());
  987. ret.emplace_back(as::buffer(&fixed_header_, 1));
  988. ret.emplace_back(as::buffer(remaining_length_buf_.data(), remaining_length_buf_.size()));
  989. ret.emplace_back(as::buffer(packet_id_.data(), packet_id_.size()));
  990. // TODO: This is wrong. The reason code MUST be provided
  991. // if there are properties. Not the other way around.
  992. // https://docs.oasis-open.org/mqtt/mqtt/v5.0/os/mqtt-v5.0-os.html#_Toc3901124
  993. // 3.4.2.1 PUBACK Reason Code
  994. // The Reason Code and Property Length can be omitted if
  995. // the Reason Code is 0x00 (Success) and there are no Properties.
  996. // In this case the PUBACK has a Remaining Length of 2.
  997. if (reason_code_ != v5::puback_reason_code::success || MQTT_ALWAYS_SEND_REASON_CODE) {
  998. ret.emplace_back(as::buffer(&reason_code_, 1));
  999. // https://docs.oasis-open.org/mqtt/mqtt/v5.0/os/mqtt-v5.0-os.html#_Toc3901126
  1000. // If the Remaining Length is less than 4 there is no Property Length and the value of 0 is used.
  1001. if (!props_.empty()) {
  1002. ret.emplace_back(as::buffer(property_length_buf_.data(), property_length_buf_.size()));
  1003. for (auto const& p : props_) {
  1004. v5::add_const_buffer_sequence(ret, p);
  1005. }
  1006. }
  1007. }
  1008. return ret;
  1009. }
  1010. /**
  1011. * @brief Get whole size of sequence
  1012. * @return whole size
  1013. */
  1014. std::size_t size() const {
  1015. return
  1016. 1 + // fixed header
  1017. remaining_length_buf_.size() +
  1018. remaining_length_;
  1019. }
  1020. /**
  1021. * @brief Get number of element of const_buffer_sequence
  1022. * @return number of element of const_buffer_sequence
  1023. */
  1024. constexpr std::size_t num_of_const_buffer_sequence() const {
  1025. return num_of_const_buffer_sequence_;
  1026. }
  1027. /**
  1028. * @brief Create one continuours buffer.
  1029. * All sequence of buffers are concatinated.
  1030. * It is useful to store to file/database.
  1031. * @return continuous buffer
  1032. */
  1033. std::string continuous_buffer() const {
  1034. std::string ret;
  1035. auto sz = size();
  1036. ret.reserve(sz);
  1037. ret.push_back(static_cast<char>(fixed_header_));
  1038. ret.append(remaining_length_buf_.data(), remaining_length_buf_.size());
  1039. // TODO: This is wrong. The reason code MUST be provided
  1040. // if there are properties. Not the other way around.
  1041. // https://docs.oasis-open.org/mqtt/mqtt/v5.0/os/mqtt-v5.0-os.html#_Toc3901124
  1042. // 3.4.2.1 PUBACK Reason Code
  1043. // The Reason Code and Property Length can be omitted if
  1044. // the Reason Code is 0x00 (Success) and there are no Properties.
  1045. // In this case the PUBACK has a Remaining Length of 2.
  1046. if (reason_code_ != v5::puback_reason_code::success || MQTT_ALWAYS_SEND_REASON_CODE) {
  1047. ret.push_back(static_cast<char>(reason_code_));
  1048. // https://docs.oasis-open.org/mqtt/mqtt/v5.0/os/mqtt-v5.0-os.html#_Toc3901126
  1049. // If the Remaining Length is less than 4 there is no Property Length and the value of 0 is used.
  1050. if (!props_.empty()) {
  1051. ret.append(property_length_buf_.data(), property_length_buf_.size());
  1052. auto it = ret.end();
  1053. ret.resize(sz);
  1054. auto end = ret.end();
  1055. for (auto const& p : props_) {
  1056. v5::fill(p, it, end);
  1057. it += static_cast<std::string::difference_type>(v5::size(p));
  1058. }
  1059. }
  1060. }
  1061. return ret;
  1062. }
  1063. std::uint8_t fixed_header_;
  1064. std::size_t remaining_length_;
  1065. boost::container::static_vector<char, 4> remaining_length_buf_;
  1066. boost::container::static_vector<char, PacketIdBytes> packet_id_;
  1067. v5::puback_reason_code reason_code_;
  1068. std::size_t property_length_;
  1069. boost::container::static_vector<char, 4> property_length_buf_;
  1070. properties props_;
  1071. std::size_t num_of_const_buffer_sequence_;
  1072. };
  1073. using puback_message = basic_puback_message<2>;
  1074. template <std::size_t PacketIdBytes>
  1075. struct basic_pubrec_message {
  1076. basic_pubrec_message(
  1077. typename packet_id_type<PacketIdBytes>::type packet_id,
  1078. pubrec_reason_code reason_code,
  1079. properties props)
  1080. : fixed_header_(make_fixed_header(control_packet_type::pubrec, 0b0000)),
  1081. reason_code_(reason_code),
  1082. property_length_(
  1083. std::accumulate(
  1084. props.begin(),
  1085. props.end(),
  1086. std::size_t(0U),
  1087. [](std::size_t total, property_variant const& pv) {
  1088. return total + v5::size(pv);
  1089. }
  1090. )
  1091. ),
  1092. props_(force_move(props)),
  1093. num_of_const_buffer_sequence_(
  1094. 1 + // fixed header
  1095. 1 + // remaining length
  1096. 1 + // packet id
  1097. // TODO: This is wrong. The reason code MUST be provided
  1098. // if there are properties. Not the other way around.
  1099. // https://docs.oasis-open.org/mqtt/mqtt/v5.0/os/mqtt-v5.0-os.html#_Toc3901134
  1100. // 3.5.2.1 PUBREC Reason Code
  1101. // The Reason Code and Property Length can be omitted if
  1102. // the Reason Code is 0x00 (Success) and there are no Properties.
  1103. // In this case the PUBREC has a Remaining Length of 2.
  1104. [&] () -> std::size_t {
  1105. if ((reason_code_ != v5::pubrec_reason_code::success) || MQTT_ALWAYS_SEND_REASON_CODE) {
  1106. // https://docs.oasis-open.org/mqtt/mqtt/v5.0/os/mqtt-v5.0-os.html#_Toc3901136
  1107. // If the Remaining Length is less than 4 there is no Property Length and the value of 0 is used.
  1108. if (props_.empty()) {
  1109. return 1; // reason code
  1110. }
  1111. else {
  1112. return
  1113. 1 + // reason code
  1114. 1 + // property length
  1115. std::accumulate( // properties
  1116. props_.begin(),
  1117. props_.end(),
  1118. std::size_t(0U),
  1119. [](std::size_t total, property_variant const& pv) {
  1120. return total + v5::num_of_const_buffer_sequence(pv);
  1121. }
  1122. );
  1123. }
  1124. }
  1125. else {
  1126. return 0;
  1127. }
  1128. } ()
  1129. )
  1130. {
  1131. add_packet_id_to_buf<PacketIdBytes>::apply(packet_id_, packet_id);
  1132. auto pb = variable_bytes(property_length_);
  1133. for (auto e : pb) {
  1134. property_length_buf_.push_back(e);
  1135. }
  1136. remaining_length_ =
  1137. PacketIdBytes + // packet id
  1138. // TODO: This is wrong. The reason code MUST be provided
  1139. // if there are properties. Not the other way around.
  1140. // https://docs.oasis-open.org/mqtt/mqtt/v5.0/os/mqtt-v5.0-os.html#_Toc3901134
  1141. // 3.5.2.1 PUBREC Reason Code
  1142. // The Reason Code and Property Length can be omitted if
  1143. // the Reason Code is 0x00 (Success) and there are no Properties.
  1144. // In this case the PUBREC has a Remaining Length of 2.
  1145. [&] () -> std::size_t {
  1146. if ((reason_code_ != v5::pubrec_reason_code::success) || MQTT_ALWAYS_SEND_REASON_CODE) {
  1147. // https://docs.oasis-open.org/mqtt/mqtt/v5.0/os/mqtt-v5.0-os.html#_Toc3901136
  1148. // If the Remaining Length is less than 4 there is no Property Length and the value of 0 is used.
  1149. if (props_.empty()) {
  1150. return 1; // reason code
  1151. }
  1152. else {
  1153. return
  1154. 1 + // reason code
  1155. property_length_buf_.size() +
  1156. property_length_;
  1157. }
  1158. }
  1159. else {
  1160. return 0;
  1161. }
  1162. } ();
  1163. auto rb = remaining_bytes(remaining_length_);
  1164. for (auto e : rb) {
  1165. remaining_length_buf_.push_back(e);
  1166. }
  1167. }
  1168. /**
  1169. * @brief Create const buffer sequence
  1170. * it is for boost asio APIs
  1171. * @return const buffer sequence
  1172. */
  1173. std::vector<as::const_buffer> const_buffer_sequence() const {
  1174. std::vector<as::const_buffer> ret;
  1175. ret.reserve(num_of_const_buffer_sequence());
  1176. ret.emplace_back(as::buffer(&fixed_header_, 1));
  1177. ret.emplace_back(as::buffer(remaining_length_buf_.data(), remaining_length_buf_.size()));
  1178. ret.emplace_back(as::buffer(packet_id_.data(), packet_id_.size()));
  1179. // TODO: This is wrong. The reason code MUST be provided
  1180. // if there are properties. Not the other way around.
  1181. // https://docs.oasis-open.org/mqtt/mqtt/v5.0/os/mqtt-v5.0-os.html#_Toc3901134
  1182. // 3.5.2.1 PUBREC Reason Code
  1183. // The Reason Code and Property Length can be omitted if
  1184. // the Reason Code is 0x00 (Success) and there are no Properties.
  1185. // In this case the PUBREC has a Remaining Length of 2.
  1186. if (reason_code_ != v5::pubrec_reason_code::success || MQTT_ALWAYS_SEND_REASON_CODE) {
  1187. ret.emplace_back(as::buffer(&reason_code_, 1));
  1188. // https://docs.oasis-open.org/mqtt/mqtt/v5.0/os/mqtt-v5.0-os.html#_Toc3901136
  1189. // If the Remaining Length is less than 4 there is no Property Length and the value of 0 is used.
  1190. if (!props_.empty()) {
  1191. ret.emplace_back(as::buffer(property_length_buf_.data(), property_length_buf_.size()));
  1192. for (auto const& p : props_) {
  1193. v5::add_const_buffer_sequence(ret, p);
  1194. }
  1195. }
  1196. }
  1197. return ret;
  1198. }
  1199. /**
  1200. * @brief Get whole size of sequence
  1201. * @return whole size
  1202. */
  1203. std::size_t size() const {
  1204. return
  1205. 1 + // fixed header
  1206. remaining_length_buf_.size() +
  1207. remaining_length_;
  1208. }
  1209. /**
  1210. * @brief Get number of element of const_buffer_sequence
  1211. * @return number of element of const_buffer_sequence
  1212. */
  1213. constexpr std::size_t num_of_const_buffer_sequence() const {
  1214. return num_of_const_buffer_sequence_;
  1215. }
  1216. /**
  1217. * @brief Create one continuours buffer.
  1218. * All sequence of buffers are concatinated.
  1219. * It is useful to store to file/database.
  1220. * @return continuous buffer
  1221. */
  1222. std::string continuous_buffer() const {
  1223. std::string ret;
  1224. auto sz = size();
  1225. ret.reserve(sz);
  1226. ret.push_back(static_cast<char>(fixed_header_));
  1227. ret.append(remaining_length_buf_.data(), remaining_length_buf_.size());
  1228. // TODO: This is wrong. The reason code MUST be provided
  1229. // if there are properties. Not the other way around.
  1230. // https://docs.oasis-open.org/mqtt/mqtt/v5.0/os/mqtt-v5.0-os.html#_Toc3901134
  1231. // 3.5.2.1 PUBREC Reason Code
  1232. // The Reason Code and Property Length can be omitted if
  1233. // the Reason Code is 0x00 (Success) and there are no Properties.
  1234. // In this case the PUBREC has a Remaining Length of 2.
  1235. if (reason_code_ != v5::pubrec_reason_code::success || MQTT_ALWAYS_SEND_REASON_CODE) {
  1236. ret.push_back(static_cast<char>(reason_code_));
  1237. // https://docs.oasis-open.org/mqtt/mqtt/v5.0/os/mqtt-v5.0-os.html#_Toc3901136
  1238. // If the Remaining Length is less than 4 there is no Property Length and the value of 0 is used.
  1239. if (!props_.empty()) {
  1240. ret.append(property_length_buf_.data(), property_length_buf_.size());
  1241. auto it = ret.end();
  1242. ret.resize(sz);
  1243. auto end = ret.end();
  1244. for (auto const& p : props_) {
  1245. v5::fill(p, it, end);
  1246. it += static_cast<std::string::difference_type>(v5::size(p));
  1247. }
  1248. }
  1249. }
  1250. return ret;
  1251. }
  1252. std::uint8_t fixed_header_;
  1253. std::size_t remaining_length_;
  1254. boost::container::static_vector<char, 4> remaining_length_buf_;
  1255. boost::container::static_vector<char, PacketIdBytes> packet_id_;
  1256. pubrec_reason_code reason_code_;
  1257. std::size_t property_length_;
  1258. boost::container::static_vector<char, 4> property_length_buf_;
  1259. properties props_;
  1260. std::size_t num_of_const_buffer_sequence_;
  1261. };
  1262. using pubrec_message = basic_pubrec_message<2>;
  1263. template <std::size_t PacketIdBytes>
  1264. struct basic_pubrel_message {
  1265. basic_pubrel_message(
  1266. typename packet_id_type<PacketIdBytes>::type packet_id,
  1267. v5::pubrel_reason_code reason_code,
  1268. properties props)
  1269. : fixed_header_(make_fixed_header(control_packet_type::pubrel, 0b0010)),
  1270. reason_code_(reason_code),
  1271. property_length_(
  1272. std::accumulate(
  1273. props.begin(),
  1274. props.end(),
  1275. std::size_t(0U),
  1276. [](std::size_t total, property_variant const& pv) {
  1277. return total + v5::size(pv);
  1278. }
  1279. )
  1280. ),
  1281. props_(force_move(props)),
  1282. num_of_const_buffer_sequence_(
  1283. 1 + // fixed header
  1284. 1 + // remaining length
  1285. 1 + // packet id
  1286. // TODO: This is wrong. The reason code MUST be provided
  1287. // if there are properties. Not the other way around.
  1288. // https://docs.oasis-open.org/mqtt/mqtt/v5.0/os/mqtt-v5.0-os.html#_Toc3901144
  1289. // 3.6.2.1 PUBREL Reason Code
  1290. // The Reason Code and Property Length can be omitted if
  1291. // the Reason Code is 0x00 (Success) and there are no Properties.
  1292. // In this case the PUBREL has a Remaining Length of 2.
  1293. [&] () -> std::size_t {
  1294. if ((reason_code_ != v5::pubrel_reason_code::success) || MQTT_ALWAYS_SEND_REASON_CODE) {
  1295. // https://docs.oasis-open.org/mqtt/mqtt/v5.0/os/mqtt-v5.0-os.html#_Toc3901146
  1296. // If the Remaining Length is less than 4 there is no Property Length and the value of 0 is used.
  1297. if (props_.empty()) {
  1298. return 1; // reason code
  1299. }
  1300. else {
  1301. return
  1302. 1 + // reason code
  1303. 1 + // property length
  1304. std::accumulate( // properties
  1305. props_.begin(),
  1306. props_.end(),
  1307. std::size_t(0U),
  1308. [](std::size_t total, property_variant const& pv) {
  1309. return total + v5::num_of_const_buffer_sequence(pv);
  1310. }
  1311. );
  1312. }
  1313. }
  1314. else {
  1315. return 0;
  1316. }
  1317. } ()
  1318. )
  1319. {
  1320. add_packet_id_to_buf<PacketIdBytes>::apply(packet_id_, packet_id);
  1321. auto pb = variable_bytes(property_length_);
  1322. for (auto e : pb) {
  1323. property_length_buf_.push_back(e);
  1324. }
  1325. remaining_length_ =
  1326. PacketIdBytes + // packet id
  1327. // TODO: This is wrong. The reason code MUST be provided
  1328. // if there are properties. Not the other way around.
  1329. // https://docs.oasis-open.org/mqtt/mqtt/v5.0/os/mqtt-v5.0-os.html#_Toc3901144
  1330. // 3.6.2.1 PUBREL Reason Code
  1331. // The Reason Code and Property Length can be omitted if
  1332. // the Reason Code is 0x00 (Success) and there are no Properties.
  1333. // In this case the PUBREL has a Remaining Length of 2.
  1334. [&] () -> std::size_t {
  1335. if ((reason_code_ != v5::pubrel_reason_code::success) || MQTT_ALWAYS_SEND_REASON_CODE) {
  1336. // https://docs.oasis-open.org/mqtt/mqtt/v5.0/os/mqtt-v5.0-os.html#_Toc3901146
  1337. // If the Remaining Length is less than 4 there is no Property Length and the value of 0 is used.
  1338. if (props_.empty()) {
  1339. return 1; // reason code
  1340. }
  1341. else {
  1342. return
  1343. 1 + // reason code
  1344. property_length_buf_.size() +
  1345. property_length_;
  1346. }
  1347. }
  1348. else {
  1349. return 0;
  1350. }
  1351. } ();
  1352. auto rb = remaining_bytes(remaining_length_);
  1353. for (auto e : rb) {
  1354. remaining_length_buf_.push_back(e);
  1355. }
  1356. }
  1357. basic_pubrel_message(buffer buf) {
  1358. if (buf.empty()) throw remaining_length_error();
  1359. fixed_header_ = static_cast<std::uint8_t>(buf.front());
  1360. buf.remove_prefix(1);
  1361. if (buf.empty()) throw remaining_length_error();
  1362. auto len_consumed = remaining_length(buf.begin(), buf.end());
  1363. remaining_length_ = std::get<0>(len_consumed);
  1364. auto consumed = std::get<1>(len_consumed);
  1365. std::copy(
  1366. buf.begin(),
  1367. std::next(buf.begin(), static_cast<buffer::difference_type>(consumed)),
  1368. std::back_inserter(remaining_length_buf_));
  1369. buf.remove_prefix(consumed);
  1370. if (buf.size() < PacketIdBytes) throw remaining_length_error();
  1371. std::copy(buf.begin(), std::next(buf.begin(), PacketIdBytes), std::back_inserter(packet_id_));
  1372. buf.remove_prefix(PacketIdBytes);
  1373. if (buf.empty()) {
  1374. num_of_const_buffer_sequence_ =
  1375. 1 + // fixed header
  1376. 1 + // remaining length
  1377. 1; // packet id
  1378. reason_code_ = v5::pubrel_reason_code::success;
  1379. return;
  1380. }
  1381. reason_code_ = static_cast<v5::pubrel_reason_code>(buf.front());
  1382. buf.remove_prefix(1);
  1383. if (buf.empty()) {
  1384. property_length_ = 0;
  1385. }
  1386. else {
  1387. auto len_consume = variable_length(
  1388. buf.begin(),
  1389. buf.end()
  1390. );
  1391. property_length_ = std::get<0>(len_consume);
  1392. auto consume = std::get<1>(len_consume);
  1393. if (consume == 0) throw property_length_error();
  1394. std::copy(
  1395. buf.begin(),
  1396. std::next(buf.begin(), static_cast<buffer::difference_type>(consume)),
  1397. std::back_inserter(property_length_buf_)
  1398. );
  1399. buf.remove_prefix(consume);
  1400. if (buf.size() != property_length_) throw property_length_error();
  1401. props_ = property::parse(buf);
  1402. buf.remove_prefix(property_length_);
  1403. }
  1404. num_of_const_buffer_sequence_ =
  1405. 1 + // fixed header
  1406. 1 + // remaining length
  1407. 1 + // packet id
  1408. // TODO: This is wrong. The reason code MUST be provided
  1409. // if there are properties. Not the other way around.
  1410. // https://docs.oasis-open.org/mqtt/mqtt/v5.0/os/mqtt-v5.0-os.html#_Toc3901144
  1411. // 3.6.2.1 PUBREL Reason Code
  1412. // The Reason Code and Property Length can be omitted if
  1413. // the Reason Code is 0x00 (Success) and there are no Properties.
  1414. // In this case the PUBREL has a Remaining Length of 2.
  1415. [&] () -> std::size_t {
  1416. if ((reason_code_ != v5::pubrel_reason_code::success) || MQTT_ALWAYS_SEND_REASON_CODE) {
  1417. // https://docs.oasis-open.org/mqtt/mqtt/v5.0/os/mqtt-v5.0-os.html#_Toc3901146
  1418. // If the Remaining Length is less than 4 there is no Property Length and the value of 0 is used.
  1419. if (props_.empty()) {
  1420. return 1; // reason code
  1421. }
  1422. else {
  1423. return
  1424. 1 + // reason code
  1425. 1 + // property length
  1426. std::accumulate( // properties
  1427. props_.begin(),
  1428. props_.end(),
  1429. std::size_t(0U),
  1430. [](std::size_t total, property_variant const& pv) {
  1431. return total + v5::num_of_const_buffer_sequence(pv);
  1432. }
  1433. );
  1434. }
  1435. }
  1436. else {
  1437. return 0;
  1438. }
  1439. } ();
  1440. }
  1441. /**
  1442. * @brief Create const buffer sequence
  1443. * it is for boost asio APIs
  1444. * @return const buffer sequence
  1445. */
  1446. std::vector<as::const_buffer> const_buffer_sequence() const {
  1447. std::vector<as::const_buffer> ret;
  1448. ret.reserve(num_of_const_buffer_sequence());
  1449. ret.emplace_back(as::buffer(&fixed_header_, 1));
  1450. ret.emplace_back(as::buffer(remaining_length_buf_.data(), remaining_length_buf_.size()));
  1451. ret.emplace_back(as::buffer(packet_id_.data(), packet_id_.size()));
  1452. // TODO: This is wrong. The reason code MUST be provided
  1453. // if there are properties. Not the other way around.
  1454. // https://docs.oasis-open.org/mqtt/mqtt/v5.0/os/mqtt-v5.0-os.html#_Toc3901144
  1455. // 3.6.2.1 PUBREL Reason Code
  1456. // The Reason Code and Property Length can be omitted if
  1457. // the Reason Code is 0x00 (Success) and there are no Properties.
  1458. // In this case the PUBREL has a Remaining Length of 2.
  1459. if(reason_code_ != v5::pubrel_reason_code::success || MQTT_ALWAYS_SEND_REASON_CODE) {
  1460. ret.emplace_back(as::buffer(&reason_code_, 1));
  1461. // https://docs.oasis-open.org/mqtt/mqtt/v5.0/os/mqtt-v5.0-os.html#_Toc3901146
  1462. // If the Remaining Length is less than 4 there is no Property Length and the value of 0 is used.
  1463. if (!props_.empty()) {
  1464. ret.emplace_back(as::buffer(property_length_buf_.data(), property_length_buf_.size()));
  1465. for (auto const& p : props_) {
  1466. v5::add_const_buffer_sequence(ret, p);
  1467. }
  1468. }
  1469. }
  1470. return ret;
  1471. }
  1472. /**
  1473. * @brief Get whole size of sequence
  1474. * @return whole size
  1475. */
  1476. std::size_t size() const {
  1477. return
  1478. 1 + // fixed header
  1479. remaining_length_buf_.size() +
  1480. remaining_length_;
  1481. }
  1482. /**
  1483. * @brief Get number of element of const_buffer_sequence
  1484. * @return number of element of const_buffer_sequence
  1485. */
  1486. constexpr std::size_t num_of_const_buffer_sequence() const {
  1487. return num_of_const_buffer_sequence_;
  1488. }
  1489. /**
  1490. * @brief Create one continuours buffer.
  1491. * All sequence of buffers are concatinated.
  1492. * It is useful to store to file/database.
  1493. * @return continuous buffer
  1494. */
  1495. std::string continuous_buffer() const {
  1496. std::string ret;
  1497. auto sz = size();
  1498. ret.reserve(sz);
  1499. ret.push_back(static_cast<char>(fixed_header_));
  1500. ret.append(remaining_length_buf_.data(), remaining_length_buf_.size());
  1501. ret.append(packet_id_.data(), packet_id_.size());
  1502. // TODO: This is wrong. The reason code MUST be provided
  1503. // if there are properties. Not the other way around.
  1504. // https://docs.oasis-open.org/mqtt/mqtt/v5.0/os/mqtt-v5.0-os.html#_Toc3901144
  1505. // 3.6.2.1 PUBREL Reason Code
  1506. // The Reason Code and Property Length can be omitted if
  1507. // the Reason Code is 0x00 (Success) and there are no Properties.
  1508. // In this case the PUBREL has a Remaining Length of 2.
  1509. if (reason_code_ != v5::pubrel_reason_code::success || MQTT_ALWAYS_SEND_REASON_CODE) {
  1510. ret.push_back(static_cast<char>(reason_code_));
  1511. // https://docs.oasis-open.org/mqtt/mqtt/v5.0/os/mqtt-v5.0-os.html#_Toc3901146
  1512. // If the Remaining Length is less than 4 there is no Property Length and the value of 0 is used.
  1513. if (!props_.empty()) {
  1514. ret.append(property_length_buf_.data(), property_length_buf_.size());
  1515. auto it = ret.end();
  1516. ret.resize(sz);
  1517. auto end = ret.end();
  1518. for (auto const& p : props_) {
  1519. v5::fill(p, it, end);
  1520. it += static_cast<std::string::difference_type>(v5::size(p));
  1521. }
  1522. }
  1523. }
  1524. return ret;
  1525. }
  1526. /**
  1527. * @brief Get packet id
  1528. * @return packet_id
  1529. */
  1530. decltype(auto) packet_id() const {
  1531. return make_packet_id<PacketIdBytes>::apply(packet_id_.begin(), packet_id_.end());
  1532. }
  1533. /**
  1534. * @brief Get reason_code
  1535. * @return reason_code
  1536. */
  1537. v5::pubrel_reason_code reason_code() const {
  1538. return reason_code_;
  1539. }
  1540. /**
  1541. * @brief Get properties
  1542. * @return properties
  1543. */
  1544. properties const& props() const {
  1545. return props_;
  1546. }
  1547. std::uint8_t fixed_header_;
  1548. std::size_t remaining_length_;
  1549. boost::container::static_vector<char, 4> remaining_length_buf_;
  1550. boost::container::static_vector<char, PacketIdBytes> packet_id_;
  1551. v5::pubrel_reason_code reason_code_;
  1552. std::size_t property_length_;
  1553. boost::container::static_vector<char, 4> property_length_buf_;
  1554. properties props_;
  1555. std::size_t num_of_const_buffer_sequence_;
  1556. };
  1557. using pubrel_message = basic_pubrel_message<2>;
  1558. using pubrel_32_message = basic_pubrel_message<4>;
  1559. template <std::size_t PacketIdBytes>
  1560. struct basic_pubcomp_message {
  1561. basic_pubcomp_message(
  1562. typename packet_id_type<PacketIdBytes>::type packet_id,
  1563. pubcomp_reason_code reason_code,
  1564. properties props)
  1565. : fixed_header_(make_fixed_header(control_packet_type::pubcomp, 0b0000)),
  1566. reason_code_(reason_code),
  1567. property_length_(
  1568. std::accumulate(
  1569. props.begin(),
  1570. props.end(),
  1571. std::size_t(0U),
  1572. [](std::size_t total, property_variant const& pv) {
  1573. return total + v5::size(pv);
  1574. }
  1575. )
  1576. ),
  1577. props_(force_move(props)),
  1578. num_of_const_buffer_sequence_(
  1579. 1 + // fixed header
  1580. 1 + // remaining length
  1581. 1 + // packet id
  1582. // TODO: This is wrong. The reason code MUST be provided
  1583. // if there are properties. Not the other way around.
  1584. // https://docs.oasis-open.org/mqtt/mqtt/v5.0/os/mqtt-v5.0-os.html#_Toc3901154
  1585. // 3.7.2.1 PUBCOMP Reason Code
  1586. // The Reason Code and Property Length can be omitted if
  1587. // the Reason Code is 0x00 (Success) and there are no Properties.
  1588. // In this case the PUBCOMP has a Remaining Length of 2.
  1589. [&] () -> std::size_t {
  1590. if ((reason_code_ != v5::pubcomp_reason_code::success) || MQTT_ALWAYS_SEND_REASON_CODE) {
  1591. // https://docs.oasis-open.org/mqtt/mqtt/v5.0/os/mqtt-v5.0-os.html#_Toc3901156
  1592. // If the Remaining Length is less than 4 there is no Property Length and the value of 0 is used.
  1593. if (props_.empty()) {
  1594. return 1; // reason code
  1595. }
  1596. else {
  1597. return
  1598. 1 + // reason code
  1599. 1 + // property length
  1600. std::accumulate( // properties
  1601. props_.begin(),
  1602. props_.end(),
  1603. std::size_t(0U),
  1604. [](std::size_t total, property_variant const& pv) {
  1605. return total + v5::num_of_const_buffer_sequence(pv);
  1606. }
  1607. );
  1608. }
  1609. }
  1610. else {
  1611. return 0;
  1612. }
  1613. } ()
  1614. )
  1615. {
  1616. add_packet_id_to_buf<PacketIdBytes>::apply(packet_id_, packet_id);
  1617. auto pb = variable_bytes(property_length_);
  1618. for (auto e : pb) {
  1619. property_length_buf_.push_back(e);
  1620. }
  1621. remaining_length_ =
  1622. PacketIdBytes + // packet id
  1623. // TODO: This is wrong. The reason code MUST be provided
  1624. // if there are properties. Not the other way around.
  1625. // https://docs.oasis-open.org/mqtt/mqtt/v5.0/os/mqtt-v5.0-os.html#_Toc3901154
  1626. // 3.7.2.1 PUBCOMP Reason Code
  1627. // The Reason Code and Property Length can be omitted if
  1628. // the Reason Code is 0x00 (Success) and there are no Properties.
  1629. // In this case the PUBCOMP has a Remaining Length of 2.
  1630. [&] () -> std::size_t {
  1631. if ((reason_code_ != v5::pubcomp_reason_code::success) || MQTT_ALWAYS_SEND_REASON_CODE) {
  1632. // https://docs.oasis-open.org/mqtt/mqtt/v5.0/os/mqtt-v5.0-os.html#_Toc3901156
  1633. // If the Remaining Length is less than 4 there is no Property Length and the value of 0 is used.
  1634. if (props_.empty()) {
  1635. return 1; // reason code
  1636. }
  1637. else {
  1638. return
  1639. 1 + // reason code
  1640. property_length_buf_.size() +
  1641. property_length_;
  1642. }
  1643. }
  1644. else {
  1645. return 0;
  1646. }
  1647. } ();
  1648. auto rb = remaining_bytes(remaining_length_);
  1649. for (auto e : rb) {
  1650. remaining_length_buf_.push_back(e);
  1651. }
  1652. }
  1653. /**
  1654. * @brief Create const buffer sequence
  1655. * it is for boost asio APIs
  1656. * @return const buffer sequence
  1657. */
  1658. std::vector<as::const_buffer> const_buffer_sequence() const {
  1659. std::vector<as::const_buffer> ret;
  1660. ret.reserve(num_of_const_buffer_sequence());
  1661. ret.emplace_back(as::buffer(&fixed_header_, 1));
  1662. ret.emplace_back(as::buffer(remaining_length_buf_.data(), remaining_length_buf_.size()));
  1663. ret.emplace_back(as::buffer(packet_id_.data(), packet_id_.size()));
  1664. // TODO: This is wrong. The reason code MUST be provided
  1665. // if there are properties. Not the other way around.
  1666. // https://docs.oasis-open.org/mqtt/mqtt/v5.0/os/mqtt-v5.0-os.html#_Toc3901154
  1667. // 3.7.2.1 PUBCOMP Reason Code
  1668. // The Reason Code and Property Length can be omitted if
  1669. // the Reason Code is 0x00 (Success) and there are no Properties.
  1670. // In this case the PUBCOMP has a Remaining Length of 2.
  1671. if (reason_code_ != v5::pubcomp_reason_code::success || MQTT_ALWAYS_SEND_REASON_CODE) {
  1672. ret.emplace_back(as::buffer(&reason_code_, 1));
  1673. // https://docs.oasis-open.org/mqtt/mqtt/v5.0/os/mqtt-v5.0-os.html#_Toc3901156
  1674. // If the Remaining Length is less than 4 there is no Property Length and the value of 0 is used.
  1675. if (!props_.empty()) {
  1676. ret.emplace_back(as::buffer(property_length_buf_.data(), property_length_buf_.size()));
  1677. for (auto const& p : props_) {
  1678. v5::add_const_buffer_sequence(ret, p);
  1679. }
  1680. }
  1681. }
  1682. return ret;
  1683. }
  1684. /**
  1685. * @brief Get whole size of sequence
  1686. * @return whole size
  1687. */
  1688. std::size_t size() const {
  1689. return
  1690. 1 + // fixed header
  1691. remaining_length_buf_.size() +
  1692. remaining_length_;
  1693. }
  1694. /**
  1695. * @brief Get number of element of const_buffer_sequence
  1696. * @return number of element of const_buffer_sequence
  1697. */
  1698. constexpr std::size_t num_of_const_buffer_sequence() const {
  1699. return num_of_const_buffer_sequence_;
  1700. }
  1701. /**
  1702. * @brief Create one continuours buffer.
  1703. * All sequence of buffers are concatinated.
  1704. * It is useful to store to file/database.
  1705. * @return continuous buffer
  1706. */
  1707. std::string continuous_buffer() const {
  1708. std::string ret;
  1709. auto sz = size();
  1710. ret.reserve(sz);
  1711. ret.push_back(static_cast<char>(fixed_header_));
  1712. ret.append(remaining_length_buf_.data(), remaining_length_buf_.size());
  1713. // TODO: This is wrong. The reason code MUST be provided
  1714. // if there are properties. Not the other way around.
  1715. // https://docs.oasis-open.org/mqtt/mqtt/v5.0/os/mqtt-v5.0-os.html#_Toc3901154
  1716. // 3.7.2.1 PUBCOMP Reason Code
  1717. // The Reason Code and Property Length can be omitted if
  1718. // the Reason Code is 0x00 (Success) and there are no Properties.
  1719. // In this case the PUBCOMP has a Remaining Length of 2.
  1720. if (reason_code_ != v5::pubcomp_reason_code::success || MQTT_ALWAYS_SEND_REASON_CODE) {
  1721. ret.push_back(static_cast<char>(reason_code_));
  1722. // https://docs.oasis-open.org/mqtt/mqtt/v5.0/os/mqtt-v5.0-os.html#_Toc3901156
  1723. // If the Remaining Length is less than 4 there is no Property Length and the value of 0 is used.
  1724. if (!props_.empty()) {
  1725. ret.append(property_length_buf_.data(), property_length_buf_.size());
  1726. auto it = ret.end();
  1727. ret.resize(sz);
  1728. auto end = ret.end();
  1729. for (auto const& p : props_) {
  1730. v5::fill(p, it, end);
  1731. it += static_cast<std::string::difference_type>(v5::size(p));
  1732. }
  1733. }
  1734. }
  1735. return ret;
  1736. }
  1737. std::uint8_t fixed_header_;
  1738. std::size_t remaining_length_;
  1739. boost::container::static_vector<char, 4> remaining_length_buf_;
  1740. boost::container::static_vector<char, PacketIdBytes> packet_id_;
  1741. pubcomp_reason_code reason_code_;
  1742. std::size_t property_length_;
  1743. boost::container::static_vector<char, 4> property_length_buf_;
  1744. properties props_;
  1745. std::size_t num_of_const_buffer_sequence_;
  1746. };
  1747. using pubcomp_message = basic_pubcomp_message<2>;
  1748. template <std::size_t PacketIdBytes>
  1749. class basic_subscribe_message {
  1750. private:
  1751. struct entry {
  1752. entry(as::const_buffer topic_filter, subscribe_options options)
  1753. : topic_filter_(topic_filter),
  1754. topic_filter_length_buf_ { num_to_2bytes(boost::numeric_cast<std::uint16_t>(topic_filter_.size())) },
  1755. options_(options)
  1756. {}
  1757. as::const_buffer topic_filter_;
  1758. boost::container::static_vector<char, 2> topic_filter_length_buf_;
  1759. subscribe_options options_;
  1760. };
  1761. public:
  1762. basic_subscribe_message(
  1763. std::vector<std::tuple<as::const_buffer, subscribe_options>> params,
  1764. typename packet_id_type<PacketIdBytes>::type packet_id,
  1765. properties props
  1766. )
  1767. : fixed_header_(make_fixed_header(control_packet_type::subscribe, 0b0010)),
  1768. remaining_length_(PacketIdBytes),
  1769. property_length_(
  1770. std::accumulate(
  1771. props.begin(),
  1772. props.end(),
  1773. std::size_t(0U),
  1774. [](std::size_t total, property_variant const& pv) {
  1775. return total + v5::size(pv);
  1776. }
  1777. )
  1778. ),
  1779. props_(force_move(props)),
  1780. num_of_const_buffer_sequence_(
  1781. 1 + // fixed header
  1782. 1 + // remaining length
  1783. 1 + // packet id
  1784. 1 + // property length
  1785. std::accumulate(
  1786. props_.begin(),
  1787. props_.end(),
  1788. std::size_t(0U),
  1789. [](std::size_t total, property_variant const& pv) {
  1790. return total + v5::num_of_const_buffer_sequence(pv);
  1791. }
  1792. ) +
  1793. params.size() * 3 // topic filter length, topic filter, qos
  1794. )
  1795. {
  1796. add_packet_id_to_buf<PacketIdBytes>::apply(packet_id_, packet_id);
  1797. auto pb = variable_bytes(property_length_);
  1798. for (auto e : pb) {
  1799. property_length_buf_.push_back(e);
  1800. }
  1801. remaining_length_ +=
  1802. property_length_buf_.size() +
  1803. property_length_;
  1804. // Check for errors before allocating.
  1805. for (auto&& e : params) {
  1806. as::const_buffer topic_filter = std::get<0>(e);
  1807. utf8string_check(topic_filter);
  1808. }
  1809. entries_.reserve(params.size());
  1810. for (auto&& e : params) {
  1811. as::const_buffer topic_filter = std::get<0>(e);
  1812. size_t size = topic_filter.size();
  1813. entries_.emplace_back(topic_filter, std::get<1>(e));
  1814. remaining_length_ +=
  1815. 2 + // topic filter length
  1816. size + // topic filter
  1817. 1; // means QoS
  1818. }
  1819. auto rb = remaining_bytes(remaining_length_);
  1820. for (auto e : rb) {
  1821. remaining_length_buf_.push_back(e);
  1822. }
  1823. }
  1824. /**
  1825. * @brief Create const buffer sequence
  1826. * it is for boost asio APIs
  1827. * @return const buffer sequence
  1828. */
  1829. std::vector<as::const_buffer> const_buffer_sequence() const {
  1830. std::vector<as::const_buffer> ret;
  1831. ret.reserve(num_of_const_buffer_sequence());
  1832. ret.emplace_back(as::buffer(&fixed_header_, 1));
  1833. ret.emplace_back(as::buffer(remaining_length_buf_.data(), remaining_length_buf_.size()));
  1834. ret.emplace_back(as::buffer(packet_id_.data(), packet_id_.size()));
  1835. ret.emplace_back(as::buffer(property_length_buf_.data(), property_length_buf_.size()));
  1836. for (auto const& p : props_) {
  1837. v5::add_const_buffer_sequence(ret, p);
  1838. }
  1839. for (auto const& e : entries_) {
  1840. ret.emplace_back(as::buffer(e.topic_filter_length_buf_.data(), e.topic_filter_length_buf_.size()));
  1841. ret.emplace_back(as::buffer(e.topic_filter_));
  1842. ret.emplace_back(as::buffer(&e.options_, 1));
  1843. }
  1844. return ret;
  1845. }
  1846. /**
  1847. * @brief Get whole size of sequence
  1848. * @return whole size
  1849. */
  1850. std::size_t size() const {
  1851. return
  1852. 1 + // fixed header
  1853. remaining_length_buf_.size() +
  1854. remaining_length_;
  1855. }
  1856. /**
  1857. * @brief Get number of element of const_buffer_sequence
  1858. * @return number of element of const_buffer_sequence
  1859. */
  1860. constexpr std::size_t num_of_const_buffer_sequence() const {
  1861. return num_of_const_buffer_sequence_;
  1862. }
  1863. /**
  1864. * @brief Create one continuours buffer.
  1865. * All sequence of buffers are concatinated.
  1866. * It is useful to store to file/database.
  1867. * @return continuous buffer
  1868. */
  1869. std::string continuous_buffer() const {
  1870. std::string ret;
  1871. ret.reserve(size());
  1872. ret.push_back(static_cast<char>(fixed_header_));
  1873. ret.append(remaining_length_buf_.data(), remaining_length_buf_.size());
  1874. ret.append(packet_id_.data(), packet_id_.size());
  1875. ret.append(property_length_buf_.data(), property_length_buf_.size());
  1876. auto it = ret.end();
  1877. ret.resize(ret.size() + property_length_);
  1878. auto end = ret.end();
  1879. for (auto const& p : props_) {
  1880. v5::fill(p, it, end);
  1881. it += static_cast<std::string::difference_type>(v5::size(p));
  1882. }
  1883. for (auto const& e : entries_) {
  1884. ret.append(e.topic_filter_length_buf_.data(), e.topic_filter_length_buf_.size());
  1885. ret.append(get_pointer(e.topic_filter_), get_size(e.topic_filter_));
  1886. ret.push_back(static_cast<char>(e.options_.operator std::uint8_t()));
  1887. }
  1888. return ret;
  1889. }
  1890. private:
  1891. std::uint8_t fixed_header_;
  1892. std::vector<entry> entries_;
  1893. boost::container::static_vector<char, PacketIdBytes> packet_id_;
  1894. std::size_t remaining_length_;
  1895. boost::container::static_vector<char, 4> remaining_length_buf_;
  1896. std::size_t property_length_;
  1897. boost::container::static_vector<char, 4> property_length_buf_;
  1898. properties props_;
  1899. std::size_t num_of_const_buffer_sequence_;
  1900. };
  1901. using subscribe_message = basic_subscribe_message<2>;
  1902. template <std::size_t PacketIdBytes>
  1903. class basic_suback_message {
  1904. public:
  1905. basic_suback_message(
  1906. std::vector<suback_reason_code> reason_codes,
  1907. typename packet_id_type<PacketIdBytes>::type packet_id,
  1908. properties props
  1909. )
  1910. : fixed_header_(make_fixed_header(control_packet_type::suback, 0b0000)),
  1911. remaining_length_(reason_codes.size() + PacketIdBytes),
  1912. property_length_(
  1913. std::accumulate(
  1914. props.begin(),
  1915. props.end(),
  1916. std::size_t(0U),
  1917. [](std::size_t total, property_variant const& pv) {
  1918. return total + v5::size(pv);
  1919. }
  1920. )
  1921. ),
  1922. props_(force_move(props)),
  1923. num_of_const_buffer_sequence_(
  1924. 1 + // fixed header
  1925. 1 + // remaining length
  1926. 1 + // packet id
  1927. 1 + // property length
  1928. std::accumulate(
  1929. props_.begin(),
  1930. props_.end(),
  1931. std::size_t(0U),
  1932. [](std::size_t total, property_variant const& pv) {
  1933. return total + v5::num_of_const_buffer_sequence(pv);
  1934. }
  1935. ) +
  1936. 1 // entries (reason code ...)
  1937. )
  1938. {
  1939. add_packet_id_to_buf<PacketIdBytes>::apply(packet_id_, packet_id);
  1940. auto pb = variable_bytes(property_length_);
  1941. for (auto e : pb) {
  1942. property_length_buf_.push_back(e);
  1943. }
  1944. remaining_length_ +=
  1945. property_length_buf_.size() +
  1946. property_length_;
  1947. auto rb = remaining_bytes(remaining_length_);
  1948. for (auto e : rb) {
  1949. remaining_length_buf_.push_back(e);
  1950. }
  1951. entries_.reserve(reason_codes.size());
  1952. for (auto e : reason_codes) {
  1953. entries_.push_back(static_cast<char>(e));
  1954. }
  1955. }
  1956. /**
  1957. * @brief Create const buffer sequence
  1958. * it is for boost asio APIs
  1959. * @return const buffer sequence
  1960. */
  1961. std::vector<as::const_buffer> const_buffer_sequence() const {
  1962. std::vector<as::const_buffer> ret;
  1963. ret.reserve(num_of_const_buffer_sequence());
  1964. ret.emplace_back(as::buffer(&fixed_header_, 1));
  1965. ret.emplace_back(as::buffer(remaining_length_buf_.data(), remaining_length_buf_.size()));
  1966. ret.emplace_back(as::buffer(packet_id_.data(), packet_id_.size()));
  1967. ret.emplace_back(as::buffer(property_length_buf_.data(), property_length_buf_.size()));
  1968. for (auto const& p : props_) {
  1969. v5::add_const_buffer_sequence(ret, p);
  1970. }
  1971. ret.emplace_back(as::buffer(entries_));
  1972. return ret;
  1973. }
  1974. /**
  1975. * @brief Get whole size of sequence
  1976. * @return whole size
  1977. */
  1978. std::size_t size() const {
  1979. return
  1980. 1 + // fixed header
  1981. remaining_length_buf_.size() +
  1982. remaining_length_;
  1983. }
  1984. /**
  1985. * @brief Get number of element of const_buffer_sequence
  1986. * @return number of element of const_buffer_sequence
  1987. */
  1988. constexpr std::size_t num_of_const_buffer_sequence() const {
  1989. return num_of_const_buffer_sequence_;
  1990. }
  1991. /**
  1992. * @brief Create one continuours buffer.
  1993. * All sequence of buffers are concatinated.
  1994. * It is useful to store to file/database.
  1995. * @return continuous buffer
  1996. */
  1997. std::string continuous_buffer() const {
  1998. std::string ret;
  1999. ret.reserve(size());
  2000. ret.push_back(static_cast<char>(fixed_header_));
  2001. ret.append(remaining_length_buf_.data(), remaining_length_buf_.size());
  2002. ret.append(packet_id_.data(), packet_id_.size());
  2003. auto it = ret.end();
  2004. ret.resize(ret.size() + property_length_);
  2005. auto end = ret.end();
  2006. for (auto const& p : props_) {
  2007. v5::fill(p, it, end);
  2008. it += static_cast<std::string::difference_type>(v5::size(p));
  2009. }
  2010. ret.append(entries_);
  2011. return ret;
  2012. }
  2013. private:
  2014. std::uint8_t fixed_header_;
  2015. std::string entries_;
  2016. boost::container::static_vector<char, PacketIdBytes> packet_id_;
  2017. std::size_t remaining_length_;
  2018. boost::container::static_vector<char, 4> remaining_length_buf_;
  2019. std::size_t property_length_;
  2020. boost::container::static_vector<char, 4> property_length_buf_;
  2021. properties props_;
  2022. std::size_t num_of_const_buffer_sequence_;
  2023. };
  2024. using suback_message = basic_suback_message<2>;
  2025. template <std::size_t PacketIdBytes>
  2026. class basic_unsubscribe_message {
  2027. private:
  2028. struct entry {
  2029. entry(as::const_buffer topic_filter)
  2030. : topic_filter_(topic_filter),
  2031. topic_filter_length_buf_ { num_to_2bytes(boost::numeric_cast<std::uint16_t>(topic_filter.size())) }
  2032. {}
  2033. as::const_buffer topic_filter_;
  2034. boost::container::static_vector<char, 2> topic_filter_length_buf_;
  2035. };
  2036. public:
  2037. basic_unsubscribe_message(
  2038. std::vector<as::const_buffer> params,
  2039. typename packet_id_type<PacketIdBytes>::type packet_id,
  2040. properties props
  2041. )
  2042. : fixed_header_(make_fixed_header(control_packet_type::unsubscribe, 0b0010)),
  2043. remaining_length_(PacketIdBytes),
  2044. property_length_(
  2045. std::accumulate(
  2046. props.begin(),
  2047. props.end(),
  2048. std::size_t(0U),
  2049. [](std::size_t total, property_variant const& pv) {
  2050. return total + v5::size(pv);
  2051. }
  2052. )
  2053. ),
  2054. props_(force_move(props)),
  2055. num_of_const_buffer_sequence_(
  2056. 1 + // fixed header
  2057. 1 + // remaining length
  2058. 1 + // packet id
  2059. 1 + // property length
  2060. std::accumulate(
  2061. props_.begin(),
  2062. props_.end(),
  2063. std::size_t(0U),
  2064. [](std::size_t total, property_variant const& pv) {
  2065. return total + v5::num_of_const_buffer_sequence(pv);
  2066. }
  2067. ) +
  2068. params.size() * 2 // topic filter length, topic filter
  2069. )
  2070. {
  2071. add_packet_id_to_buf<PacketIdBytes>::apply(packet_id_, packet_id);
  2072. auto pb = variable_bytes(property_length_);
  2073. for (auto e : pb) {
  2074. property_length_buf_.push_back(e);
  2075. }
  2076. remaining_length_ +=
  2077. property_length_buf_.size() +
  2078. property_length_;
  2079. // Check for errors before allocating.
  2080. for (auto&& e : params) {
  2081. utf8string_check(e);
  2082. }
  2083. entries_.reserve(params.size());
  2084. for (auto&& e : params) {
  2085. auto size = e.size();
  2086. entries_.emplace_back(e);
  2087. remaining_length_ +=
  2088. 2 + // topic filter length
  2089. size; // topic filter
  2090. }
  2091. auto rb = remaining_bytes(remaining_length_);
  2092. for (auto e : rb) {
  2093. remaining_length_buf_.push_back(e);
  2094. }
  2095. }
  2096. /**
  2097. * @brief Create const buffer sequence
  2098. * it is for boost asio APIs
  2099. * @return const buffer sequence
  2100. */
  2101. std::vector<as::const_buffer> const_buffer_sequence() const {
  2102. std::vector<as::const_buffer> ret;
  2103. ret.reserve(num_of_const_buffer_sequence());
  2104. ret.emplace_back(as::buffer(&fixed_header_, 1));
  2105. ret.emplace_back(as::buffer(remaining_length_buf_.data(), remaining_length_buf_.size()));
  2106. ret.emplace_back(as::buffer(packet_id_.data(), packet_id_.size()));
  2107. ret.emplace_back(as::buffer(property_length_buf_.data(), property_length_buf_.size()));
  2108. for (auto const& p : props_) {
  2109. v5::add_const_buffer_sequence(ret, p);
  2110. }
  2111. for (auto const& e : entries_) {
  2112. ret.emplace_back(as::buffer(e.topic_filter_length_buf_.data(), e.topic_filter_length_buf_.size()));
  2113. ret.emplace_back(as::buffer(e.topic_filter_));
  2114. }
  2115. return ret;
  2116. }
  2117. /**
  2118. * @brief Get whole size of sequence
  2119. * @return whole size
  2120. */
  2121. std::size_t size() const {
  2122. return
  2123. 1 + // fixed header
  2124. remaining_length_buf_.size() +
  2125. remaining_length_;
  2126. }
  2127. /**
  2128. * @brief Get number of element of const_buffer_sequence
  2129. * @return number of element of const_buffer_sequence
  2130. */
  2131. constexpr std::size_t num_of_const_buffer_sequence() const {
  2132. return num_of_const_buffer_sequence_;
  2133. }
  2134. /**
  2135. * @brief Create one continuours buffer.
  2136. * All sequence of buffers are concatinated.
  2137. * It is useful to store to file/database.
  2138. * @return continuous buffer
  2139. */
  2140. std::string continuous_buffer() const {
  2141. std::string ret;
  2142. ret.reserve(size());
  2143. ret.push_back(static_cast<char>(fixed_header_));
  2144. ret.append(remaining_length_buf_.data(), remaining_length_buf_.size());
  2145. ret.append(packet_id_.data(), packet_id_.size());
  2146. auto it = ret.end();
  2147. ret.resize(ret.size() + property_length_);
  2148. auto end = ret.end();
  2149. for (auto const& p : props_) {
  2150. v5::fill(p, it, end);
  2151. it += static_cast<std::string::difference_type>(v5::size(p));
  2152. }
  2153. for (auto const& e : entries_) {
  2154. ret.append(e.topic_filter_length_buf_.data(), e.topic_filter_length_buf_.size());
  2155. ret.append(get_pointer(e.topic_filter_), get_size(e.topic_filter_));
  2156. }
  2157. return ret;
  2158. }
  2159. private:
  2160. std::uint8_t fixed_header_;
  2161. std::vector<entry> entries_;
  2162. boost::container::static_vector<char, PacketIdBytes> packet_id_;
  2163. std::size_t remaining_length_;
  2164. boost::container::static_vector<char, 4> remaining_length_buf_;
  2165. std::size_t property_length_;
  2166. boost::container::static_vector<char, 4> property_length_buf_;
  2167. properties props_;
  2168. std::size_t num_of_const_buffer_sequence_;
  2169. };
  2170. using unsubscribe_message = basic_unsubscribe_message<2>;
  2171. template <std::size_t PacketIdBytes>
  2172. class basic_unsuback_message {
  2173. public:
  2174. basic_unsuback_message(
  2175. std::vector<v5::unsuback_reason_code> reason_codes,
  2176. typename packet_id_type<PacketIdBytes>::type packet_id,
  2177. properties props
  2178. )
  2179. : fixed_header_(make_fixed_header(control_packet_type::unsuback, 0b0000)),
  2180. reason_codes_(force_move(reason_codes)),
  2181. remaining_length_(reason_codes_.size() + PacketIdBytes),
  2182. property_length_(
  2183. std::accumulate(
  2184. props.begin(),
  2185. props.end(),
  2186. std::size_t(0U),
  2187. [](std::size_t total, property_variant const& pv) {
  2188. return total + v5::size(pv);
  2189. }
  2190. )
  2191. ),
  2192. props_(force_move(props)),
  2193. num_of_const_buffer_sequence_(
  2194. 1 + // fixed header
  2195. 1 + // remaining length
  2196. 1 + // packet id
  2197. 1 + // property length
  2198. std::accumulate(
  2199. props_.begin(),
  2200. props_.end(),
  2201. std::size_t(0U),
  2202. [](std::size_t total, property_variant const& pv) {
  2203. return total + v5::num_of_const_buffer_sequence(pv);
  2204. }
  2205. )
  2206. )
  2207. {
  2208. add_packet_id_to_buf<PacketIdBytes>::apply(packet_id_, packet_id);
  2209. auto pb = variable_bytes(property_length_);
  2210. for (auto e : pb) {
  2211. property_length_buf_.push_back(e);
  2212. }
  2213. remaining_length_ +=
  2214. property_length_buf_.size() +
  2215. property_length_;
  2216. auto rb = remaining_bytes(remaining_length_);
  2217. for (auto e : rb) {
  2218. remaining_length_buf_.push_back(e);
  2219. }
  2220. }
  2221. /**
  2222. * @brief Create const buffer sequence
  2223. * it is for boost asio APIs
  2224. * @return const buffer sequence
  2225. */
  2226. std::vector<as::const_buffer> const_buffer_sequence() const {
  2227. std::vector<as::const_buffer> ret;
  2228. ret.reserve(num_of_const_buffer_sequence());
  2229. ret.emplace_back(as::buffer(&fixed_header_, 1));
  2230. ret.emplace_back(as::buffer(remaining_length_buf_.data(), remaining_length_buf_.size()));
  2231. ret.emplace_back(as::buffer(packet_id_.data(), packet_id_.size()));
  2232. ret.emplace_back(as::buffer(property_length_buf_.data(), property_length_buf_.size()));
  2233. for (auto const& p : props_) {
  2234. v5::add_const_buffer_sequence(ret, p);
  2235. }
  2236. ret.emplace_back(as::buffer(reinterpret_cast<char const*>(reason_codes_.data()), reason_codes_.size()));
  2237. return ret;
  2238. }
  2239. /**
  2240. * @brief Get whole size of sequence
  2241. * @return whole size
  2242. */
  2243. std::size_t size() const {
  2244. return
  2245. 1 + // fixed header
  2246. remaining_length_buf_.size() +
  2247. remaining_length_;
  2248. }
  2249. /**
  2250. * @brief Get number of element of const_buffer_sequence
  2251. * @return number of element of const_buffer_sequence
  2252. */
  2253. constexpr std::size_t num_of_const_buffer_sequence() const {
  2254. return num_of_const_buffer_sequence_;
  2255. }
  2256. /**
  2257. * @brief Create one continuours buffer.
  2258. * All sequence of buffers are concatinated.
  2259. * It is useful to store to file/database.
  2260. * @return continuous buffer
  2261. */
  2262. std::string continuous_buffer() const {
  2263. std::string ret;
  2264. ret.reserve(size());
  2265. ret.push_back(static_cast<char>(fixed_header_));
  2266. ret.append(remaining_length_buf_.data(), remaining_length_buf_.size());
  2267. ret.append(packet_id_.data(), packet_id_.size());
  2268. auto it = ret.end();
  2269. ret.resize(ret.size() + property_length_);
  2270. auto end = ret.end();
  2271. for (auto const& p : props_) {
  2272. v5::fill(p, it, end);
  2273. it += static_cast<std::string::difference_type>(v5::size(p));
  2274. }
  2275. ret.append(reinterpret_cast<char const*>(reason_codes_.data()), reason_codes_.size());
  2276. return ret;
  2277. }
  2278. private:
  2279. std::uint8_t fixed_header_;
  2280. std::vector<v5::unsuback_reason_code> reason_codes_;
  2281. boost::container::static_vector<char, PacketIdBytes> packet_id_;
  2282. std::size_t remaining_length_;
  2283. boost::container::static_vector<char, 4> remaining_length_buf_;
  2284. std::size_t property_length_;
  2285. boost::container::static_vector<char, 4> property_length_buf_;
  2286. properties props_;
  2287. std::size_t num_of_const_buffer_sequence_;
  2288. };
  2289. using unsuback_message = basic_unsuback_message<2>;
  2290. struct pingreq_message : detail::header_only_message {
  2291. pingreq_message()
  2292. : detail::header_only_message(control_packet_type::pingreq, 0b0000)
  2293. {}
  2294. };
  2295. struct pingresp_message : detail::header_only_message {
  2296. pingresp_message()
  2297. : detail::header_only_message(control_packet_type::pingresp, 0b0000)
  2298. {}
  2299. };
  2300. struct disconnect_message {
  2301. disconnect_message(
  2302. v5::disconnect_reason_code reason_code,
  2303. properties props
  2304. )
  2305. : fixed_header_(make_fixed_header(control_packet_type::disconnect, 0b0000)),
  2306. remaining_length_(0),
  2307. reason_code_(reason_code),
  2308. property_length_(
  2309. std::accumulate(
  2310. props.begin(),
  2311. props.end(),
  2312. std::size_t(0U),
  2313. [](std::size_t total, property_variant const& pv) {
  2314. return total + v5::size(pv);
  2315. }
  2316. )
  2317. ),
  2318. props_(force_move(props)),
  2319. num_of_const_buffer_sequence_(
  2320. 1 + // fixed header
  2321. 1 + // remaining length
  2322. (
  2323. // TODO: This is wrong. The reason code MUST be provided
  2324. // if there are properties. Not the other way around.
  2325. // https://docs.oasis-open.org/mqtt/mqtt/v5.0/os/mqtt-v5.0-os.html#_Toc3901144
  2326. // 3.14.2.1 Disconnect Reason Code
  2327. // The Reason Code and Property Length can be omitted if
  2328. // the Reason Code is 0x00 (Normal disconnecton) and there are no
  2329. // Properties. In this case the DISCONNECT has a Remaining Length of 0.
  2330. reason_code_ != v5::disconnect_reason_code::normal_disconnection || MQTT_ALWAYS_SEND_REASON_CODE ? (
  2331. 1 + // reason code
  2332. 1 + // property length
  2333. std::accumulate(
  2334. props_.begin(),
  2335. props_.end(),
  2336. std::size_t(0U),
  2337. [](std::size_t total, property_variant const& pv) {
  2338. return total + v5::num_of_const_buffer_sequence(pv);
  2339. }
  2340. )
  2341. )
  2342. : 0
  2343. )
  2344. )
  2345. {
  2346. auto pb = variable_bytes(property_length_);
  2347. for (auto e : pb) {
  2348. property_length_buf_.push_back(e);
  2349. }
  2350. // TODO: This is wrong. The reason code MUST be provided
  2351. // if there are properties. Not the other way around.
  2352. // https://docs.oasis-open.org/mqtt/mqtt/v5.0/os/mqtt-v5.0-os.html#_Toc3901144
  2353. // 3.14.2.1 Disconnect Reason Code
  2354. // The Reason Code and Property Length can be omitted if
  2355. // the Reason Code is 0x00 (Normal disconnecton) and there are no
  2356. // Properties. In this case the DISCONNECT has a Remaining Length of 0.
  2357. if (reason_code_ != v5::disconnect_reason_code::normal_disconnection || MQTT_ALWAYS_SEND_REASON_CODE) {
  2358. remaining_length_ =
  2359. 1 + // reason code
  2360. property_length_buf_.size() +
  2361. property_length_;
  2362. }
  2363. auto rb = remaining_bytes(remaining_length_);
  2364. for (auto e : rb) {
  2365. remaining_length_buf_.push_back(e);
  2366. }
  2367. }
  2368. /**
  2369. * @brief Create const buffer sequence
  2370. * it is for boost asio APIs
  2371. * @return const buffer sequence
  2372. */
  2373. std::vector<as::const_buffer> const_buffer_sequence() const {
  2374. std::vector<as::const_buffer> ret;
  2375. ret.reserve(num_of_const_buffer_sequence());
  2376. ret.emplace_back(as::buffer(&fixed_header_, 1));
  2377. ret.emplace_back(as::buffer(remaining_length_buf_.data(), remaining_length_buf_.size()));
  2378. // TODO: This is wrong. The reason code MUST be provided
  2379. // if there are properties. Not the other way around.
  2380. // https://docs.oasis-open.org/mqtt/mqtt/v5.0/os/mqtt-v5.0-os.html#_Toc3901144
  2381. // 3.14.2.1 Disconnect Reason Code
  2382. // The Reason Code and Property Length can be omitted if
  2383. // the Reason Code is 0x00 (Normal disconnecton) and there are no
  2384. // Properties. In this case the DISCONNECT has a Remaining Length of 0.
  2385. if (reason_code_ != v5::disconnect_reason_code::normal_disconnection || MQTT_ALWAYS_SEND_REASON_CODE) {
  2386. ret.emplace_back(as::buffer(&reason_code_, 1));
  2387. ret.emplace_back(as::buffer(property_length_buf_.data(), property_length_buf_.size()));
  2388. for (auto const& p : props_) {
  2389. v5::add_const_buffer_sequence(ret, p);
  2390. }
  2391. }
  2392. return ret;
  2393. }
  2394. /**
  2395. * @brief Get whole size of sequence
  2396. * @return whole size
  2397. */
  2398. std::size_t size() const {
  2399. return
  2400. 1 + // fixed header
  2401. remaining_length_buf_.size() +
  2402. remaining_length_;
  2403. }
  2404. /**
  2405. * @brief Get number of element of const_buffer_sequence
  2406. * @return number of element of const_buffer_sequence
  2407. */
  2408. constexpr std::size_t num_of_const_buffer_sequence() const {
  2409. return num_of_const_buffer_sequence_;
  2410. }
  2411. /**
  2412. * @brief Create one continuours buffer.
  2413. * All sequence of buffers are concatinated.
  2414. * It is useful to store to file/database.
  2415. * @return continuous buffer
  2416. */
  2417. std::string continuous_buffer() const {
  2418. std::string ret;
  2419. ret.reserve(size());
  2420. ret.push_back(static_cast<char>(fixed_header_));
  2421. ret.append(remaining_length_buf_.data(), remaining_length_buf_.size());
  2422. // TODO: This is wrong. The reason code MUST be provided
  2423. // if there are properties. Not the other way around.
  2424. // https://docs.oasis-open.org/mqtt/mqtt/v5.0/os/mqtt-v5.0-os.html#_Toc3901144
  2425. // 3.14.2.1 Disconnect Reason Code
  2426. // The Reason Code and Property Length can be omitted if
  2427. // the Reason Code is 0x00 (Normal disconnecton) and there are no
  2428. // Properties. In this case the DISCONNECT has a Remaining Length of 0.
  2429. if (reason_code_ != v5::disconnect_reason_code::normal_disconnection || MQTT_ALWAYS_SEND_REASON_CODE) {
  2430. ret.push_back(static_cast<char>(reason_code_));
  2431. auto it = ret.end();
  2432. ret.resize(ret.size() + property_length_);
  2433. auto end = ret.end();
  2434. for (auto const& p : props_) {
  2435. v5::fill(p, it, end);
  2436. it += static_cast<std::string::difference_type>(v5::size(p));
  2437. }
  2438. }
  2439. return ret;
  2440. }
  2441. private:
  2442. std::uint8_t fixed_header_;
  2443. std::size_t remaining_length_;
  2444. boost::container::static_vector<char, 4> remaining_length_buf_;
  2445. v5::disconnect_reason_code reason_code_;
  2446. std::size_t property_length_;
  2447. boost::container::static_vector<char, 4> property_length_buf_;
  2448. properties props_;
  2449. std::size_t num_of_const_buffer_sequence_;
  2450. };
  2451. struct auth_message {
  2452. auth_message(
  2453. v5::auth_reason_code reason_code,
  2454. properties props
  2455. )
  2456. : fixed_header_(make_fixed_header(control_packet_type::auth, 0b0000)),
  2457. remaining_length_(0),
  2458. reason_code_(reason_code),
  2459. property_length_(
  2460. std::accumulate(
  2461. props.begin(),
  2462. props.end(),
  2463. std::size_t(0U),
  2464. [](std::size_t total, property_variant const& pv) {
  2465. return total + v5::size(pv);
  2466. }
  2467. )
  2468. ),
  2469. props_(force_move(props)),
  2470. num_of_const_buffer_sequence_(
  2471. 1 + // fixed header
  2472. 1 + // remaining length
  2473. (
  2474. // TODO: This is wrong. The reason code MUST be provided
  2475. // if there are properties. Not the other way around.
  2476. // https://docs.oasis-open.org/mqtt/mqtt/v5.0/os/mqtt-v5.0-os.html#_Toc3901220
  2477. // 3.15.2.1 Authenticate Reason Code
  2478. // The Reason Code and Property Length can be omitted if
  2479. // the Reason Code is 0x00 (Success) and there are no
  2480. // Properties. In this case the AUTH has a Remaining Length of 0.
  2481. reason_code_ != v5::auth_reason_code::success || MQTT_ALWAYS_SEND_REASON_CODE ?
  2482. (
  2483. 1 + // reason code
  2484. 1 + // property length
  2485. std::accumulate(
  2486. props_.begin(),
  2487. props_.end(),
  2488. std::size_t(0U),
  2489. [](std::size_t total, property_variant const& pv) {
  2490. return total + v5::num_of_const_buffer_sequence(pv);
  2491. }
  2492. )
  2493. )
  2494. : 0
  2495. )
  2496. )
  2497. {
  2498. auto pb = variable_bytes(property_length_);
  2499. for (auto e : pb) {
  2500. property_length_buf_.push_back(e);
  2501. }
  2502. // TODO: This is wrong. The reason code MUST be provided
  2503. // if there are properties. Not the other way around.
  2504. // https://docs.oasis-open.org/mqtt/mqtt/v5.0/os/mqtt-v5.0-os.html#_Toc3901220
  2505. // 3.15.2.1 Authenticate Reason Code
  2506. // The Reason Code and Property Length can be omitted if
  2507. // the Reason Code is 0x00 (Success) and there are no
  2508. // Properties. In this case the AUTH has a Remaining Length of 0.
  2509. if (reason_code_ != v5::auth_reason_code::success || MQTT_ALWAYS_SEND_REASON_CODE) {
  2510. remaining_length_ =
  2511. 1 + // reason code
  2512. property_length_buf_.size() +
  2513. property_length_;
  2514. }
  2515. auto rb = remaining_bytes(remaining_length_);
  2516. for (auto e : rb) {
  2517. remaining_length_buf_.push_back(e);
  2518. }
  2519. }
  2520. /**
  2521. * @brief Create const buffer sequence
  2522. * it is for boost asio APIs
  2523. * @return const buffer sequence
  2524. */
  2525. std::vector<as::const_buffer> const_buffer_sequence() const {
  2526. std::vector<as::const_buffer> ret;
  2527. ret.reserve(num_of_const_buffer_sequence());
  2528. ret.emplace_back(as::buffer(&fixed_header_, 1));
  2529. ret.emplace_back(as::buffer(remaining_length_buf_.data(), remaining_length_buf_.size()));
  2530. // TODO: This is wrong. The reason code MUST be provided
  2531. // if there are properties. Not the other way around.
  2532. // https://docs.oasis-open.org/mqtt/mqtt/v5.0/os/mqtt-v5.0-os.html#_Toc3901220
  2533. // 3.15.2.1 Authenticate Reason Code
  2534. // The Reason Code and Property Length can be omitted if
  2535. // the Reason Code is 0x00 (Success) and there are no
  2536. // Properties. In this case the AUTH has a Remaining Length of 0.
  2537. if (reason_code_ != v5::auth_reason_code::success || MQTT_ALWAYS_SEND_REASON_CODE) {
  2538. ret.emplace_back(as::buffer(&reason_code_, 1));
  2539. ret.emplace_back(as::buffer(property_length_buf_.data(), property_length_buf_.size()));
  2540. for (auto const& p : props_) {
  2541. v5::add_const_buffer_sequence(ret, p);
  2542. }
  2543. }
  2544. return ret;
  2545. }
  2546. /**
  2547. * @brief Get whole size of sequence
  2548. * @return whole size
  2549. */
  2550. std::size_t size() const {
  2551. return
  2552. 1 + // fixed header
  2553. remaining_length_buf_.size() +
  2554. remaining_length_;
  2555. }
  2556. /**
  2557. * @brief Get number of element of const_buffer_sequence
  2558. * @return number of element of const_buffer_sequence
  2559. */
  2560. constexpr std::size_t num_of_const_buffer_sequence() const {
  2561. return num_of_const_buffer_sequence_;
  2562. }
  2563. /**
  2564. * @brief Create one continuours buffer.
  2565. * All sequence of buffers are concatinated.
  2566. * It is useful to store to file/database.
  2567. * @return continuous buffer
  2568. */
  2569. std::string continuous_buffer() const {
  2570. std::string ret;
  2571. ret.reserve(size());
  2572. ret.push_back(static_cast<char>(fixed_header_));
  2573. ret.append(remaining_length_buf_.data(), remaining_length_buf_.size());
  2574. // TODO: This is wrong. The reason code MUST be provided
  2575. // if there are properties. Not the other way around.
  2576. // https://docs.oasis-open.org/mqtt/mqtt/v5.0/os/mqtt-v5.0-os.html#_Toc3901220
  2577. // 3.15.2.1 Authenticate Reason Code
  2578. // The Reason Code and Property Length can be omitted if
  2579. // the Reason Code is 0x00 (Success) and there are no
  2580. // Properties. In this case the AUTH has a Remaining Length of 0.
  2581. if (reason_code_ != v5::auth_reason_code::success || MQTT_ALWAYS_SEND_REASON_CODE) {
  2582. ret.push_back(static_cast<char>(reason_code_));
  2583. auto it = ret.end();
  2584. ret.resize(ret.size() + property_length_);
  2585. auto end = ret.end();
  2586. for (auto const& p : props_) {
  2587. v5::fill(p, it, end);
  2588. it += static_cast<std::string::difference_type>(v5::size(p));
  2589. }
  2590. }
  2591. return ret;
  2592. }
  2593. private:
  2594. std::uint8_t fixed_header_;
  2595. std::size_t remaining_length_;
  2596. boost::container::static_vector<char, 4> remaining_length_buf_;
  2597. v5::auth_reason_code reason_code_;
  2598. std::size_t property_length_;
  2599. boost::container::static_vector<char, 4> property_length_buf_;
  2600. properties props_;
  2601. std::size_t num_of_const_buffer_sequence_;
  2602. };
  2603. } // namespace v5
  2604. } // namespace MQTT_NS
  2605. #endif // MQTT_V5_MESSAGE_HPP