client.hpp 98 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083
  1. // Copyright Takatoshi Kondo 2015
  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_CLIENT_HPP)
  7. #define MQTT_CLIENT_HPP
  8. #include <mqtt/variant.hpp> // should be top to configure variant limit
  9. #include <string>
  10. #include <vector>
  11. #include <functional>
  12. #include <type_traits>
  13. #include <mqtt/namespace.hpp>
  14. #include <mqtt/optional.hpp>
  15. #include <boost/lexical_cast.hpp>
  16. #include <boost/asio.hpp>
  17. #include <mqtt/endpoint.hpp>
  18. #include <mqtt/move.hpp>
  19. #include <mqtt/constant.hpp>
  20. #include <mqtt/callable_overlay.hpp>
  21. #include <mqtt/strand.hpp>
  22. #include <mqtt/null_strand.hpp>
  23. namespace MQTT_NS {
  24. namespace as = boost::asio;
  25. namespace mi = boost::multi_index;
  26. template <typename Socket, std::size_t PacketIdBytes = 2>
  27. class client;
  28. template <typename Ioc>
  29. std::shared_ptr<
  30. callable_overlay<
  31. client<
  32. tcp_endpoint<
  33. as::ip::tcp::socket,
  34. strand
  35. >
  36. >
  37. >
  38. >
  39. make_client(Ioc& ioc, std::string host, std::string port, protocol_version version = protocol_version::v3_1_1);
  40. template <typename Ioc>
  41. std::shared_ptr<
  42. callable_overlay<
  43. client<
  44. tcp_endpoint<
  45. as::ip::tcp::socket,
  46. strand
  47. >
  48. >
  49. >
  50. >
  51. make_client(Ioc& ioc, std::string host, std::uint16_t port, protocol_version version = protocol_version::v3_1_1);
  52. template <typename Ioc>
  53. std::shared_ptr<
  54. callable_overlay<
  55. client<
  56. tcp_endpoint<
  57. as::ip::tcp::socket,
  58. null_strand
  59. >
  60. >
  61. >
  62. >
  63. make_client_no_strand(Ioc& ioc, std::string host, std::string port, protocol_version version = protocol_version::v3_1_1);
  64. template <typename Ioc>
  65. std::shared_ptr<
  66. callable_overlay<
  67. client<
  68. tcp_endpoint<
  69. as::ip::tcp::socket,
  70. null_strand
  71. >
  72. >
  73. >
  74. >
  75. make_client_no_strand(Ioc& ioc, std::string host, std::uint16_t port, protocol_version version = protocol_version::v3_1_1);
  76. #if defined(MQTT_USE_WS)
  77. template <typename Ioc>
  78. std::shared_ptr<
  79. callable_overlay<
  80. client<
  81. ws_endpoint<
  82. as::ip::tcp::socket,
  83. strand
  84. >
  85. >
  86. >
  87. >
  88. make_client_ws(Ioc& ioc, std::string host, std::string port, std::string path = "/", protocol_version version = protocol_version::v3_1_1);
  89. template <typename Ioc>
  90. std::shared_ptr<
  91. callable_overlay<
  92. client<
  93. ws_endpoint<
  94. as::ip::tcp::socket,
  95. strand
  96. >
  97. >
  98. >
  99. >
  100. make_client_ws(Ioc& ioc, std::string host, std::uint16_t port, std::string path = "/", protocol_version version = protocol_version::v3_1_1);
  101. template <typename Ioc>
  102. std::shared_ptr<
  103. callable_overlay<
  104. client<
  105. ws_endpoint<
  106. as::ip::tcp::socket,
  107. null_strand
  108. >
  109. >
  110. >
  111. >
  112. make_client_no_strand_ws(Ioc& ioc, std::string host, std::string port, std::string path = "/", protocol_version version = protocol_version::v3_1_1);
  113. template <typename Ioc>
  114. std::shared_ptr<
  115. callable_overlay<
  116. client<
  117. ws_endpoint<
  118. as::ip::tcp::socket,
  119. null_strand
  120. >
  121. >
  122. >
  123. >
  124. make_client_no_strand_ws(Ioc& ioc, std::string host, std::uint16_t port, std::string path = "/", protocol_version version = protocol_version::v3_1_1);
  125. #endif // defined(MQTT_USE_WS)
  126. #if defined(MQTT_USE_TLS)
  127. template <typename Ioc>
  128. std::shared_ptr<
  129. callable_overlay<
  130. client<
  131. tcp_endpoint<
  132. tls::stream<as::ip::tcp::socket>,
  133. strand
  134. >
  135. >
  136. >
  137. >
  138. make_tls_client(Ioc& ioc, std::string host, std::string port, protocol_version version = protocol_version::v3_1_1);
  139. template <typename Ioc>
  140. std::shared_ptr<
  141. callable_overlay<
  142. client<
  143. tcp_endpoint<
  144. tls::stream<as::ip::tcp::socket>,
  145. strand
  146. >
  147. >
  148. >
  149. >
  150. make_tls_client(Ioc& ioc, std::string host, std::uint16_t port, protocol_version version = protocol_version::v3_1_1);
  151. template <typename Ioc>
  152. std::shared_ptr<
  153. callable_overlay<
  154. client<
  155. tcp_endpoint<
  156. tls::stream<as::ip::tcp::socket>,
  157. null_strand
  158. >
  159. >
  160. >
  161. >
  162. make_tls_client_no_strand(Ioc& ioc, std::string host, std::string port, protocol_version version = protocol_version::v3_1_1);
  163. template <typename Ioc>
  164. std::shared_ptr<
  165. callable_overlay<
  166. client<
  167. tcp_endpoint<
  168. tls::stream<as::ip::tcp::socket>,
  169. null_strand
  170. >
  171. >
  172. >
  173. >
  174. make_tls_client_no_strand(Ioc& ioc, std::string host, std::uint16_t port, protocol_version version = protocol_version::v3_1_1);
  175. #if defined(MQTT_USE_WS)
  176. template <typename Ioc>
  177. std::shared_ptr<
  178. callable_overlay<
  179. client<
  180. ws_endpoint<
  181. tls::stream<as::ip::tcp::socket>,
  182. strand
  183. >
  184. >
  185. >
  186. >
  187. make_tls_client_ws(Ioc& ioc, std::string host, std::string port, std::string path = "/", protocol_version version = protocol_version::v3_1_1);
  188. template <typename Ioc>
  189. std::shared_ptr<
  190. callable_overlay<
  191. client<
  192. ws_endpoint<
  193. tls::stream<as::ip::tcp::socket>,
  194. strand
  195. >
  196. >
  197. >
  198. >
  199. make_tls_client_ws(Ioc& ioc, std::string host, std::uint16_t port, std::string path = "/", protocol_version version = protocol_version::v3_1_1);
  200. template <typename Ioc>
  201. std::shared_ptr<
  202. callable_overlay<
  203. client<
  204. ws_endpoint<
  205. tls::stream<as::ip::tcp::socket>,
  206. null_strand
  207. >
  208. >
  209. >
  210. >
  211. make_tls_client_no_strand_ws(Ioc& ioc, std::string host, std::string port, std::string path = "/", protocol_version version = protocol_version::v3_1_1);
  212. template <typename Ioc>
  213. std::shared_ptr<
  214. callable_overlay<
  215. client<
  216. ws_endpoint<
  217. tls::stream<as::ip::tcp::socket>,
  218. null_strand
  219. >
  220. >
  221. >
  222. >
  223. make_tls_client_no_strand_ws(Ioc& ioc, std::string host, std::uint16_t port, std::string path = "/", protocol_version version = protocol_version::v3_1_1);
  224. #endif // defined(MQTT_USE_WS)
  225. #endif // defined(MQTT_USE_TLS)
  226. // 32bit Packet Id (experimental)
  227. template <typename Ioc>
  228. std::shared_ptr<
  229. callable_overlay<
  230. client<
  231. tcp_endpoint<
  232. as::ip::tcp::socket,
  233. strand
  234. >,
  235. 4
  236. >
  237. >
  238. >
  239. make_client_32(Ioc& ioc, std::string host, std::string port, protocol_version version = protocol_version::v3_1_1);
  240. template <typename Ioc>
  241. std::shared_ptr<
  242. callable_overlay<
  243. client<
  244. tcp_endpoint<
  245. as::ip::tcp::socket,
  246. strand
  247. >,
  248. 4
  249. >
  250. >
  251. >
  252. make_client_32(Ioc& ioc, std::string host, std::uint16_t port, protocol_version version = protocol_version::v3_1_1);
  253. template <typename Ioc>
  254. std::shared_ptr<
  255. callable_overlay<
  256. client<
  257. tcp_endpoint<
  258. as::ip::tcp::socket,
  259. null_strand
  260. >,
  261. 4
  262. >
  263. >
  264. >
  265. make_client_no_strand_32(Ioc& ioc, std::string host, std::string port, protocol_version version = protocol_version::v3_1_1);
  266. template <typename Ioc>
  267. std::shared_ptr<
  268. callable_overlay<
  269. client<
  270. tcp_endpoint<
  271. as::ip::tcp::socket,
  272. null_strand
  273. >,
  274. 4
  275. >
  276. >
  277. >
  278. make_client_no_strand_32(Ioc& ioc, std::string host, std::uint16_t port, protocol_version version = protocol_version::v3_1_1);
  279. #if defined(MQTT_USE_WS)
  280. template <typename Ioc>
  281. std::shared_ptr<
  282. callable_overlay<
  283. client<
  284. ws_endpoint<
  285. as::ip::tcp::socket,
  286. strand
  287. >,
  288. 4
  289. >
  290. >
  291. >
  292. make_client_ws_32(Ioc& ioc, std::string host, std::string port, std::string path = "/", protocol_version version = protocol_version::v3_1_1);
  293. template <typename Ioc>
  294. std::shared_ptr<
  295. callable_overlay<
  296. client<
  297. ws_endpoint<
  298. as::ip::tcp::socket,
  299. strand
  300. >,
  301. 4
  302. >
  303. >
  304. >
  305. make_client_ws_32(Ioc& ioc, std::string host, std::uint16_t port, std::string path = "/", protocol_version version = protocol_version::v3_1_1);
  306. template <typename Ioc>
  307. std::shared_ptr<
  308. callable_overlay<
  309. client<
  310. ws_endpoint<
  311. as::ip::tcp::socket,
  312. null_strand
  313. >,
  314. 4
  315. >
  316. >
  317. >
  318. make_client_no_strand_ws_32(Ioc& ioc, std::string host, std::string port, std::string path = "/", protocol_version version = protocol_version::v3_1_1);
  319. template <typename Ioc>
  320. std::shared_ptr<
  321. callable_overlay<
  322. client<
  323. ws_endpoint<
  324. as::ip::tcp::socket,
  325. null_strand
  326. >,
  327. 4
  328. >
  329. >
  330. >
  331. make_client_no_strand_ws_32(Ioc& ioc, std::string host, std::uint16_t port, std::string path = "/", protocol_version version = protocol_version::v3_1_1);
  332. #endif // defined(MQTT_USE_WS)
  333. #if defined(MQTT_USE_TLS)
  334. template <typename Ioc>
  335. std::shared_ptr<
  336. callable_overlay<
  337. client<
  338. tcp_endpoint<
  339. tls::stream<as::ip::tcp::socket>,
  340. strand
  341. >,
  342. 4
  343. >
  344. >
  345. >
  346. make_tls_client_32(Ioc& ioc, std::string host, std::string port, protocol_version version = protocol_version::v3_1_1);
  347. template <typename Ioc>
  348. std::shared_ptr<
  349. callable_overlay<
  350. client<
  351. tcp_endpoint<
  352. tls::stream<as::ip::tcp::socket>,
  353. strand
  354. >,
  355. 4
  356. >
  357. >
  358. >
  359. make_tls_client_32(Ioc& ioc, std::string host, std::uint16_t port, protocol_version version = protocol_version::v3_1_1);
  360. template <typename Ioc>
  361. std::shared_ptr<
  362. callable_overlay<
  363. client<
  364. tcp_endpoint<
  365. tls::stream<as::ip::tcp::socket>,
  366. null_strand
  367. >,
  368. 4
  369. >
  370. >
  371. >
  372. make_tls_client_no_strand_32(Ioc& ioc, std::string host, std::string port, protocol_version version = protocol_version::v3_1_1);
  373. template <typename Ioc>
  374. std::shared_ptr<
  375. callable_overlay<
  376. client<
  377. tcp_endpoint<
  378. tls::stream<as::ip::tcp::socket>,
  379. null_strand
  380. >,
  381. 4
  382. >
  383. >
  384. >
  385. make_tls_client_no_strand_32(Ioc& ioc, std::string host, std::uint16_t port, protocol_version version = protocol_version::v3_1_1);
  386. #if defined(MQTT_USE_WS)
  387. template <typename Ioc>
  388. std::shared_ptr<
  389. callable_overlay<
  390. client<
  391. ws_endpoint<
  392. tls::stream<as::ip::tcp::socket>,
  393. strand
  394. >,
  395. 4
  396. >
  397. >
  398. >
  399. make_tls_client_ws_32(Ioc& ioc, std::string host, std::string port, std::string path = "/", protocol_version version = protocol_version::v3_1_1);
  400. template <typename Ioc>
  401. std::shared_ptr<
  402. callable_overlay<
  403. client<
  404. ws_endpoint<
  405. tls::stream<as::ip::tcp::socket>,
  406. strand
  407. >,
  408. 4
  409. >
  410. >
  411. >
  412. make_tls_client_ws_32(Ioc& ioc, std::string host, std::uint16_t port, std::string path = "/", protocol_version version = protocol_version::v3_1_1);
  413. template <typename Ioc>
  414. std::shared_ptr<
  415. callable_overlay<
  416. client<
  417. ws_endpoint<
  418. tls::stream<as::ip::tcp::socket>,
  419. null_strand
  420. >,
  421. 4
  422. >
  423. >
  424. >
  425. make_tls_client_no_strand_ws_32(Ioc& ioc, std::string host, std::string port, std::string path = "/", protocol_version version = protocol_version::v3_1_1);
  426. template <typename Ioc>
  427. std::shared_ptr<
  428. callable_overlay<
  429. client<
  430. ws_endpoint<
  431. tls::stream<as::ip::tcp::socket>,
  432. null_strand
  433. >,
  434. 4
  435. >
  436. >
  437. >
  438. make_tls_client_no_strand_ws_32(Ioc& ioc, std::string host, std::uint16_t port, std::string path = "/", protocol_version version = protocol_version::v3_1_1);
  439. #endif // defined(MQTT_USE_WS)
  440. #endif // defined(MQTT_USE_TLS)
  441. template <typename Socket, std::size_t PacketIdBytes>
  442. class client : public endpoint<std::mutex, std::lock_guard, PacketIdBytes> {
  443. using this_type = client<Socket, PacketIdBytes>;
  444. using base = endpoint<std::mutex, std::lock_guard, PacketIdBytes>;
  445. protected:
  446. struct constructor_access{};
  447. public:
  448. using async_handler_t = typename base::async_handler_t;
  449. /**
  450. * Constructor used by factory functions at the end of this file.
  451. */
  452. template<typename ... Args>
  453. explicit client(constructor_access, Args && ... args)
  454. : client(std::forward<Args>(args)...)
  455. { }
  456. /**
  457. * @brief Create no tls client with strand.
  458. * @param ioc io_context object.
  459. * @param host hostname
  460. * @param port port number
  461. * @return client object
  462. */
  463. template <typename Ioc>
  464. friend std::shared_ptr<
  465. callable_overlay<
  466. client<
  467. tcp_endpoint<
  468. as::ip::tcp::socket,
  469. strand
  470. >
  471. >
  472. >
  473. >
  474. make_client(Ioc& ioc, std::string host, std::string port, protocol_version version);
  475. /**
  476. * @brief Create no tls client without strand.
  477. * @param ioc io_context object.
  478. * @param host hostname
  479. * @param port port number
  480. * @return client object
  481. */
  482. template <typename Ioc>
  483. friend std::shared_ptr<
  484. callable_overlay<
  485. client<
  486. tcp_endpoint<
  487. as::ip::tcp::socket,
  488. null_strand
  489. >
  490. >
  491. >
  492. >
  493. make_client_no_strand(Ioc& ioc, std::string host, std::string port, protocol_version version);
  494. #if defined(MQTT_USE_WS)
  495. /**
  496. * @brief Create no tls websocket client with strand.
  497. * @param ioc io_context object.
  498. * @param host hostname
  499. * @param port port number
  500. * @param path path string
  501. * @return client object.
  502. * strand is controlled by ws_endpoint, not endpoint, so client has null_strand template argument.
  503. */
  504. template <typename Ioc>
  505. friend std::shared_ptr<
  506. callable_overlay<
  507. client<
  508. ws_endpoint<
  509. as::ip::tcp::socket,
  510. strand
  511. >
  512. >
  513. >
  514. >
  515. make_client_ws(Ioc& ioc, std::string host, std::string port, std::string path, protocol_version version);
  516. /**
  517. * @brief Create no tls websocket client without strand.
  518. * @param ioc io_context object.
  519. * @param host hostname
  520. * @param port port number
  521. * @param path path string
  522. * @return client object
  523. */
  524. template <typename Ioc>
  525. friend std::shared_ptr<
  526. callable_overlay<
  527. client<
  528. ws_endpoint<
  529. as::ip::tcp::socket,
  530. null_strand
  531. >
  532. >
  533. >
  534. >
  535. make_client_no_strand_ws(Ioc& ioc, std::string host, std::string port, std::string path, protocol_version version);
  536. #endif // defined(MQTT_USE_WS)
  537. #if defined(MQTT_USE_TLS)
  538. /**
  539. * @brief Create tls client with strand.
  540. * @param ioc io_context object.
  541. * @param host hostname
  542. * @param port port number
  543. * @return client object
  544. */
  545. template <typename Ioc>
  546. friend std::shared_ptr<
  547. callable_overlay<
  548. client<
  549. tcp_endpoint<
  550. tls::stream<as::ip::tcp::socket>,
  551. strand
  552. >
  553. >
  554. >
  555. >
  556. make_tls_client(Ioc& ioc, std::string host, std::string port, protocol_version version);
  557. /**
  558. * @brief Create tls client without strand.
  559. * @param ioc io_context object.
  560. * @param host hostname
  561. * @param port port number
  562. * @return client object
  563. */
  564. template <typename Ioc>
  565. friend std::shared_ptr<
  566. callable_overlay<
  567. client<
  568. tcp_endpoint<
  569. tls::stream<as::ip::tcp::socket>,
  570. null_strand
  571. >
  572. >
  573. >
  574. >
  575. make_tls_client_no_strand(Ioc& ioc, std::string host, std::string port, protocol_version version);
  576. #if defined(MQTT_USE_WS)
  577. /**
  578. * @brief Create no tls websocket client with strand.
  579. * @param ioc io_context object.
  580. * @param host hostname
  581. * @param port port number
  582. * @param path path string
  583. * @return client object.
  584. * strand is controlled by ws_endpoint, not endpoint, so client has null_strand template argument.
  585. */
  586. template <typename Ioc>
  587. friend std::shared_ptr<
  588. callable_overlay<
  589. client<
  590. ws_endpoint<
  591. tls::stream<as::ip::tcp::socket>,
  592. strand
  593. >
  594. >
  595. >
  596. >
  597. make_tls_client_ws(Ioc& ioc, std::string host, std::string port, std::string path, protocol_version version);
  598. /**
  599. * @brief Create no tls websocket client without strand.
  600. * @param ioc io_context object.
  601. * @param host hostname
  602. * @param port port number
  603. * @param path path string
  604. * @return client object
  605. */
  606. template <typename Ioc>
  607. friend std::shared_ptr<
  608. callable_overlay<
  609. client<
  610. ws_endpoint<
  611. tls::stream<as::ip::tcp::socket>,
  612. null_strand
  613. >
  614. >
  615. >
  616. >
  617. make_tls_client_no_strand_ws(Ioc& ioc, std::string host, std::string port, std::string path, protocol_version version);
  618. #endif // defined(MQTT_USE_WS)
  619. #endif // defined(MQTT_USE_TLS)
  620. /**
  621. * @brief Create no tls client with strand.
  622. * @param ioc io_context object.
  623. * @param host hostname
  624. * @param port port number
  625. * @return client object
  626. */
  627. template <typename Ioc>
  628. friend std::shared_ptr<
  629. callable_overlay<
  630. client<
  631. tcp_endpoint<
  632. as::ip::tcp::socket,
  633. strand
  634. >,
  635. 4
  636. >
  637. >
  638. >
  639. make_client_32(Ioc& ioc, std::string host, std::string port, protocol_version version);
  640. /**
  641. * @brief Create no tls client without strand.
  642. * @param ioc io_context object.
  643. * @param host hostname
  644. * @param port port number
  645. * @return client object
  646. */
  647. template <typename Ioc>
  648. friend std::shared_ptr<
  649. callable_overlay<
  650. client<
  651. tcp_endpoint<
  652. as::ip::tcp::socket,
  653. null_strand
  654. >,
  655. 4
  656. >
  657. >
  658. >
  659. make_client_no_strand_32(Ioc& ioc, std::string host, std::string port, protocol_version version);
  660. #if defined(MQTT_USE_WS)
  661. /**
  662. * @brief Create no tls websocket client with strand.
  663. * @param ioc io_context object.
  664. * @param host hostname
  665. * @param port port number
  666. * @param path path string
  667. * @return client object.
  668. * strand is controlled by ws_endpoint, not endpoint, so client has null_strand template argument.
  669. */
  670. template <typename Ioc>
  671. friend std::shared_ptr<
  672. callable_overlay<
  673. client<
  674. ws_endpoint<
  675. as::ip::tcp::socket,
  676. strand
  677. >,
  678. 4
  679. >
  680. >
  681. >
  682. make_client_ws_32(Ioc& ioc, std::string host, std::string port, std::string path, protocol_version version);
  683. /**
  684. * @brief Create no tls websocket client without strand.
  685. * @param ioc io_context object.
  686. * @param host hostname
  687. * @param port port number
  688. * @param path path string
  689. * @return client object
  690. */
  691. template <typename Ioc>
  692. friend std::shared_ptr<
  693. callable_overlay<
  694. client<
  695. ws_endpoint<
  696. as::ip::tcp::socket,
  697. null_strand
  698. >,
  699. 4
  700. >
  701. >
  702. >
  703. make_client_no_strand_ws_32(Ioc& ioc, std::string host, std::string port, std::string path, protocol_version version);
  704. #endif // defined(MQTT_USE_WS)
  705. #if defined(MQTT_USE_TLS)
  706. /**
  707. * @brief Create tls client with strand.
  708. * @param ioc io_context object.
  709. * @param host hostname
  710. * @param port port number
  711. * @return client object
  712. */
  713. template <typename Ioc>
  714. friend std::shared_ptr<
  715. callable_overlay<
  716. client<
  717. tcp_endpoint<
  718. tls::stream<as::ip::tcp::socket>,
  719. strand
  720. >,
  721. 4
  722. >
  723. >
  724. >
  725. make_tls_client_32(Ioc& ioc, std::string host, std::string port, protocol_version version);
  726. /**
  727. * @brief Create tls client without strand.
  728. * @param ioc io_context object.
  729. * @param host hostname
  730. * @param port port number
  731. * @return client object
  732. */
  733. template <typename Ioc>
  734. friend std::shared_ptr<
  735. callable_overlay<
  736. client<
  737. tcp_endpoint<
  738. tls::stream<as::ip::tcp::socket>,
  739. null_strand
  740. >,
  741. 4
  742. >
  743. >
  744. >
  745. make_tls_client_no_strand_32(Ioc& ioc, std::string host, std::string port, protocol_version version);
  746. #if defined(MQTT_USE_WS)
  747. /**
  748. * @brief Create no tls websocket client with strand.
  749. * @param ioc io_context object.
  750. * @param host hostname
  751. * @param port port number
  752. * @param path path string
  753. * @return client object.
  754. * strand is controlled by ws_endpoint, not endpoint, so client has null_strand template argument.
  755. */
  756. template <typename Ioc>
  757. friend std::shared_ptr<
  758. callable_overlay<
  759. client<
  760. ws_endpoint<
  761. tls::stream<as::ip::tcp::socket>,
  762. strand
  763. >,
  764. 4
  765. >
  766. >
  767. >
  768. make_tls_client_ws_32(Ioc& ioc, std::string host, std::string port, std::string path, protocol_version version);
  769. /**
  770. * @brief Create no tls websocket client without strand.
  771. * @param ioc io_context object.
  772. * @param host hostname
  773. * @param port port number
  774. * @param path path string
  775. * @return client object
  776. */
  777. template <typename Ioc>
  778. friend std::shared_ptr<
  779. callable_overlay<
  780. client<
  781. ws_endpoint<
  782. tls::stream<as::ip::tcp::socket>,
  783. null_strand
  784. >,
  785. 4
  786. >
  787. >
  788. >
  789. make_tls_client_no_strand_ws_32(Ioc& ioc, std::string host, std::string port, std::string path, protocol_version version);
  790. #endif // defined(MQTT_USE_WS)
  791. #endif // defined(MQTT_USE_TLS)
  792. /**
  793. * @brief Set host.
  794. * @param host host to connect
  795. *
  796. * The host is used from the next connect() or async_connect() call.
  797. */
  798. void set_host(std::string host) {
  799. host_ = force_move(host);
  800. }
  801. /**
  802. * @brief Set port.
  803. * @param port port tp connect
  804. *
  805. * The port is used from the next connect() or async_connect() call.
  806. */
  807. void set_port(std::string port) {
  808. port_ = force_move(port);
  809. }
  810. /**
  811. * @brief Set clean session.
  812. * @param cs clean session
  813. *
  814. * This function should be called before calling connect().<BR>
  815. * See http://docs.oasis-open.org/mqtt/mqtt/v3.1.1/os/mqtt-v3.1.1-os.html#_Toc385349231<BR>
  816. * 3.1.2.4 Clean Session<BR>
  817. * After constructing a endpoint, the clean session is set to false.
  818. */
  819. void set_clean_session(bool cs) {
  820. set_clean_start(cs);
  821. }
  822. /**
  823. * @brief Set clean start.
  824. * @param cs clean start
  825. *
  826. * This function should be called before calling connect().<BR>
  827. * See https://docs.oasis-open.org/mqtt/mqtt/v5.0/os/mqtt-v5.0-os.html#_Toc3901039<BR>
  828. * 3.1.2.4 Clean Start<BR>
  829. * After constructing a endpoint, the clean start is set to false.
  830. */
  831. void set_clean_start(bool cs) {
  832. base::clean_start_ = cs;
  833. }
  834. /**
  835. * @brief Set username.
  836. * @param name username
  837. *
  838. * This function should be called before calling connect().<BR>
  839. * See https://docs.oasis-open.org/mqtt/mqtt/v5.0/os/mqtt-v5.0-os.html#_Toc3901071<BR>
  840. * 3.1.3.5 User Name
  841. */
  842. void set_user_name(std::string name) {
  843. user_name_ = force_move(name);
  844. }
  845. /**
  846. * @brief Clear username.
  847. *
  848. * This function should be called before calling connect().<BR>
  849. * See https://docs.oasis-open.org/mqtt/mqtt/v5.0/os/mqtt-v5.0-os.html#_Toc3901071<BR>
  850. * 3.1.3.5 User Name
  851. */
  852. void set_user_name() {
  853. user_name_ = nullopt;
  854. }
  855. /**
  856. * @brief Set password.
  857. * @param password password
  858. *
  859. * This function should be called before calling connect().<BR>
  860. * See https://docs.oasis-open.org/mqtt/mqtt/v5.0/os/mqtt-v5.0-os.html#_Toc3901072<BR>
  861. * 3.1.3.6 Password
  862. */
  863. void set_password(std::string password) {
  864. password_ = force_move(password);
  865. }
  866. /**
  867. * @brief Clear password.
  868. *
  869. * This function should be called before calling connect().<BR>
  870. * See https://docs.oasis-open.org/mqtt/mqtt/v5.0/os/mqtt-v5.0-os.html#_Toc3901072<BR>
  871. * 3.1.3.6 Password
  872. */
  873. void set_password() {
  874. password_ = nullopt;
  875. }
  876. /**
  877. * @brief Set will.
  878. * @param w will
  879. *
  880. * This function should be called before calling connect().<BR>
  881. * 'will' would be send when endpoint is disconnected without calling disconnect().
  882. */
  883. void set_will(will w) {
  884. will_ = force_move(w);
  885. }
  886. /**
  887. * @brief Clear will.
  888. *
  889. * This function should be called before calling connect().<BR>
  890. */
  891. void set_will() {
  892. will_ = nullopt;
  893. }
  894. template<typename ... Args>
  895. void set_will(Args && ... args) {
  896. will_.emplace(std::forward<Args>(args)...);
  897. }
  898. #if defined(MQTT_USE_TLS)
  899. /**
  900. * @brief Get boost asio ssl context.
  901. * @return ssl context
  902. */
  903. tls::context& get_ssl_context() {
  904. static_assert(has_tls<std::decay_t<decltype(*this)>>::value, "Client is required to support TLS.");
  905. return ctx_;
  906. }
  907. /**
  908. * @brief Get boost asio ssl context.
  909. * @return ssl context
  910. */
  911. tls::context const& get_ssl_context() const {
  912. static_assert(has_tls<std::decay_t<decltype(*this)>>::value, "Client is required to support TLS.");
  913. return ctx_;
  914. }
  915. #endif // defined(MQTT_USE_TLS)
  916. /**
  917. * @brief Set a keep alive second and a ping duration.
  918. * @param keep_alive_sec keep alive seconds
  919. * @param ping_ms ping sending interval
  920. *
  921. * When a endpoint connects to a broker, the endpoint notifies keep_alive_sec to
  922. * the broker.
  923. * After connecting, the broker starts counting a timeout, and the endpoint starts
  924. * sending ping packets for each ping_ms.
  925. * When the broker receives a ping packet, timeout timer is reset.
  926. * If the broker doesn't receive a ping packet within keep_alive_sec, the endpoint
  927. * is disconnected.<BR>
  928. * See https://docs.oasis-open.org/mqtt/mqtt/v5.0/os/mqtt-v5.0-os.html#_Toc464635115<BR>
  929. * 3.1.2.10 Keep Alive
  930. */
  931. void set_keep_alive_sec(std::uint16_t keep_alive_sec, std::chrono::steady_clock::duration ping) {
  932. if ((ping_duration_ != std::chrono::steady_clock::duration::zero()) && base::connected() && (ping == std::chrono::steady_clock::duration::zero())) {
  933. cancel_ping_timer();
  934. }
  935. keep_alive_sec_ = keep_alive_sec;
  936. ping_duration_ = force_move(ping);
  937. }
  938. /**
  939. * @brief Set a keep alive second and a ping milli seconds.
  940. * @param keep_alive_sec keep alive seconds
  941. * @param ping_ms ping sending interval
  942. *
  943. * When a endpoint connects to a broker, the endpoint notifies keep_alive_sec to
  944. * the broker.
  945. * After connecting, the broker starts counting a timeout, and the endpoint starts
  946. * sending ping packets for each ping_ms.
  947. * When the broker receives a ping packet, timeout timer is reset.
  948. * If the broker doesn't receive a ping packet within keep_alive_sec, the endpoint
  949. * is disconnected.<BR>
  950. * See https://docs.oasis-open.org/mqtt/mqtt/v5.0/os/mqtt-v5.0-os.html#_Toc464635115<BR>
  951. * 3.1.2.10 Keep Alive
  952. */
  953. void set_keep_alive_sec_ping_ms(std::uint16_t keep_alive_sec, std::size_t ping_ms) {
  954. set_keep_alive_sec(keep_alive_sec, std::chrono::milliseconds(ping_ms));
  955. }
  956. /**
  957. * @brief Set a keep alive second and a ping milli seconds.
  958. * @param keep_alive_sec keep alive seconds
  959. *
  960. * Call set_keep_alive_sec(keep_alive_sec, std::chrono::seconds(keep_alive_sec / 2))<BR>
  961. * ping_ms is set to a half of keep_alive_sec.<BR>
  962. * See https://docs.oasis-open.org/mqtt/mqtt/v5.0/os/mqtt-v5.0-os.html#_Toc464635115<BR>
  963. * 3.1.2.10 Keep Alive
  964. */
  965. void set_keep_alive_sec(std::uint16_t keep_alive_sec) {
  966. set_keep_alive_sec(keep_alive_sec, std::chrono::seconds(keep_alive_sec / 2));
  967. }
  968. /**
  969. * @brief Connect to a broker
  970. * Before calling connect(), call set_xxx member functions to configure the connection.
  971. * @param session_life_keeper the passed object lifetime will be kept during the session.
  972. */
  973. void connect(any session_life_keeper = any()) {
  974. connect(v5::properties{}, force_move(session_life_keeper));
  975. }
  976. /**
  977. * @brief Connect to a broker
  978. * Before calling connect(), call set_xxx member functions to configure the connection.
  979. * @param ec error code
  980. * @param session_life_keeper the passed object lifetime will be kept during the session.
  981. */
  982. void connect(boost::system::error_code& ec, any session_life_keeper = any()) {
  983. connect(v5::properties{}, ec, force_move(session_life_keeper));
  984. }
  985. /**
  986. * @brief Connect to a broker
  987. * Before calling connect(), call set_xxx member functions to configure the connection.
  988. * @param props properties
  989. * See https://docs.oasis-open.org/mqtt/mqtt/v5.0/os/mqtt-v5.0-os.html#_Toc3901046<BR>
  990. * 3.1.2.11 CONNECT Properties
  991. * @param session_life_keeper the passed object lifetime will be kept during the session.
  992. */
  993. void connect(v5::properties props, any session_life_keeper = any()) {
  994. setup_socket(socket_);
  995. connect_impl(force_move(props), force_move(session_life_keeper), false);
  996. }
  997. /**
  998. * @brief Connect to a broker
  999. * Before calling connect(), call set_xxx member functions to configure the connection.
  1000. * @param props properties
  1001. * See https://docs.oasis-open.org/mqtt/mqtt/v5.0/os/mqtt-v5.0-os.html#_Toc3901046<BR>
  1002. * 3.1.2.11 CONNECT Properties
  1003. * @param ec error code
  1004. * @param session_life_keeper the passed object lifetime will be kept during the session.
  1005. */
  1006. void connect(
  1007. v5::properties props,
  1008. boost::system::error_code& ec,
  1009. any session_life_keeper = any()) {
  1010. setup_socket(socket_);
  1011. connect_impl(force_move(props), force_move(session_life_keeper), ec, false);
  1012. }
  1013. /**
  1014. * @brief Connect to a broker
  1015. * Before calling connect(), call set_xxx member functions to configure the connection.
  1016. * @param socket The library uses the socket instead of internal generation.
  1017. * You can configure the socket prior to connect.
  1018. * @param session_life_keeper the passed object lifetime will be kept during the session.
  1019. * @param underlying_connected If the given socket has aleady been connected, then true.
  1020. * Connecting processes are skipped and send MQTT CONNECT packet.
  1021. * If false then do all underlying connecting process and then
  1022. * send MQTT CONNECT packet.
  1023. */
  1024. void connect(std::shared_ptr<Socket>&& socket, any session_life_keeper = any(), bool underlying_connected = false) {
  1025. connect(force_move(socket), v5::properties{}, force_move(session_life_keeper), underlying_connected);
  1026. }
  1027. /**
  1028. * @brief Connect to a broker
  1029. * Before calling connect(), call set_xxx member functions to configure the connection.
  1030. * @param socket The library uses the socket instead of internal generation.
  1031. * You can configure the socket prior to connect.
  1032. * @param ec error code
  1033. * @param session_life_keeper the passed object lifetime will be kept during the session.
  1034. * @param underlying_connected If the given socket has aleady been connected, then true.
  1035. * Connecting processes are skipped and send MQTT CONNECT packet.
  1036. * If false then do all underlying connecting process and then
  1037. * send MQTT CONNECT packet.
  1038. */
  1039. void connect(
  1040. std::shared_ptr<Socket>&& socket,
  1041. boost::system::error_code& ec,
  1042. any session_life_keeper = any(),
  1043. bool underlying_connected = false) {
  1044. connect(force_move(socket), v5::properties{}, ec, force_move(session_life_keeper), underlying_connected);
  1045. }
  1046. /**
  1047. * @brief Connect to a broker
  1048. * Before calling connect(), call set_xxx member functions to configure the connection.
  1049. * @param socket The library uses the socket instead of internal generation.
  1050. * You can configure the socket prior to connect.
  1051. * @param props properties
  1052. * See https://docs.oasis-open.org/mqtt/mqtt/v5.0/os/mqtt-v5.0-os.html#_Toc3901046<BR>
  1053. * 3.1.2.11 CONNECT Properties
  1054. * @param session_life_keeper the passed object lifetime will be kept during the session.
  1055. * @param underlying_connected If the given socket has aleady been connected, then true.
  1056. * Connecting processes are skipped and send MQTT CONNECT packet.
  1057. * If false then do all underlying connecting process and then
  1058. * send MQTT CONNECT packet.
  1059. */
  1060. void connect(
  1061. std::shared_ptr<Socket>&& socket,
  1062. v5::properties props,
  1063. any session_life_keeper = any(),
  1064. bool underlying_connected = false) {
  1065. socket_ = force_move(socket);
  1066. base::socket_sp_ref() = socket_;
  1067. connect_impl(force_move(props), force_move(session_life_keeper), underlying_connected);
  1068. }
  1069. /**
  1070. * @brief Connect to a broker
  1071. * Before calling connect(), call set_xxx member functions to configure the connection.
  1072. * @param socket The library uses the socket instead of internal generation.
  1073. * You can configure the socket prior to connect.
  1074. * @param props properties
  1075. * See https://docs.oasis-open.org/mqtt/mqtt/v5.0/os/mqtt-v5.0-os.html#_Toc3901046<BR>
  1076. * 3.1.2.11 CONNECT Properties
  1077. * @param ec error code
  1078. * @param session_life_keeper the passed object lifetime will be kept during the session.
  1079. * @param underlying_connected If the given socket has aleady been connected, then true.
  1080. * Connecting processes are skipped and send MQTT CONNECT packet.
  1081. * If false then do all underlying connecting process and then
  1082. * send MQTT CONNECT packet.
  1083. */
  1084. void connect(
  1085. std::shared_ptr<Socket>&& socket,
  1086. v5::properties props,
  1087. boost::system::error_code& ec,
  1088. any session_life_keeper = any(),
  1089. bool underlying_connected = false) {
  1090. socket_ = force_move(socket);
  1091. base::socket_sp_ref() = socket_;
  1092. connect_impl(force_move(props), force_move(session_life_keeper), ec, underlying_connected);
  1093. }
  1094. /**
  1095. * @brief Connect to a broker
  1096. * Before calling connect(), call set_xxx member functions to configure the connection.
  1097. */
  1098. void async_connect() {
  1099. async_connect(any(), async_handler_t());
  1100. }
  1101. /**
  1102. * @brief Connect to a broker
  1103. * Before calling connect(), call set_xxx member functions to configure the connection.
  1104. * @param session_life_keeper the passed object lifetime will be kept during the session.
  1105. */
  1106. template <typename T>
  1107. // to avoid ambiguousness between any and async_handler_t
  1108. std::enable_if_t<
  1109. !std::is_convertible<T, async_handler_t>::value
  1110. >
  1111. async_connect(T session_life_keeper) {
  1112. async_connect(force_move(session_life_keeper), async_handler_t());
  1113. }
  1114. /**
  1115. * @brief Connect to a broker
  1116. * Before calling connect(), call set_xxx member functions to configure the connection.
  1117. * @param func finish handler that is called when the underlying connection process is finished
  1118. */
  1119. void async_connect(async_handler_t func) {
  1120. async_connect(any(), force_move(func));
  1121. }
  1122. /**
  1123. * @brief Connect to a broker
  1124. * Before calling connect(), call set_xxx member functions to configure the connection.
  1125. * @param session_life_keeper the passed object lifetime will be kept during the session.
  1126. * @param func finish handler that is called when the underlying connection process is finished
  1127. */
  1128. void async_connect(any session_life_keeper, async_handler_t func) {
  1129. async_connect(v5::properties{}, force_move(session_life_keeper), force_move(func));
  1130. }
  1131. /**
  1132. * @brief Connect to a broker
  1133. * Before calling connect(), call set_xxx member functions to configure the connection.
  1134. * @param props properties
  1135. * See https://docs.oasis-open.org/mqtt/mqtt/v5.0/os/mqtt-v5.0-os.html#_Toc3901046<BR>
  1136. * 3.1.2.11 CONNECT Properties
  1137. * @param session_life_keeper the passed object lifetime will be kept during the session.
  1138. */
  1139. template <typename T>
  1140. // to avoid ambiguousness between any and async_handler_t
  1141. std::enable_if_t<
  1142. !std::is_convertible<T, async_handler_t>::value
  1143. >
  1144. async_connect(v5::properties props, T session_life_keeper) {
  1145. async_connect(force_move(props), force_move(session_life_keeper), async_handler_t());
  1146. }
  1147. /**
  1148. * @brief Connect to a broker
  1149. * Before calling connect(), call set_xxx member functions to configure the connection.
  1150. * @param props properties
  1151. * See https://docs.oasis-open.org/mqtt/mqtt/v5.0/os/mqtt-v5.0-os.html#_Toc3901046<BR>
  1152. * 3.1.2.11 CONNECT Properties
  1153. * @param func finish handler that is called when the underlying connection process is finished
  1154. */
  1155. void async_connect(v5::properties props, async_handler_t func) {
  1156. async_connect(force_move(props), any(), force_move(func));
  1157. }
  1158. /**
  1159. * @brief Connect to a broker
  1160. * Before calling connect(), call set_xxx member functions to configure the connection.
  1161. * @param props properties
  1162. * See https://docs.oasis-open.org/mqtt/mqtt/v5.0/os/mqtt-v5.0-os.html#_Toc3901046<BR>
  1163. * 3.1.2.11 CONNECT Properties
  1164. * @param session_life_keeper the passed object lifetime will be kept during the session.
  1165. * @param func finish handler that is called when the underlying connection process is finished
  1166. */
  1167. void async_connect(v5::properties props, any session_life_keeper, async_handler_t func) {
  1168. setup_socket(socket_);
  1169. async_connect_impl(force_move(props), force_move(session_life_keeper), force_move(func), false);
  1170. }
  1171. /**
  1172. * @brief Connect to a broker
  1173. * Before calling connect(), call set_xxx member functions to configure the connection.
  1174. * @param socket The library uses the socket instead of internal generation.
  1175. * You can configure the socket prior to connect.
  1176. * @param underlying_connected If the given socket has aleady been connected, then true.
  1177. * Connecting processes are skipped and send MQTT CONNECT packet.
  1178. * If false then do all underlying connecting process and then
  1179. * send MQTT CONNECT packet.
  1180. */
  1181. void async_connect(std::shared_ptr<Socket>&& socket, bool underlying_connected = false) {
  1182. async_connect(force_move(socket), v5::properties{}, any(), async_handler_t(), underlying_connected);
  1183. }
  1184. /**
  1185. * @brief Connect to a broker
  1186. * Before calling connect(), call set_xxx member functions to configure the connection.
  1187. * @param socket The library uses the socket instead of internal generation.
  1188. * You can configure the socket prior to connect.
  1189. * @param session_life_keeper the passed object lifetime will be kept during the session.
  1190. * @param underlying_connected If the given socket has aleady been connected, then true.
  1191. * Connecting processes are skipped and send MQTT CONNECT packet.
  1192. * If false then do all underlying connecting process and then
  1193. * send MQTT CONNECT packet.
  1194. */
  1195. template <typename T>
  1196. // to avoid ambiguousness between any and async_handler_t
  1197. std::enable_if_t<
  1198. !std::is_convertible<T, async_handler_t>::value
  1199. >
  1200. async_connect(std::shared_ptr<Socket>&& socket, T session_life_keeper, bool underlying_connected = false) {
  1201. async_connect(force_move(socket), v5::properties{}, force_move(session_life_keeper), async_handler_t(), underlying_connected);
  1202. }
  1203. /**
  1204. * @brief Connect to a broker
  1205. * Before calling connect(), call set_xxx member functions to configure the connection.
  1206. * @param socket The library uses the socket instead of internal generation.
  1207. * You can configure the socket prior to connect.
  1208. * @param func finish handler that is called when the underlying connection process is finished
  1209. * @param underlying_connected If the given socket has aleady been connected, then true.
  1210. * Connecting processes are skipped and send MQTT CONNECT packet.
  1211. * If false then do all underlying connecting process and then
  1212. * send MQTT CONNECT packet.
  1213. */
  1214. void async_connect(std::shared_ptr<Socket>&& socket, async_handler_t func, bool underlying_connected = false) {
  1215. async_connect(force_move(socket), v5::properties{}, any(), force_move(func), underlying_connected);
  1216. }
  1217. /**
  1218. * @brief Connect to a broker
  1219. * Before calling connect(), call set_xxx member functions to configure the connection.
  1220. * @param socket The library uses the socket instead of internal generation.
  1221. * You can configure the socket prior to connect.
  1222. * @param session_life_keeper the passed object lifetime will be kept during the session.
  1223. * @param func finish handler that is called when the underlying connection process is finished
  1224. * @param underlying_connected If the given socket has aleady been connected, then true.
  1225. * Connecting processes are skipped and send MQTT CONNECT packet.
  1226. * If false then do all underlying connecting process and then
  1227. * send MQTT CONNECT packet.
  1228. */
  1229. void async_connect(std::shared_ptr<Socket>&& socket, any session_life_keeper, async_handler_t func, bool underlying_connected = false) {
  1230. async_connect(force_move(socket), v5::properties{}, force_move(session_life_keeper), force_move(func), underlying_connected);
  1231. }
  1232. /**
  1233. * @brief Connect to a broker
  1234. * Before calling connect(), call set_xxx member functions to configure the connection.
  1235. * @param socket The library uses the socket instead of internal generation.
  1236. * You can configure the socket prior to connect.
  1237. * @param props properties
  1238. * See https://docs.oasis-open.org/mqtt/mqtt/v5.0/os/mqtt-v5.0-os.html#_Toc3901046<BR>
  1239. * 3.1.2.11 CONNECT Properties
  1240. * @param underlying_connected If the given socket has aleady been connected, then true.
  1241. * Connecting processes are skipped and send MQTT CONNECT packet.
  1242. * If false then do all underlying connecting process and then
  1243. * send MQTT CONNECT packet.
  1244. */
  1245. void async_connect(std::shared_ptr<Socket>&& socket, v5::properties props, bool underlying_connected = false) {
  1246. async_connect(force_move(socket), force_move(props), any(), async_handler_t(), underlying_connected);
  1247. }
  1248. /**
  1249. * @brief Connect to a broker
  1250. * Before calling connect(), call set_xxx member functions to configure the connection.
  1251. * @param socket The library uses the socket instead of internal generation.
  1252. * You can configure the socket prior to connect.
  1253. * @param props properties
  1254. * See https://docs.oasis-open.org/mqtt/mqtt/v5.0/os/mqtt-v5.0-os.html#_Toc3901046<BR>
  1255. * 3.1.2.11 CONNECT Properties
  1256. * @param session_life_keeper the passed object lifetime will be kept during the session.
  1257. * @param underlying_connected If the given socket has aleady been connected, then true.
  1258. * Connecting processes are skipped and send MQTT CONNECT packet.
  1259. * If false then do all underlying connecting process and then
  1260. * send MQTT CONNECT packet.
  1261. */
  1262. template <typename T>
  1263. // to avoid ambiguousness between any and async_handler_t
  1264. std::enable_if_t<
  1265. !std::is_convertible<T, async_handler_t>::value
  1266. >
  1267. async_connect(std::shared_ptr<Socket>&& socket, v5::properties props, T session_life_keeper, bool underlying_connected = false) {
  1268. async_connect(force_move(socket), force_move(props), force_move(session_life_keeper), async_handler_t(), underlying_connected);
  1269. }
  1270. /**
  1271. * @brief Connect to a broker
  1272. * Before calling connect(), call set_xxx member functions to configure the connection.
  1273. * @param socket The library uses the socket instead of internal generation.
  1274. * You can configure the socket prior to connect.
  1275. * @param props properties
  1276. * See https://docs.oasis-open.org/mqtt/mqtt/v5.0/os/mqtt-v5.0-os.html#_Toc3901046<BR>
  1277. * 3.1.2.11 CONNECT Properties
  1278. * @param func finish handler that is called when the underlying connection process is finished
  1279. * @param underlying_connected If the given socket has aleady been connected, then true.
  1280. * Connecting processes are skipped and send MQTT CONNECT packet.
  1281. * If false then do all underlying connecting process and then
  1282. * send MQTT CONNECT packet.
  1283. */
  1284. void async_connect(std::shared_ptr<Socket>&& socket, v5::properties props, async_handler_t func, bool underlying_connected = false) {
  1285. async_connect(force_move(socket), force_move(props), any(), force_move(func), underlying_connected);
  1286. }
  1287. /**
  1288. * @brief Connect to a broker
  1289. * Before calling connect(), call set_xxx member functions to configure the connection.
  1290. * @param socket The library uses the socket instead of internal generation.
  1291. * You can configure the socket prior to connect.
  1292. * @param props properties
  1293. * See https://docs.oasis-open.org/mqtt/mqtt/v5.0/os/mqtt-v5.0-os.html#_Toc3901046<BR>
  1294. * 3.1.2.11 CONNECT Properties
  1295. * @param session_life_keeper the passed object lifetime will be kept during the session.
  1296. * @param func finish handler that is called when the underlying connection process is finished
  1297. * @param underlying_connected If the given socket has aleady been connected, then true.
  1298. * Connecting processes are skipped and send MQTT CONNECT packet.
  1299. * If false then do all underlying connecting process and then
  1300. * send MQTT CONNECT packet.
  1301. */
  1302. void async_connect(std::shared_ptr<Socket>&& socket, v5::properties props, any session_life_keeper, async_handler_t func, bool underlying_connected = false) {
  1303. socket_ = force_move(socket);
  1304. base::socket_sp_ref() = socket_;
  1305. async_connect_impl(force_move(props), force_move(session_life_keeper), force_move(func), underlying_connected);
  1306. }
  1307. /**
  1308. * @brief Disconnect
  1309. * Send a disconnect packet to the connected broker. It is a clean disconnecting sequence.
  1310. * The broker disconnects the endpoint after receives the disconnect packet.<BR>
  1311. * When the endpoint disconnects using disconnect(), a will won't send.<BR>
  1312. * See https://docs.oasis-open.org/mqtt/mqtt/v5.0/os/mqtt-v5.0-os.html#_Toc3901205<BR>
  1313. * @param timeout after timeout elapsed, force_disconnect() is automatically called.
  1314. * @param reason_code
  1315. * DISCONNECT Reason Code<BR>
  1316. * See https://docs.oasis-open.org/mqtt/mqtt/v5.0/os/mqtt-v5.0-os.html#_Toc3901208<BR>
  1317. * 3.14.2.1 Disconnect Reason Code
  1318. * @param props
  1319. * Properties<BR>
  1320. * See https://docs.oasis-open.org/mqtt/mqtt/v5.0/os/mqtt-v5.0-os.html#_Toc3901209<BR>
  1321. * 3.14.2.2 DISCONNECT Properties
  1322. */
  1323. void disconnect(
  1324. std::chrono::steady_clock::duration timeout,
  1325. v5::disconnect_reason_code reason_code = v5::disconnect_reason_code::normal_disconnection,
  1326. v5::properties props = {}
  1327. ) {
  1328. if (ping_duration_ != std::chrono::steady_clock::duration::zero()) cancel_ping_timer();
  1329. if (base::connected()) {
  1330. std::weak_ptr<this_type> wp(std::static_pointer_cast<this_type>(this->shared_from_this()));
  1331. {
  1332. std::lock_guard<std::mutex> g(mtx_tim_close_);
  1333. tim_close_.expires_after(force_move(timeout));
  1334. tim_close_.async_wait(
  1335. [wp = force_move(wp)](error_code ec) {
  1336. if (auto sp = wp.lock()) {
  1337. if (!ec) {
  1338. sp->socket()->post(
  1339. [sp] {
  1340. sp->force_disconnect();
  1341. }
  1342. );
  1343. }
  1344. }
  1345. }
  1346. );
  1347. }
  1348. base::disconnect(reason_code, force_move(props));
  1349. }
  1350. }
  1351. /**
  1352. * @brief Disconnect
  1353. * Send a disconnect packet to the connected broker. It is a clean disconnecting sequence.
  1354. * The broker disconnects the endpoint after receives the disconnect packet.<BR>
  1355. * When the endpoint disconnects using disconnect(), a will won't send.<BR>
  1356. * See https://docs.oasis-open.org/mqtt/mqtt/v5.0/os/mqtt-v5.0-os.html#_Toc3901205<BR>
  1357. * @param reason_code
  1358. * DISCONNECT Reason Code<BR>
  1359. * See https://docs.oasis-open.org/mqtt/mqtt/v5.0/os/mqtt-v5.0-os.html#_Toc3901208<BR>
  1360. * 3.14.2.1 Disconnect Reason Code
  1361. * @param props
  1362. * Properties<BR>
  1363. * See https://docs.oasis-open.org/mqtt/mqtt/v5.0/os/mqtt-v5.0-os.html#_Toc3901209<BR>
  1364. * 3.14.2.2 DISCONNECT Properties
  1365. */
  1366. void disconnect(
  1367. v5::disconnect_reason_code reason_code = v5::disconnect_reason_code::normal_disconnection,
  1368. v5::properties props = {}
  1369. ) {
  1370. if (ping_duration_ != std::chrono::steady_clock::duration::zero()) cancel_ping_timer();
  1371. if (base::connected()) {
  1372. base::disconnect(reason_code, force_move(props));
  1373. }
  1374. }
  1375. /**
  1376. * @brief Disconnect
  1377. * Send a disconnect packet to the connected broker. It is a clean disconnecting sequence.
  1378. * The broker disconnects the endpoint after receives the disconnect packet.<BR>
  1379. * When the endpoint disconnects using disconnect(), a will won't send.<BR>
  1380. * See https://docs.oasis-open.org/mqtt/mqtt/v5.0/os/mqtt-v5.0-os.html#_Toc3901205<BR>
  1381. * @param timeout after timeout elapsed, force_disconnect() is automatically called.
  1382. * @param func A callback function that is called when async operation will finish.
  1383. */
  1384. void async_disconnect(
  1385. std::chrono::steady_clock::duration timeout,
  1386. async_handler_t func = async_handler_t()) {
  1387. if (ping_duration_ != std::chrono::steady_clock::duration::zero()) cancel_ping_timer();
  1388. if (base::connected()) {
  1389. std::weak_ptr<this_type> wp(std::static_pointer_cast<this_type>(this->shared_from_this()));
  1390. {
  1391. std::lock_guard<std::mutex> g(mtx_tim_close_);
  1392. tim_close_.expires_after(force_move(timeout));
  1393. tim_close_.async_wait(
  1394. [wp = force_move(wp)](error_code ec) {
  1395. if (auto sp = wp.lock()) {
  1396. if (!ec) {
  1397. sp->socket()->post(
  1398. [sp] {
  1399. sp->force_disconnect();
  1400. }
  1401. );
  1402. }
  1403. }
  1404. }
  1405. );
  1406. }
  1407. base::async_disconnect(force_move(func));
  1408. }
  1409. }
  1410. /**
  1411. * @brief Disconnect
  1412. * Send a disconnect packet to the connected broker. It is a clean disconnecting sequence.
  1413. * The broker disconnects the endpoint after receives the disconnect packet.<BR>
  1414. * When the endpoint disconnects using disconnect(), a will won't send.<BR>
  1415. * See https://docs.oasis-open.org/mqtt/mqtt/v5.0/os/mqtt-v5.0-os.html#_Toc3901205<BR>
  1416. * @param timeout after timeout elapsed, force_disconnect() is automatically called.
  1417. * @param reason_code
  1418. * DISCONNECT Reason Code<BR>
  1419. * See https://docs.oasis-open.org/mqtt/mqtt/v5.0/os/mqtt-v5.0-os.html#_Toc3901208<BR>
  1420. * 3.14.2.1 Disconnect Reason Code
  1421. * @param props
  1422. * Properties<BR>
  1423. * See https://docs.oasis-open.org/mqtt/mqtt/v5.0/os/mqtt-v5.0-os.html#_Toc3901209<BR>
  1424. * 3.14.2.2 DISCONNECT Properties
  1425. * @param func A callback function that is called when async operation will finish.
  1426. */
  1427. void async_disconnect(
  1428. std::chrono::steady_clock::duration timeout,
  1429. v5::disconnect_reason_code reason_code,
  1430. v5::properties props,
  1431. async_handler_t func = async_handler_t()) {
  1432. if (ping_duration_ != std::chrono::steady_clock::duration::zero()) cancel_ping_timer();
  1433. if (base::connected()) {
  1434. std::weak_ptr<this_type> wp(std::static_pointer_cast<this_type>(this->shared_from_this()));
  1435. {
  1436. std::lock_guard<std::mutex> g(mtx_tim_close_);
  1437. tim_close_.expires_after(force_move(timeout));
  1438. tim_close_.async_wait(
  1439. [wp = force_move(wp)](error_code ec) {
  1440. if (auto sp = wp.lock()) {
  1441. if (!ec) {
  1442. sp->socket()->post(
  1443. [sp] {
  1444. sp->force_disconnect();
  1445. }
  1446. );
  1447. }
  1448. }
  1449. }
  1450. );
  1451. }
  1452. base::async_disconnect(reason_code, force_move(props), force_move(func));
  1453. }
  1454. }
  1455. /**
  1456. * @brief Disconnect
  1457. * Send a disconnect packet to the connected broker. It is a clean disconnecting sequence.
  1458. * The broker disconnects the endpoint after receives the disconnect packet.<BR>
  1459. * When the endpoint disconnects using disconnect(), a will won't send.<BR>
  1460. * See https://docs.oasis-open.org/mqtt/mqtt/v5.0/os/mqtt-v5.0-os.html#_Toc3901205<BR>
  1461. * @param func A callback function that is called when async operation will finish.
  1462. */
  1463. void async_disconnect(
  1464. async_handler_t func = async_handler_t()) {
  1465. if (ping_duration_ != std::chrono::steady_clock::duration::zero()) cancel_ping_timer();
  1466. if (base::connected()) {
  1467. base::async_disconnect(force_move(func));
  1468. }
  1469. }
  1470. /**
  1471. * @brief Disconnect
  1472. * Send a disconnect packet to the connected broker. It is a clean disconnecting sequence.
  1473. * The broker disconnects the endpoint after receives the disconnect packet.<BR>
  1474. * When the endpoint disconnects using disconnect(), a will won't send.<BR>
  1475. * See https://docs.oasis-open.org/mqtt/mqtt/v5.0/os/mqtt-v5.0-os.html#_Toc3901205<BR>
  1476. * @param reason_code
  1477. * DISCONNECT Reason Code<BR>
  1478. * See https://docs.oasis-open.org/mqtt/mqtt/v5.0/os/mqtt-v5.0-os.html#_Toc3901208<BR>
  1479. * 3.14.2.1 Disconnect Reason Code
  1480. * @param props
  1481. * Properties<BR>
  1482. * See https://docs.oasis-open.org/mqtt/mqtt/v5.0/os/mqtt-v5.0-os.html#_Toc3901209<BR>
  1483. * 3.14.2.2 DISCONNECT Properties
  1484. * @param func A callback function that is called when async operation will finish.
  1485. */
  1486. void async_disconnect(
  1487. v5::disconnect_reason_code reason_code,
  1488. v5::properties props,
  1489. async_handler_t func = async_handler_t()) {
  1490. if (ping_duration_ != std::chrono::steady_clock::duration::zero()) cancel_ping_timer();
  1491. if (base::connected()) {
  1492. base::async_disconnect(reason_code, force_move(props), force_move(func));
  1493. }
  1494. }
  1495. /**
  1496. * @brief Disconnect by endpoint
  1497. * Force disconnect. It is not a clean disconnect sequence.<BR>
  1498. * When the endpoint disconnects using force_disconnect(), a will will send.<BR>
  1499. */
  1500. void force_disconnect() {
  1501. if (ping_duration_ != std::chrono::steady_clock::duration::zero()) cancel_ping_timer();
  1502. {
  1503. std::lock_guard<std::mutex> g(mtx_tim_close_);
  1504. tim_close_.cancel();
  1505. }
  1506. base::force_disconnect();
  1507. }
  1508. /**
  1509. * @brief Disconnect by endpoint
  1510. * @param func A callback function that is called when async operation will finish.
  1511. * Force disconnect. It is not a clean disconnect sequence.<BR>
  1512. * When the endpoint disconnects using force_disconnect(), a will will send.<BR>
  1513. */
  1514. void async_force_disconnect(
  1515. async_handler_t func = async_handler_t()) {
  1516. if (ping_duration_ != std::chrono::steady_clock::duration::zero()) cancel_ping_timer();
  1517. {
  1518. std::lock_guard<std::mutex> g(mtx_tim_close_);
  1519. tim_close_.cancel();
  1520. }
  1521. base::async_force_disconnect(force_move(func));
  1522. }
  1523. std::shared_ptr<Socket> const& socket() const {
  1524. return socket_;
  1525. }
  1526. std::shared_ptr<Socket>& socket() {
  1527. return socket_;
  1528. }
  1529. protected:
  1530. client(as::io_context& ioc,
  1531. std::string host,
  1532. std::string port
  1533. #if defined(MQTT_USE_WS)
  1534. ,
  1535. std::string path = "/"
  1536. #endif // defined(MQTT_USE_WS)
  1537. ,
  1538. protocol_version version = protocol_version::v3_1_1,
  1539. bool async_operation = false
  1540. )
  1541. :base(ioc, version, async_operation),
  1542. ioc_(ioc),
  1543. tim_ping_(ioc_),
  1544. tim_close_(ioc_),
  1545. host_(force_move(host)),
  1546. port_(force_move(port)),
  1547. async_operation_(async_operation)
  1548. #if defined(MQTT_USE_WS)
  1549. ,
  1550. path_(force_move(path))
  1551. #endif // defined(MQTT_USE_WS)
  1552. {
  1553. #if defined(MQTT_USE_TLS)
  1554. ctx_.set_verify_mode(tls::verify_peer);
  1555. #endif // defined(MQTT_USE_TLS)
  1556. setup_socket(socket_);
  1557. }
  1558. private:
  1559. template <typename Strand>
  1560. void setup_socket(std::shared_ptr<tcp_endpoint<as::ip::tcp::socket, Strand>>& socket) {
  1561. socket = std::make_shared<Socket>(ioc_);
  1562. base::socket_sp_ref() = socket;
  1563. }
  1564. #if defined(MQTT_USE_WS)
  1565. template <typename Strand>
  1566. void setup_socket(std::shared_ptr<ws_endpoint<as::ip::tcp::socket, Strand>>& socket) {
  1567. socket = std::make_shared<Socket>(ioc_);
  1568. base::socket_sp_ref() = socket;
  1569. }
  1570. #endif // defined(MQTT_USE_WS)
  1571. #if defined(MQTT_USE_TLS)
  1572. template <typename Strand>
  1573. void setup_socket(std::shared_ptr<tcp_endpoint<tls::stream<as::ip::tcp::socket>, Strand>>& socket) {
  1574. socket = std::make_shared<Socket>(ioc_, ctx_);
  1575. base::socket_sp_ref() = socket;
  1576. }
  1577. #if defined(MQTT_USE_WS)
  1578. template <typename Strand>
  1579. void setup_socket(std::shared_ptr<ws_endpoint<tls::stream<as::ip::tcp::socket>, Strand>>& socket) {
  1580. socket = std::make_shared<Socket>(ioc_, ctx_);
  1581. base::socket_sp_ref() = socket;
  1582. }
  1583. #endif // defined(MQTT_USE_WS)
  1584. #endif // defined(MQTT_USE_TLS)
  1585. void start_session(v5::properties props, any session_life_keeper) {
  1586. base::async_read_control_packet_type(force_move(session_life_keeper));
  1587. // sync base::connect() refer to parameters only in the function.
  1588. // So they can be passed as view.
  1589. base::connect(
  1590. buffer(string_view(base::get_client_id())),
  1591. ( user_name_ ? buffer(string_view(user_name_.value()))
  1592. : optional<buffer>() ),
  1593. ( password_ ? buffer(string_view(password_.value()))
  1594. : optional<buffer>() ),
  1595. will_,
  1596. keep_alive_sec_,
  1597. force_move(props)
  1598. );
  1599. }
  1600. void async_start_session(v5::properties props, any session_life_keeper, async_handler_t func) {
  1601. base::async_read_control_packet_type(force_move(session_life_keeper));
  1602. // sync base::connect() refer to parameters only in the function.
  1603. // So they can be passed as view.
  1604. base::async_connect(
  1605. buffer(string_view(base::get_client_id())),
  1606. ( user_name_ ? buffer(string_view(user_name_.value()))
  1607. : optional<buffer>() ),
  1608. ( password_ ? buffer(string_view(password_.value()))
  1609. : optional<buffer>() ),
  1610. will_,
  1611. keep_alive_sec_,
  1612. force_move(props),
  1613. force_move(func)
  1614. );
  1615. }
  1616. template <typename Strand>
  1617. void handshake_socket(
  1618. tcp_endpoint<as::ip::tcp::socket, Strand>&,
  1619. v5::properties props,
  1620. any session_life_keeper,
  1621. bool /*underlying_connected*/) {
  1622. start_session(force_move(props), force_move(session_life_keeper));
  1623. }
  1624. template <typename Strand>
  1625. void handshake_socket(
  1626. tcp_endpoint<as::ip::tcp::socket, Strand>&,
  1627. v5::properties props,
  1628. any session_life_keeper,
  1629. boost::system::error_code& ec,
  1630. bool /*underlying_connected*/) {
  1631. start_session(force_move(props), force_move(session_life_keeper));
  1632. ec = boost::system::errc::make_error_code(boost::system::errc::success);
  1633. }
  1634. #if defined(MQTT_USE_WS)
  1635. template <typename Strand>
  1636. void handshake_socket(
  1637. ws_endpoint<as::ip::tcp::socket, Strand>& socket,
  1638. v5::properties props,
  1639. any session_life_keeper,
  1640. bool underlying_connected) {
  1641. if (!underlying_connected) {
  1642. socket.handshake(host_, path_);
  1643. }
  1644. start_session(force_move(props), force_move(session_life_keeper));
  1645. }
  1646. template <typename Strand>
  1647. void handshake_socket(
  1648. ws_endpoint<as::ip::tcp::socket, Strand>& socket,
  1649. v5::properties props,
  1650. any session_life_keeper,
  1651. boost::system::error_code& ec,
  1652. bool underlying_connected) {
  1653. if (!underlying_connected) {
  1654. ec = boost::system::errc::make_error_code(boost::system::errc::success);
  1655. }
  1656. else {
  1657. socket.handshake(host_, path_, ec);
  1658. if (ec) return;
  1659. }
  1660. start_session(force_move(props), force_move(session_life_keeper));
  1661. }
  1662. #endif // defined(MQTT_USE_WS)
  1663. #if defined(MQTT_USE_TLS)
  1664. template <typename Strand>
  1665. void handshake_socket(
  1666. tcp_endpoint<tls::stream<as::ip::tcp::socket>, Strand>& socket,
  1667. v5::properties props,
  1668. any session_life_keeper,
  1669. bool underlying_connected) {
  1670. if (!underlying_connected) {
  1671. socket.handshake(tls::stream_base::client);
  1672. }
  1673. start_session(force_move(props), force_move(session_life_keeper));
  1674. }
  1675. template <typename Strand>
  1676. void handshake_socket(
  1677. tcp_endpoint<tls::stream<as::ip::tcp::socket>, Strand>& socket,
  1678. v5::properties props,
  1679. any session_life_keeper,
  1680. boost::system::error_code& ec,
  1681. bool underlying_connected) {
  1682. if (underlying_connected) {
  1683. ec = boost::system::errc::make_error_code(boost::system::errc::success);
  1684. }
  1685. else {
  1686. socket.handshake(tls::stream_base::client, ec);
  1687. if (ec) return;
  1688. }
  1689. start_session(force_move(props), force_move(session_life_keeper));
  1690. }
  1691. #if defined(MQTT_USE_WS)
  1692. template <typename Strand>
  1693. void handshake_socket(
  1694. ws_endpoint<tls::stream<as::ip::tcp::socket>, Strand>& socket,
  1695. v5::properties props,
  1696. any session_life_keeper,
  1697. bool underlying_connected) {
  1698. if (!underlying_connected) {
  1699. socket.next_layer().handshake(tls::stream_base::client);
  1700. socket.handshake(host_, path_);
  1701. }
  1702. start_session(force_move(props), force_move(session_life_keeper));
  1703. }
  1704. template <typename Strand>
  1705. void handshake_socket(
  1706. ws_endpoint<tls::stream<as::ip::tcp::socket>, Strand>& socket,
  1707. v5::properties props,
  1708. any session_life_keeper,
  1709. boost::system::error_code& ec,
  1710. bool underlying_connected) {
  1711. if (underlying_connected) {
  1712. ec = boost::system::errc::make_error_code(boost::system::errc::success);
  1713. }
  1714. else {
  1715. socket.next_layer().handshake(tls::stream_base::client, ec);
  1716. if (ec) return;
  1717. socket.handshake(host_, path_, ec);
  1718. if (ec) return;
  1719. }
  1720. start_session(force_move(props), force_move(session_life_keeper));
  1721. }
  1722. #endif // defined(MQTT_USE_WS)
  1723. #endif // defined(MQTT_USE_TLS)
  1724. template <typename Strand>
  1725. void async_handshake_socket(
  1726. tcp_endpoint<as::ip::tcp::socket, Strand>&,
  1727. v5::properties props,
  1728. any session_life_keeper,
  1729. async_handler_t func,
  1730. bool /*underlying_connected*/) {
  1731. async_start_session(force_move(props), force_move(session_life_keeper), force_move(func));
  1732. }
  1733. #if defined(MQTT_USE_WS)
  1734. template <typename Strand>
  1735. void async_handshake_socket(
  1736. ws_endpoint<as::ip::tcp::socket, Strand>& socket,
  1737. v5::properties props,
  1738. any session_life_keeper,
  1739. async_handler_t func,
  1740. bool underlying_connected) {
  1741. if (underlying_connected) {
  1742. async_start_session(force_move(props), force_move(session_life_keeper), force_move(func));
  1743. return;
  1744. }
  1745. socket.async_handshake(
  1746. host_,
  1747. path_,
  1748. [
  1749. this,
  1750. self = this->shared_from_this(),
  1751. session_life_keeper = force_move(session_life_keeper),
  1752. props = force_move(props),
  1753. func = force_move(func)
  1754. ]
  1755. (error_code ec) mutable {
  1756. if (ec) {
  1757. if (func) func(ec);
  1758. return;
  1759. }
  1760. async_start_session(force_move(props), force_move(session_life_keeper), force_move(func));
  1761. });
  1762. }
  1763. #endif // defined(MQTT_USE_WS)
  1764. #if defined(MQTT_USE_TLS)
  1765. template <typename Strand>
  1766. void async_handshake_socket(
  1767. tcp_endpoint<tls::stream<as::ip::tcp::socket>, Strand>& socket,
  1768. v5::properties props,
  1769. any session_life_keeper,
  1770. async_handler_t func,
  1771. bool underlying_connected) {
  1772. if (underlying_connected) {
  1773. async_start_session(force_move(props), force_move(session_life_keeper), force_move(func));
  1774. return;
  1775. }
  1776. socket.async_handshake(
  1777. tls::stream_base::client,
  1778. [
  1779. this,
  1780. self = this->shared_from_this(),
  1781. session_life_keeper = force_move(session_life_keeper),
  1782. props = force_move(props),
  1783. func = force_move(func)
  1784. ]
  1785. (error_code ec) mutable {
  1786. if (ec) {
  1787. if (func) func(ec);
  1788. return;
  1789. }
  1790. async_start_session(force_move(props), force_move(session_life_keeper), force_move(func));
  1791. });
  1792. }
  1793. #if defined(MQTT_USE_WS)
  1794. template <typename Strand>
  1795. void async_handshake_socket(
  1796. ws_endpoint<tls::stream<as::ip::tcp::socket>, Strand>& socket,
  1797. v5::properties props,
  1798. any session_life_keeper,
  1799. async_handler_t func,
  1800. bool underlying_connected) {
  1801. if (underlying_connected) {
  1802. async_start_session(force_move(props), force_move(session_life_keeper), force_move(func));
  1803. return;
  1804. }
  1805. socket.next_layer().async_handshake(
  1806. tls::stream_base::client,
  1807. [
  1808. this,
  1809. self = this->shared_from_this(),
  1810. session_life_keeper = force_move(session_life_keeper),
  1811. &socket,
  1812. props = force_move(props),
  1813. func = force_move(func)
  1814. ]
  1815. (error_code ec) mutable {
  1816. if (ec) {
  1817. if (func) func(ec);
  1818. return;
  1819. }
  1820. socket.async_handshake(
  1821. host_,
  1822. path_,
  1823. [
  1824. this,
  1825. self,
  1826. session_life_keeper = force_move(session_life_keeper),
  1827. props = force_move(props),
  1828. func = force_move(func)
  1829. ]
  1830. (error_code ec) mutable {
  1831. if (ec) {
  1832. if (func) func(ec);
  1833. return;
  1834. }
  1835. async_start_session(force_move(props), force_move(session_life_keeper), force_move(func));
  1836. });
  1837. });
  1838. }
  1839. #endif // defined(MQTT_USE_WS)
  1840. #endif // defined(MQTT_USE_TLS)
  1841. void connect_impl(
  1842. v5::properties props,
  1843. any session_life_keeper,
  1844. bool underlying_connected) {
  1845. if (!underlying_connected) {
  1846. as::ip::tcp::resolver r(ioc_);
  1847. auto eps = r.resolve(host_, port_);
  1848. as::connect(socket_->lowest_layer(), eps.begin(), eps.end());
  1849. }
  1850. base::set_connect();
  1851. if (ping_duration_ != std::chrono::steady_clock::duration::zero()) {
  1852. set_ping_timer();
  1853. }
  1854. handshake_socket(*socket_, force_move(props), force_move(session_life_keeper), underlying_connected);
  1855. }
  1856. void connect_impl(
  1857. v5::properties props,
  1858. any session_life_keeper,
  1859. boost::system::error_code& ec,
  1860. bool underlying_connected) {
  1861. if (!underlying_connected) {
  1862. as::ip::tcp::resolver r(ioc_);
  1863. auto eps = r.resolve(host_, port_, ec);
  1864. if (ec) return;
  1865. as::connect(socket_->lowest_layer(), eps.begin(), eps.end(), ec);
  1866. if (ec) return;
  1867. }
  1868. base::set_connect();
  1869. if (ping_duration_ != std::chrono::steady_clock::duration::zero()) {
  1870. set_ping_timer();
  1871. }
  1872. handshake_socket(*socket_, force_move(props), force_move(session_life_keeper), ec, underlying_connected);
  1873. }
  1874. void async_connect_impl(
  1875. v5::properties props,
  1876. any session_life_keeper,
  1877. async_handler_t func,
  1878. bool underlying_connected) {
  1879. if (underlying_connected) {
  1880. base::set_connect();
  1881. if (ping_duration_ != std::chrono::steady_clock::duration::zero()) {
  1882. set_ping_timer();
  1883. }
  1884. async_handshake_socket(*socket_, force_move(props), force_move(session_life_keeper), force_move(func), underlying_connected);
  1885. }
  1886. else {
  1887. auto r = std::make_shared<as::ip::tcp::resolver>(ioc_);
  1888. auto p = r.get();
  1889. p->async_resolve(
  1890. host_,
  1891. port_,
  1892. [
  1893. this,
  1894. self = this->shared_from_this(),
  1895. props = force_move(props),
  1896. session_life_keeper = force_move(session_life_keeper),
  1897. func = force_move(func),
  1898. r = force_move(r),
  1899. underlying_connected
  1900. ]
  1901. (
  1902. error_code ec,
  1903. as::ip::tcp::resolver::results_type eps
  1904. ) mutable {
  1905. if (ec) {
  1906. if (func) func(ec);
  1907. return;
  1908. }
  1909. as::async_connect(
  1910. socket_->lowest_layer(), eps.begin(), eps.end(),
  1911. [
  1912. this,
  1913. self = force_move(self),
  1914. props = force_move(props),
  1915. session_life_keeper = force_move(session_life_keeper),
  1916. func = force_move(func),
  1917. underlying_connected
  1918. ]
  1919. (error_code ec, auto /* unused */) mutable {
  1920. if (ec) {
  1921. if (func) func(ec);
  1922. return;
  1923. }
  1924. base::set_connect();
  1925. if (ping_duration_ != std::chrono::steady_clock::duration::zero()) {
  1926. set_ping_timer();
  1927. }
  1928. async_handshake_socket(*socket_, force_move(props), force_move(session_life_keeper), force_move(func), underlying_connected);
  1929. }
  1930. );
  1931. }
  1932. );
  1933. }
  1934. }
  1935. protected:
  1936. void on_pre_send() noexcept override {
  1937. if (ping_duration_ != std::chrono::steady_clock::duration::zero()) {
  1938. reset_ping_timer();
  1939. }
  1940. }
  1941. private:
  1942. void handle_timer(error_code ec) {
  1943. if (!ec) {
  1944. if (async_operation_) {
  1945. base::async_pingreq();
  1946. }
  1947. else {
  1948. base::pingreq();
  1949. }
  1950. }
  1951. }
  1952. void set_ping_timer_no_lock() {
  1953. tim_ping_.expires_after(ping_duration_);
  1954. std::weak_ptr<this_type> wp(std::static_pointer_cast<this_type>(this->shared_from_this()));
  1955. tim_ping_.async_wait(
  1956. [wp = force_move(wp)](error_code ec) {
  1957. if (auto sp = wp.lock()) {
  1958. sp->handle_timer(ec);
  1959. }
  1960. }
  1961. );
  1962. }
  1963. void cancel_timer_no_lock() {
  1964. tim_ping_.cancel();
  1965. }
  1966. void set_ping_timer() {
  1967. std::lock_guard<std::mutex> g(mtx_tim_ping_);
  1968. set_ping_timer_no_lock();
  1969. }
  1970. void cancel_ping_timer() {
  1971. std::lock_guard<std::mutex> g(mtx_tim_ping_);
  1972. cancel_timer_no_lock();
  1973. }
  1974. void reset_ping_timer() {
  1975. std::lock_guard<std::mutex> g(mtx_tim_ping_);
  1976. cancel_timer_no_lock();
  1977. set_ping_timer_no_lock();
  1978. }
  1979. protected:
  1980. void on_close() noexcept override {
  1981. if (ping_duration_ != std::chrono::steady_clock::duration::zero()) cancel_ping_timer();
  1982. }
  1983. void on_error(error_code ec) noexcept override {
  1984. (void)ec;
  1985. if (ping_duration_ != std::chrono::steady_clock::duration::zero()) cancel_ping_timer();
  1986. }
  1987. // Ensure that only code that knows the *exact* type of an object
  1988. // inheriting from this abstract base class can destruct it.
  1989. // This avoids issues of the destructor not triggering destruction
  1990. // of derived classes, and any member variables contained in them.
  1991. // Note: Not virtual to avoid need for a vtable when possible.
  1992. ~client() = default;
  1993. private:
  1994. #if defined(MQTT_USE_TLS)
  1995. template <typename T>
  1996. struct has_tls : std::false_type {
  1997. };
  1998. template <typename U>
  1999. struct has_tls<client<tcp_endpoint<tls::stream<as::ip::tcp::socket>, U>>> : std::true_type {
  2000. };
  2001. #if defined(MQTT_USE_WS)
  2002. template <typename U>
  2003. struct has_tls<client<ws_endpoint<tls::stream<as::ip::tcp::socket>, U>>> : std::true_type {
  2004. };
  2005. #endif // defined(MQTT_USE_WS)
  2006. #endif // defined(MQTT_USE_TLS)
  2007. std::shared_ptr<Socket> socket_;
  2008. as::io_context& ioc_;
  2009. std::mutex mtx_tim_ping_;
  2010. as::steady_timer tim_ping_;
  2011. std::mutex mtx_tim_close_;
  2012. as::steady_timer tim_close_;
  2013. std::string host_;
  2014. std::string port_;
  2015. std::uint16_t keep_alive_sec_{0};
  2016. std::chrono::steady_clock::duration ping_duration_{std::chrono::steady_clock::duration::zero()};
  2017. optional<will> will_;
  2018. optional<std::string> user_name_;
  2019. optional<std::string> password_;
  2020. bool async_operation_ = false;
  2021. #if defined(MQTT_USE_TLS)
  2022. tls::context ctx_{tls::context::tlsv12};
  2023. #endif // defined(MQTT_USE_TLS)
  2024. #if defined(MQTT_USE_WS)
  2025. std::string path_;
  2026. #endif // defined(MQTT_USE_WS)
  2027. };
  2028. template <typename Ioc>
  2029. inline std::shared_ptr<
  2030. callable_overlay<
  2031. client<
  2032. tcp_endpoint<
  2033. as::ip::tcp::socket,
  2034. strand
  2035. >
  2036. >
  2037. >
  2038. >
  2039. make_client(Ioc& ioc, std::string host, std::string port, protocol_version version) {
  2040. static_assert(std::is_same<Ioc, as::io_context>::value, "The type of ioc must be boost::asio::io_context");
  2041. using client_t = client<
  2042. tcp_endpoint<
  2043. as::ip::tcp::socket,
  2044. strand
  2045. >
  2046. >;
  2047. return std::make_shared<callable_overlay<client_t>>(
  2048. client_t::constructor_access(),
  2049. ioc,
  2050. force_move(host),
  2051. force_move(port),
  2052. #if defined(MQTT_USE_WS)
  2053. "/",
  2054. #endif // defined(MQTT_USE_WS)
  2055. version
  2056. );
  2057. }
  2058. template <typename Ioc>
  2059. inline std::shared_ptr<
  2060. callable_overlay<
  2061. client<
  2062. tcp_endpoint<
  2063. as::ip::tcp::socket,
  2064. strand
  2065. >
  2066. >
  2067. >
  2068. >
  2069. make_client(Ioc& ioc, std::string host, std::uint16_t port, protocol_version version) {
  2070. static_assert(std::is_same<Ioc, as::io_context>::value, "The type of ioc must be boost::asio::io_context");
  2071. return make_client(
  2072. ioc,
  2073. force_move(host),
  2074. std::to_string(port),
  2075. version
  2076. );
  2077. }
  2078. template <typename Ioc>
  2079. inline std::shared_ptr<
  2080. callable_overlay<
  2081. client<
  2082. tcp_endpoint<
  2083. as::ip::tcp::socket,
  2084. null_strand
  2085. >
  2086. >
  2087. >
  2088. >
  2089. make_client_no_strand(Ioc& ioc, std::string host, std::string port, protocol_version version) {
  2090. static_assert(std::is_same<Ioc, as::io_context>::value, "The type of ioc must be boost::asio::io_context");
  2091. using client_t = client<
  2092. tcp_endpoint<
  2093. as::ip::tcp::socket,
  2094. null_strand
  2095. >
  2096. >;
  2097. return std::make_shared<callable_overlay<client_t>>(
  2098. client_t::constructor_access(),
  2099. ioc,
  2100. force_move(host),
  2101. force_move(port),
  2102. #if defined(MQTT_USE_WS)
  2103. "/",
  2104. #endif // defined(MQTT_USE_WS)
  2105. version
  2106. );
  2107. }
  2108. template <typename Ioc>
  2109. inline std::shared_ptr<
  2110. callable_overlay<
  2111. client<
  2112. tcp_endpoint<
  2113. as::ip::tcp::socket,
  2114. null_strand
  2115. >
  2116. >
  2117. >
  2118. >
  2119. make_client_no_strand(Ioc& ioc, std::string host, std::uint16_t port, protocol_version version) {
  2120. static_assert(std::is_same<Ioc, as::io_context>::value, "The type of ioc must be boost::asio::io_context");
  2121. return make_client_no_strand(
  2122. ioc,
  2123. force_move(host),
  2124. std::to_string(port),
  2125. version
  2126. );
  2127. }
  2128. #if defined(MQTT_USE_WS)
  2129. template <typename Ioc>
  2130. inline std::shared_ptr<
  2131. callable_overlay<
  2132. client<
  2133. ws_endpoint<
  2134. as::ip::tcp::socket,
  2135. strand
  2136. >
  2137. >
  2138. >
  2139. >
  2140. make_client_ws(Ioc& ioc, std::string host, std::string port, std::string path, protocol_version version) {
  2141. static_assert(std::is_same<Ioc, as::io_context>::value, "The type of ioc must be boost::asio::io_context");
  2142. using client_t = client<
  2143. ws_endpoint<
  2144. as::ip::tcp::socket,
  2145. strand
  2146. >
  2147. >;
  2148. return std::make_shared<callable_overlay<client_t>>(
  2149. client_t::constructor_access(),
  2150. ioc,
  2151. force_move(host),
  2152. force_move(port),
  2153. force_move(path),
  2154. version
  2155. );
  2156. }
  2157. template <typename Ioc>
  2158. inline std::shared_ptr<
  2159. callable_overlay<
  2160. client<
  2161. ws_endpoint<
  2162. as::ip::tcp::socket,
  2163. strand
  2164. >
  2165. >
  2166. >
  2167. >
  2168. make_client_ws(Ioc& ioc, std::string host, std::uint16_t port, std::string path, protocol_version version) {
  2169. static_assert(std::is_same<Ioc, as::io_context>::value, "The type of ioc must be boost::asio::io_context");
  2170. return make_client_ws(
  2171. ioc,
  2172. force_move(host),
  2173. std::to_string(port),
  2174. force_move(path),
  2175. version
  2176. );
  2177. }
  2178. template <typename Ioc>
  2179. inline std::shared_ptr<
  2180. callable_overlay<
  2181. client<
  2182. ws_endpoint<
  2183. as::ip::tcp::socket,
  2184. null_strand
  2185. >
  2186. >
  2187. >
  2188. >
  2189. make_client_no_strand_ws(Ioc& ioc, std::string host, std::string port, std::string path, protocol_version version) {
  2190. static_assert(std::is_same<Ioc, as::io_context>::value, "The type of ioc must be boost::asio::io_context");
  2191. using client_t = client<
  2192. ws_endpoint<
  2193. as::ip::tcp::socket,
  2194. null_strand
  2195. >
  2196. >;
  2197. return std::make_shared<callable_overlay<client_t>>(
  2198. client_t::constructor_access(),
  2199. ioc,
  2200. force_move(host),
  2201. force_move(port),
  2202. force_move(path),
  2203. version
  2204. );
  2205. }
  2206. template <typename Ioc>
  2207. inline std::shared_ptr<
  2208. callable_overlay<
  2209. client<
  2210. ws_endpoint<
  2211. as::ip::tcp::socket,
  2212. null_strand
  2213. >
  2214. >
  2215. >
  2216. >
  2217. make_client_no_strand_ws(Ioc& ioc, std::string host, std::uint16_t port, std::string path, protocol_version version) {
  2218. static_assert(std::is_same<Ioc, as::io_context>::value, "The type of ioc must be boost::asio::io_context");
  2219. return make_client_no_strand_ws(
  2220. ioc,
  2221. force_move(host),
  2222. std::to_string(port),
  2223. force_move(path),
  2224. version
  2225. );
  2226. }
  2227. #endif // defined(MQTT_USE_WS)
  2228. #if defined(MQTT_USE_TLS)
  2229. template <typename Ioc>
  2230. inline std::shared_ptr<
  2231. callable_overlay<
  2232. client<
  2233. tcp_endpoint<
  2234. tls::stream<as::ip::tcp::socket>,
  2235. strand
  2236. >
  2237. >
  2238. >
  2239. >
  2240. make_tls_client(Ioc& ioc, std::string host, std::string port, protocol_version version) {
  2241. static_assert(std::is_same<Ioc, as::io_context>::value, "The type of ioc must be boost::asio::io_context");
  2242. using client_t = client<
  2243. tcp_endpoint<
  2244. tls::stream<as::ip::tcp::socket>,
  2245. strand
  2246. >
  2247. >;
  2248. return std::make_shared<callable_overlay<client_t>>(
  2249. client_t::constructor_access(),
  2250. ioc,
  2251. force_move(host),
  2252. force_move(port),
  2253. #if defined(MQTT_USE_WS)
  2254. "/",
  2255. #endif // defined(MQTT_USE_WS)
  2256. version
  2257. );
  2258. }
  2259. template <typename Ioc>
  2260. inline std::shared_ptr<
  2261. callable_overlay<
  2262. client<
  2263. tcp_endpoint<
  2264. tls::stream<as::ip::tcp::socket>,
  2265. strand
  2266. >
  2267. >
  2268. >
  2269. >
  2270. make_tls_client(Ioc& ioc, std::string host, std::uint16_t port, protocol_version version) {
  2271. static_assert(std::is_same<Ioc, as::io_context>::value, "The type of ioc must be boost::asio::io_context");
  2272. return make_tls_client(
  2273. ioc,
  2274. force_move(host),
  2275. std::to_string(port),
  2276. version
  2277. );
  2278. }
  2279. template <typename Ioc>
  2280. inline std::shared_ptr<
  2281. callable_overlay<
  2282. client<
  2283. tcp_endpoint<
  2284. tls::stream<as::ip::tcp::socket>,
  2285. null_strand
  2286. >
  2287. >
  2288. >
  2289. >
  2290. make_tls_client_no_strand(Ioc& ioc, std::string host, std::string port, protocol_version version) {
  2291. static_assert(std::is_same<Ioc, as::io_context>::value, "The type of ioc must be boost::asio::io_context");
  2292. using client_t = client<
  2293. tcp_endpoint<
  2294. tls::stream<as::ip::tcp::socket>,
  2295. null_strand
  2296. >
  2297. >;
  2298. return std::make_shared<callable_overlay<client_t>>(
  2299. client_t::constructor_access(),
  2300. ioc,
  2301. force_move(host),
  2302. force_move(port),
  2303. #if defined(MQTT_USE_WS)
  2304. "/",
  2305. #endif // defined(MQTT_USE_WS)
  2306. version
  2307. );
  2308. }
  2309. template <typename Ioc>
  2310. inline std::shared_ptr<
  2311. callable_overlay<
  2312. client<
  2313. tcp_endpoint<
  2314. tls::stream<as::ip::tcp::socket>,
  2315. null_strand
  2316. >
  2317. >
  2318. >
  2319. >
  2320. make_tls_client_no_strand(Ioc& ioc, std::string host, std::uint16_t port, protocol_version version) {
  2321. static_assert(std::is_same<Ioc, as::io_context>::value, "The type of ioc must be boost::asio::io_context");
  2322. return make_tls_client_no_strand(
  2323. ioc,
  2324. force_move(host),
  2325. std::to_string(port),
  2326. version
  2327. );
  2328. }
  2329. #if defined(MQTT_USE_WS)
  2330. template <typename Ioc>
  2331. inline std::shared_ptr<
  2332. callable_overlay<
  2333. client<
  2334. ws_endpoint<
  2335. tls::stream<as::ip::tcp::socket>,
  2336. strand
  2337. >
  2338. >
  2339. >
  2340. >
  2341. make_tls_client_ws(Ioc& ioc, std::string host, std::string port, std::string path, protocol_version version) {
  2342. static_assert(std::is_same<Ioc, as::io_context>::value, "The type of ioc must be boost::asio::io_context");
  2343. using client_t = client<
  2344. ws_endpoint<
  2345. tls::stream<as::ip::tcp::socket>,
  2346. strand
  2347. >
  2348. >;
  2349. return std::make_shared<callable_overlay<client_t>>(
  2350. client_t::constructor_access(),
  2351. ioc,
  2352. force_move(host),
  2353. force_move(port),
  2354. force_move(path),
  2355. version
  2356. );
  2357. }
  2358. template <typename Ioc>
  2359. inline std::shared_ptr<
  2360. callable_overlay<
  2361. client<
  2362. ws_endpoint<
  2363. tls::stream<as::ip::tcp::socket>,
  2364. strand
  2365. >
  2366. >
  2367. >
  2368. >
  2369. make_tls_client_ws(Ioc& ioc, std::string host, std::uint16_t port, std::string path, protocol_version version) {
  2370. static_assert(std::is_same<Ioc, as::io_context>::value, "The type of ioc must be boost::asio::io_context");
  2371. return make_tls_client_ws(
  2372. ioc,
  2373. force_move(host),
  2374. std::to_string(port),
  2375. force_move(path),
  2376. version
  2377. );
  2378. }
  2379. template <typename Ioc>
  2380. inline std::shared_ptr<
  2381. callable_overlay<
  2382. client<
  2383. ws_endpoint<
  2384. tls::stream<as::ip::tcp::socket>,
  2385. null_strand
  2386. >
  2387. >
  2388. >
  2389. >
  2390. make_tls_client_no_strand_ws(Ioc& ioc, std::string host, std::string port, std::string path, protocol_version version) {
  2391. static_assert(std::is_same<Ioc, as::io_context>::value, "The type of ioc must be boost::asio::io_context");
  2392. using client_t = client<
  2393. ws_endpoint<
  2394. tls::stream<as::ip::tcp::socket>,
  2395. null_strand
  2396. >
  2397. >;
  2398. return std::make_shared<callable_overlay<client_t>>(
  2399. client_t::constructor_access(),
  2400. ioc,
  2401. force_move(host),
  2402. force_move(port),
  2403. force_move(path),
  2404. version
  2405. );
  2406. }
  2407. template <typename Ioc>
  2408. inline std::shared_ptr<
  2409. callable_overlay<
  2410. client<
  2411. ws_endpoint<
  2412. tls::stream<as::ip::tcp::socket>,
  2413. null_strand
  2414. >
  2415. >
  2416. >
  2417. >
  2418. make_tls_client_no_strand_ws(Ioc& ioc, std::string host, std::uint16_t port, std::string path, protocol_version version) {
  2419. static_assert(std::is_same<Ioc, as::io_context>::value, "The type of ioc must be boost::asio::io_context");
  2420. return make_tls_client_no_strand_ws(
  2421. ioc,
  2422. force_move(host),
  2423. std::to_string(port),
  2424. force_move(path),
  2425. version
  2426. );
  2427. }
  2428. #endif // defined(MQTT_USE_WS)
  2429. #endif // defined(MQTT_USE_TLS)
  2430. // 32bit Packet Id (experimental)
  2431. template <typename Ioc>
  2432. inline std::shared_ptr<
  2433. callable_overlay<
  2434. client<
  2435. tcp_endpoint<
  2436. as::ip::tcp::socket,
  2437. strand
  2438. >,
  2439. 4
  2440. >
  2441. >
  2442. >
  2443. make_client_32(Ioc& ioc, std::string host, std::string port, protocol_version version) {
  2444. static_assert(std::is_same<Ioc, as::io_context>::value, "The type of ioc must be boost::asio::io_context");
  2445. using client_t = client<
  2446. tcp_endpoint<
  2447. as::ip::tcp::socket,
  2448. strand
  2449. >,
  2450. 4
  2451. >;
  2452. return std::make_shared<callable_overlay<client_t>>(
  2453. client_t::constructor_access(),
  2454. ioc,
  2455. force_move(host),
  2456. force_move(port),
  2457. #if defined(MQTT_USE_WS)
  2458. "/",
  2459. #endif // defined(MQTT_USE_WS)
  2460. version
  2461. );
  2462. }
  2463. template <typename Ioc>
  2464. inline std::shared_ptr<
  2465. callable_overlay<
  2466. client<
  2467. tcp_endpoint<
  2468. as::ip::tcp::socket,
  2469. strand
  2470. >,
  2471. 4
  2472. >
  2473. >
  2474. >
  2475. make_client_32(Ioc& ioc, std::string host, std::uint16_t port, protocol_version version) {
  2476. static_assert(std::is_same<Ioc, as::io_context>::value, "The type of ioc must be boost::asio::io_context");
  2477. return make_client_32(
  2478. ioc,
  2479. force_move(host),
  2480. std::to_string(port),
  2481. version
  2482. );
  2483. }
  2484. template <typename Ioc>
  2485. inline std::shared_ptr<
  2486. callable_overlay<
  2487. client<
  2488. tcp_endpoint<
  2489. as::ip::tcp::socket,
  2490. null_strand
  2491. >,
  2492. 4
  2493. >
  2494. >
  2495. >
  2496. make_client_no_strand_32(Ioc& ioc, std::string host, std::string port, protocol_version version) {
  2497. static_assert(std::is_same<Ioc, as::io_context>::value, "The type of ioc must be boost::asio::io_context");
  2498. using client_t = client<
  2499. tcp_endpoint<
  2500. as::ip::tcp::socket,
  2501. null_strand
  2502. >,
  2503. 4
  2504. >;
  2505. return std::make_shared<callable_overlay<client_t>>(
  2506. client_t::constructor_access(),
  2507. ioc,
  2508. force_move(host),
  2509. force_move(port),
  2510. #if defined(MQTT_USE_WS)
  2511. "/",
  2512. #endif // defined(MQTT_USE_WS)
  2513. version
  2514. );
  2515. }
  2516. template <typename Ioc>
  2517. inline std::shared_ptr<
  2518. callable_overlay<
  2519. client<
  2520. tcp_endpoint<
  2521. as::ip::tcp::socket,
  2522. null_strand
  2523. >,
  2524. 4
  2525. >
  2526. >
  2527. >
  2528. make_client_no_strand_32(Ioc& ioc, std::string host, std::uint16_t port, protocol_version version) {
  2529. static_assert(std::is_same<Ioc, as::io_context>::value, "The type of ioc must be boost::asio::io_context");
  2530. return make_client_no_strand_32(
  2531. ioc,
  2532. force_move(host),
  2533. std::to_string(port),
  2534. version
  2535. );
  2536. }
  2537. #if defined(MQTT_USE_WS)
  2538. template <typename Ioc>
  2539. inline std::shared_ptr<
  2540. callable_overlay<
  2541. client<
  2542. ws_endpoint<
  2543. as::ip::tcp::socket,
  2544. strand
  2545. >,
  2546. 4
  2547. >
  2548. >
  2549. >
  2550. make_client_ws_32(Ioc& ioc, std::string host, std::string port, std::string path, protocol_version version) {
  2551. static_assert(std::is_same<Ioc, as::io_context>::value, "The type of ioc must be boost::asio::io_context");
  2552. using client_t = client<
  2553. ws_endpoint<
  2554. as::ip::tcp::socket,
  2555. strand
  2556. >,
  2557. 4
  2558. >;
  2559. return std::make_shared<callable_overlay<client_t>>(
  2560. client_t::constructor_access(),
  2561. ioc,
  2562. force_move(host),
  2563. force_move(port),
  2564. force_move(path),
  2565. version
  2566. );
  2567. }
  2568. template <typename Ioc>
  2569. inline std::shared_ptr<
  2570. callable_overlay<
  2571. client<
  2572. ws_endpoint<
  2573. as::ip::tcp::socket,
  2574. strand
  2575. >,
  2576. 4
  2577. >
  2578. >
  2579. >
  2580. make_client_ws_32(Ioc& ioc, std::string host, std::uint16_t port, std::string path, protocol_version version) {
  2581. static_assert(std::is_same<Ioc, as::io_context>::value, "The type of ioc must be boost::asio::io_context");
  2582. return make_client_ws_32(
  2583. ioc,
  2584. force_move(host),
  2585. std::to_string(port),
  2586. force_move(path),
  2587. version
  2588. );
  2589. }
  2590. template <typename Ioc>
  2591. inline std::shared_ptr<
  2592. callable_overlay<
  2593. client<
  2594. ws_endpoint<
  2595. as::ip::tcp::socket,
  2596. null_strand
  2597. >,
  2598. 4
  2599. >
  2600. >
  2601. >
  2602. make_client_no_strand_ws_32(Ioc& ioc, std::string host, std::string port, std::string path, protocol_version version) {
  2603. static_assert(std::is_same<Ioc, as::io_context>::value, "The type of ioc must be boost::asio::io_context");
  2604. using client_t = client<
  2605. ws_endpoint<
  2606. as::ip::tcp::socket,
  2607. null_strand
  2608. >,
  2609. 4
  2610. >;
  2611. return std::make_shared<callable_overlay<client_t>>(
  2612. client_t::constructor_access(),
  2613. ioc,
  2614. force_move(host),
  2615. force_move(port),
  2616. force_move(path),
  2617. version
  2618. );
  2619. }
  2620. template <typename Ioc>
  2621. inline std::shared_ptr<
  2622. callable_overlay<
  2623. client<
  2624. ws_endpoint<
  2625. as::ip::tcp::socket,
  2626. null_strand
  2627. >,
  2628. 4
  2629. >
  2630. >
  2631. >
  2632. make_client_no_strand_ws_32(Ioc& ioc, std::string host, std::uint16_t port, std::string path, protocol_version version) {
  2633. static_assert(std::is_same<Ioc, as::io_context>::value, "The type of ioc must be boost::asio::io_context");
  2634. return make_client_no_strand_ws_32(
  2635. ioc,
  2636. force_move(host),
  2637. std::to_string(port),
  2638. force_move(path),
  2639. version
  2640. );
  2641. }
  2642. #endif // defined(MQTT_USE_WS)
  2643. #if defined(MQTT_USE_TLS)
  2644. template <typename Ioc>
  2645. inline std::shared_ptr<
  2646. callable_overlay<
  2647. client<
  2648. tcp_endpoint<
  2649. tls::stream<as::ip::tcp::socket>,
  2650. strand
  2651. >,
  2652. 4
  2653. >
  2654. >
  2655. >
  2656. make_tls_client_32(Ioc& ioc, std::string host, std::string port, protocol_version version) {
  2657. static_assert(std::is_same<Ioc, as::io_context>::value, "The type of ioc must be boost::asio::io_context");
  2658. using client_t = client<
  2659. tcp_endpoint<
  2660. tls::stream<as::ip::tcp::socket>,
  2661. strand
  2662. >,
  2663. 4
  2664. >;
  2665. return std::make_shared<callable_overlay<client_t>>(
  2666. client_t::constructor_access(),
  2667. ioc,
  2668. force_move(host),
  2669. force_move(port),
  2670. #if defined(MQTT_USE_WS)
  2671. "/",
  2672. #endif // defined(MQTT_USE_WS)
  2673. version
  2674. );
  2675. }
  2676. template <typename Ioc>
  2677. inline std::shared_ptr<
  2678. callable_overlay<
  2679. client<
  2680. tcp_endpoint<
  2681. tls::stream<as::ip::tcp::socket>,
  2682. strand
  2683. >,
  2684. 4
  2685. >
  2686. >
  2687. >
  2688. make_tls_client_32(Ioc& ioc, std::string host, std::uint16_t port, protocol_version version) {
  2689. static_assert(std::is_same<Ioc, as::io_context>::value, "The type of ioc must be boost::asio::io_context");
  2690. return make_tls_client_32(
  2691. ioc,
  2692. force_move(host),
  2693. std::to_string(port),
  2694. version
  2695. );
  2696. }
  2697. template <typename Ioc>
  2698. inline std::shared_ptr<
  2699. callable_overlay<
  2700. client<
  2701. tcp_endpoint<
  2702. tls::stream<as::ip::tcp::socket>,
  2703. null_strand
  2704. >,
  2705. 4
  2706. >
  2707. >
  2708. >
  2709. make_tls_client_no_strand_32(Ioc& ioc, std::string host, std::string port, protocol_version version) {
  2710. static_assert(std::is_same<Ioc, as::io_context>::value, "The type of ioc must be boost::asio::io_context");
  2711. using client_t = client<
  2712. tcp_endpoint<
  2713. tls::stream<as::ip::tcp::socket>,
  2714. null_strand
  2715. >,
  2716. 4
  2717. >;
  2718. return std::make_shared<callable_overlay<client_t>>(
  2719. client_t::constructor_access(),
  2720. ioc,
  2721. force_move(host),
  2722. force_move(port),
  2723. #if defined(MQTT_USE_WS)
  2724. "/",
  2725. #endif // defined(MQTT_USE_WS)
  2726. version
  2727. );
  2728. }
  2729. template <typename Ioc>
  2730. inline std::shared_ptr<
  2731. callable_overlay<
  2732. client<
  2733. tcp_endpoint<
  2734. tls::stream<as::ip::tcp::socket>,
  2735. null_strand
  2736. >,
  2737. 4
  2738. >
  2739. >
  2740. >
  2741. make_tls_client_no_strand_32(Ioc& ioc, std::string host, std::uint16_t port, protocol_version version) {
  2742. static_assert(std::is_same<Ioc, as::io_context>::value, "The type of ioc must be boost::asio::io_context");
  2743. return make_tls_client_no_strand_32(
  2744. ioc,
  2745. force_move(host),
  2746. std::to_string(port),
  2747. version
  2748. );
  2749. }
  2750. #if defined(MQTT_USE_WS)
  2751. template <typename Ioc>
  2752. inline std::shared_ptr<
  2753. callable_overlay<
  2754. client<
  2755. ws_endpoint<
  2756. tls::stream<as::ip::tcp::socket>,
  2757. strand
  2758. >,
  2759. 4
  2760. >
  2761. >
  2762. >
  2763. make_tls_client_ws_32(Ioc& ioc, std::string host, std::string port, std::string path, protocol_version version) {
  2764. static_assert(std::is_same<Ioc, as::io_context>::value, "The type of ioc must be boost::asio::io_context");
  2765. using client_t = client<
  2766. ws_endpoint<
  2767. tls::stream<as::ip::tcp::socket>,
  2768. strand
  2769. >,
  2770. 4
  2771. >;
  2772. return std::make_shared<callable_overlay<client_t>>(
  2773. client_t::constructor_access(),
  2774. ioc,
  2775. force_move(host),
  2776. force_move(port),
  2777. force_move(path),
  2778. version
  2779. );
  2780. }
  2781. template <typename Ioc>
  2782. inline std::shared_ptr<
  2783. callable_overlay<
  2784. client<
  2785. ws_endpoint<
  2786. tls::stream<as::ip::tcp::socket>,
  2787. strand
  2788. >,
  2789. 4
  2790. >
  2791. >
  2792. >
  2793. make_tls_client_ws_32(Ioc& ioc, std::string host, std::uint16_t port, std::string path, protocol_version version) {
  2794. static_assert(std::is_same<Ioc, as::io_context>::value, "The type of ioc must be boost::asio::io_context");
  2795. return make_tls_client_ws_32(
  2796. ioc,
  2797. force_move(host),
  2798. std::to_string(port),
  2799. force_move(path),
  2800. version
  2801. );
  2802. }
  2803. template <typename Ioc>
  2804. inline std::shared_ptr<
  2805. callable_overlay<
  2806. client<
  2807. ws_endpoint<
  2808. tls::stream<as::ip::tcp::socket>,
  2809. null_strand
  2810. >,
  2811. 4
  2812. >
  2813. >
  2814. >
  2815. make_tls_client_no_strand_ws_32(Ioc& ioc, std::string host, std::string port, std::string path, protocol_version version) {
  2816. static_assert(std::is_same<Ioc, as::io_context>::value, "The type of ioc must be boost::asio::io_context");
  2817. using client_t = client<
  2818. ws_endpoint<
  2819. tls::stream<as::ip::tcp::socket>,
  2820. null_strand
  2821. >,
  2822. 4
  2823. >;
  2824. return std::make_shared<callable_overlay<client_t>>(
  2825. client_t::constructor_access(),
  2826. ioc,
  2827. force_move(host),
  2828. force_move(port),
  2829. force_move(path),
  2830. version
  2831. );
  2832. }
  2833. template <typename Ioc>
  2834. inline std::shared_ptr<
  2835. callable_overlay<
  2836. client<
  2837. ws_endpoint<
  2838. tls::stream<as::ip::tcp::socket>,
  2839. null_strand
  2840. >,
  2841. 4
  2842. >
  2843. >
  2844. >
  2845. make_tls_client_no_strand_ws_32(Ioc& ioc, std::string host, std::uint16_t port, std::string path, protocol_version version) {
  2846. static_assert(std::is_same<Ioc, as::io_context>::value, "The type of ioc must be boost::asio::io_context");
  2847. return make_tls_client_no_strand_ws_32(
  2848. ioc,
  2849. force_move(host),
  2850. std::to_string(port),
  2851. force_move(path),
  2852. version
  2853. );
  2854. }
  2855. #endif // defined(MQTT_USE_WS)
  2856. #endif // defined(MQTT_USE_TLS)
  2857. } // namespace MQTT_NS
  2858. #endif // MQTT_CLIENT_HPP