stream.hpp 114 KB

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