stream.hpp 113 KB

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