stream.hpp 111 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894
  1. //
  2. // Copyright (c) 2016-2019 Vinnie Falco (vinnie dot falco at gmail dot com)
  3. //
  4. // Distributed under the Boost Software License, Version 1.0. (See accompanying
  5. // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  6. //
  7. // Official repository: https://github.com/boostorg/beast
  8. //
  9. #ifndef BHO_BEAST_WEBSOCKET_STREAM_HPP
  10. #define BHO_BEAST_WEBSOCKET_STREAM_HPP
  11. #include <asio2/bho/beast/core/detail/config.hpp>
  12. #include <asio2/bho/beast/websocket/error.hpp>
  13. #include <asio2/bho/beast/websocket/option.hpp>
  14. #include <asio2/bho/beast/websocket/rfc6455.hpp>
  15. #include <asio2/bho/beast/websocket/stream_base.hpp>
  16. #include <asio2/bho/beast/websocket/stream_fwd.hpp>
  17. #include <asio2/bho/beast/websocket/detail/hybi13.hpp>
  18. #include <asio2/bho/beast/websocket/detail/impl_base.hpp>
  19. #include <asio2/bho/beast/websocket/detail/pmd_extension.hpp>
  20. #include <asio2/bho/beast/websocket/detail/prng.hpp>
  21. #include <asio2/bho/beast/core/role.hpp>
  22. #include <asio2/bho/beast/core/stream_traits.hpp>
  23. #include <asio2/bho/beast/core/string.hpp>
  24. #include <asio2/bho/beast/http/detail/type_traits.hpp>
  25. #include <asio/async_result.hpp>
  26. #include <asio/error.hpp>
  27. #include <algorithm>
  28. #include <cstdint>
  29. #include <functional>
  30. #include <limits>
  31. #include <memory>
  32. #include <type_traits>
  33. #include <random>
  34. namespace bho {
  35. namespace beast {
  36. namespace websocket {
  37. /** The type of received control frame.
  38. Values of this type are passed to the control frame
  39. callback set using @ref stream::control_callback.
  40. */
  41. enum class frame_type
  42. {
  43. /// A close frame was received
  44. close,
  45. /// A ping frame was received
  46. ping,
  47. /// A pong frame was received
  48. pong
  49. };
  50. namespace detail {
  51. class frame_test;
  52. } // detail
  53. //--------------------------------------------------------------------
  54. /** Provides message-oriented functionality using WebSocket.
  55. The @ref stream class template provides asynchronous and blocking
  56. message-oriented functionality necessary for clients and servers
  57. to utilize the WebSocket protocol.
  58. For asynchronous operations, the application must ensure
  59. that they are are all performed within the same implicit
  60. or explicit strand.
  61. @par Thread Safety
  62. @e Distinct @e objects: Safe.@n
  63. @e Shared @e objects: Unsafe.
  64. The application must also ensure that all asynchronous
  65. operations are performed within the same implicit or explicit strand.
  66. @par Example
  67. To declare the @ref stream object with a @ref tcp_stream in a
  68. multi-threaded asynchronous program using a strand, you may write:
  69. @code
  70. websocket::stream<tcp_stream> ws{net::make_strand(ioc)};
  71. @endcode
  72. Alternatively, for a single-threaded or synchronous application
  73. you may write:
  74. @code
  75. websocket::stream<tcp_stream> ws(ioc);
  76. @endcode
  77. @tparam NextLayer The type representing the next layer, to which
  78. data will be read and written during operations. For synchronous
  79. operations, the type must support the <em>SyncStream</em> concept.
  80. For asynchronous operations, the type must support the
  81. <em>AsyncStream</em> concept.
  82. @tparam deflateSupported A `bool` indicating whether or not the
  83. stream will be capable of negotiating the permessage-deflate websocket
  84. extension. Note that even if this is set to `true`, the permessage
  85. deflate options (set by the caller at runtime) must still have the
  86. feature enabled for a successful negotiation to occur.
  87. @note A stream object must not be moved or destroyed while there
  88. are pending asynchronous operations associated with it.
  89. @par Concepts
  90. @li <em>AsyncStream</em>
  91. @li <em>DynamicBuffer</em>
  92. @li <em>SyncStream</em>
  93. @see
  94. @li <a href="https://tools.ietf.org/html/rfc6455#section-4.1">Websocket Opening Handshake Client Requirements (RFC6455)</a>
  95. @li <a href="https://tools.ietf.org/html/rfc6455#section-4.2">Websocket Opening Handshake Server Requirements (RFC6455)</a>
  96. @li <a href="https://tools.ietf.org/html/rfc6455#section-7.1.2">Websocket Closing Handshake (RFC6455)</a>
  97. @li <a href="https://tools.ietf.org/html/rfc6455#section-5.5.1">Websocket Close (RFC6455)</a>
  98. @li <a href="https://tools.ietf.org/html/rfc6455#section-5.5.2">WebSocket Ping (RFC6455)</a>
  99. @li <a href="https://tools.ietf.org/html/rfc6455#section-5.5.3">WebSocket Pong (RFC6455)</a>
  100. @li <a href="https://tools.ietf.org/html/rfc7230#section-5.4">Host field (RFC7230)</a>
  101. @li <a href="https://tools.ietf.org/html/rfc7230#section-3.1.1">request-target (RFC7230)</a>
  102. @li <a href="https://tools.ietf.org/html/rfc7230#section-5.3.1">origin-form (RFC7230)</a>
  103. */
  104. template<
  105. class NextLayer,
  106. bool deflateSupported>
  107. class stream
  108. #if ! BHO_BEAST_DOXYGEN
  109. : private stream_base
  110. #endif
  111. {
  112. struct impl_type;
  113. std::shared_ptr<impl_type> impl_;
  114. using time_point = typename
  115. std::chrono::steady_clock::time_point;
  116. using control_cb_type =
  117. std::function<void(frame_type, string_view)>;
  118. #ifndef BHO_BEAST_DOXYGEN
  119. friend class close_test;
  120. friend class frame_test;
  121. friend class ping_test;
  122. friend class read2_test;
  123. friend class read3_test;
  124. friend class stream_test;
  125. friend class write_test;
  126. /* The read buffer has to be at least as large
  127. as the largest possible control frame including
  128. the frame header.
  129. */
  130. static std::size_t constexpr max_control_frame_size = 2 + 8 + 4 + 125;
  131. static std::size_t constexpr tcp_frame_size = 1536;
  132. #endif
  133. static time_point never() noexcept
  134. {
  135. return (time_point::max)();
  136. }
  137. public:
  138. /// Indicates if the permessage-deflate extension is supported
  139. using is_deflate_supported =
  140. std::integral_constant<bool, deflateSupported>;
  141. /// The type of the next layer.
  142. using next_layer_type =
  143. typename std::remove_reference<NextLayer>::type;
  144. /// The type of the executor associated with the object.
  145. using executor_type =
  146. beast::executor_type<next_layer_type>;
  147. /// Rebinds the stream type to another executor.
  148. template<class Executor1>
  149. struct rebind_executor
  150. {
  151. /// The stream type when rebound to the specified executor.
  152. using other = stream<
  153. typename next_layer_type::template rebind_executor<Executor1>::other,
  154. deflateSupported>;
  155. };
  156. /** Destructor
  157. Destroys the stream and all associated resources.
  158. @note A stream object must not be destroyed while there
  159. are pending asynchronous operations associated with it.
  160. */
  161. ~stream();
  162. /** Constructor
  163. If `NextLayer` is move constructible, this function
  164. will move-construct a new stream from the existing stream.
  165. After the move, the only valid operation on the moved-from
  166. object is destruction.
  167. */
  168. stream(stream&&) = default;
  169. /// Move assignment (deleted)
  170. stream& operator=(stream&&) = delete;
  171. /** Constructor
  172. This constructor creates a websocket stream and initializes
  173. the next layer object.
  174. @throws Any exceptions thrown by the NextLayer constructor.
  175. @param args The arguments to be passed to initialize the
  176. next layer object. The arguments are forwarded to the next
  177. layer's constructor.
  178. */
  179. template<class... Args>
  180. explicit
  181. stream(Args&&... args);
  182. /** Rebinding constructor
  183. *
  184. * This constructor creates a the websocket stream from a
  185. * websocket stream with a different executor.
  186. *
  187. * @throw Any exception thrown by the NextLayer rebind constructor.
  188. *
  189. * @param other The other websocket stream to construct from.
  190. */
  191. template<class Other>
  192. explicit
  193. stream(stream<Other> && other);
  194. //--------------------------------------------------------------------------
  195. /** Get the executor associated with the object.
  196. This function may be used to obtain the executor object that the
  197. stream uses to dispatch handlers for asynchronous operations.
  198. @return A copy of the executor that stream will use to dispatch handlers.
  199. */
  200. executor_type
  201. get_executor() noexcept;
  202. /** Get a reference to the next layer
  203. This function returns a reference to the next layer
  204. in a stack of stream layers.
  205. @return A reference to the next layer in the stack of
  206. stream layers.
  207. */
  208. next_layer_type&
  209. next_layer() noexcept;
  210. /** Get a reference to the next layer
  211. This function returns a reference to the next layer in a
  212. stack of stream layers.
  213. @return A reference to the next layer in the stack of
  214. stream layers.
  215. */
  216. next_layer_type const&
  217. next_layer() const noexcept;
  218. //--------------------------------------------------------------------------
  219. //
  220. // Observers
  221. //
  222. //--------------------------------------------------------------------------
  223. /** Returns `true` if the stream is open.
  224. The stream is open after a successful handshake, and when
  225. no error has occurred.
  226. */
  227. bool
  228. is_open() const noexcept;
  229. /** Returns `true` if the latest message data indicates binary.
  230. This function informs the caller of whether the last
  231. received message frame represents a message with the
  232. binary opcode.
  233. If there is no last message frame, the return value is
  234. undefined.
  235. */
  236. bool
  237. got_binary() const noexcept;
  238. /** Returns `true` if the latest message data indicates text.
  239. This function informs the caller of whether the last
  240. received message frame represents a message with the
  241. text opcode.
  242. If there is no last message frame, the return value is
  243. undefined.
  244. */
  245. bool
  246. got_text() const
  247. {
  248. return ! got_binary();
  249. }
  250. /// Returns `true` if the last completed read finished the current message.
  251. bool
  252. is_message_done() const noexcept;
  253. /** Returns the close reason received from the remote peer.
  254. This is only valid after a read completes with error::closed.
  255. */
  256. close_reason const&
  257. reason() const noexcept;
  258. /** Returns a suggested maximum buffer size for the next call to read.
  259. This function returns a reasonable upper limit on the number
  260. of bytes for the size of the buffer passed in the next call
  261. to read. The number is determined by the state of the current
  262. frame and whether or not the permessage-deflate extension is
  263. enabled.
  264. @param initial_size A non-zero size representing the caller's
  265. desired buffer size for when there is no information which may
  266. be used to calculate a more specific value. For example, when
  267. reading the first frame header of a message.
  268. */
  269. std::size_t
  270. read_size_hint(
  271. std::size_t initial_size = +tcp_frame_size) const;
  272. /** Returns a suggested maximum buffer size for the next call to read.
  273. This function returns a reasonable upper limit on the number
  274. of bytes for the size of the buffer passed in the next call
  275. to read. The number is determined by the state of the current
  276. frame and whether or not the permessage-deflate extension is
  277. enabled.
  278. @param buffer The buffer which will be used for reading. The
  279. implementation will query the buffer to obtain the optimum
  280. size of a subsequent call to `buffer.prepare` based on the
  281. state of the current frame, if any.
  282. */
  283. template<class DynamicBuffer
  284. #if ! BHO_BEAST_DOXYGEN
  285. , class = typename std::enable_if<
  286. ! std::is_integral<DynamicBuffer>::value>::type
  287. #endif
  288. >
  289. std::size_t
  290. read_size_hint(
  291. DynamicBuffer& buffer) const;
  292. //--------------------------------------------------------------------------
  293. //
  294. // Settings
  295. //
  296. //--------------------------------------------------------------------------
  297. #if BHO_BEAST_DOXYGEN
  298. /// Get the option value
  299. template<class Option>
  300. void
  301. get_option(Option& opt);
  302. /// Set the option value
  303. template<class Option>
  304. void
  305. set_option(Option opt);
  306. #else
  307. void set_option(decorator opt);
  308. #endif
  309. /** Set the timeout option
  310. @throws system_error on failure to reset the
  311. timer.
  312. */
  313. void
  314. set_option(timeout const& opt);
  315. /// Get the timeout option
  316. void
  317. get_option(timeout& opt);
  318. /** Set the permessage-deflate extension options
  319. @throws invalid_argument if `deflateSupported == false`, and either
  320. `client_enable` or `server_enable` is `true`.
  321. */
  322. void
  323. set_option(permessage_deflate const& o);
  324. /// Get the permessage-deflate extension options
  325. void
  326. get_option(permessage_deflate& o);
  327. /** Set the automatic fragmentation option.
  328. Determines if outgoing message payloads are broken up into
  329. multiple pieces.
  330. When the automatic fragmentation size is turned on, outgoing
  331. message payloads are broken up into multiple frames no larger
  332. than the write buffer size.
  333. The default setting is to fragment messages.
  334. @param value A `bool` indicating if auto fragmentation should be on.
  335. @par Example
  336. Setting the automatic fragmentation option:
  337. @code
  338. ws.auto_fragment(true);
  339. @endcode
  340. */
  341. void
  342. auto_fragment(bool value);
  343. /// Returns `true` if the automatic fragmentation option is set.
  344. bool
  345. auto_fragment() const;
  346. /** Set the binary message write option.
  347. This controls whether or not outgoing message opcodes
  348. are set to binary or text. The setting is only applied
  349. at the start when a caller begins a new message. Changing
  350. the opcode after a message is started will only take effect
  351. after the current message being sent is complete.
  352. The default setting is to send text messages.
  353. @param value `true` if outgoing messages should indicate
  354. binary, or `false` if they should indicate text.
  355. @par Example
  356. Setting the message type to binary.
  357. @code
  358. ws.binary(true);
  359. @endcode
  360. */
  361. void
  362. binary(bool value);
  363. /// Returns `true` if the binary message write option is set.
  364. bool
  365. binary() const;
  366. /** Set a callback to be invoked on each incoming control frame.
  367. Sets the callback to be invoked whenever a ping, pong,
  368. or close control frame is received during a call to one
  369. of the following functions:
  370. @li @ref beast::websocket::stream::read
  371. @li @ref beast::websocket::stream::read_some
  372. @li @ref beast::websocket::stream::async_read
  373. @li @ref beast::websocket::stream::async_read_some
  374. Unlike completion handlers, the callback will be invoked
  375. for each control frame during a call to any synchronous
  376. or asynchronous read function. The operation is passive,
  377. with no associated error code, and triggered by reads.
  378. For close frames, the close reason code may be obtained by
  379. calling the function @ref reason.
  380. @param cb The function object to call, which must be
  381. invocable with this equivalent signature:
  382. @code
  383. void
  384. callback(
  385. frame_type kind, // The type of frame
  386. string_view payload // The payload in the frame
  387. );
  388. @endcode
  389. The implementation type-erases the callback which may require
  390. a dynamic allocation. To prevent the possibility of a dynamic
  391. allocation, use `std::ref` to wrap the callback.
  392. If the read operation which receives the control frame is
  393. an asynchronous operation, the callback will be invoked using
  394. the same method as that used to invoke the final handler.
  395. @note Incoming ping and close frames are automatically
  396. handled. Pings are responded to with pongs, and a close frame
  397. is responded to with a close frame leading to the closure of
  398. the stream. It is not necessary to manually send pings, pongs,
  399. or close frames from inside the control callback.
  400. Attempting to manually send a close frame from inside the
  401. control callback after receiving a close frame will result
  402. in undefined behavior.
  403. */
  404. void
  405. control_callback(std::function<void(frame_type, string_view)> cb);
  406. /** Reset the control frame callback.
  407. This function removes any previously set control frame callback.
  408. */
  409. void
  410. control_callback();
  411. /** Set the maximum incoming message size option.
  412. Sets the largest permissible incoming message size. Message
  413. frame fields indicating a size that would bring the total
  414. message size over this limit will cause a protocol failure.
  415. The default setting is 16 megabytes. A value of zero indicates
  416. a limit of the maximum value of a `std::uint64_t`.
  417. @par Example
  418. Setting the maximum read message size.
  419. @code
  420. ws.read_message_max(65536);
  421. @endcode
  422. @param amount The limit on the size of incoming messages.
  423. */
  424. void
  425. read_message_max(std::size_t amount);
  426. /// Returns the maximum incoming message size setting.
  427. std::size_t
  428. read_message_max() const;
  429. /** Set whether the PRNG is cryptographically secure
  430. This controls whether or not the source of pseudo-random
  431. numbers used to produce the masks required by the WebSocket
  432. protocol are of cryptographic quality. When the setting is
  433. `true`, a strong algorithm is used which cannot be guessed
  434. by observing outputs. When the setting is `false`, a much
  435. faster algorithm is used.
  436. Masking is only performed by streams operating in the client
  437. mode. For streams operating in the server mode, this setting
  438. has no effect.
  439. By default, newly constructed streams use a secure PRNG.
  440. If the WebSocket stream is used with an encrypted SSL or TLS
  441. next layer, if it is known to the application that intermediate
  442. proxies are not vulnerable to cache poisoning, or if the
  443. application is designed such that an attacker cannot send
  444. arbitrary inputs to the stream interface, then the faster
  445. algorithm may be used.
  446. For more information please consult the WebSocket protocol RFC.
  447. @param value `true` if the PRNG algorithm should be
  448. cryptographically secure.
  449. */
  450. void
  451. secure_prng(bool value);
  452. /** Set the write buffer size option.
  453. Sets the size of the write buffer used by the implementation to
  454. send frames. The write buffer is needed when masking payload data
  455. in the client role, compressing frames, or auto-fragmenting message
  456. data.
  457. Lowering the size of the buffer can decrease the memory requirements
  458. for each connection, while increasing the size of the buffer can reduce
  459. the number of calls made to the next layer to write data.
  460. The default setting is 4096. The minimum value is 8.
  461. The write buffer size can only be changed when the stream is not
  462. open. Undefined behavior results if the option is modified after a
  463. successful WebSocket handshake.
  464. @par Example
  465. Setting the write buffer size.
  466. @code
  467. ws.write_buffer_bytes(8192);
  468. @endcode
  469. @param amount The size of the write buffer in bytes.
  470. */
  471. void
  472. write_buffer_bytes(std::size_t amount);
  473. /// Returns the size of the write buffer.
  474. std::size_t
  475. write_buffer_bytes() const;
  476. /** Set the text message write option.
  477. This controls whether or not outgoing message opcodes
  478. are set to binary or text. The setting is only applied
  479. at the start when a caller begins a new message. Changing
  480. the opcode after a message is started will only take effect
  481. after the current message being sent is complete.
  482. The default setting is to send text messages.
  483. @param value `true` if outgoing messages should indicate
  484. text, or `false` if they should indicate binary.
  485. @par Example
  486. Setting the message type to text.
  487. @code
  488. ws.text(true);
  489. @endcode
  490. */
  491. void
  492. text(bool value);
  493. /// Returns `true` if the text message write option is set.
  494. bool
  495. text() const;
  496. /** Set the compress message write option.
  497. This controls whether or not outgoing messages should be
  498. compressed. The setting is only applied when
  499. @li The template parameter `deflateSupported` is true
  500. @li Compression is enable. This is controlled with `stream::set_option`
  501. @li Client and server have negotiated permessage-deflate settings
  502. @li The message is larger than `permessage_deflate::msg_size_threshold`
  503. This function permits adjusting per-message compression.
  504. Changing the opcode after a message is started will only take effect
  505. after the current message being sent is complete.
  506. The default setting is to compress messages whenever the conditions
  507. above are true.
  508. @param value `true` if outgoing messages should be compressed
  509. @par Example
  510. Disabling compression for a single message.
  511. @code
  512. ws.compress(false);
  513. ws.write(net::buffer(s), ec);
  514. ws.compress(true);
  515. @endcode
  516. */
  517. void
  518. compress(bool value);
  519. /// Returns `true` if the compress message write option is set.
  520. bool
  521. compress() const;
  522. /*
  523. timer settings
  524. * Timer is disabled
  525. * Close on timeout
  526. - no complete frame received, OR
  527. - no complete frame sent
  528. * Ping on timeout
  529. - ping on no complete frame received
  530. * if can't ping?
  531. */
  532. //--------------------------------------------------------------------------
  533. //
  534. // Handshaking (Client)
  535. //
  536. //--------------------------------------------------------------------------
  537. /** Perform the WebSocket handshake in the client role.
  538. This function is used to perform the
  539. <a href="https://en.wikipedia.org/wiki/WebSocket#Protocol_handshake">WebSocket handshake</a>,
  540. required before messages can be sent and received. During the handshake,
  541. the client sends the Websocket Upgrade HTTP request, and the server
  542. replies with an HTTP response indicating the result of the handshake.
  543. The call blocks until one of the following conditions is true:
  544. @li The request is sent and the response is received.
  545. @li An error occurs.
  546. The algorithm, known as a <em>composed operation</em>, is implemented
  547. in terms of calls to the next layer's `read_some` and `write_some`
  548. functions.
  549. The handshake is successful if the received HTTP response
  550. indicates the upgrade was accepted by the server, represented by a
  551. <a href="https://tools.ietf.org/html/rfc7230#section-3.1.2">status-code</a>
  552. of @ref beast::http::status::switching_protocols.
  553. @param host The name of the remote host. This is required by
  554. the HTTP protocol to set the "Host" header field.
  555. @param target The request-target, in origin-form. The server may use the
  556. target to distinguish different services on the same listening port.
  557. @throws system_error Thrown on failure.
  558. @par Example
  559. @code
  560. ws.handshake("localhost", "/");
  561. @endcode
  562. @see
  563. @li <a href="https://tools.ietf.org/html/rfc6455#section-4.1">Websocket Opening Handshake Client Requirements (RFC6455)</a>
  564. @li <a href="https://tools.ietf.org/html/rfc7230#section-5.4">Host field (RFC7230)</a>
  565. @li <a href="https://tools.ietf.org/html/rfc7230#section-3.1.1">request-target (RFC7230)</a>
  566. @li <a href="https://tools.ietf.org/html/rfc7230#section-5.3.1">origin-form (RFC7230)</a>
  567. */
  568. void
  569. handshake(
  570. string_view host,
  571. string_view target);
  572. /** Perform the WebSocket handshake in the client role.
  573. This function is used to perform the
  574. <a href="https://en.wikipedia.org/wiki/WebSocket#Protocol_handshake">WebSocket handshake</a>,
  575. required before messages can be sent and received. During the handshake,
  576. the client sends the Websocket Upgrade HTTP request, and the server
  577. replies with an HTTP response indicating the result of the handshake.
  578. The call blocks until one of the following conditions is true:
  579. @li The request is sent and the response is received.
  580. @li An error occurs.
  581. The algorithm, known as a <em>composed operation</em>, is implemented
  582. in terms of calls to the next layer's `read_some` and `write_some`
  583. functions.
  584. The handshake is successful if the received HTTP response
  585. indicates the upgrade was accepted by the server, represented by a
  586. <a href="https://tools.ietf.org/html/rfc7230#section-3.1.2">status-code</a>
  587. of @ref beast::http::status::switching_protocols.
  588. @param res The HTTP Upgrade response returned by the remote
  589. endpoint. The caller may use the response to access any
  590. additional information sent by the server. Note that the response object
  591. referenced by this parameter will be updated as long as the stream has
  592. received a valid HTTP response. If not (for example because of a communications
  593. error), the response contents will be undefined except for the result() which
  594. will bet set to 500, Internal Server Error.
  595. @param host The name of the remote host. This is required by
  596. the HTTP protocol to set the "Host" header field.
  597. @param target The request-target, in origin-form. The server may use the
  598. target to distinguish different services on the same listening port.
  599. @throws system_error Thrown on failure.
  600. @par Example
  601. @code
  602. response_type res;
  603. ws.handshake(res, "localhost", "/");
  604. std::cout << res;
  605. @endcode
  606. @see
  607. @li <a href="https://tools.ietf.org/html/rfc6455#section-4.1">Websocket Opening Handshake Client Requirements (RFC6455)</a>
  608. @li <a href="https://tools.ietf.org/html/rfc7230#section-5.4">Host field (RFC7230)</a>
  609. @li <a href="https://tools.ietf.org/html/rfc7230#section-3.1.1">request-target (RFC7230)</a>
  610. @li <a href="https://tools.ietf.org/html/rfc7230#section-5.3.1">origin-form (RFC7230)</a>
  611. */
  612. void
  613. handshake(
  614. response_type& res,
  615. string_view host,
  616. string_view target);
  617. /** Perform the WebSocket handshake in the client role.
  618. This function is used to perform the
  619. <a href="https://en.wikipedia.org/wiki/WebSocket#Protocol_handshake">WebSocket handshake</a>,
  620. required before messages can be sent and received. During the handshake,
  621. the client sends the Websocket Upgrade HTTP request, and the server
  622. replies with an HTTP response indicating the result of the handshake.
  623. The call blocks until one of the following conditions is true:
  624. @li The request is sent and the response is received.
  625. @li An error occurs.
  626. The algorithm, known as a <em>composed operation</em>, is implemented
  627. in terms of calls to the next layer's `read_some` and `write_some`
  628. functions.
  629. The handshake is successful if the received HTTP response
  630. indicates the upgrade was accepted by the server, represented by a
  631. <a href="https://tools.ietf.org/html/rfc7230#section-3.1.2">status-code</a>
  632. of @ref beast::http::status::switching_protocols.
  633. @param host The name of the remote host. This is required by
  634. the HTTP protocol to set the "Host" header field.
  635. @param target The request-target, in origin-form. The server may use the
  636. target to distinguish different services on the same listening port.
  637. @param ec Set to indicate what error occurred, if any.
  638. @par Example
  639. @code
  640. error_code ec;
  641. ws.handshake("localhost", "/", ec);
  642. @endcode
  643. @see
  644. @li <a href="https://tools.ietf.org/html/rfc6455#section-4.1">Websocket Opening Handshake Client Requirements (RFC6455)</a>
  645. @li <a href="https://tools.ietf.org/html/rfc7230#section-5.4">Host field (RFC7230)</a>
  646. @li <a href="https://tools.ietf.org/html/rfc7230#section-3.1.1">request-target (RFC7230)</a>
  647. @li <a href="https://tools.ietf.org/html/rfc7230#section-5.3.1">origin-form (RFC7230)</a>
  648. */
  649. void
  650. handshake(
  651. string_view host,
  652. string_view target,
  653. error_code& ec);
  654. /** Perform the WebSocket handshake in the client role.
  655. This function is used to perform the
  656. <a href="https://en.wikipedia.org/wiki/WebSocket#Protocol_handshake">WebSocket handshake</a>,
  657. required before messages can be sent and received. During the handshake,
  658. the client sends the Websocket Upgrade HTTP request, and the server
  659. replies with an HTTP response indicating the result of the handshake.
  660. The call blocks until one of the following conditions is true:
  661. @li The request is sent and the response is received.
  662. @li An error occurs.
  663. The algorithm, known as a <em>composed operation</em>, is implemented
  664. in terms of calls to the next layer's `read_some` and `write_some`
  665. functions.
  666. The handshake is successful if the received HTTP response
  667. indicates the upgrade was accepted by the server, represented by a
  668. <a href="https://tools.ietf.org/html/rfc7230#section-3.1.2">status-code</a>
  669. of @ref beast::http::status::switching_protocols.
  670. @param res The HTTP Upgrade response returned by the remote
  671. endpoint. The caller may use the response to access any
  672. additional information sent by the server.
  673. @param host The name of the remote host. This is required by
  674. the HTTP protocol to set the "Host" header field.
  675. @param target The request-target, in origin-form. The server may use the
  676. target to distinguish different services on the same listening port.
  677. @param ec Set to indicate what error occurred, if any.
  678. @par Example
  679. @code
  680. error_code ec;
  681. response_type res;
  682. ws.handshake(res, "localhost", "/", ec);
  683. if(! ec)
  684. std::cout << res;
  685. @endcode
  686. @see
  687. @li <a href="https://tools.ietf.org/html/rfc6455#section-4.1">Websocket Opening Handshake Client Requirements (RFC6455)</a>
  688. @li <a href="https://tools.ietf.org/html/rfc7230#section-5.4">Host field (RFC7230)</a>
  689. @li <a href="https://tools.ietf.org/html/rfc7230#section-3.1.1">request-target (RFC7230)</a>
  690. @li <a href="https://tools.ietf.org/html/rfc7230#section-5.3.1">origin-form (RFC7230)</a>
  691. */
  692. void
  693. handshake(
  694. response_type& res,
  695. string_view host,
  696. string_view target,
  697. error_code& ec);
  698. /** Perform the WebSocket handshake asynchronously in the client role.
  699. This initiating function is used to asynchronously begin performing the
  700. <a href="https://en.wikipedia.org/wiki/WebSocket#Protocol_handshake">WebSocket handshake</a>,
  701. required before messages can be sent and received. During the handshake,
  702. the client sends the Websocket Upgrade HTTP request, and the server
  703. replies with an HTTP response indicating the result of the handshake.
  704. This call always returns immediately. The asynchronous operation
  705. will continue until one of the following conditions is true:
  706. @li The request is sent and the response is received.
  707. @li An error occurs.
  708. The algorithm, known as a <em>composed asynchronous operation</em>,
  709. is implemented in terms of calls to the next layer's `async_read_some`
  710. and `async_write_some` functions. No other operation may be performed
  711. on the stream until this operation completes.
  712. The handshake is successful if the received HTTP response
  713. indicates the upgrade was accepted by the server, represented by a
  714. <a href="https://tools.ietf.org/html/rfc7230#section-3.1.2">status-code</a>
  715. of @ref beast::http::status::switching_protocols.
  716. @param host The name of the remote host. This is required by
  717. the HTTP protocol to set the "Host" header field.
  718. The implementation will not access the string data after the
  719. initiating function returns.
  720. @param target The request-target, in origin-form. The server may use the
  721. target to distinguish different services on the same listening port.
  722. The implementation will not access the string data after the
  723. initiating function returns.
  724. @param handler The completion handler to invoke when the operation
  725. completes. The implementation takes ownership of the handler by
  726. performing a decay-copy. The equivalent function signature of
  727. the handler must be:
  728. @code
  729. void handler(
  730. error_code const& ec // Result of operation
  731. );
  732. @endcode
  733. If the handler has an associated immediate executor,
  734. an immediate completion will be dispatched to it.
  735. Otherwise, the handler will not be invoked from within
  736. this function. Invocation of the handler will be performed
  737. by dispatching to the immediate executor. If no
  738. immediate executor is specified, this is equivalent
  739. to using `net::post`.
  740. @par Example
  741. @code
  742. ws.async_handshake("localhost", "/",
  743. [](error_code ec)
  744. {
  745. if(ec)
  746. std::cerr << "Error: " << ec.message() << "\n";
  747. });
  748. @endcode
  749. @see
  750. @li <a href="https://tools.ietf.org/html/rfc6455#section-4.1">Websocket Opening Handshake Client Requirements (RFC6455)</a>
  751. @li <a href="https://tools.ietf.org/html/rfc7230#section-5.4">Host field (RFC7230)</a>
  752. @li <a href="https://tools.ietf.org/html/rfc7230#section-3.1.1">request-target (RFC7230)</a>
  753. @li <a href="https://tools.ietf.org/html/rfc7230#section-5.3.1">origin-form (RFC7230)</a>
  754. */
  755. template<
  756. BHO_BEAST_ASYNC_TPARAM1 HandshakeHandler =
  757. net::default_completion_token_t<executor_type>
  758. >
  759. BHO_BEAST_ASYNC_RESULT1(HandshakeHandler)
  760. async_handshake(
  761. string_view host,
  762. string_view target,
  763. HandshakeHandler&& handler =
  764. net::default_completion_token_t<
  765. executor_type>{});
  766. /** Perform the WebSocket handshake asynchronously in the client role.
  767. This initiating function is used to asynchronously begin performing the
  768. <a href="https://en.wikipedia.org/wiki/WebSocket#Protocol_handshake">WebSocket handshake</a>,
  769. required before messages can be sent and received. During the handshake,
  770. the client sends the Websocket Upgrade HTTP request, and the server
  771. replies with an HTTP response indicating the result of the handshake.
  772. This call always returns immediately. The asynchronous operation
  773. will continue until one of the following conditions is true:
  774. @li The request is sent and the response is received.
  775. @li An error occurs.
  776. The algorithm, known as a <em>composed asynchronous operation</em>,
  777. is implemented in terms of calls to the next layer's `async_read_some`
  778. and `async_write_some` functions. No other operation may be performed
  779. on the stream until this operation completes.
  780. The handshake is successful if the received HTTP response
  781. indicates the upgrade was accepted by the server, represented by a
  782. <a href="https://tools.ietf.org/html/rfc7230#section-3.1.2">status-code</a>
  783. of @ref beast::http::status::switching_protocols.
  784. @param res The HTTP Upgrade response returned by the remote
  785. endpoint. The caller may use the response to access any
  786. additional information sent by the server. This object will
  787. be assigned before the completion handler is invoked.
  788. @param host The name of the remote host. This is required by
  789. the HTTP protocol to set the "Host" header field.
  790. The implementation will not access the string data after the
  791. initiating function returns.
  792. @param target The request-target, in origin-form. The server may use the
  793. target to distinguish different services on the same listening port.
  794. The implementation will not access the string data after the
  795. initiating function returns.
  796. @param handler The completion handler to invoke when the operation
  797. completes. The implementation takes ownership of the handler by
  798. performing a decay-copy. The equivalent function signature of
  799. the handler must be:
  800. @code
  801. void handler(
  802. error_code const& ec // Result of operation
  803. );
  804. @endcode
  805. If the handler has an associated immediate executor,
  806. an immediate completion will be dispatched to it.
  807. Otherwise, the handler will not be invoked from within
  808. this function. Invocation of the handler will be performed
  809. by dispatching to the immediate executor. If no
  810. immediate executor is specified, this is equivalent
  811. to using `net::post`.
  812. @par Example
  813. @code
  814. response_type res;
  815. ws.async_handshake(res, "localhost", "/",
  816. [&res](error_code ec)
  817. {
  818. if(ec)
  819. std::cerr << "Error: " << ec.message() << "\n";
  820. else
  821. std::cout << res;
  822. });
  823. @endcode
  824. @see
  825. @li <a href="https://tools.ietf.org/html/rfc6455#section-4.1">Websocket Opening Handshake Client Requirements (RFC6455)</a>
  826. @li <a href="https://tools.ietf.org/html/rfc7230#section-5.4">Host field (RFC7230)</a>
  827. @li <a href="https://tools.ietf.org/html/rfc7230#section-3.1.1">request-target (RFC7230)</a>
  828. @li <a href="https://tools.ietf.org/html/rfc7230#section-5.3.1">origin-form (RFC7230)</a>
  829. */
  830. template<
  831. BHO_BEAST_ASYNC_TPARAM1 HandshakeHandler =
  832. net::default_completion_token_t<executor_type>
  833. >
  834. BHO_BEAST_ASYNC_RESULT1(HandshakeHandler)
  835. async_handshake(
  836. response_type& res,
  837. string_view host,
  838. string_view target,
  839. HandshakeHandler&& handler =
  840. net::default_completion_token_t<
  841. executor_type>{});
  842. //--------------------------------------------------------------------------
  843. //
  844. // Handshaking (Server)
  845. //
  846. //--------------------------------------------------------------------------
  847. /** Perform the WebSocket handshake in the server role.
  848. This function is used to perform the
  849. <a href="https://en.wikipedia.org/wiki/WebSocket#Protocol_handshake">WebSocket handshake</a>,
  850. required before messages can be sent and received. During the handshake,
  851. the client sends the Websocket Upgrade HTTP request, and the server
  852. replies with an HTTP response indicating the result of the handshake.
  853. The call blocks until one of the following conditions is true:
  854. @li The request is received and the response is sent.
  855. @li An error occurs.
  856. The algorithm, known as a <em>composed operation</em>, is implemented
  857. in terms of calls to the next layer's `read_some` and `write_some`
  858. functions.
  859. If a valid upgrade request is received, an HTTP response with a
  860. <a href="https://tools.ietf.org/html/rfc7230#section-3.1.2">status-code</a>
  861. of @ref beast::http::status::switching_protocols is sent to
  862. the peer, otherwise a non-successful error is associated with
  863. the operation.
  864. If the request size exceeds the capacity of the stream's
  865. internal buffer, the error @ref error::buffer_overflow will be
  866. indicated. To handle larger requests, an application should
  867. read the HTTP request directly using @ref http::read and then
  868. pass the request to the appropriate overload of @ref accept or
  869. @ref async_accept
  870. @throws system_error Thrown on failure.
  871. @see
  872. @li <a href="https://tools.ietf.org/html/rfc6455#section-4.2">Websocket Opening Handshake Server Requirements (RFC6455)</a>
  873. */
  874. void
  875. accept();
  876. /** Read and respond to a WebSocket HTTP Upgrade request.
  877. This function is used to perform the
  878. <a href="https://en.wikipedia.org/wiki/WebSocket#Protocol_handshake">WebSocket handshake</a>,
  879. required before messages can be sent and received. During the handshake,
  880. the client sends the Websocket Upgrade HTTP request, and the server
  881. replies with an HTTP response indicating the result of the handshake.
  882. The call blocks until one of the following conditions is true:
  883. @li The request is received and the response is sent.
  884. @li An error occurs.
  885. The algorithm, known as a <em>composed operation</em>, is implemented
  886. in terms of calls to the next layer's `read_some` and `write_some`
  887. functions.
  888. If a valid upgrade request is received, an HTTP response with a
  889. <a href="https://tools.ietf.org/html/rfc7230#section-3.1.2">status-code</a>
  890. of @ref beast::http::status::switching_protocols is sent to
  891. the peer, otherwise a non-successful error is associated with
  892. the operation.
  893. If the request size exceeds the capacity of the stream's
  894. internal buffer, the error @ref error::buffer_overflow will be
  895. indicated. To handle larger requests, an application should
  896. read the HTTP request directly using @ref http::read and then
  897. pass the request to the appropriate overload of @ref accept or
  898. @ref async_accept
  899. @param ec Set to indicate what error occurred, if any.
  900. @see
  901. @li <a href="https://tools.ietf.org/html/rfc6455#section-4.2">Websocket Opening Handshake Server Requirements (RFC6455)</a>
  902. */
  903. void
  904. accept(error_code& ec);
  905. /** Read and respond to a WebSocket HTTP Upgrade request.
  906. This function is used to perform the
  907. <a href="https://en.wikipedia.org/wiki/WebSocket#Protocol_handshake">WebSocket handshake</a>,
  908. required before messages can be sent and received. During the handshake,
  909. the client sends the Websocket Upgrade HTTP request, and the server
  910. replies with an HTTP response indicating the result of the handshake.
  911. The call blocks until one of the following conditions is true:
  912. @li The request is received and the response is sent.
  913. @li An error occurs.
  914. The algorithm, known as a <em>composed operation</em>, is implemented
  915. in terms of calls to the next layer's `read_some` and `write_some`
  916. functions.
  917. If a valid upgrade request is received, an HTTP response with a
  918. <a href="https://tools.ietf.org/html/rfc7230#section-3.1.2">status-code</a>
  919. of @ref beast::http::status::switching_protocols is sent to
  920. the peer, otherwise a non-successful error is associated with
  921. the operation.
  922. If the request size exceeds the capacity of the stream's
  923. internal buffer, the error @ref error::buffer_overflow will be
  924. indicated. To handle larger requests, an application should
  925. read the HTTP request directly using @ref http::read and then
  926. pass the request to the appropriate overload of @ref accept or
  927. @ref async_accept
  928. @param buffers Caller provided data that has already been
  929. received on the stream. The implementation will copy the
  930. caller provided data before the function returns.
  931. @throws system_error Thrown on failure.
  932. @see
  933. @li <a href="https://tools.ietf.org/html/rfc6455#section-4.2">Websocket Opening Handshake Server Requirements (RFC6455)</a>
  934. */
  935. template<class ConstBufferSequence>
  936. #if BHO_BEAST_DOXYGEN
  937. void
  938. #else
  939. typename std::enable_if<! http::detail::is_header<
  940. ConstBufferSequence>::value>::type
  941. #endif
  942. accept(ConstBufferSequence const& buffers);
  943. /** Read and respond to a WebSocket HTTP Upgrade request.
  944. This function is used to perform the
  945. <a href="https://en.wikipedia.org/wiki/WebSocket#Protocol_handshake">WebSocket handshake</a>,
  946. required before messages can be sent and received. During the handshake,
  947. the client sends the Websocket Upgrade HTTP request, and the server
  948. replies with an HTTP response indicating the result of the handshake.
  949. The call blocks until one of the following conditions is true:
  950. @li The request is received and the response is sent.
  951. @li An error occurs.
  952. The algorithm, known as a <em>composed operation</em>, is implemented
  953. in terms of calls to the next layer's `read_some` and `write_some`
  954. functions.
  955. If a valid upgrade request is received, an HTTP response with a
  956. <a href="https://tools.ietf.org/html/rfc7230#section-3.1.2">status-code</a>
  957. of @ref beast::http::status::switching_protocols is sent to
  958. the peer, otherwise a non-successful error is associated with
  959. the operation.
  960. If the request size exceeds the capacity of the stream's
  961. internal buffer, the error @ref error::buffer_overflow will be
  962. indicated. To handle larger requests, an application should
  963. read the HTTP request directly using @ref http::read and then
  964. pass the request to the appropriate overload of @ref accept or
  965. @ref async_accept
  966. @param buffers Caller provided data that has already been
  967. received on the stream. The implementation will copy the
  968. caller provided data before the function returns.
  969. @param ec Set to indicate what error occurred, if any.
  970. @see
  971. @li <a href="https://tools.ietf.org/html/rfc6455#section-4.2">Websocket Opening Handshake Server Requirements (RFC6455)</a>
  972. */
  973. template<class ConstBufferSequence>
  974. #if BHO_BEAST_DOXYGEN
  975. void
  976. #else
  977. typename std::enable_if<! http::detail::is_header<
  978. ConstBufferSequence>::value>::type
  979. #endif
  980. accept(
  981. ConstBufferSequence const& buffers,
  982. error_code& ec);
  983. /** Respond to a WebSocket HTTP Upgrade request
  984. This function is used to perform the
  985. <a href="https://en.wikipedia.org/wiki/WebSocket#Protocol_handshake">WebSocket handshake</a>,
  986. required before messages can be sent and received. During the handshake,
  987. the client sends the Websocket Upgrade HTTP request, and the server
  988. replies with an HTTP response indicating the result of the handshake.
  989. The call blocks until one of the following conditions is true:
  990. @li The response is sent.
  991. @li An error occurs.
  992. The algorithm, known as a <em>composed operation</em>, is implemented
  993. in terms of calls to the next layer's `read_some` and `write_some`
  994. functions.
  995. If a valid upgrade request is received, an HTTP response with a
  996. <a href="https://tools.ietf.org/html/rfc7230#section-3.1.2">status-code</a>
  997. of @ref beast::http::status::switching_protocols is sent to
  998. the peer, otherwise a non-successful error is associated with
  999. the operation.
  1000. @param req An object containing the HTTP Upgrade request.
  1001. Ownership is not transferred, the implementation will not
  1002. access this object from other threads.
  1003. @throws system_error Thrown on failure.
  1004. @see
  1005. @li <a href="https://tools.ietf.org/html/rfc6455#section-4.2">Websocket Opening Handshake Server Requirements (RFC6455)</a>
  1006. */
  1007. template<class Body, class Allocator>
  1008. void
  1009. accept(http::request<Body,
  1010. http::basic_fields<Allocator>> const& req);
  1011. /** Respond to a WebSocket HTTP Upgrade request
  1012. This function is used to perform the
  1013. <a href="https://en.wikipedia.org/wiki/WebSocket#Protocol_handshake">WebSocket handshake</a>,
  1014. required before messages can be sent and received. During the handshake,
  1015. the client sends the Websocket Upgrade HTTP request, and the server
  1016. replies with an HTTP response indicating the result of the handshake.
  1017. The call blocks until one of the following conditions is true:
  1018. @li The response is sent.
  1019. @li An error occurs.
  1020. The algorithm, known as a <em>composed operation</em>, is implemented
  1021. in terms of calls to the next layer's `read_some` and `write_some`
  1022. functions.
  1023. If a valid upgrade request is received, an HTTP response with a
  1024. <a href="https://tools.ietf.org/html/rfc7230#section-3.1.2">status-code</a>
  1025. of @ref beast::http::status::switching_protocols is sent to
  1026. the peer, otherwise a non-successful error is associated with
  1027. the operation.
  1028. @param req An object containing the HTTP Upgrade request.
  1029. Ownership is not transferred, the implementation will not
  1030. access this object from other threads.
  1031. @param ec Set to indicate what error occurred, if any.
  1032. @see
  1033. @li <a href="https://tools.ietf.org/html/rfc6455#section-4.2">Websocket Opening Handshake Server Requirements (RFC6455)</a>
  1034. */
  1035. template<class Body, class Allocator>
  1036. void
  1037. accept(http::request<Body,
  1038. http::basic_fields<Allocator>> const& req,
  1039. error_code& ec);
  1040. /** Perform the WebSocket handshake asynchronously in the server role.
  1041. This initiating function is used to asynchronously begin performing the
  1042. <a href="https://en.wikipedia.org/wiki/WebSocket#Protocol_handshake">WebSocket handshake</a>,
  1043. required before messages can be sent and received. During the handshake,
  1044. the client sends the Websocket Upgrade HTTP request, and the server
  1045. replies with an HTTP response indicating the result of the handshake.
  1046. This call always returns immediately. The asynchronous operation
  1047. will continue until one of the following conditions is true:
  1048. @li The request is received and the response is sent.
  1049. @li An error occurs.
  1050. The algorithm, known as a <em>composed asynchronous operation</em>,
  1051. is implemented in terms of calls to the next layer's `async_read_some`
  1052. and `async_write_some` functions. No other operation may be performed
  1053. on the stream until this operation completes.
  1054. If a valid upgrade request is received, an HTTP response with a
  1055. <a href="https://tools.ietf.org/html/rfc7230#section-3.1.2">status-code</a>
  1056. of @ref beast::http::status::switching_protocols is sent to
  1057. the peer, otherwise a non-successful error is associated with
  1058. the operation.
  1059. If the request size exceeds the capacity of the stream's
  1060. internal buffer, the error @ref error::buffer_overflow will be
  1061. indicated. To handle larger requests, an application should
  1062. read the HTTP request directly using @ref http::async_read and then
  1063. pass the request to the appropriate overload of @ref accept or
  1064. @ref async_accept
  1065. @param handler The completion handler to invoke when the operation
  1066. completes. The implementation takes ownership of the handler by
  1067. performing a decay-copy. The equivalent function signature of
  1068. the handler must be:
  1069. @code
  1070. void handler(
  1071. error_code const& ec // Result of operation
  1072. );
  1073. @endcode
  1074. If the handler has an associated immediate executor,
  1075. an immediate completion will be dispatched to it.
  1076. Otherwise, the handler will not be invoked from within
  1077. this function. Invocation of the handler will be performed
  1078. by dispatching to the immediate executor. If no
  1079. immediate executor is specified, this is equivalent
  1080. to using `net::post`.
  1081. @see
  1082. @li <a href="https://tools.ietf.org/html/rfc6455#section-4.2">Websocket Opening Handshake Server Requirements (RFC6455)</a>
  1083. */
  1084. template<
  1085. BHO_BEAST_ASYNC_TPARAM1 AcceptHandler =
  1086. net::default_completion_token_t<executor_type>
  1087. >
  1088. BHO_BEAST_ASYNC_RESULT1(AcceptHandler)
  1089. async_accept(
  1090. AcceptHandler&& handler =
  1091. net::default_completion_token_t<
  1092. executor_type>{});
  1093. /** Perform the WebSocket handshake asynchronously in the server role.
  1094. This initiating function is used to asynchronously begin performing the
  1095. <a href="https://en.wikipedia.org/wiki/WebSocket#Protocol_handshake">WebSocket handshake</a>,
  1096. required before messages can be sent and received. During the handshake,
  1097. the client sends the Websocket Upgrade HTTP request, and the server
  1098. replies with an HTTP response indicating the result of the handshake.
  1099. This call always returns immediately. The asynchronous operation
  1100. will continue until one of the following conditions is true:
  1101. @li The request is received and the response is sent.
  1102. @li An error occurs.
  1103. The algorithm, known as a <em>composed asynchronous operation</em>,
  1104. is implemented in terms of calls to the next layer's `async_read_some`
  1105. and `async_write_some` functions. No other operation may be performed
  1106. on the stream until this operation completes.
  1107. If a valid upgrade request is received, an HTTP response with a
  1108. <a href="https://tools.ietf.org/html/rfc7230#section-3.1.2">status-code</a>
  1109. of @ref beast::http::status::switching_protocols is sent to
  1110. the peer, otherwise a non-successful error is associated with
  1111. the operation.
  1112. If the request size exceeds the capacity of the stream's
  1113. internal buffer, the error @ref error::buffer_overflow will be
  1114. indicated. To handle larger requests, an application should
  1115. read the HTTP request directly using @ref http::async_read and then
  1116. pass the request to the appropriate overload of @ref accept or
  1117. @ref async_accept
  1118. @param buffers Caller provided data that has already been
  1119. received on the stream. This may be used for implementations
  1120. allowing multiple protocols on the same stream. The
  1121. buffered data will first be applied to the handshake, and
  1122. then to received WebSocket frames. The implementation will
  1123. copy the caller provided data before the function returns.
  1124. @param handler The completion handler to invoke when the operation
  1125. completes. The implementation takes ownership of the handler by
  1126. performing a decay-copy. The equivalent function signature of
  1127. the handler must be:
  1128. @code
  1129. void handler(
  1130. error_code const& ec // Result of operation
  1131. );
  1132. @endcode
  1133. If the handler has an associated immediate executor,
  1134. an immediate completion will be dispatched to it.
  1135. Otherwise, the handler will not be invoked from within
  1136. this function. Invocation of the handler will be performed
  1137. by dispatching to the immediate executor. If no
  1138. immediate executor is specified, this is equivalent
  1139. to using `net::post`.
  1140. @see
  1141. @li <a href="https://tools.ietf.org/html/rfc6455#section-4.2">Websocket Opening Handshake Server Requirements (RFC6455)</a>
  1142. */
  1143. template<
  1144. class ConstBufferSequence,
  1145. BHO_BEAST_ASYNC_TPARAM1 AcceptHandler =
  1146. net::default_completion_token_t<executor_type>
  1147. >
  1148. BHO_BEAST_ASYNC_RESULT1(AcceptHandler)
  1149. async_accept(
  1150. ConstBufferSequence const& buffers,
  1151. AcceptHandler&& handler =
  1152. net::default_completion_token_t<
  1153. executor_type>{}
  1154. #ifndef BHO_BEAST_DOXYGEN
  1155. , typename std::enable_if<
  1156. ! http::detail::is_header<
  1157. ConstBufferSequence>::value>::type* = 0
  1158. #endif
  1159. );
  1160. /** Perform the WebSocket handshake asynchronously in the server role.
  1161. This initiating function is used to asynchronously begin performing the
  1162. <a href="https://en.wikipedia.org/wiki/WebSocket#Protocol_handshake">WebSocket handshake</a>,
  1163. required before messages can be sent and received. During the handshake,
  1164. the client sends the Websocket Upgrade HTTP request, and the server
  1165. replies with an HTTP response indicating the result of the handshake.
  1166. This call always returns immediately. The asynchronous operation
  1167. will continue until one of the following conditions is true:
  1168. @li The request is received and the response is sent.
  1169. @li An error occurs.
  1170. The algorithm, known as a <em>composed asynchronous operation</em>,
  1171. is implemented in terms of calls to the next layer's `async_read_some`
  1172. and `async_write_some` functions. No other operation may be performed
  1173. on the stream until this operation completes.
  1174. If a valid upgrade request is received, an HTTP response with a
  1175. <a href="https://tools.ietf.org/html/rfc7230#section-3.1.2">status-code</a>
  1176. of @ref beast::http::status::switching_protocols is sent to
  1177. the peer, otherwise a non-successful error is associated with
  1178. the operation.
  1179. @param req An object containing the HTTP Upgrade request.
  1180. Ownership is not transferred, the implementation will not access
  1181. this object from other threads.
  1182. @param handler The completion handler to invoke when the operation
  1183. completes. The implementation takes ownership of the handler by
  1184. performing a decay-copy. The equivalent function signature of
  1185. the handler must be:
  1186. @code
  1187. void handler(
  1188. error_code const& ec // Result of operation
  1189. );
  1190. @endcode
  1191. If the handler has an associated immediate executor,
  1192. an immediate completion will be dispatched to it.
  1193. Otherwise, the handler will not be invoked from within
  1194. this function. Invocation of the handler will be performed
  1195. by dispatching to the immediate executor. If no
  1196. immediate executor is specified, this is equivalent
  1197. to using `net::post`.
  1198. @see
  1199. @li <a href="https://tools.ietf.org/html/rfc6455#section-4.2">Websocket Opening Handshake Server Requirements (RFC6455)</a>
  1200. */
  1201. template<
  1202. class Body, class Allocator,
  1203. BHO_BEAST_ASYNC_TPARAM1 AcceptHandler =
  1204. net::default_completion_token_t<executor_type>
  1205. >
  1206. BHO_BEAST_ASYNC_RESULT1(AcceptHandler)
  1207. async_accept(
  1208. http::request<Body,
  1209. http::basic_fields<Allocator>> const& req,
  1210. AcceptHandler&& handler =
  1211. net::default_completion_token_t<
  1212. executor_type>{});
  1213. //--------------------------------------------------------------------------
  1214. //
  1215. // Close Frames
  1216. //
  1217. //--------------------------------------------------------------------------
  1218. /** Send a websocket close control frame.
  1219. This function is used to send a
  1220. <a href="https://tools.ietf.org/html/rfc6455#section-5.5.1">close frame</a>,
  1221. which begins the websocket closing handshake. The session ends when
  1222. both ends of the connection have sent and received a close frame.
  1223. The call blocks until one of the following conditions is true:
  1224. @li The close frame is written.
  1225. @li An error occurs.
  1226. The algorithm, known as a <em>composed operation</em>, is implemented
  1227. in terms of calls to the next layer's `write_some` function.
  1228. After beginning the closing handshake, the program should not write
  1229. further message data, pings, or pongs. Instead, the program should
  1230. continue reading message data until an error occurs. A read returning
  1231. @ref error::closed indicates a successful connection closure.
  1232. @param cr The reason for the close.
  1233. If the close reason specifies a close code other than
  1234. @ref beast::websocket::close_code::none, the close frame is
  1235. sent with the close code and optional reason string. Otherwise,
  1236. the close frame is sent with no payload.
  1237. @throws system_error Thrown on failure.
  1238. @see
  1239. @li <a href="https://tools.ietf.org/html/rfc6455#section-7.1.2">Websocket Closing Handshake (RFC6455)</a>
  1240. */
  1241. void
  1242. close(close_reason const& cr);
  1243. /** Send a websocket close control frame.
  1244. This function is used to send a
  1245. <a href="https://tools.ietf.org/html/rfc6455#section-5.5.1">close frame</a>,
  1246. which begins the websocket closing handshake. The session ends when
  1247. both ends of the connection have sent and received a close frame.
  1248. The call blocks until one of the following conditions is true:
  1249. @li The close frame is written.
  1250. @li An error occurs.
  1251. The algorithm, known as a <em>composed operation</em>, is implemented
  1252. in terms of calls to the next layer's `write_some` function.
  1253. After beginning the closing handshake, the program should not write
  1254. further message data, pings, or pongs. Instead, the program should
  1255. continue reading message data until an error occurs. A read returning
  1256. @ref error::closed indicates a successful connection closure.
  1257. @param cr The reason for the close.
  1258. If the close reason specifies a close code other than
  1259. @ref beast::websocket::close_code::none, the close frame is
  1260. sent with the close code and optional reason string. Otherwise,
  1261. the close frame is sent with no payload.
  1262. @param ec Set to indicate what error occurred, if any.
  1263. @see
  1264. @li <a href="https://tools.ietf.org/html/rfc6455#section-7.1.2">Websocket Closing Handshake (RFC6455)</a>
  1265. */
  1266. void
  1267. close(close_reason const& cr, error_code& ec);
  1268. /** Send a websocket close control frame asynchronously.
  1269. This function is used to asynchronously send a
  1270. <a href="https://tools.ietf.org/html/rfc6455#section-5.5.1">close frame</a>,
  1271. which begins the websocket closing handshake. The session ends when
  1272. both ends of the connection have sent and received a close frame.
  1273. This call always returns immediately. The asynchronous operation
  1274. will continue until one of the following conditions is true:
  1275. @li The close frame finishes sending.
  1276. @li An error occurs.
  1277. The algorithm, known as a <em>composed asynchronous operation</em>,
  1278. is implemented in terms of calls to the next layer's `async_write_some`
  1279. function. No other operations except for message reading operations
  1280. should be initiated on the stream after a close operation is started.
  1281. After beginning the closing handshake, the program should not write
  1282. further message data, pings, or pongs. Instead, the program should
  1283. continue reading message data until an error occurs. A read returning
  1284. @ref error::closed indicates a successful connection closure.
  1285. @param cr The reason for the close.
  1286. If the close reason specifies a close code other than
  1287. @ref beast::websocket::close_code::none, the close frame is
  1288. sent with the close code and optional reason string. Otherwise,
  1289. the close frame is sent with no payload.
  1290. @param handler The completion handler to invoke when the operation
  1291. completes. The implementation takes ownership of the handler by
  1292. performing a decay-copy. The equivalent function signature of
  1293. the handler must be:
  1294. @code
  1295. void handler(
  1296. error_code const& ec // Result of operation
  1297. );
  1298. @endcode
  1299. If the handler has an associated immediate executor,
  1300. an immediate completion will be dispatched to it.
  1301. Otherwise, the handler will not be invoked from within
  1302. this function. Invocation of the handler will be performed
  1303. by dispatching to the immediate executor. If no
  1304. immediate executor is specified, this is equivalent
  1305. to using `net::post`.
  1306. @par Per-Operation Cancellation
  1307. This asynchronous operation supports cancellation for the following
  1308. net::cancellation_type values:
  1309. @li @c net::cancellation_type::terminal
  1310. @li @c net::cancellation_type::total
  1311. `total` cancellation succeeds if the operation is suspended due to ongoing
  1312. control operations such as a ping/pong.
  1313. `terminal` cancellation succeeds when supported by the underlying stream.
  1314. @note `terminal` cancellation will may close the underlying socket.
  1315. @see
  1316. @li <a href="https://tools.ietf.org/html/rfc6455#section-7.1.2">Websocket Closing Handshake (RFC6455)</a>
  1317. */
  1318. template<
  1319. BHO_BEAST_ASYNC_TPARAM1 CloseHandler =
  1320. net::default_completion_token_t<executor_type>
  1321. >
  1322. BHO_BEAST_ASYNC_RESULT1(CloseHandler)
  1323. async_close(
  1324. close_reason const& cr,
  1325. CloseHandler&& handler =
  1326. net::default_completion_token_t<
  1327. executor_type>{});
  1328. //--------------------------------------------------------------------------
  1329. //
  1330. // Ping/Pong Frames
  1331. //
  1332. //--------------------------------------------------------------------------
  1333. /** Send a websocket ping control frame.
  1334. This function is used to send a
  1335. <a href="https://tools.ietf.org/html/rfc6455#section-5.5.2">ping frame</a>,
  1336. which usually elicits an automatic pong control frame response from
  1337. the peer.
  1338. The call blocks until one of the following conditions is true:
  1339. @li The ping frame is written.
  1340. @li An error occurs.
  1341. The algorithm, known as a <em>composed operation</em>, is implemented
  1342. in terms of calls to the next layer's `write_some` function.
  1343. @param payload The payload of the ping message, which may be empty.
  1344. @throws system_error Thrown on failure.
  1345. */
  1346. void
  1347. ping(ping_data const& payload);
  1348. /** Send a websocket ping control frame.
  1349. This function is used to send a
  1350. <a href="https://tools.ietf.org/html/rfc6455#section-5.5.2">ping frame</a>,
  1351. which usually elicits an automatic pong control frame response from
  1352. the peer.
  1353. The call blocks until one of the following conditions is true:
  1354. @li The ping frame is written.
  1355. @li An error occurs.
  1356. The algorithm, known as a <em>composed operation</em>, is implemented
  1357. in terms of calls to the next layer's `write_some` function.
  1358. @param payload The payload of the ping message, which may be empty.
  1359. @param ec Set to indicate what error occurred, if any.
  1360. */
  1361. void
  1362. ping(ping_data const& payload, error_code& ec);
  1363. /** Send a websocket ping control frame asynchronously.
  1364. This function is used to asynchronously send a
  1365. <a href="https://tools.ietf.org/html/rfc6455#section-5.5.2">ping frame</a>,
  1366. which usually elicits an automatic pong control frame response from
  1367. the peer.
  1368. @li The ping frame is written.
  1369. @li An error occurs.
  1370. The algorithm, known as a <em>composed asynchronous operation</em>,
  1371. is implemented in terms of calls to the next layer's `async_write_some`
  1372. function. The program must ensure that no other calls to @ref ping,
  1373. @ref pong, @ref async_ping, or @ref async_pong are performed until
  1374. this operation completes.
  1375. If a close frame is sent or received before the ping frame is
  1376. sent, the error received by this completion handler will be
  1377. `net::error::operation_aborted`.
  1378. @param payload The payload of the ping message, which may be empty.
  1379. The implementation will not access the contents of this object after
  1380. the initiating function returns.
  1381. @param handler The completion handler to invoke when the operation
  1382. completes. The implementation takes ownership of the handler by
  1383. performing a decay-copy. The equivalent function signature of
  1384. the handler must be:
  1385. @code
  1386. void handler(
  1387. error_code const& ec // Result of operation
  1388. );
  1389. @endcode
  1390. If the handler has an associated immediate executor,
  1391. an immediate completion will be dispatched to it.
  1392. Otherwise, the handler will not be invoked from within
  1393. this function. Invocation of the handler will be performed
  1394. by dispatching to the immediate executor. If no
  1395. immediate executor is specified, this is equivalent
  1396. to using `net::post`.
  1397. @par Per-Operation Cancellation
  1398. This asynchronous operation supports cancellation for the following
  1399. net::cancellation_type values:
  1400. @li @c net::cancellation_type::terminal
  1401. @li @c net::cancellation_type::total
  1402. `total` cancellation succeeds if the operation is suspended due to ongoing
  1403. control operations such as a ping/pong.
  1404. `terminal` cancellation succeeds when supported by the underlying stream.
  1405. `terminal` cancellation leaves the stream in an undefined state,
  1406. so that only closing it is guaranteed to succeed.
  1407. */
  1408. template<
  1409. BHO_BEAST_ASYNC_TPARAM1 PingHandler =
  1410. net::default_completion_token_t<executor_type>
  1411. >
  1412. BHO_BEAST_ASYNC_RESULT1(PingHandler)
  1413. async_ping(
  1414. ping_data const& payload,
  1415. PingHandler&& handler =
  1416. net::default_completion_token_t<
  1417. executor_type>{});
  1418. /** Send a websocket pong control frame.
  1419. This function is used to send a
  1420. <a href="https://tools.ietf.org/html/rfc6455#section-5.5.3">pong frame</a>,
  1421. which is usually sent automatically in response to a ping frame
  1422. from the remote peer.
  1423. The call blocks until one of the following conditions is true:
  1424. @li The pong frame is written.
  1425. @li An error occurs.
  1426. The algorithm, known as a <em>composed operation</em>, is implemented
  1427. in terms of calls to the next layer's `write_some` function.
  1428. WebSocket allows pong frames to be sent at any time, without first
  1429. receiving a ping. An unsolicited pong sent in this fashion may
  1430. indicate to the remote peer that the connection is still active.
  1431. @param payload The payload of the pong message, which may be empty.
  1432. @throws system_error Thrown on failure.
  1433. */
  1434. void
  1435. pong(ping_data const& payload);
  1436. /** Send a websocket pong control frame.
  1437. This function is used to send a
  1438. <a href="https://tools.ietf.org/html/rfc6455#section-5.5.3">pong frame</a>,
  1439. which is usually sent automatically in response to a ping frame
  1440. from the remote peer.
  1441. The call blocks until one of the following conditions is true:
  1442. @li The pong frame is written.
  1443. @li An error occurs.
  1444. The algorithm, known as a <em>composed operation</em>, is implemented
  1445. in terms of calls to the next layer's `write_some` function.
  1446. WebSocket allows pong frames to be sent at any time, without first
  1447. receiving a ping. An unsolicited pong sent in this fashion may
  1448. indicate to the remote peer that the connection is still active.
  1449. @param payload The payload of the pong message, which may be empty.
  1450. @param ec Set to indicate what error occurred, if any.
  1451. */
  1452. void
  1453. pong(ping_data const& payload, error_code& ec);
  1454. /** Send a websocket pong control frame asynchronously.
  1455. This function is used to asynchronously send a
  1456. <a href="https://tools.ietf.org/html/rfc6455#section-5.5.3">pong frame</a>,
  1457. which is usually sent automatically in response to a ping frame
  1458. from the remote peer.
  1459. @li The pong frame is written.
  1460. @li An error occurs.
  1461. The algorithm, known as a <em>composed asynchronous operation</em>,
  1462. is implemented in terms of calls to the next layer's `async_write_some`
  1463. function. The program must ensure that no other calls to @ref ping,
  1464. @ref pong, @ref async_ping, or @ref async_pong are performed until
  1465. this operation completes.
  1466. If a close frame is sent or received before the pong frame is
  1467. sent, the error received by this completion handler will be
  1468. `net::error::operation_aborted`.
  1469. WebSocket allows pong frames to be sent at any time, without first
  1470. receiving a ping. An unsolicited pong sent in this fashion may
  1471. indicate to the remote peer that the connection is still active.
  1472. @param payload The payload of the pong message, which may be empty.
  1473. The implementation will not access the contents of this object after
  1474. the initiating function returns.
  1475. @param handler The completion handler to invoke when the operation
  1476. completes. The implementation takes ownership of the handler by
  1477. performing a decay-copy. The equivalent function signature of
  1478. the handler must be:
  1479. @code
  1480. void handler(
  1481. error_code const& ec // Result of operation
  1482. );
  1483. @endcode
  1484. If the handler has an associated immediate executor,
  1485. an immediate completion will be dispatched to it.
  1486. Otherwise, the handler will not be invoked from within
  1487. this function. Invocation of the handler will be performed
  1488. by dispatching to the immediate executor. If no
  1489. immediate executor is specified, this is equivalent
  1490. to using `net::post`.
  1491. @par Per-Operation Cancellation
  1492. This asynchronous operation supports cancellation for the following
  1493. net::cancellation_type values:
  1494. @li @c net::cancellation_type::terminal
  1495. @li @c net::cancellation_type::total
  1496. `total` cancellation succeeds if the operation is suspended due to ongoing
  1497. control operations such as a ping/pong.
  1498. `terminal` cancellation succeeds when supported by the underlying stream.
  1499. `terminal` cancellation leaves the stream in an undefined state,
  1500. so that only closing it is guaranteed to succeed.
  1501. */
  1502. template<
  1503. BHO_BEAST_ASYNC_TPARAM1 PongHandler =
  1504. net::default_completion_token_t<executor_type>
  1505. >
  1506. BHO_BEAST_ASYNC_RESULT1(PongHandler)
  1507. async_pong(
  1508. ping_data const& payload,
  1509. PongHandler&& handler =
  1510. net::default_completion_token_t<
  1511. executor_type>{});
  1512. //--------------------------------------------------------------------------
  1513. //
  1514. // Reading
  1515. //
  1516. //--------------------------------------------------------------------------
  1517. /** Read a complete message.
  1518. This function is used to read a complete message.
  1519. The call blocks until one of the following is true:
  1520. @li A complete message is received.
  1521. @li A close frame is received. In this case the error indicated by
  1522. the function will be @ref error::closed.
  1523. @li An error occurs.
  1524. The algorithm, known as a <em>composed operation</em>, is implemented
  1525. in terms of calls to the next layer's `read_some` and `write_some`
  1526. functions.
  1527. Received message data is appended to the buffer.
  1528. The functions @ref got_binary and @ref got_text may be used
  1529. to query the stream and determine the type of the last received message.
  1530. Until the call returns, the implementation will read incoming control
  1531. frames and handle them automatically as follows:
  1532. @li The @ref control_callback will be invoked for each control frame.
  1533. @li For each received ping frame, a pong frame will be
  1534. automatically sent.
  1535. @li If a close frame is received, the WebSocket closing handshake is
  1536. performed. In this case, when the function returns, the error
  1537. @ref error::closed will be indicated.
  1538. @return The number of message payload bytes appended to the buffer.
  1539. @param buffer A dynamic buffer to append message data to.
  1540. @throws system_error Thrown on failure.
  1541. */
  1542. template<class DynamicBuffer>
  1543. std::size_t
  1544. read(DynamicBuffer& buffer);
  1545. /** Read a complete message.
  1546. This function is used to read a complete message.
  1547. The call blocks until one of the following is true:
  1548. @li A complete message is received.
  1549. @li A close frame is received. In this case the error indicated by
  1550. the function will be @ref error::closed.
  1551. @li An error occurs.
  1552. The algorithm, known as a <em>composed operation</em>, is implemented
  1553. in terms of calls to the next layer's `read_some` and `write_some`
  1554. functions.
  1555. Received message data is appended to the buffer.
  1556. The functions @ref got_binary and @ref got_text may be used
  1557. to query the stream and determine the type of the last received message.
  1558. Until the call returns, the implementation will read incoming control
  1559. frames and handle them automatically as follows:
  1560. @li The @ref control_callback will be invoked for each control frame.
  1561. @li For each received ping frame, a pong frame will be
  1562. automatically sent.
  1563. @li If a close frame is received, the WebSocket closing handshake is
  1564. performed. In this case, when the function returns, the error
  1565. @ref error::closed will be indicated.
  1566. @return The number of message payload bytes appended to the buffer.
  1567. @param buffer A dynamic buffer to append message data to.
  1568. @param ec Set to indicate what error occurred, if any.
  1569. */
  1570. template<class DynamicBuffer>
  1571. std::size_t
  1572. read(DynamicBuffer& buffer, error_code& ec);
  1573. /** Read a complete message asynchronously.
  1574. This function is used to asynchronously read a complete message.
  1575. This call always returns immediately. The asynchronous operation
  1576. will continue until one of the following conditions is true:
  1577. @li A complete message is received.
  1578. @li A close frame is received. In this case the error indicated by
  1579. the function will be @ref error::closed.
  1580. @li An error occurs.
  1581. The algorithm, known as a <em>composed asynchronous operation</em>,
  1582. is implemented in terms of calls to the next layer's `async_read_some`
  1583. and `async_write_some` functions. The program must ensure that no other
  1584. calls to @ref read, @ref read_some, @ref async_read, or @ref async_read_some
  1585. are performed until this operation completes.
  1586. Received message data is appended to the buffer.
  1587. The functions @ref got_binary and @ref got_text may be used
  1588. to query the stream and determine the type of the last received message.
  1589. Until the operation completes, the implementation will read incoming
  1590. control frames and handle them automatically as follows:
  1591. @li The @ref control_callback will be invoked for each control frame.
  1592. @li For each received ping frame, a pong frame will be
  1593. automatically sent.
  1594. @li If a close frame is received, the WebSocket close procedure is
  1595. performed. In this case, when the function returns, the error
  1596. @ref error::closed will be indicated.
  1597. Pong frames and close frames sent by the implementation while the
  1598. read operation is outstanding do not prevent the application from
  1599. also writing message data, sending pings, sending pongs, or sending
  1600. close frames.
  1601. @param buffer A dynamic buffer to append message data to.
  1602. @param handler The completion handler to invoke when the operation
  1603. completes. The implementation takes ownership of the handler by
  1604. performing a decay-copy. The equivalent function signature of
  1605. the handler must be:
  1606. @code
  1607. void handler(
  1608. error_code const& ec, // Result of operation
  1609. std::size_t bytes_written // Number of bytes appended to buffer
  1610. );
  1611. @endcode
  1612. If the handler has an associated immediate executor,
  1613. an immediate completion will be dispatched to it.
  1614. Otherwise, the handler will not be invoked from within
  1615. this function. Invocation of the handler will be performed
  1616. by dispatching to the immediate executor. If no
  1617. immediate executor is specified, this is equivalent
  1618. to using `net::post`.
  1619. @par Per-Operation Cancellation
  1620. This asynchronous operation supports cancellation for the following
  1621. net::cancellation_type values:
  1622. @li @c net::cancellation_type::terminal
  1623. @li @c net::cancellation_type::total
  1624. `total` cancellation succeeds if the operation is suspended due to ongoing
  1625. control operations such as a ping/pong.
  1626. `terminal` cancellation succeeds when supported by the underlying stream.
  1627. `terminal` cancellation leaves the stream in an undefined state,
  1628. so that only closing it is guaranteed to succeed.
  1629. */
  1630. template<
  1631. class DynamicBuffer,
  1632. BHO_BEAST_ASYNC_TPARAM2 ReadHandler =
  1633. net::default_completion_token_t<
  1634. executor_type>>
  1635. BHO_BEAST_ASYNC_RESULT2(ReadHandler)
  1636. async_read(
  1637. DynamicBuffer& buffer,
  1638. ReadHandler&& handler =
  1639. net::default_completion_token_t<
  1640. executor_type>{});
  1641. //--------------------------------------------------------------------------
  1642. /** Read some message data.
  1643. This function is used to read some message data.
  1644. The call blocks until one of the following is true:
  1645. @li Some message data is received.
  1646. @li A close frame is received. In this case the error indicated by
  1647. the function will be @ref error::closed.
  1648. @li An error occurs.
  1649. The algorithm, known as a <em>composed operation</em>, is implemented
  1650. in terms of calls to the next layer's `read_some` and `write_some`
  1651. functions.
  1652. Received message data is appended to the buffer.
  1653. The functions @ref got_binary and @ref got_text may be used
  1654. to query the stream and determine the type of the last received message.
  1655. The function @ref is_message_done may be called to determine if the
  1656. message received by the last read operation is complete.
  1657. Until the call returns, the implementation will read incoming control
  1658. frames and handle them automatically as follows:
  1659. @li The @ref control_callback will be invoked for each control frame.
  1660. @li For each received ping frame, a pong frame will be
  1661. automatically sent.
  1662. @li If a close frame is received, the WebSocket closing handshake is
  1663. performed. In this case, when the function returns, the error
  1664. @ref error::closed will be indicated.
  1665. @return The number of message payload bytes appended to the buffer.
  1666. @param buffer A dynamic buffer to append message data to.
  1667. @param limit An upper limit on the number of bytes this function
  1668. will append into the buffer. If this value is zero, then a reasonable
  1669. size will be chosen automatically.
  1670. @throws system_error Thrown on failure.
  1671. */
  1672. template<class DynamicBuffer>
  1673. std::size_t
  1674. read_some(
  1675. DynamicBuffer& buffer,
  1676. std::size_t limit);
  1677. /** Read some message data.
  1678. This function is used to read some message data.
  1679. The call blocks until one of the following is true:
  1680. @li Some message data is received.
  1681. @li A close frame is received. In this case the error indicated by
  1682. the function will be @ref error::closed.
  1683. @li An error occurs.
  1684. The algorithm, known as a <em>composed operation</em>, is implemented
  1685. in terms of calls to the next layer's `read_some` and `write_some`
  1686. functions.
  1687. Received message data is appended to the buffer.
  1688. The functions @ref got_binary and @ref got_text may be used
  1689. to query the stream and determine the type of the last received message.
  1690. The function @ref is_message_done may be called to determine if the
  1691. message received by the last read operation is complete.
  1692. Until the call returns, the implementation will read incoming control
  1693. frames and handle them automatically as follows:
  1694. @li The @ref control_callback will be invoked for each control frame.
  1695. @li For each received ping frame, a pong frame will be
  1696. automatically sent.
  1697. @li If a close frame is received, the WebSocket closing handshake is
  1698. performed. In this case, when the function returns, the error
  1699. @ref error::closed will be indicated.
  1700. @return The number of message payload bytes appended to the buffer.
  1701. @param buffer A dynamic buffer to append message data to.
  1702. @param limit An upper limit on the number of bytes this function
  1703. will append into the buffer. If this value is zero, then a reasonable
  1704. size will be chosen automatically.
  1705. @param ec Set to indicate what error occurred, if any.
  1706. */
  1707. template<class DynamicBuffer>
  1708. std::size_t
  1709. read_some(
  1710. DynamicBuffer& buffer,
  1711. std::size_t limit,
  1712. error_code& ec);
  1713. /** Read some message data asynchronously.
  1714. This function is used to asynchronously read some message data.
  1715. This call always returns immediately. The asynchronous operation
  1716. will continue until one of the following conditions is true:
  1717. @li Some message data is received.
  1718. @li A close frame is received. In this case the error indicated by
  1719. the function will be @ref error::closed.
  1720. @li An error occurs.
  1721. The algorithm, known as a <em>composed asynchronous operation</em>,
  1722. is implemented in terms of calls to the next layer's `async_read_some`
  1723. and `async_write_some` functions. The program must ensure that no other
  1724. calls to @ref read, @ref read_some, @ref async_read, or @ref async_read_some
  1725. are performed until this operation completes.
  1726. Received message data is appended to the buffer.
  1727. The functions @ref got_binary and @ref got_text may be used
  1728. to query the stream and determine the type of the last received message.
  1729. Until the operation completes, the implementation will read incoming
  1730. control frames and handle them automatically as follows:
  1731. @li The @ref control_callback will be invoked for each control frame.
  1732. @li For each received ping frame, a pong frame will be
  1733. automatically sent.
  1734. @li If a close frame is received, the WebSocket close procedure is
  1735. performed. In this case, when the function returns, the error
  1736. @ref error::closed will be indicated.
  1737. Pong frames and close frames sent by the implementation while the
  1738. read operation is outstanding do not prevent the application from
  1739. also writing message data, sending pings, sending pongs, or sending
  1740. close frames.
  1741. @param buffer A dynamic buffer to append message data to.
  1742. @param limit An upper limit on the number of bytes this function
  1743. will append into the buffer. If this value is zero, then a reasonable
  1744. size will be chosen automatically.
  1745. @param handler The completion handler to invoke when the operation
  1746. completes. The implementation takes ownership of the handler by
  1747. performing a decay-copy. The equivalent function signature of
  1748. the handler must be:
  1749. @code
  1750. void handler(
  1751. error_code const& ec, // Result of operation
  1752. std::size_t bytes_written // Number of bytes appended to buffer
  1753. );
  1754. @endcode
  1755. If the handler has an associated immediate executor,
  1756. an immediate completion will be dispatched to it.
  1757. Otherwise, the handler will not be invoked from within
  1758. this function. Invocation of the handler will be performed
  1759. by dispatching to the immediate executor. If no
  1760. immediate executor is specified, this is equivalent
  1761. to using `net::post`.
  1762. @par Per-Operation Cancellation
  1763. This asynchronous operation supports cancellation for the following
  1764. net::cancellation_type values:
  1765. @li @c net::cancellation_type::terminal
  1766. @li @c net::cancellation_type::total
  1767. `total` cancellation succeeds if the operation is suspended due to ongoing
  1768. control operations such as a ping/pong.
  1769. `terminal` cancellation succeeds when supported by the underlying stream.
  1770. `terminal` cancellation leaves the stream in an undefined state,
  1771. so that only closing it is guaranteed to succeed.
  1772. */
  1773. template<
  1774. class DynamicBuffer,
  1775. BHO_BEAST_ASYNC_TPARAM2 ReadHandler =
  1776. net::default_completion_token_t<
  1777. executor_type>>
  1778. BHO_BEAST_ASYNC_RESULT2(ReadHandler)
  1779. async_read_some(
  1780. DynamicBuffer& buffer,
  1781. std::size_t limit,
  1782. ReadHandler&& handler =
  1783. net::default_completion_token_t<
  1784. executor_type>{});
  1785. //--------------------------------------------------------------------------
  1786. /** Read some message data.
  1787. This function is used to read some message data.
  1788. The call blocks until one of the following is true:
  1789. @li Some message data is received.
  1790. @li A close frame is received. In this case the error indicated by
  1791. the function will be @ref error::closed.
  1792. @li An error occurs.
  1793. The algorithm, known as a <em>composed operation</em>, is implemented
  1794. in terms of calls to the next layer's `read_some` and `write_some`
  1795. functions.
  1796. The functions @ref got_binary and @ref got_text may be used
  1797. to query the stream and determine the type of the last received message.
  1798. The function @ref is_message_done may be called to determine if the
  1799. message received by the last read operation is complete.
  1800. Until the call returns, the implementation will read incoming control
  1801. frames and handle them automatically as follows:
  1802. @li The @ref control_callback will be invoked for each control frame.
  1803. @li For each received ping frame, a pong frame will be
  1804. automatically sent.
  1805. @li If a close frame is received, the WebSocket closing handshake is
  1806. performed. In this case, when the function returns, the error
  1807. @ref error::closed will be indicated.
  1808. @return The number of message payload bytes appended to the buffer.
  1809. @param buffers A buffer sequence to write message data into.
  1810. The previous contents of the buffers will be overwritten, starting
  1811. from the beginning.
  1812. @throws system_error Thrown on failure.
  1813. */
  1814. template<class MutableBufferSequence>
  1815. std::size_t
  1816. read_some(
  1817. MutableBufferSequence const& buffers);
  1818. /** Read some message data.
  1819. This function is used to read some message data.
  1820. The call blocks until one of the following is true:
  1821. @li Some message data is received.
  1822. @li A close frame is received. In this case the error indicated by
  1823. the function will be @ref error::closed.
  1824. @li An error occurs.
  1825. The algorithm, known as a <em>composed operation</em>, is implemented
  1826. in terms of calls to the next layer's `read_some` and `write_some`
  1827. functions.
  1828. The functions @ref got_binary and @ref got_text may be used
  1829. to query the stream and determine the type of the last received message.
  1830. The function @ref is_message_done may be called to determine if the
  1831. message received by the last read operation is complete.
  1832. Until the call returns, the implementation will read incoming control
  1833. frames and handle them automatically as follows:
  1834. @li The @ref control_callback will be invoked for each control frame.
  1835. @li For each received ping frame, a pong frame will be
  1836. automatically sent.
  1837. @li If a close frame is received, the WebSocket closing handshake is
  1838. performed. In this case, when the function returns, the error
  1839. @ref error::closed will be indicated.
  1840. @return The number of message payload bytes appended to the buffer.
  1841. @param buffers A buffer sequence to write message data into.
  1842. The previous contents of the buffers will be overwritten, starting
  1843. from the beginning.
  1844. @param ec Set to indicate what error occurred, if any.
  1845. @par Per-Operation Cancellation
  1846. This asynchronous operation supports cancellation for the following
  1847. net::cancellation_type values:
  1848. @li @c net::cancellation_type::terminal
  1849. @li @c net::cancellation_type::total
  1850. `total` cancellation succeeds if the operation is suspended due to ongoing
  1851. control operations such as a ping/pong.
  1852. `terminal` cancellation succeeds when supported by the underlying stream.
  1853. `terminal` cancellation leaves the stream in an undefined state,
  1854. so that only closing it is guaranteed to succeed.
  1855. */
  1856. template<class MutableBufferSequence>
  1857. std::size_t
  1858. read_some(
  1859. MutableBufferSequence const& buffers,
  1860. error_code& ec);
  1861. /** Read some message data asynchronously.
  1862. This function is used to asynchronously read some message data.
  1863. This call always returns immediately. The asynchronous operation
  1864. will continue until one of the following conditions is true:
  1865. @li Some message data is received.
  1866. @li A close frame is received. In this case the error indicated by
  1867. the function will be @ref error::closed.
  1868. @li An error occurs.
  1869. The algorithm, known as a <em>composed asynchronous operation</em>,
  1870. is implemented in terms of calls to the next layer's `async_read_some`
  1871. and `async_write_some` functions. The program must ensure that no other
  1872. calls to @ref read, @ref read_some, @ref async_read, or @ref async_read_some
  1873. are performed until this operation completes.
  1874. Received message data is appended to the buffer.
  1875. The functions @ref got_binary and @ref got_text may be used
  1876. to query the stream and determine the type of the last received message.
  1877. Until the operation completes, the implementation will read incoming
  1878. control frames and handle them automatically as follows:
  1879. @li The @ref control_callback will be invoked for each control frame.
  1880. @li For each received ping frame, a pong frame will be
  1881. automatically sent.
  1882. @li If a close frame is received, the WebSocket close procedure is
  1883. performed. In this case, when the function returns, the error
  1884. @ref error::closed will be indicated.
  1885. Pong frames and close frames sent by the implementation while the
  1886. read operation is outstanding do not prevent the application from
  1887. also writing message data, sending pings, sending pongs, or sending
  1888. close frames.
  1889. @param buffers A buffer sequence to write message data into.
  1890. The previous contents of the buffers will be overwritten, starting
  1891. from the beginning.
  1892. The implementation will make copies of this object as needed, but
  1893. but ownership of the underlying memory is not transferred. The
  1894. caller is responsible for ensuring that the memory locations
  1895. pointed to by the buffer sequence remain valid until the
  1896. completion handler is called.
  1897. @param handler The completion handler to invoke when the operation
  1898. completes. The implementation takes ownership of the handler by
  1899. performing a decay-copy. The equivalent function signature of
  1900. the handler must be:
  1901. @code
  1902. void handler(
  1903. error_code const& ec, // Result of operation
  1904. std::size_t bytes_written // Number of bytes written to the buffers
  1905. );
  1906. @endcode
  1907. If the handler has an associated immediate executor,
  1908. an immediate completion will be dispatched to it.
  1909. Otherwise, the handler will not be invoked from within
  1910. this function. Invocation of the handler will be performed
  1911. by dispatching to the immediate executor. If no
  1912. immediate executor is specified, this is equivalent
  1913. to using `net::post`.
  1914. @par Per-Operation Cancellation
  1915. This asynchronous operation supports cancellation for the following
  1916. net::cancellation_type values:
  1917. @li @c net::cancellation_type::terminal
  1918. @li @c net::cancellation_type::total
  1919. `total` cancellation succeeds if the operation is suspended due to ongoing
  1920. control operations such as a ping/pong.
  1921. `terminal` cancellation succeeds when supported by the underlying stream.
  1922. `terminal` cancellation leaves the stream in an undefined state,
  1923. so that only closing it is guaranteed to succeed.
  1924. */
  1925. template<
  1926. class MutableBufferSequence,
  1927. BHO_BEAST_ASYNC_TPARAM2 ReadHandler =
  1928. net::default_completion_token_t<
  1929. executor_type>>
  1930. BHO_BEAST_ASYNC_RESULT2(ReadHandler)
  1931. async_read_some(
  1932. MutableBufferSequence const& buffers,
  1933. ReadHandler&& handler =
  1934. net::default_completion_token_t<
  1935. executor_type>{});
  1936. //--------------------------------------------------------------------------
  1937. //
  1938. // Writing
  1939. //
  1940. //--------------------------------------------------------------------------
  1941. /** Write a complete message.
  1942. This function is used to write a complete message.
  1943. The call blocks until one of the following is true:
  1944. @li The message is written.
  1945. @li An error occurs.
  1946. The algorithm, known as a <em>composed operation</em>, is implemented
  1947. in terms of calls to the next layer's `write_some` function.
  1948. The current setting of the @ref binary option controls
  1949. whether the message opcode is set to text or binary. If the
  1950. @ref auto_fragment option is set, the message will be split
  1951. into one or more frames as necessary. The actual payload contents
  1952. sent may be transformed as per the WebSocket protocol settings.
  1953. @param buffers The buffers containing the message to send.
  1954. @return The number of bytes sent from the buffers.
  1955. @throws system_error Thrown on failure.
  1956. */
  1957. template<class ConstBufferSequence>
  1958. std::size_t
  1959. write(ConstBufferSequence const& buffers);
  1960. /** Write a complete message.
  1961. This function is used to write a complete message.
  1962. The call blocks until one of the following is true:
  1963. @li The complete message is written.
  1964. @li An error occurs.
  1965. The algorithm, known as a <em>composed operation</em>, is implemented
  1966. in terms of calls to the next layer's `write_some` function.
  1967. The current setting of the @ref binary option controls
  1968. whether the message opcode is set to text or binary. If the
  1969. @ref auto_fragment option is set, the message will be split
  1970. into one or more frames as necessary. The actual payload contents
  1971. sent may be transformed as per the WebSocket protocol settings.
  1972. @param buffers The buffers containing the message to send.
  1973. @param ec Set to indicate what error occurred, if any.
  1974. @return The number of bytes sent from the buffers.
  1975. */
  1976. template<class ConstBufferSequence>
  1977. std::size_t
  1978. write(ConstBufferSequence const& buffers, error_code& ec);
  1979. /** Write a complete message asynchronously.
  1980. This function is used to asynchronously write a complete message.
  1981. This call always returns immediately. The asynchronous operation
  1982. will continue until one of the following conditions is true:
  1983. @li The complete message is written.
  1984. @li An error occurs.
  1985. The algorithm, known as a <em>composed asynchronous operation</em>,
  1986. is implemented in terms of calls to the next layer's
  1987. `async_write_some` function. The program must ensure that no other
  1988. calls to @ref write, @ref write_some, @ref async_write, or
  1989. @ref async_write_some are performed until this operation completes.
  1990. The current setting of the @ref binary option controls
  1991. whether the message opcode is set to text or binary. If the
  1992. @ref auto_fragment option is set, the message will be split
  1993. into one or more frames as necessary. The actual payload contents
  1994. sent may be transformed as per the WebSocket protocol settings.
  1995. @param buffers A buffer sequence containing the entire message
  1996. payload. The implementation will make copies of this object
  1997. as needed, but ownership of the underlying memory is not
  1998. transferred. The caller is responsible for ensuring that
  1999. the memory locations pointed to by buffers remains valid
  2000. until the completion handler is called.
  2001. @param handler The completion handler to invoke when the operation
  2002. completes. The implementation takes ownership of the handler by
  2003. performing a decay-copy. The equivalent function signature of
  2004. the handler must be:
  2005. @code
  2006. void handler(
  2007. error_code const& ec, // Result of operation
  2008. std::size_t bytes_transferred // Number of bytes sent from the
  2009. // buffers. If an error occurred,
  2010. // this will be less than the buffer_size.
  2011. );
  2012. @endcode
  2013. If the handler has an associated immediate executor,
  2014. an immediate completion will be dispatched to it.
  2015. Otherwise, the handler will not be invoked from within
  2016. this function. Invocation of the handler will be performed
  2017. by dispatching to the immediate executor. If no
  2018. immediate executor is specified, this is equivalent
  2019. to using `net::post`.
  2020. @par Per-Operation Cancellation
  2021. This asynchronous operation supports cancellation for the following
  2022. net::cancellation_type values:
  2023. @li @c net::cancellation_type::terminal
  2024. @li @c net::cancellation_type::total
  2025. `total` cancellation succeeds if the operation is suspended due to ongoing
  2026. control operations such as a ping/pong.
  2027. `terminal` cancellation succeeds when supported by the underlying stream.
  2028. `terminal` cancellation leaves the stream in an undefined state,
  2029. so that only closing it is guaranteed to succeed.
  2030. */
  2031. template<
  2032. class ConstBufferSequence,
  2033. BHO_BEAST_ASYNC_TPARAM2 WriteHandler =
  2034. net::default_completion_token_t<
  2035. executor_type>>
  2036. BHO_BEAST_ASYNC_RESULT2(WriteHandler)
  2037. async_write(
  2038. ConstBufferSequence const& buffers,
  2039. WriteHandler&& handler =
  2040. net::default_completion_token_t<
  2041. executor_type>{});
  2042. /** Write some message data.
  2043. This function is used to send part of a message.
  2044. The call blocks until one of the following is true:
  2045. @li The message data is written.
  2046. @li An error occurs.
  2047. The algorithm, known as a <em>composed operation</em>, is implemented
  2048. in terms of calls to the next layer's `write_some` function.
  2049. If this is the beginning of a new message, the message opcode
  2050. will be set to text or binary based on the current setting of
  2051. the @ref binary (or @ref text) option. The actual payload sent
  2052. may be transformed as per the WebSocket protocol settings.
  2053. @param fin `true` if this is the last part of the message.
  2054. @param buffers The buffers containing the message part to send.
  2055. @return The number of bytes sent from the buffers.
  2056. @throws system_error Thrown on failure.
  2057. */
  2058. template<class ConstBufferSequence>
  2059. std::size_t
  2060. write_some(bool fin, ConstBufferSequence const& buffers);
  2061. /** Write some message data.
  2062. This function is used to send part of a message.
  2063. The call blocks until one of the following is true:
  2064. @li The message data is written.
  2065. @li An error occurs.
  2066. The algorithm, known as a <em>composed operation</em>, is implemented
  2067. in terms of calls to the next layer's `write_some` function.
  2068. If this is the beginning of a new message, the message opcode
  2069. will be set to text or binary based on the current setting of
  2070. the @ref binary (or @ref text) option. The actual payload sent
  2071. may be transformed as per the WebSocket protocol settings.
  2072. This function always writes a complete WebSocket frame (not WebSocket
  2073. message) upon successful completion, so it is well defined to perform
  2074. ping, pong, and close operations after this operation completes.
  2075. @param fin `true` if this is the last part of the message.
  2076. @param buffers The buffers containing the message part to send.
  2077. @param ec Set to indicate what error occurred, if any.
  2078. @return The number of bytes sent from the buffers.
  2079. @return The number of bytes consumed in the input buffers.
  2080. */
  2081. template<class ConstBufferSequence>
  2082. std::size_t
  2083. write_some(bool fin,
  2084. ConstBufferSequence const& buffers, error_code& ec);
  2085. /** Write some message data asynchronously.
  2086. This function is used to asynchronously write part of a message.
  2087. This call always returns immediately. The asynchronous operation
  2088. will continue until one of the following conditions is true:
  2089. @li The message data is written.
  2090. @li An error occurs.
  2091. The algorithm, known as a <em>composed asynchronous operation</em>,
  2092. is implemented in terms of calls to the next layer's
  2093. `async_write_some` function. The program must ensure that no other
  2094. calls to @ref write, @ref write_some, @ref async_write, or
  2095. @ref async_write_some are performed until this operation completes.
  2096. If this is the beginning of a new message, the message opcode
  2097. will be set to text or binary based on the current setting of
  2098. the @ref binary (or @ref text) option. The actual payload sent
  2099. may be transformed as per the WebSocket protocol settings.
  2100. This function always writes a complete WebSocket frame (not WebSocket
  2101. message) upon successful completion, so it is well defined to perform
  2102. ping, pong, and close operations in parallel to this operation.
  2103. @param fin `true` if this is the last part of the message.
  2104. @param buffers The buffers containing the message part to send.
  2105. The implementation will make copies of this object
  2106. as needed, but ownership of the underlying memory is not
  2107. transferred. The caller is responsible for ensuring that
  2108. the memory locations pointed to by buffers remains valid
  2109. until the completion handler is called.
  2110. @param handler The completion handler to invoke when the operation
  2111. completes. The implementation takes ownership of the handler by
  2112. performing a decay-copy. The equivalent function signature of
  2113. the handler must be:
  2114. @code
  2115. void handler(
  2116. error_code const& ec, // Result of operation
  2117. std::size_t bytes_transferred // Number of bytes sent from the
  2118. // buffers. If an error occurred,
  2119. // this will be less than the buffer_size.
  2120. );
  2121. @endcode
  2122. If the handler has an associated immediate executor,
  2123. an immediate completion will be dispatched to it.
  2124. Otherwise, the handler will not be invoked from within
  2125. this function. Invocation of the handler will be performed
  2126. by dispatching to the immediate executor. If no
  2127. immediate executor is specified, this is equivalent
  2128. to using `net::post`.
  2129. @par Per-Operation Cancellation
  2130. This asynchronous operation supports cancellation for the following
  2131. net::cancellation_type values:
  2132. @li @c net::cancellation_type::terminal
  2133. @li @c net::cancellation_type::total
  2134. `total` cancellation succeeds if the operation is suspended due to ongoing
  2135. control operations such as a ping/pong.
  2136. `terminal` cancellation succeeds when supported by the underlying stream.
  2137. `terminal` cancellation leaves the stream in an undefined state,
  2138. so that only closing it is guaranteed to succeed.
  2139. */
  2140. template<
  2141. class ConstBufferSequence,
  2142. BHO_BEAST_ASYNC_TPARAM2 WriteHandler =
  2143. net::default_completion_token_t<
  2144. executor_type>>
  2145. BHO_BEAST_ASYNC_RESULT2(WriteHandler)
  2146. async_write_some(
  2147. bool fin,
  2148. ConstBufferSequence const& buffers,
  2149. WriteHandler&& handler =
  2150. net::default_completion_token_t<
  2151. executor_type>{});
  2152. private:
  2153. template<class, class> class accept_op;
  2154. template<class> class close_op;
  2155. template<class> class handshake_op;
  2156. template<class> class ping_op;
  2157. template<class> class idle_ping_op;
  2158. template<class, class> class read_some_op;
  2159. template<class, class> class read_op;
  2160. template<class> class response_op;
  2161. template<class, class> class write_some_op;
  2162. template<class, class> class write_op;
  2163. struct run_accept_op;
  2164. struct run_close_op;
  2165. struct run_handshake_op;
  2166. struct run_ping_op;
  2167. struct run_idle_ping_op;
  2168. struct run_read_some_op;
  2169. struct run_read_op;
  2170. struct run_response_op;
  2171. struct run_write_some_op;
  2172. struct run_write_op;
  2173. static void default_decorate_req(request_type&) {}
  2174. static void default_decorate_res(response_type&) {}
  2175. //
  2176. // accept / handshake
  2177. //
  2178. template<class Buffers, class Decorator>
  2179. void
  2180. do_accept(
  2181. Buffers const& buffers,
  2182. Decorator const& decorator,
  2183. error_code& ec);
  2184. template<
  2185. class Body, class Allocator,
  2186. class Decorator>
  2187. void
  2188. do_accept(
  2189. http::request<Body,
  2190. http::basic_fields<Allocator>> const& req,
  2191. Decorator const& decorator,
  2192. error_code& ec);
  2193. template<class RequestDecorator>
  2194. void
  2195. do_handshake(response_type* res_p,
  2196. string_view host, string_view target,
  2197. RequestDecorator const& decorator,
  2198. error_code& ec);
  2199. //
  2200. // fail
  2201. //
  2202. void
  2203. do_fail(
  2204. std::uint16_t code,
  2205. error_code ev,
  2206. error_code& ec);
  2207. };
  2208. /** Manually provide a one-time seed to initialize the PRNG
  2209. This function invokes the specified seed sequence to produce a seed
  2210. suitable for use with the pseudo-random number generator used to
  2211. create masks and perform WebSocket protocol handshakes.
  2212. If a seed is not manually provided, the implementation will
  2213. perform a one-time seed generation using `std::random_device`. This
  2214. function may be used when the application runs in an environment
  2215. where the random device is unreliable or does not provide sufficient
  2216. entropy.
  2217. @par Preconditions
  2218. This function may not be called after any websocket @ref stream objects
  2219. have been constructed.
  2220. @param ss A reference to a `std::seed_seq` which will be used to seed
  2221. the pseudo-random number generator. The seed sequence should have at
  2222. least 256 bits of entropy.
  2223. @see stream::secure_prng
  2224. */
  2225. inline
  2226. void
  2227. seed_prng(std::seed_seq& ss)
  2228. {
  2229. detail::prng_seed(&ss);
  2230. }
  2231. } // websocket
  2232. } // beast
  2233. } // bho
  2234. #include <asio2/bho/beast/websocket/impl/stream_impl.hpp> // must be first
  2235. #include <asio2/bho/beast/websocket/impl/accept.hpp>
  2236. #include <asio2/bho/beast/websocket/impl/close.hpp>
  2237. #include <asio2/bho/beast/websocket/impl/handshake.hpp>
  2238. #include <asio2/bho/beast/websocket/impl/ping.hpp>
  2239. #include <asio2/bho/beast/websocket/impl/read.hpp>
  2240. #include <asio2/bho/beast/websocket/impl/stream.hpp>
  2241. #include <asio2/bho/beast/websocket/impl/write.hpp>
  2242. #endif