stream.hpp 102 KB

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