stream.hpp 111 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925
  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. #ifndef BOOST_BEAST_DOXYGEN
  1098. , typename std::enable_if<
  1099. ! net::is_const_buffer_sequence<
  1100. AcceptHandler>::value>::type* = nullptr
  1101. #endif
  1102. );
  1103. /** Perform the WebSocket handshake asynchronously in the server role.
  1104. This initiating function is used to asynchronously begin performing the
  1105. <a href="https://en.wikipedia.org/wiki/WebSocket#Protocol_handshake">WebSocket handshake</a>,
  1106. required before messages can be sent and received. During the handshake,
  1107. the client sends the Websocket Upgrade HTTP request, and the server
  1108. replies with an HTTP response indicating the result of the handshake.
  1109. This call always returns immediately. The asynchronous operation
  1110. will continue until one of the following conditions is true:
  1111. @li The request is received and the response is sent.
  1112. @li An error occurs.
  1113. The algorithm, known as a <em>composed asynchronous operation</em>,
  1114. is implemented in terms of calls to the next layer's `async_read_some`
  1115. and `async_write_some` functions. No other operation may be performed
  1116. on the stream until this operation completes.
  1117. If a valid upgrade request is received, an HTTP response with a
  1118. <a href="https://tools.ietf.org/html/rfc7230#section-3.1.2">status-code</a>
  1119. of @ref beast::http::status::switching_protocols is sent to
  1120. the peer, otherwise a non-successful error is associated with
  1121. the operation.
  1122. If the request size exceeds the capacity of the stream's
  1123. internal buffer, the error @ref error::buffer_overflow will be
  1124. indicated. To handle larger requests, an application should
  1125. read the HTTP request directly using @ref http::async_read and then
  1126. pass the request to the appropriate overload of @ref accept or
  1127. @ref async_accept
  1128. @param buffers Caller provided data that has already been
  1129. received on the stream. This may be used for implementations
  1130. allowing multiple protocols on the same stream. The
  1131. buffered data will first be applied to the handshake, and
  1132. then to received WebSocket frames. The implementation will
  1133. copy the caller provided data before the function returns.
  1134. @param handler The completion handler to invoke when the operation
  1135. completes. The implementation takes ownership of the handler by
  1136. performing a decay-copy. The equivalent function signature of
  1137. the handler must be:
  1138. @code
  1139. void handler(
  1140. error_code const& ec // Result of operation
  1141. );
  1142. @endcode
  1143. If the handler has an associated immediate executor,
  1144. an immediate completion will be dispatched to it.
  1145. Otherwise, the handler will not be invoked from within
  1146. this function. Invocation of the handler will be performed
  1147. by dispatching to the immediate executor. If no
  1148. immediate executor is specified, this is equivalent
  1149. to using `net::post`.
  1150. @see
  1151. @li <a href="https://tools.ietf.org/html/rfc6455#section-4.2">Websocket Opening Handshake Server Requirements (RFC6455)</a>
  1152. */
  1153. template<
  1154. class ConstBufferSequence,
  1155. BOOST_BEAST_ASYNC_TPARAM1 AcceptHandler =
  1156. net::default_completion_token_t<executor_type>
  1157. >
  1158. BOOST_BEAST_ASYNC_RESULT1(AcceptHandler)
  1159. async_accept(
  1160. ConstBufferSequence const& buffers,
  1161. AcceptHandler&& handler =
  1162. net::default_completion_token_t<
  1163. executor_type>{}
  1164. #ifndef BOOST_BEAST_DOXYGEN
  1165. , typename std::enable_if<
  1166. net::is_const_buffer_sequence<
  1167. ConstBufferSequence>::value>::type* = 0
  1168. , typename std::enable_if<
  1169. ! http::detail::is_header<
  1170. ConstBufferSequence>::value>::type* = 0
  1171. #endif
  1172. );
  1173. /** Perform the WebSocket handshake asynchronously in the server role.
  1174. This initiating function is used to asynchronously begin performing the
  1175. <a href="https://en.wikipedia.org/wiki/WebSocket#Protocol_handshake">WebSocket handshake</a>,
  1176. required before messages can be sent and received. During the handshake,
  1177. the client sends the Websocket Upgrade HTTP request, and the server
  1178. replies with an HTTP response indicating the result of the handshake.
  1179. This call always returns immediately. The asynchronous operation
  1180. will continue until one of the following conditions is true:
  1181. @li The request is received and the response is sent.
  1182. @li An error occurs.
  1183. The algorithm, known as a <em>composed asynchronous operation</em>,
  1184. is implemented in terms of calls to the next layer's `async_read_some`
  1185. and `async_write_some` functions. No other operation may be performed
  1186. on the stream until this operation completes.
  1187. If a valid upgrade request is received, an HTTP response with a
  1188. <a href="https://tools.ietf.org/html/rfc7230#section-3.1.2">status-code</a>
  1189. of @ref beast::http::status::switching_protocols is sent to
  1190. the peer, otherwise a non-successful error is associated with
  1191. the operation.
  1192. @param req An object containing the HTTP Upgrade request.
  1193. Ownership is not transferred, the implementation will not access
  1194. this object from other threads.
  1195. @param handler The completion handler to invoke when the operation
  1196. completes. The implementation takes ownership of the handler by
  1197. performing a decay-copy. The equivalent function signature of
  1198. the handler must be:
  1199. @code
  1200. void handler(
  1201. error_code const& ec // Result of operation
  1202. );
  1203. @endcode
  1204. If the handler has an associated immediate executor,
  1205. an immediate completion will be dispatched to it.
  1206. Otherwise, the handler will not be invoked from within
  1207. this function. Invocation of the handler will be performed
  1208. by dispatching to the immediate executor. If no
  1209. immediate executor is specified, this is equivalent
  1210. to using `net::post`.
  1211. @see
  1212. @li <a href="https://tools.ietf.org/html/rfc6455#section-4.2">Websocket Opening Handshake Server Requirements (RFC6455)</a>
  1213. */
  1214. template<
  1215. class Body, class Allocator,
  1216. BOOST_BEAST_ASYNC_TPARAM1 AcceptHandler =
  1217. net::default_completion_token_t<executor_type>
  1218. >
  1219. BOOST_BEAST_ASYNC_RESULT1(AcceptHandler)
  1220. async_accept(
  1221. http::request<Body,
  1222. http::basic_fields<Allocator>> const& req,
  1223. AcceptHandler&& handler =
  1224. net::default_completion_token_t<
  1225. executor_type>{});
  1226. //--------------------------------------------------------------------------
  1227. //
  1228. // Close Frames
  1229. //
  1230. //--------------------------------------------------------------------------
  1231. /** Send a websocket close control frame.
  1232. This function is used to send a
  1233. <a href="https://tools.ietf.org/html/rfc6455#section-5.5.1">close frame</a>,
  1234. which begins the websocket closing handshake. The session ends when
  1235. both ends of the connection have sent and received a close frame.
  1236. The call blocks until one of the following conditions is true:
  1237. @li The close frame is written.
  1238. @li An error occurs.
  1239. The algorithm, known as a <em>composed operation</em>, is implemented
  1240. in terms of calls to the next layer's `write_some` function.
  1241. After beginning the closing handshake, the program should not write
  1242. further message data, pings, or pongs. Instead, the program should
  1243. continue reading message data until an error occurs. A read returning
  1244. @ref error::closed indicates a successful connection closure.
  1245. @param cr The reason for the close.
  1246. If the close reason specifies a close code other than
  1247. @ref beast::websocket::close_code::none, the close frame is
  1248. sent with the close code and optional reason string. Otherwise,
  1249. the close frame is sent with no payload.
  1250. @throws system_error Thrown on failure.
  1251. @see
  1252. @li <a href="https://tools.ietf.org/html/rfc6455#section-7.1.2">Websocket Closing Handshake (RFC6455)</a>
  1253. */
  1254. void
  1255. close(close_reason const& cr);
  1256. /** Send a websocket close control frame.
  1257. This function is used to send a
  1258. <a href="https://tools.ietf.org/html/rfc6455#section-5.5.1">close frame</a>,
  1259. which begins the websocket closing handshake. The session ends when
  1260. both ends of the connection have sent and received a close frame.
  1261. The call blocks until one of the following conditions is true:
  1262. @li The close frame is written.
  1263. @li An error occurs.
  1264. The algorithm, known as a <em>composed operation</em>, is implemented
  1265. in terms of calls to the next layer's `write_some` function.
  1266. After beginning the closing handshake, the program should not write
  1267. further message data, pings, or pongs. Instead, the program should
  1268. continue reading message data until an error occurs. A read returning
  1269. @ref error::closed indicates a successful connection closure.
  1270. @param cr The reason for the close.
  1271. If the close reason specifies a close code other than
  1272. @ref beast::websocket::close_code::none, the close frame is
  1273. sent with the close code and optional reason string. Otherwise,
  1274. the close frame is sent with no payload.
  1275. @param ec Set to indicate what error occurred, if any.
  1276. @see
  1277. @li <a href="https://tools.ietf.org/html/rfc6455#section-7.1.2">Websocket Closing Handshake (RFC6455)</a>
  1278. */
  1279. void
  1280. close(close_reason const& cr, error_code& ec);
  1281. /** Send a websocket close control frame asynchronously.
  1282. This function is used to asynchronously send a
  1283. <a href="https://tools.ietf.org/html/rfc6455#section-5.5.1">close frame</a>,
  1284. which begins the websocket closing handshake. The session ends when
  1285. both ends of the connection have sent and received a close frame.
  1286. This call always returns immediately. The asynchronous operation
  1287. will continue until one of the following conditions is true:
  1288. @li The close frame finishes sending.
  1289. @li An error occurs.
  1290. The algorithm, known as a <em>composed asynchronous operation</em>,
  1291. is implemented in terms of calls to the next layer's `async_write_some`
  1292. function. No other operations except for message reading operations
  1293. should be initiated on the stream after a close operation is started.
  1294. After beginning the closing handshake, the program should not write
  1295. further message data, pings, or pongs. Instead, the program should
  1296. continue reading message data until an error occurs. A read returning
  1297. @ref error::closed indicates a successful connection closure.
  1298. @param cr The reason for the close.
  1299. If the close reason specifies a close code other than
  1300. @ref beast::websocket::close_code::none, the close frame is
  1301. sent with the close code and optional reason string. Otherwise,
  1302. the close frame is sent with no payload.
  1303. @param handler The completion handler to invoke when the operation
  1304. completes. The implementation takes ownership of the handler by
  1305. performing a decay-copy. The equivalent function signature of
  1306. the handler must be:
  1307. @code
  1308. void handler(
  1309. error_code const& ec // Result of operation
  1310. );
  1311. @endcode
  1312. If the handler has an associated immediate executor,
  1313. an immediate completion will be dispatched to it.
  1314. Otherwise, the handler will not be invoked from within
  1315. this function. Invocation of the handler will be performed
  1316. by dispatching to the immediate executor. If no
  1317. immediate executor is specified, this is equivalent
  1318. to using `net::post`.
  1319. @par Per-Operation Cancellation
  1320. This asynchronous operation supports cancellation for the following
  1321. net::cancellation_type values:
  1322. @li @c net::cancellation_type::terminal
  1323. @li @c net::cancellation_type::total
  1324. `total` cancellation succeeds if the operation is suspended due to ongoing
  1325. control operations such as a ping/pong.
  1326. `terminal` cancellation succeeds when supported by the underlying stream.
  1327. @note `terminal` cancellation will may close the underlying socket.
  1328. @see
  1329. @li <a href="https://tools.ietf.org/html/rfc6455#section-7.1.2">Websocket Closing Handshake (RFC6455)</a>
  1330. */
  1331. template<
  1332. BOOST_BEAST_ASYNC_TPARAM1 CloseHandler =
  1333. net::default_completion_token_t<executor_type>
  1334. >
  1335. BOOST_BEAST_ASYNC_RESULT1(CloseHandler)
  1336. async_close(
  1337. close_reason const& cr,
  1338. CloseHandler&& handler =
  1339. net::default_completion_token_t<
  1340. executor_type>{});
  1341. //--------------------------------------------------------------------------
  1342. //
  1343. // Ping/Pong Frames
  1344. //
  1345. //--------------------------------------------------------------------------
  1346. /** Send a websocket ping control frame.
  1347. This function is used to send a
  1348. <a href="https://tools.ietf.org/html/rfc6455#section-5.5.2">ping frame</a>,
  1349. which usually elicits an automatic pong control frame response from
  1350. the peer.
  1351. The call blocks until one of the following conditions is true:
  1352. @li The ping frame is written.
  1353. @li An error occurs.
  1354. The algorithm, known as a <em>composed operation</em>, is implemented
  1355. in terms of calls to the next layer's `write_some` function.
  1356. @param payload The payload of the ping message, which may be empty.
  1357. @throws system_error Thrown on failure.
  1358. */
  1359. void
  1360. ping(ping_data const& payload);
  1361. /** Send a websocket ping control frame.
  1362. This function is used to send a
  1363. <a href="https://tools.ietf.org/html/rfc6455#section-5.5.2">ping frame</a>,
  1364. which usually elicits an automatic pong control frame response from
  1365. the peer.
  1366. The call blocks until one of the following conditions is true:
  1367. @li The ping frame is written.
  1368. @li An error occurs.
  1369. The algorithm, known as a <em>composed operation</em>, is implemented
  1370. in terms of calls to the next layer's `write_some` function.
  1371. @param payload The payload of the ping message, which may be empty.
  1372. @param ec Set to indicate what error occurred, if any.
  1373. */
  1374. void
  1375. ping(ping_data const& payload, error_code& ec);
  1376. /** Send a websocket ping control frame asynchronously.
  1377. This function is used to asynchronously send a
  1378. <a href="https://tools.ietf.org/html/rfc6455#section-5.5.2">ping frame</a>,
  1379. which usually elicits an automatic pong control frame response from
  1380. the peer.
  1381. @li The ping frame is written.
  1382. @li An error occurs.
  1383. The algorithm, known as a <em>composed asynchronous operation</em>,
  1384. is implemented in terms of calls to the next layer's `async_write_some`
  1385. function. The program must ensure that no other calls to @ref ping,
  1386. @ref pong, @ref async_ping, or @ref async_pong are performed until
  1387. this operation completes.
  1388. If a close frame is sent or received before the ping frame is
  1389. sent, the error received by this completion handler will be
  1390. `net::error::operation_aborted`.
  1391. @param payload The payload of the ping message, which may be empty.
  1392. The implementation will not access the contents of this object after
  1393. the initiating function returns.
  1394. @param handler The completion handler to invoke when the operation
  1395. completes. The implementation takes ownership of the handler by
  1396. performing a decay-copy. The equivalent function signature of
  1397. the handler must be:
  1398. @code
  1399. void handler(
  1400. error_code const& ec // Result of operation
  1401. );
  1402. @endcode
  1403. If the handler has an associated immediate executor,
  1404. an immediate completion will be dispatched to it.
  1405. Otherwise, the handler will not be invoked from within
  1406. this function. Invocation of the handler will be performed
  1407. by dispatching to the immediate executor. If no
  1408. immediate executor is specified, this is equivalent
  1409. to using `net::post`.
  1410. @par Per-Operation Cancellation
  1411. This asynchronous operation supports cancellation for the following
  1412. net::cancellation_type values:
  1413. @li @c net::cancellation_type::terminal
  1414. @li @c net::cancellation_type::total
  1415. `total` cancellation succeeds if the operation is suspended due to ongoing
  1416. control operations such as a ping/pong.
  1417. `terminal` cancellation succeeds when supported by the underlying stream.
  1418. `terminal` cancellation leaves the stream in an undefined state,
  1419. so that only closing it is guaranteed to succeed.
  1420. */
  1421. template<
  1422. BOOST_BEAST_ASYNC_TPARAM1 PingHandler =
  1423. net::default_completion_token_t<executor_type>
  1424. >
  1425. BOOST_BEAST_ASYNC_RESULT1(PingHandler)
  1426. async_ping(
  1427. ping_data const& payload,
  1428. PingHandler&& handler =
  1429. net::default_completion_token_t<
  1430. executor_type>{});
  1431. /** Send a websocket pong control frame.
  1432. This function is used to send a
  1433. <a href="https://tools.ietf.org/html/rfc6455#section-5.5.3">pong frame</a>,
  1434. which is usually sent automatically in response to a ping frame
  1435. from the remote peer.
  1436. The call blocks until one of the following conditions is true:
  1437. @li The pong frame is written.
  1438. @li An error occurs.
  1439. The algorithm, known as a <em>composed operation</em>, is implemented
  1440. in terms of calls to the next layer's `write_some` function.
  1441. WebSocket allows pong frames to be sent at any time, without first
  1442. receiving a ping. An unsolicited pong sent in this fashion may
  1443. indicate to the remote peer that the connection is still active.
  1444. @param payload The payload of the pong message, which may be empty.
  1445. @throws system_error Thrown on failure.
  1446. */
  1447. void
  1448. pong(ping_data const& payload);
  1449. /** Send a websocket pong control frame.
  1450. This function is used to send a
  1451. <a href="https://tools.ietf.org/html/rfc6455#section-5.5.3">pong frame</a>,
  1452. which is usually sent automatically in response to a ping frame
  1453. from the remote peer.
  1454. The call blocks until one of the following conditions is true:
  1455. @li The pong frame is written.
  1456. @li An error occurs.
  1457. The algorithm, known as a <em>composed operation</em>, is implemented
  1458. in terms of calls to the next layer's `write_some` function.
  1459. WebSocket allows pong frames to be sent at any time, without first
  1460. receiving a ping. An unsolicited pong sent in this fashion may
  1461. indicate to the remote peer that the connection is still active.
  1462. @param payload The payload of the pong message, which may be empty.
  1463. @param ec Set to indicate what error occurred, if any.
  1464. */
  1465. void
  1466. pong(ping_data const& payload, error_code& ec);
  1467. /** Send a websocket pong control frame asynchronously.
  1468. This function is used to asynchronously send a
  1469. <a href="https://tools.ietf.org/html/rfc6455#section-5.5.3">pong frame</a>,
  1470. which is usually sent automatically in response to a ping frame
  1471. from the remote peer.
  1472. @li The pong frame is written.
  1473. @li An error occurs.
  1474. The algorithm, known as a <em>composed asynchronous operation</em>,
  1475. is implemented in terms of calls to the next layer's `async_write_some`
  1476. function. The program must ensure that no other calls to @ref ping,
  1477. @ref pong, @ref async_ping, or @ref async_pong are performed until
  1478. this operation completes.
  1479. If a close frame is sent or received before the pong frame is
  1480. sent, the error received by this completion handler will be
  1481. `net::error::operation_aborted`.
  1482. WebSocket allows pong frames to be sent at any time, without first
  1483. receiving a ping. An unsolicited pong sent in this fashion may
  1484. indicate to the remote peer that the connection is still active.
  1485. @param payload The payload of the pong message, which may be empty.
  1486. The implementation will not access the contents of this object after
  1487. the initiating function returns.
  1488. @param handler The completion handler to invoke when the operation
  1489. completes. The implementation takes ownership of the handler by
  1490. performing a decay-copy. The equivalent function signature of
  1491. the handler must be:
  1492. @code
  1493. void handler(
  1494. error_code const& ec // Result of operation
  1495. );
  1496. @endcode
  1497. If the handler has an associated immediate executor,
  1498. an immediate completion will be dispatched to it.
  1499. Otherwise, the handler will not be invoked from within
  1500. this function. Invocation of the handler will be performed
  1501. by dispatching to the immediate executor. If no
  1502. immediate executor is specified, this is equivalent
  1503. to using `net::post`.
  1504. @par Per-Operation Cancellation
  1505. This asynchronous operation supports cancellation for the following
  1506. net::cancellation_type values:
  1507. @li @c net::cancellation_type::terminal
  1508. @li @c net::cancellation_type::total
  1509. `total` cancellation succeeds if the operation is suspended due to ongoing
  1510. control operations such as a ping/pong.
  1511. `terminal` cancellation succeeds when supported by the underlying stream.
  1512. `terminal` cancellation leaves the stream in an undefined state,
  1513. so that only closing it is guaranteed to succeed.
  1514. */
  1515. template<
  1516. BOOST_BEAST_ASYNC_TPARAM1 PongHandler =
  1517. net::default_completion_token_t<executor_type>
  1518. >
  1519. BOOST_BEAST_ASYNC_RESULT1(PongHandler)
  1520. async_pong(
  1521. ping_data const& payload,
  1522. PongHandler&& handler =
  1523. net::default_completion_token_t<
  1524. executor_type>{});
  1525. //--------------------------------------------------------------------------
  1526. //
  1527. // Reading
  1528. //
  1529. //--------------------------------------------------------------------------
  1530. /** Read a complete message.
  1531. This function is used to read a complete message.
  1532. The call blocks until one of the following is true:
  1533. @li A complete message is received.
  1534. @li A close frame is received. In this case the error indicated by
  1535. the function will be @ref error::closed.
  1536. @li An error occurs.
  1537. The algorithm, known as a <em>composed operation</em>, is implemented
  1538. in terms of calls to the next layer's `read_some` and `write_some`
  1539. functions.
  1540. Received message data is appended to the buffer.
  1541. The functions @ref got_binary and @ref got_text may be used
  1542. to query the stream and determine the type of the last received message.
  1543. Until the call returns, the implementation will read incoming control
  1544. frames and handle them automatically as follows:
  1545. @li The @ref control_callback will be invoked for each control frame.
  1546. @li For each received ping frame, a pong frame will be
  1547. automatically sent.
  1548. @li If a close frame is received, the WebSocket closing handshake is
  1549. performed. In this case, when the function returns, the error
  1550. @ref error::closed will be indicated.
  1551. @return The number of message payload bytes appended to the buffer.
  1552. @param buffer A dynamic buffer to append message data to.
  1553. @throws system_error Thrown on failure.
  1554. */
  1555. template<class DynamicBuffer>
  1556. std::size_t
  1557. read(DynamicBuffer& buffer);
  1558. /** Read a complete message.
  1559. This function is used to read a complete message.
  1560. The call blocks until one of the following is true:
  1561. @li A complete message is received.
  1562. @li A close frame is received. In this case the error indicated by
  1563. the function will be @ref error::closed.
  1564. @li An error occurs.
  1565. The algorithm, known as a <em>composed operation</em>, is implemented
  1566. in terms of calls to the next layer's `read_some` and `write_some`
  1567. functions.
  1568. Received message data is appended to the buffer.
  1569. The functions @ref got_binary and @ref got_text may be used
  1570. to query the stream and determine the type of the last received message.
  1571. Until the call returns, the implementation will read incoming control
  1572. frames and handle them automatically as follows:
  1573. @li The @ref control_callback will be invoked for each control frame.
  1574. @li For each received ping frame, a pong frame will be
  1575. automatically sent.
  1576. @li If a close frame is received, the WebSocket closing handshake is
  1577. performed. In this case, when the function returns, the error
  1578. @ref error::closed will be indicated.
  1579. @return The number of message payload bytes appended to the buffer.
  1580. @param buffer A dynamic buffer to append message data to.
  1581. @param ec Set to indicate what error occurred, if any.
  1582. */
  1583. template<class DynamicBuffer>
  1584. std::size_t
  1585. read(DynamicBuffer& buffer, error_code& ec);
  1586. /** Read a complete message asynchronously.
  1587. This function is used to asynchronously read a complete message.
  1588. This call always returns immediately. The asynchronous operation
  1589. will continue until one of the following conditions is true:
  1590. @li A complete message is received.
  1591. @li A close frame is received. In this case the error indicated by
  1592. the function will be @ref error::closed.
  1593. @li An error occurs.
  1594. The algorithm, known as a <em>composed asynchronous operation</em>,
  1595. is implemented in terms of calls to the next layer's `async_read_some`
  1596. and `async_write_some` functions. The program must ensure that no other
  1597. calls to @ref read, @ref read_some, @ref async_read, or @ref async_read_some
  1598. are performed until this operation completes.
  1599. Received message data is appended to the buffer.
  1600. The functions @ref got_binary and @ref got_text may be used
  1601. to query the stream and determine the type of the last received message.
  1602. Until the operation completes, the implementation will read incoming
  1603. control frames and handle them automatically as follows:
  1604. @li The @ref control_callback will be invoked for each control frame.
  1605. @li For each received ping frame, a pong frame will be
  1606. automatically sent.
  1607. @li If a close frame is received, the WebSocket close procedure is
  1608. performed. In this case, when the function returns, the error
  1609. @ref error::closed will be indicated.
  1610. Pong frames and close frames sent by the implementation while the
  1611. read operation is outstanding do not prevent the application from
  1612. also writing message data, sending pings, sending pongs, or sending
  1613. close frames.
  1614. @param buffer A dynamic buffer to append message data to.
  1615. @param handler The completion handler to invoke when the operation
  1616. completes. The implementation takes ownership of the handler by
  1617. performing a decay-copy. The equivalent function signature of
  1618. the handler must be:
  1619. @code
  1620. void handler(
  1621. error_code const& ec, // Result of operation
  1622. std::size_t bytes_written // Number of bytes appended to buffer
  1623. );
  1624. @endcode
  1625. If the handler has an associated immediate executor,
  1626. an immediate completion will be dispatched to it.
  1627. Otherwise, the handler will not be invoked from within
  1628. this function. Invocation of the handler will be performed
  1629. by dispatching to the immediate executor. If no
  1630. immediate executor is specified, this is equivalent
  1631. to using `net::post`.
  1632. @par Per-Operation Cancellation
  1633. This asynchronous operation supports cancellation for the following
  1634. net::cancellation_type values:
  1635. @li @c net::cancellation_type::terminal
  1636. @li @c net::cancellation_type::total
  1637. `total` cancellation succeeds if the operation is suspended due to ongoing
  1638. control operations such as a ping/pong.
  1639. `terminal` cancellation succeeds when supported by the underlying stream.
  1640. `terminal` cancellation leaves the stream in an undefined state,
  1641. so that only closing it is guaranteed to succeed.
  1642. */
  1643. template<
  1644. class DynamicBuffer,
  1645. BOOST_BEAST_ASYNC_TPARAM2 ReadHandler =
  1646. net::default_completion_token_t<
  1647. executor_type>>
  1648. BOOST_BEAST_ASYNC_RESULT2(ReadHandler)
  1649. async_read(
  1650. DynamicBuffer& buffer,
  1651. ReadHandler&& handler =
  1652. net::default_completion_token_t<
  1653. executor_type>{});
  1654. //--------------------------------------------------------------------------
  1655. /** Read some message data.
  1656. This function is used to read some message data.
  1657. The call blocks until one of the following is true:
  1658. @li Some message data is received.
  1659. @li A close frame is received. In this case the error indicated by
  1660. the function will be @ref error::closed.
  1661. @li An error occurs.
  1662. The algorithm, known as a <em>composed operation</em>, is implemented
  1663. in terms of calls to the next layer's `read_some` and `write_some`
  1664. functions.
  1665. Received message data is appended to the buffer.
  1666. The functions @ref got_binary and @ref got_text may be used
  1667. to query the stream and determine the type of the last received message.
  1668. The function @ref is_message_done may be called to determine if the
  1669. message received by the last read operation is complete.
  1670. Until the call returns, the implementation will read incoming control
  1671. frames and handle them automatically as follows:
  1672. @li The @ref control_callback will be invoked for each control frame.
  1673. @li For each received ping frame, a pong frame will be
  1674. automatically sent.
  1675. @li If a close frame is received, the WebSocket closing handshake is
  1676. performed. In this case, when the function returns, the error
  1677. @ref error::closed will be indicated.
  1678. @return The number of message payload bytes appended to the buffer.
  1679. @param buffer A dynamic buffer to append message data to.
  1680. @param limit An upper limit on the number of bytes this function
  1681. will append into the buffer. If this value is zero, then a reasonable
  1682. size will be chosen automatically.
  1683. @throws system_error Thrown on failure.
  1684. */
  1685. template<class DynamicBuffer>
  1686. std::size_t
  1687. read_some(
  1688. DynamicBuffer& buffer,
  1689. std::size_t limit);
  1690. /** Read some message data.
  1691. This function is used to read some message data.
  1692. The call blocks until one of the following is true:
  1693. @li Some message data is received.
  1694. @li A close frame is received. In this case the error indicated by
  1695. the function will be @ref error::closed.
  1696. @li An error occurs.
  1697. The algorithm, known as a <em>composed operation</em>, is implemented
  1698. in terms of calls to the next layer's `read_some` and `write_some`
  1699. functions.
  1700. Received message data is appended to the buffer.
  1701. The functions @ref got_binary and @ref got_text may be used
  1702. to query the stream and determine the type of the last received message.
  1703. The function @ref is_message_done may be called to determine if the
  1704. message received by the last read operation is complete.
  1705. Until the call returns, the implementation will read incoming control
  1706. frames and handle them automatically as follows:
  1707. @li The @ref control_callback will be invoked for each control frame.
  1708. @li For each received ping frame, a pong frame will be
  1709. automatically sent.
  1710. @li If a close frame is received, the WebSocket closing handshake is
  1711. performed. In this case, when the function returns, the error
  1712. @ref error::closed will be indicated.
  1713. @return The number of message payload bytes appended to the buffer.
  1714. @param buffer A dynamic buffer to append message data to.
  1715. @param limit An upper limit on the number of bytes this function
  1716. will append into the buffer. If this value is zero, then a reasonable
  1717. size will be chosen automatically.
  1718. @param ec Set to indicate what error occurred, if any.
  1719. */
  1720. template<class DynamicBuffer>
  1721. std::size_t
  1722. read_some(
  1723. DynamicBuffer& buffer,
  1724. std::size_t limit,
  1725. error_code& ec);
  1726. /** Read some message data asynchronously.
  1727. This function is used to asynchronously read some message data.
  1728. This call always returns immediately. The asynchronous operation
  1729. will continue until one of the following conditions is true:
  1730. @li Some message data is received.
  1731. @li A close frame is received. In this case the error indicated by
  1732. the function will be @ref error::closed.
  1733. @li An error occurs.
  1734. The algorithm, known as a <em>composed asynchronous operation</em>,
  1735. is implemented in terms of calls to the next layer's `async_read_some`
  1736. and `async_write_some` functions. The program must ensure that no other
  1737. calls to @ref read, @ref read_some, @ref async_read, or @ref async_read_some
  1738. are performed until this operation completes.
  1739. Received message data is appended to the buffer.
  1740. The functions @ref got_binary and @ref got_text may be used
  1741. to query the stream and determine the type of the last received message.
  1742. Until the operation completes, the implementation will read incoming
  1743. control frames and handle them automatically as follows:
  1744. @li The @ref control_callback will be invoked for each control frame.
  1745. @li For each received ping frame, a pong frame will be
  1746. automatically sent.
  1747. @li If a close frame is received, the WebSocket close procedure is
  1748. performed. In this case, when the function returns, the error
  1749. @ref error::closed will be indicated.
  1750. Pong frames and close frames sent by the implementation while the
  1751. read operation is outstanding do not prevent the application from
  1752. also writing message data, sending pings, sending pongs, or sending
  1753. close frames.
  1754. @param buffer A dynamic buffer to append message data to.
  1755. @param limit An upper limit on the number of bytes this function
  1756. will append into the buffer. If this value is zero, then a reasonable
  1757. size will be chosen automatically.
  1758. @param handler The completion handler to invoke when the operation
  1759. completes. The implementation takes ownership of the handler by
  1760. performing a decay-copy. The equivalent function signature of
  1761. the handler must be:
  1762. @code
  1763. void handler(
  1764. error_code const& ec, // Result of operation
  1765. std::size_t bytes_written // Number of bytes appended to buffer
  1766. );
  1767. @endcode
  1768. If the handler has an associated immediate executor,
  1769. an immediate completion will be dispatched to it.
  1770. Otherwise, the handler will not be invoked from within
  1771. this function. Invocation of the handler will be performed
  1772. by dispatching to the immediate executor. If no
  1773. immediate executor is specified, this is equivalent
  1774. to using `net::post`.
  1775. @par Per-Operation Cancellation
  1776. This asynchronous operation supports cancellation for the following
  1777. net::cancellation_type values:
  1778. @li @c net::cancellation_type::terminal
  1779. @li @c net::cancellation_type::total
  1780. `total` cancellation succeeds if the operation is suspended due to ongoing
  1781. control operations such as a ping/pong.
  1782. `terminal` cancellation succeeds when supported by the underlying stream.
  1783. `terminal` cancellation leaves the stream in an undefined state,
  1784. so that only closing it is guaranteed to succeed.
  1785. */
  1786. template<
  1787. class DynamicBuffer,
  1788. BOOST_BEAST_ASYNC_TPARAM2 ReadHandler =
  1789. net::default_completion_token_t<
  1790. executor_type>>
  1791. BOOST_BEAST_ASYNC_RESULT2(ReadHandler)
  1792. async_read_some(
  1793. DynamicBuffer& buffer,
  1794. std::size_t limit,
  1795. ReadHandler&& handler =
  1796. net::default_completion_token_t<
  1797. executor_type>{});
  1798. //--------------------------------------------------------------------------
  1799. /** Read some message data.
  1800. This function is used to read some message data.
  1801. The call blocks until one of the following is true:
  1802. @li Some message data is received.
  1803. @li A close frame is received. In this case the error indicated by
  1804. the function will be @ref error::closed.
  1805. @li An error occurs.
  1806. The algorithm, known as a <em>composed operation</em>, is implemented
  1807. in terms of calls to the next layer's `read_some` and `write_some`
  1808. functions.
  1809. The functions @ref got_binary and @ref got_text may be used
  1810. to query the stream and determine the type of the last received message.
  1811. The function @ref is_message_done may be called to determine if the
  1812. message received by the last read operation is complete.
  1813. Until the call returns, the implementation will read incoming control
  1814. frames and handle them automatically as follows:
  1815. @li The @ref control_callback will be invoked for each control frame.
  1816. @li For each received ping frame, a pong frame will be
  1817. automatically sent.
  1818. @li If a close frame is received, the WebSocket closing handshake is
  1819. performed. In this case, when the function returns, the error
  1820. @ref error::closed will be indicated.
  1821. @return The number of message payload bytes appended to the buffer.
  1822. @param buffers A buffer sequence to write message data into.
  1823. The previous contents of the buffers will be overwritten, starting
  1824. from the beginning.
  1825. @throws system_error Thrown on failure.
  1826. */
  1827. template<class MutableBufferSequence>
  1828. std::size_t
  1829. read_some(
  1830. MutableBufferSequence const& buffers);
  1831. /** Read some message data.
  1832. This function is used to read some message data.
  1833. The call blocks until one of the following is true:
  1834. @li Some message data is received.
  1835. @li A close frame is received. In this case the error indicated by
  1836. the function will be @ref error::closed.
  1837. @li An error occurs.
  1838. The algorithm, known as a <em>composed operation</em>, is implemented
  1839. in terms of calls to the next layer's `read_some` and `write_some`
  1840. functions.
  1841. The functions @ref got_binary and @ref got_text may be used
  1842. to query the stream and determine the type of the last received message.
  1843. The function @ref is_message_done may be called to determine if the
  1844. message received by the last read operation is complete.
  1845. Until the call returns, the implementation will read incoming control
  1846. frames and handle them automatically as follows:
  1847. @li The @ref control_callback will be invoked for each control frame.
  1848. @li For each received ping frame, a pong frame will be
  1849. automatically sent.
  1850. @li If a close frame is received, the WebSocket closing handshake is
  1851. performed. In this case, when the function returns, the error
  1852. @ref error::closed will be indicated.
  1853. @return The number of message payload bytes appended to the buffer.
  1854. @param buffers A buffer sequence to write message data into.
  1855. The previous contents of the buffers will be overwritten, starting
  1856. from the beginning.
  1857. @param ec Set to indicate what error occurred, if any.
  1858. @par Per-Operation Cancellation
  1859. This asynchronous operation supports cancellation for the following
  1860. net::cancellation_type values:
  1861. @li @c net::cancellation_type::terminal
  1862. @li @c net::cancellation_type::total
  1863. `total` cancellation succeeds if the operation is suspended due to ongoing
  1864. control operations such as a ping/pong.
  1865. `terminal` cancellation succeeds when supported by the underlying stream.
  1866. `terminal` cancellation leaves the stream in an undefined state,
  1867. so that only closing it is guaranteed to succeed.
  1868. */
  1869. template<class MutableBufferSequence>
  1870. std::size_t
  1871. read_some(
  1872. MutableBufferSequence const& buffers,
  1873. error_code& ec);
  1874. /** Read some message data asynchronously.
  1875. This function is used to asynchronously read some message data.
  1876. This call always returns immediately. The asynchronous operation
  1877. will continue until one of the following conditions is true:
  1878. @li Some message data is received.
  1879. @li A close frame is received. In this case the error indicated by
  1880. the function will be @ref error::closed.
  1881. @li An error occurs.
  1882. The algorithm, known as a <em>composed asynchronous operation</em>,
  1883. is implemented in terms of calls to the next layer's `async_read_some`
  1884. and `async_write_some` functions. The program must ensure that no other
  1885. calls to @ref read, @ref read_some, @ref async_read, or @ref async_read_some
  1886. are performed until this operation completes.
  1887. Received message data is appended to the buffer.
  1888. The functions @ref got_binary and @ref got_text may be used
  1889. to query the stream and determine the type of the last received message.
  1890. Until the operation completes, the implementation will read incoming
  1891. control frames and handle them automatically as follows:
  1892. @li The @ref control_callback will be invoked for each control frame.
  1893. @li For each received ping frame, a pong frame will be
  1894. automatically sent.
  1895. @li If a close frame is received, the WebSocket close procedure is
  1896. performed. In this case, when the function returns, the error
  1897. @ref error::closed will be indicated.
  1898. Pong frames and close frames sent by the implementation while the
  1899. read operation is outstanding do not prevent the application from
  1900. also writing message data, sending pings, sending pongs, or sending
  1901. close frames.
  1902. @param buffers A buffer sequence to write message data into.
  1903. The previous contents of the buffers will be overwritten, starting
  1904. from the beginning.
  1905. The implementation will make copies of this object as needed, but
  1906. but ownership of the underlying memory is not transferred. The
  1907. caller is responsible for ensuring that the memory locations
  1908. pointed to by the buffer sequence remain valid until the
  1909. completion handler is called.
  1910. @param handler The completion handler to invoke when the operation
  1911. completes. The implementation takes ownership of the handler by
  1912. performing a decay-copy. The equivalent function signature of
  1913. the handler must be:
  1914. @code
  1915. void handler(
  1916. error_code const& ec, // Result of operation
  1917. std::size_t bytes_written // Number of bytes written to the buffers
  1918. );
  1919. @endcode
  1920. If the handler has an associated immediate executor,
  1921. an immediate completion will be dispatched to it.
  1922. Otherwise, the handler will not be invoked from within
  1923. this function. Invocation of the handler will be performed
  1924. by dispatching to the immediate executor. If no
  1925. immediate executor is specified, this is equivalent
  1926. to using `net::post`.
  1927. @par Per-Operation Cancellation
  1928. This asynchronous operation supports cancellation for the following
  1929. net::cancellation_type values:
  1930. @li @c net::cancellation_type::terminal
  1931. @li @c net::cancellation_type::total
  1932. `total` cancellation succeeds if the operation is suspended due to ongoing
  1933. control operations such as a ping/pong.
  1934. `terminal` cancellation succeeds when supported by the underlying stream.
  1935. `terminal` cancellation leaves the stream in an undefined state,
  1936. so that only closing it is guaranteed to succeed.
  1937. */
  1938. template<
  1939. class MutableBufferSequence,
  1940. BOOST_BEAST_ASYNC_TPARAM2 ReadHandler =
  1941. net::default_completion_token_t<
  1942. executor_type>>
  1943. BOOST_BEAST_ASYNC_RESULT2(ReadHandler)
  1944. async_read_some(
  1945. MutableBufferSequence const& buffers,
  1946. ReadHandler&& handler =
  1947. net::default_completion_token_t<
  1948. executor_type>{});
  1949. //--------------------------------------------------------------------------
  1950. //
  1951. // Writing
  1952. //
  1953. //--------------------------------------------------------------------------
  1954. /** Write a complete message.
  1955. This function is used to write a complete message.
  1956. The call blocks until one of the following is true:
  1957. @li The message is written.
  1958. @li An error occurs.
  1959. The algorithm, known as a <em>composed operation</em>, is implemented
  1960. in terms of calls to the next layer's `write_some` function.
  1961. The current setting of the @ref binary option controls
  1962. whether the message opcode is set to text or binary. If the
  1963. @ref auto_fragment option is set, the message will be split
  1964. into one or more frames as necessary. The actual payload contents
  1965. sent may be transformed as per the WebSocket protocol settings.
  1966. @param buffers The buffers containing the message to send.
  1967. @return The number of bytes sent from the buffers.
  1968. @throws system_error Thrown on failure.
  1969. */
  1970. template<class ConstBufferSequence>
  1971. std::size_t
  1972. write(ConstBufferSequence const& buffers);
  1973. /** Write a complete message.
  1974. This function is used to write a complete message.
  1975. The call blocks until one of the following is true:
  1976. @li The complete message is written.
  1977. @li An error occurs.
  1978. The algorithm, known as a <em>composed operation</em>, is implemented
  1979. in terms of calls to the next layer's `write_some` function.
  1980. The current setting of the @ref binary option controls
  1981. whether the message opcode is set to text or binary. If the
  1982. @ref auto_fragment option is set, the message will be split
  1983. into one or more frames as necessary. The actual payload contents
  1984. sent may be transformed as per the WebSocket protocol settings.
  1985. @param buffers The buffers containing the message to send.
  1986. @param ec Set to indicate what error occurred, if any.
  1987. @return The number of bytes sent from the buffers.
  1988. */
  1989. template<class ConstBufferSequence>
  1990. std::size_t
  1991. write(ConstBufferSequence const& buffers, error_code& ec);
  1992. /** Write a complete message asynchronously.
  1993. This function is used to asynchronously write a complete message.
  1994. This call always returns immediately. The asynchronous operation
  1995. will continue until one of the following conditions is true:
  1996. @li The complete message is written.
  1997. @li An error occurs.
  1998. The algorithm, known as a <em>composed asynchronous operation</em>,
  1999. is implemented in terms of calls to the next layer's
  2000. `async_write_some` function. The program must ensure that no other
  2001. calls to @ref write, @ref write_some, @ref async_write, or
  2002. @ref async_write_some are performed until this operation completes.
  2003. The current setting of the @ref binary option controls
  2004. whether the message opcode is set to text or binary. If the
  2005. @ref auto_fragment option is set, the message will be split
  2006. into one or more frames as necessary. The actual payload contents
  2007. sent may be transformed as per the WebSocket protocol settings.
  2008. @param buffers A buffer sequence containing the entire message
  2009. payload. The implementation will make copies of this object
  2010. as needed, but ownership of the underlying memory is not
  2011. transferred. The caller is responsible for ensuring that
  2012. the memory locations pointed to by buffers remains valid
  2013. until the completion handler is called.
  2014. @param handler The completion handler to invoke when the operation
  2015. completes. The implementation takes ownership of the handler by
  2016. performing a decay-copy. The equivalent function signature of
  2017. the handler must be:
  2018. @code
  2019. void handler(
  2020. error_code const& ec, // Result of operation
  2021. std::size_t bytes_transferred // Number of bytes sent from the
  2022. // buffers. If an error occurred,
  2023. // this will be less than the buffer_size.
  2024. );
  2025. @endcode
  2026. If the handler has an associated immediate executor,
  2027. an immediate completion will be dispatched to it.
  2028. Otherwise, the handler will not be invoked from within
  2029. this function. Invocation of the handler will be performed
  2030. by dispatching to the immediate executor. If no
  2031. immediate executor is specified, this is equivalent
  2032. to using `net::post`.
  2033. @par Per-Operation Cancellation
  2034. This asynchronous operation supports cancellation for the following
  2035. net::cancellation_type values:
  2036. @li @c net::cancellation_type::terminal
  2037. @li @c net::cancellation_type::total
  2038. `total` cancellation succeeds if the operation is suspended due to ongoing
  2039. control operations such as a ping/pong.
  2040. `terminal` cancellation succeeds when supported by the underlying stream.
  2041. `terminal` cancellation leaves the stream in an undefined state,
  2042. so that only closing it is guaranteed to succeed.
  2043. */
  2044. template<
  2045. class ConstBufferSequence,
  2046. BOOST_BEAST_ASYNC_TPARAM2 WriteHandler =
  2047. net::default_completion_token_t<
  2048. executor_type>>
  2049. BOOST_BEAST_ASYNC_RESULT2(WriteHandler)
  2050. async_write(
  2051. ConstBufferSequence const& buffers,
  2052. WriteHandler&& handler =
  2053. net::default_completion_token_t<
  2054. executor_type>{});
  2055. /** Write some message data.
  2056. This function is used to send part of a message.
  2057. The call blocks until one of the following is true:
  2058. @li The message data is written.
  2059. @li An error occurs.
  2060. The algorithm, known as a <em>composed operation</em>, is implemented
  2061. in terms of calls to the next layer's `write_some` function.
  2062. If this is the beginning of a new message, the message opcode
  2063. will be set to text or binary based on the current setting of
  2064. the @ref binary (or @ref text) option. The actual payload sent
  2065. may be transformed as per the WebSocket protocol settings.
  2066. @param fin `true` if this is the last part of the message.
  2067. @param buffers The buffers containing the message part to send.
  2068. @return The number of bytes sent from the buffers.
  2069. @throws system_error Thrown on failure.
  2070. */
  2071. template<class ConstBufferSequence>
  2072. std::size_t
  2073. write_some(bool fin, ConstBufferSequence const& buffers);
  2074. /** Write some message data.
  2075. This function is used to send part of a message.
  2076. The call blocks until one of the following is true:
  2077. @li The message data is written.
  2078. @li An error occurs.
  2079. The algorithm, known as a <em>composed operation</em>, is implemented
  2080. in terms of calls to the next layer's `write_some` function.
  2081. If this is the beginning of a new message, the message opcode
  2082. will be set to text or binary based on the current setting of
  2083. the @ref binary (or @ref text) option. The actual payload sent
  2084. may be transformed as per the WebSocket protocol settings.
  2085. This function always writes a complete WebSocket frame (not WebSocket
  2086. message) upon successful completion, so it is well defined to perform
  2087. ping, pong, and close operations after this operation completes.
  2088. @param fin `true` if this is the last part of the message.
  2089. @param buffers The buffers containing the message part to send.
  2090. @param ec Set to indicate what error occurred, if any.
  2091. @return The number of bytes sent from the buffers.
  2092. @return The number of bytes consumed in the input buffers.
  2093. */
  2094. template<class ConstBufferSequence>
  2095. std::size_t
  2096. write_some(bool fin,
  2097. ConstBufferSequence const& buffers, error_code& ec);
  2098. /** Write some message data asynchronously.
  2099. This function is used to asynchronously write part of a message.
  2100. This call always returns immediately. The asynchronous operation
  2101. will continue until one of the following conditions is true:
  2102. @li The message data is written.
  2103. @li An error occurs.
  2104. The algorithm, known as a <em>composed asynchronous operation</em>,
  2105. is implemented in terms of calls to the next layer's
  2106. `async_write_some` function. The program must ensure that no other
  2107. calls to @ref write, @ref write_some, @ref async_write, or
  2108. @ref async_write_some are performed until this operation completes.
  2109. If this is the beginning of a new message, the message opcode
  2110. will be set to text or binary based on the current setting of
  2111. the @ref binary (or @ref text) option. The actual payload sent
  2112. may be transformed as per the WebSocket protocol settings.
  2113. This function always writes a complete WebSocket frame (not WebSocket
  2114. message) upon successful completion, so it is well defined to perform
  2115. ping, pong, and close operations in parallel to this operation.
  2116. @param fin `true` if this is the last part of the message.
  2117. @param buffers The buffers containing the message part to send.
  2118. The implementation will make copies of this object
  2119. as needed, but ownership of the underlying memory is not
  2120. transferred. The caller is responsible for ensuring that
  2121. the memory locations pointed to by buffers remains valid
  2122. until the completion handler is called.
  2123. @param handler The completion handler to invoke when the operation
  2124. completes. The implementation takes ownership of the handler by
  2125. performing a decay-copy. The equivalent function signature of
  2126. the handler must be:
  2127. @code
  2128. void handler(
  2129. error_code const& ec, // Result of operation
  2130. std::size_t bytes_transferred // Number of bytes sent from the
  2131. // buffers. If an error occurred,
  2132. // this will be less than the buffer_size.
  2133. );
  2134. @endcode
  2135. If the handler has an associated immediate executor,
  2136. an immediate completion will be dispatched to it.
  2137. Otherwise, the handler will not be invoked from within
  2138. this function. Invocation of the handler will be performed
  2139. by dispatching to the immediate executor. If no
  2140. immediate executor is specified, this is equivalent
  2141. to using `net::post`.
  2142. @par Per-Operation Cancellation
  2143. This asynchronous operation supports cancellation for the following
  2144. net::cancellation_type values:
  2145. @li @c net::cancellation_type::terminal
  2146. @li @c net::cancellation_type::total
  2147. `total` cancellation succeeds if the operation is suspended due to ongoing
  2148. control operations such as a ping/pong.
  2149. `terminal` cancellation succeeds when supported by the underlying stream.
  2150. `terminal` cancellation leaves the stream in an undefined state,
  2151. so that only closing it is guaranteed to succeed.
  2152. */
  2153. template<
  2154. class ConstBufferSequence,
  2155. BOOST_BEAST_ASYNC_TPARAM2 WriteHandler =
  2156. net::default_completion_token_t<
  2157. executor_type>>
  2158. BOOST_BEAST_ASYNC_RESULT2(WriteHandler)
  2159. async_write_some(
  2160. bool fin,
  2161. ConstBufferSequence const& buffers,
  2162. WriteHandler&& handler =
  2163. net::default_completion_token_t<
  2164. executor_type>{});
  2165. private:
  2166. template<class, class> class accept_op;
  2167. template<class> class close_op;
  2168. template<class> class handshake_op;
  2169. template<class> class ping_op;
  2170. template<class> class idle_ping_op;
  2171. template<class, class> class read_some_op;
  2172. template<class, class> class read_op;
  2173. template<class> class response_op;
  2174. template<class, class> class write_some_op;
  2175. template<class, class> class write_op;
  2176. struct run_accept_op;
  2177. struct run_close_op;
  2178. struct run_handshake_op;
  2179. struct run_ping_op;
  2180. struct run_idle_ping_op;
  2181. struct run_read_some_op;
  2182. struct run_read_op;
  2183. struct run_response_op;
  2184. struct run_write_some_op;
  2185. struct run_write_op;
  2186. static void default_decorate_req(request_type&) {}
  2187. static void default_decorate_res(response_type&) {}
  2188. //
  2189. // accept / handshake
  2190. //
  2191. template<class Buffers, class Decorator>
  2192. void
  2193. do_accept(
  2194. Buffers const& buffers,
  2195. Decorator const& decorator,
  2196. error_code& ec);
  2197. template<
  2198. class Body, class Allocator,
  2199. class Decorator>
  2200. void
  2201. do_accept(
  2202. http::request<Body,
  2203. http::basic_fields<Allocator>> const& req,
  2204. Decorator const& decorator,
  2205. error_code& ec);
  2206. template<class RequestDecorator>
  2207. void
  2208. do_handshake(response_type* res_p,
  2209. string_view host, string_view target,
  2210. RequestDecorator const& decorator,
  2211. error_code& ec);
  2212. //
  2213. // fail
  2214. //
  2215. void
  2216. do_fail(
  2217. std::uint16_t code,
  2218. error_code ev,
  2219. error_code& ec);
  2220. };
  2221. /** Manually provide a one-time seed to initialize the PRNG
  2222. This function invokes the specified seed sequence to produce a seed
  2223. suitable for use with the pseudo-random number generator used to
  2224. create masks and perform WebSocket protocol handshakes.
  2225. If a seed is not manually provided, the implementation will
  2226. perform a one-time seed generation using `std::random_device`. This
  2227. function may be used when the application runs in an environment
  2228. where the random device is unreliable or does not provide sufficient
  2229. entropy.
  2230. @par Preconditions
  2231. This function may not be called after any websocket @ref stream objects
  2232. have been constructed.
  2233. @param ss A reference to a `std::seed_seq` which will be used to seed
  2234. the pseudo-random number generator. The seed sequence should have at
  2235. least 256 bits of entropy.
  2236. @see stream::secure_prng
  2237. */
  2238. inline
  2239. void
  2240. seed_prng(std::seed_seq& ss)
  2241. {
  2242. detail::prng_seed(&ss);
  2243. }
  2244. } // websocket
  2245. } // beast
  2246. } // boost
  2247. #include <boost/beast/websocket/impl/stream_impl.hpp> // must be first
  2248. #include <boost/beast/websocket/impl/accept.hpp>
  2249. #include <boost/beast/websocket/impl/close.hpp>
  2250. #include <boost/beast/websocket/impl/handshake.hpp>
  2251. #include <boost/beast/websocket/impl/ping.hpp>
  2252. #include <boost/beast/websocket/impl/read.hpp>
  2253. #include <boost/beast/websocket/impl/stream.hpp>
  2254. #include <boost/beast/websocket/impl/write.hpp>
  2255. #endif