stream.hpp 109 KB

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