stream.hpp 104 KB

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