stream.hpp 111 KB

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