write.hpp 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913
  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_IMPL_WRITE_HPP
  10. #define BOOST_BEAST_WEBSOCKET_IMPL_WRITE_HPP
  11. #include <boost/beast/websocket/detail/mask.hpp>
  12. #include <boost/beast/core/async_base.hpp>
  13. #include <boost/beast/core/buffer_traits.hpp>
  14. #include <boost/beast/core/buffers_cat.hpp>
  15. #include <boost/beast/core/buffers_prefix.hpp>
  16. #include <boost/beast/core/buffers_range.hpp>
  17. #include <boost/beast/core/buffers_suffix.hpp>
  18. #include <boost/beast/core/flat_static_buffer.hpp>
  19. #include <boost/beast/core/stream_traits.hpp>
  20. #include <boost/beast/core/detail/bind_continuation.hpp>
  21. #include <boost/beast/core/detail/clamp.hpp>
  22. #include <boost/beast/core/detail/config.hpp>
  23. #include <boost/beast/websocket/detail/frame.hpp>
  24. #include <boost/beast/websocket/impl/stream_impl.hpp>
  25. #include <boost/asio/coroutine.hpp>
  26. #include <boost/assert.hpp>
  27. #include <boost/config.hpp>
  28. #include <boost/throw_exception.hpp>
  29. #include <algorithm>
  30. #include <memory>
  31. namespace boost {
  32. namespace beast {
  33. namespace websocket {
  34. template<class NextLayer, bool deflateSupported>
  35. template<class Handler, class Buffers>
  36. class stream<NextLayer, deflateSupported>::write_some_op
  37. : public beast::async_base<
  38. Handler, beast::executor_type<stream>>
  39. , public asio::coroutine
  40. {
  41. enum
  42. {
  43. do_nomask_nofrag,
  44. do_nomask_frag,
  45. do_mask_nofrag,
  46. do_mask_frag,
  47. do_deflate
  48. };
  49. boost::weak_ptr<impl_type> wp_;
  50. buffers_suffix<Buffers> cb_;
  51. detail::frame_header fh_;
  52. detail::prepared_key key_;
  53. std::size_t bytes_transferred_ = 0;
  54. std::size_t remain_;
  55. std::size_t in_;
  56. int how_;
  57. bool fin_;
  58. bool more_ = false; // for ubsan
  59. bool cont_ = false;
  60. public:
  61. static constexpr int id = 2; // for soft_mutex
  62. template<class Handler_>
  63. write_some_op(
  64. Handler_&& h,
  65. boost::shared_ptr<impl_type> const& sp,
  66. bool fin,
  67. Buffers const& bs)
  68. : beast::async_base<Handler,
  69. beast::executor_type<stream>>(
  70. std::forward<Handler_>(h),
  71. sp->stream().get_executor())
  72. , wp_(sp)
  73. , cb_(bs)
  74. , fin_(fin)
  75. {
  76. auto& impl = *sp;
  77. // Set up the outgoing frame header
  78. if(! impl.wr_cont)
  79. {
  80. impl.begin_msg(beast::buffer_bytes(bs));
  81. fh_.rsv1 = impl.wr_compress;
  82. }
  83. else
  84. {
  85. fh_.rsv1 = false;
  86. }
  87. fh_.rsv2 = false;
  88. fh_.rsv3 = false;
  89. fh_.op = impl.wr_cont ?
  90. detail::opcode::cont : impl.wr_opcode;
  91. fh_.mask =
  92. impl.role == role_type::client;
  93. // Choose a write algorithm
  94. if(impl.wr_compress)
  95. {
  96. how_ = do_deflate;
  97. }
  98. else if(! fh_.mask)
  99. {
  100. if(! impl.wr_frag)
  101. {
  102. how_ = do_nomask_nofrag;
  103. }
  104. else
  105. {
  106. BOOST_ASSERT(impl.wr_buf_size != 0);
  107. remain_ = beast::buffer_bytes(cb_);
  108. if(remain_ > impl.wr_buf_size)
  109. how_ = do_nomask_frag;
  110. else
  111. how_ = do_nomask_nofrag;
  112. }
  113. }
  114. else
  115. {
  116. if(! impl.wr_frag)
  117. {
  118. how_ = do_mask_nofrag;
  119. }
  120. else
  121. {
  122. BOOST_ASSERT(impl.wr_buf_size != 0);
  123. remain_ = beast::buffer_bytes(cb_);
  124. if(remain_ > impl.wr_buf_size)
  125. how_ = do_mask_frag;
  126. else
  127. how_ = do_mask_nofrag;
  128. }
  129. }
  130. (*this)({}, 0, false);
  131. }
  132. void operator()(
  133. error_code ec = {},
  134. std::size_t bytes_transferred = 0,
  135. bool cont = true);
  136. };
  137. template<class NextLayer, bool deflateSupported>
  138. template<class Handler, class Buffers>
  139. void
  140. stream<NextLayer, deflateSupported>::
  141. write_some_op<Handler, Buffers>::
  142. operator()(
  143. error_code ec,
  144. std::size_t bytes_transferred,
  145. bool cont)
  146. {
  147. using beast::detail::clamp;
  148. std::size_t n;
  149. net::mutable_buffer b;
  150. auto sp = wp_.lock();
  151. if(! sp)
  152. {
  153. BOOST_BEAST_ASSIGN_EC(ec, net::error::operation_aborted);
  154. bytes_transferred_ = 0;
  155. return this->complete(cont, ec, bytes_transferred_);
  156. }
  157. auto& impl = *sp;
  158. BOOST_ASIO_CORO_REENTER(*this)
  159. {
  160. // Acquire the write lock
  161. if(! impl.wr_block.try_lock(this))
  162. {
  163. do_suspend:
  164. BOOST_ASIO_CORO_YIELD
  165. {
  166. BOOST_ASIO_HANDLER_LOCATION((
  167. __FILE__, __LINE__,
  168. fin_ ?
  169. "websocket::async_write" :
  170. "websocket::async_write_some"
  171. ));
  172. this->set_allowed_cancellation(net::cancellation_type::all);
  173. impl.op_wr.emplace(std::move(*this),
  174. net::cancellation_type::all);
  175. }
  176. if (ec)
  177. return this->complete(cont, ec, bytes_transferred_);
  178. this->set_allowed_cancellation(net::cancellation_type::terminal);
  179. impl.wr_block.lock(this);
  180. BOOST_ASIO_CORO_YIELD
  181. {
  182. BOOST_ASIO_HANDLER_LOCATION((
  183. __FILE__, __LINE__,
  184. fin_ ?
  185. "websocket::async_write" :
  186. "websocket::async_write_some"
  187. ));
  188. const auto ex = this->get_immediate_executor();
  189. net::dispatch(ex, std::move(*this));
  190. }
  191. BOOST_ASSERT(impl.wr_block.is_locked(this));
  192. }
  193. if(impl.check_stop_now(ec))
  194. goto upcall;
  195. //------------------------------------------------------------------
  196. if(how_ == do_nomask_nofrag)
  197. {
  198. // send a single frame
  199. fh_.fin = fin_;
  200. fh_.len = beast::buffer_bytes(cb_);
  201. impl.wr_fb.clear();
  202. detail::write<flat_static_buffer_base>(
  203. impl.wr_fb, fh_);
  204. impl.wr_cont = ! fin_;
  205. BOOST_ASIO_CORO_YIELD
  206. {
  207. BOOST_ASIO_HANDLER_LOCATION((
  208. __FILE__, __LINE__,
  209. fin_ ?
  210. "websocket::async_write" :
  211. "websocket::async_write_some"
  212. ));
  213. net::async_write(impl.stream(),
  214. buffers_cat(
  215. net::const_buffer(impl.wr_fb.data()),
  216. net::const_buffer(0, 0),
  217. cb_,
  218. buffers_prefix(0, cb_)
  219. ),
  220. beast::detail::bind_continuation(std::move(*this)));
  221. }
  222. bytes_transferred_ += clamp(fh_.len);
  223. if(impl.check_stop_now(ec))
  224. goto upcall;
  225. goto upcall;
  226. }
  227. //------------------------------------------------------------------
  228. if(how_ == do_nomask_frag)
  229. {
  230. // send multiple frames
  231. for(;;)
  232. {
  233. n = clamp(remain_, impl.wr_buf_size);
  234. fh_.len = n;
  235. remain_ -= n;
  236. fh_.fin = fin_ ? remain_ == 0 : false;
  237. impl.wr_fb.clear();
  238. detail::write<flat_static_buffer_base>(
  239. impl.wr_fb, fh_);
  240. impl.wr_cont = ! fin_;
  241. // Send frame
  242. BOOST_ASIO_CORO_YIELD
  243. {
  244. BOOST_ASIO_HANDLER_LOCATION((
  245. __FILE__, __LINE__,
  246. fin_ ?
  247. "websocket::async_write" :
  248. "websocket::async_write_some"
  249. ));
  250. buffers_suffix<Buffers> empty_cb(cb_);
  251. empty_cb.consume(~std::size_t(0));
  252. net::async_write(impl.stream(),
  253. buffers_cat(
  254. net::const_buffer(impl.wr_fb.data()),
  255. net::const_buffer(0, 0),
  256. empty_cb,
  257. buffers_prefix(clamp(fh_.len), cb_)
  258. ),
  259. beast::detail::bind_continuation(std::move(*this)));
  260. }
  261. n = clamp(fh_.len); // restore `n` on yield
  262. bytes_transferred_ += n;
  263. if(impl.check_stop_now(ec))
  264. goto upcall;
  265. if(remain_ == 0)
  266. break;
  267. cb_.consume(n);
  268. fh_.op = detail::opcode::cont;
  269. // Give up the write lock in between each frame
  270. // so that outgoing control frames might be sent.
  271. impl.wr_block.unlock(this);
  272. if( impl.op_close.maybe_invoke()
  273. || impl.op_idle_ping.maybe_invoke()
  274. || impl.op_rd.maybe_invoke()
  275. || impl.op_ping.maybe_invoke())
  276. {
  277. BOOST_ASSERT(impl.wr_block.is_locked());
  278. goto do_suspend;
  279. }
  280. impl.wr_block.lock(this);
  281. }
  282. goto upcall;
  283. }
  284. //------------------------------------------------------------------
  285. if(how_ == do_mask_nofrag)
  286. {
  287. // send a single frame using multiple writes
  288. remain_ = beast::buffer_bytes(cb_);
  289. fh_.fin = fin_;
  290. fh_.len = remain_;
  291. fh_.key = impl.create_mask();
  292. detail::prepare_key(key_, fh_.key);
  293. impl.wr_fb.clear();
  294. detail::write<flat_static_buffer_base>(
  295. impl.wr_fb, fh_);
  296. n = clamp(remain_, impl.wr_buf_size);
  297. net::buffer_copy(net::buffer(
  298. impl.wr_buf.get(), n), cb_);
  299. detail::mask_inplace(net::buffer(
  300. impl.wr_buf.get(), n), key_);
  301. remain_ -= n;
  302. impl.wr_cont = ! fin_;
  303. // write frame header and some payload
  304. BOOST_ASIO_CORO_YIELD
  305. {
  306. BOOST_ASIO_HANDLER_LOCATION((
  307. __FILE__, __LINE__,
  308. fin_ ?
  309. "websocket::async_write" :
  310. "websocket::async_write_some"
  311. ));
  312. buffers_suffix<Buffers> empty_cb(cb_);
  313. empty_cb.consume(~std::size_t(0));
  314. net::async_write(impl.stream(),
  315. buffers_cat(
  316. net::const_buffer(impl.wr_fb.data()),
  317. net::const_buffer(net::buffer(impl.wr_buf.get(), n)),
  318. empty_cb,
  319. buffers_prefix(0, empty_cb)
  320. ),
  321. beast::detail::bind_continuation(std::move(*this)));
  322. }
  323. // VFALCO What about consuming the buffer on error?
  324. if(bytes_transferred > impl.wr_fb.size())
  325. bytes_transferred_ += bytes_transferred - impl.wr_fb.size();
  326. if(impl.check_stop_now(ec))
  327. goto upcall;
  328. while(remain_ > 0)
  329. {
  330. cb_.consume(impl.wr_buf_size);
  331. n = clamp(remain_, impl.wr_buf_size);
  332. net::buffer_copy(net::buffer(
  333. impl.wr_buf.get(), n), cb_);
  334. detail::mask_inplace(net::buffer(
  335. impl.wr_buf.get(), n), key_);
  336. remain_ -= n;
  337. // write more payload
  338. BOOST_ASIO_CORO_YIELD
  339. {
  340. BOOST_ASIO_HANDLER_LOCATION((
  341. __FILE__, __LINE__,
  342. fin_ ?
  343. "websocket::async_write" :
  344. "websocket::async_write_some"
  345. ));
  346. buffers_suffix<Buffers> empty_cb(cb_);
  347. empty_cb.consume(~std::size_t(0));
  348. net::async_write(impl.stream(),
  349. buffers_cat(
  350. net::const_buffer(0, 0),
  351. net::const_buffer(net::buffer(impl.wr_buf.get(), n)),
  352. empty_cb,
  353. buffers_prefix(0, empty_cb)
  354. ),
  355. beast::detail::bind_continuation(std::move(*this)));
  356. }
  357. bytes_transferred_ += bytes_transferred;
  358. if(impl.check_stop_now(ec))
  359. goto upcall;
  360. }
  361. goto upcall;
  362. }
  363. //------------------------------------------------------------------
  364. if(how_ == do_mask_frag)
  365. {
  366. // send multiple frames
  367. for(;;)
  368. {
  369. n = clamp(remain_, impl.wr_buf_size);
  370. remain_ -= n;
  371. fh_.len = n;
  372. fh_.key = impl.create_mask();
  373. fh_.fin = fin_ ? remain_ == 0 : false;
  374. detail::prepare_key(key_, fh_.key);
  375. net::buffer_copy(net::buffer(
  376. impl.wr_buf.get(), n), cb_);
  377. detail::mask_inplace(net::buffer(
  378. impl.wr_buf.get(), n), key_);
  379. impl.wr_fb.clear();
  380. detail::write<flat_static_buffer_base>(
  381. impl.wr_fb, fh_);
  382. impl.wr_cont = ! fin_;
  383. // Send frame
  384. BOOST_ASIO_CORO_YIELD
  385. {
  386. BOOST_ASIO_HANDLER_LOCATION((
  387. __FILE__, __LINE__,
  388. fin_ ?
  389. "websocket::async_write" :
  390. "websocket::async_write_some"
  391. ));
  392. buffers_suffix<Buffers> empty_cb(cb_);
  393. empty_cb.consume(~std::size_t(0));
  394. net::async_write(impl.stream(),
  395. buffers_cat(
  396. net::const_buffer(impl.wr_fb.data()),
  397. net::const_buffer(net::buffer(impl.wr_buf.get(), n)),
  398. empty_cb,
  399. buffers_prefix(0, empty_cb)
  400. ),
  401. beast::detail::bind_continuation(std::move(*this)));
  402. }
  403. if(bytes_transferred > impl.wr_fb.size())
  404. n = bytes_transferred - impl.wr_fb.size();
  405. else
  406. n = 0;
  407. bytes_transferred_ += n;
  408. if(impl.check_stop_now(ec))
  409. goto upcall;
  410. if(remain_ == 0)
  411. break;
  412. cb_.consume(n);
  413. fh_.op = detail::opcode::cont;
  414. // Give up the write lock in between each frame
  415. // so that outgoing control frames might be sent.
  416. impl.wr_block.unlock(this);
  417. if( impl.op_close.maybe_invoke()
  418. || impl.op_idle_ping.maybe_invoke()
  419. || impl.op_rd.maybe_invoke()
  420. || impl.op_ping.maybe_invoke())
  421. {
  422. BOOST_ASSERT(impl.wr_block.is_locked());
  423. goto do_suspend;
  424. }
  425. impl.wr_block.lock(this);
  426. }
  427. goto upcall;
  428. }
  429. //------------------------------------------------------------------
  430. if(how_ == do_deflate)
  431. {
  432. // send compressed frames
  433. for(;;)
  434. {
  435. b = net::buffer(impl.wr_buf.get(),
  436. impl.wr_buf_size);
  437. more_ = impl.deflate(b, cb_, fin_, in_, ec);
  438. if(impl.check_stop_now(ec))
  439. goto upcall;
  440. n = beast::buffer_bytes(b);
  441. if(n == 0)
  442. {
  443. // The input was consumed, but there is
  444. // no output due to compression latency.
  445. BOOST_ASSERT(! fin_);
  446. BOOST_ASSERT(beast::buffer_bytes(cb_) == 0);
  447. goto upcall;
  448. }
  449. if(fh_.mask)
  450. {
  451. fh_.key = impl.create_mask();
  452. detail::prepared_key key;
  453. detail::prepare_key(key, fh_.key);
  454. detail::mask_inplace(b, key);
  455. }
  456. fh_.fin = ! more_;
  457. fh_.len = n;
  458. impl.wr_fb.clear();
  459. detail::write<
  460. flat_static_buffer_base>(impl.wr_fb, fh_);
  461. impl.wr_cont = ! fin_;
  462. // Send frame
  463. BOOST_ASIO_CORO_YIELD
  464. {
  465. BOOST_ASIO_HANDLER_LOCATION((
  466. __FILE__, __LINE__,
  467. fin_ ?
  468. "websocket::async_write" :
  469. "websocket::async_write_some"
  470. ));
  471. buffers_suffix<Buffers> empty_cb(cb_);
  472. empty_cb.consume(~std::size_t(0));
  473. net::async_write(impl.stream(),
  474. buffers_cat(
  475. net::const_buffer(impl.wr_fb.data()),
  476. net::const_buffer(b),
  477. empty_cb,
  478. buffers_prefix(0, empty_cb)
  479. ),
  480. beast::detail::bind_continuation(std::move(*this)));
  481. }
  482. bytes_transferred_ += in_;
  483. if(impl.check_stop_now(ec))
  484. goto upcall;
  485. if(more_)
  486. {
  487. fh_.op = detail::opcode::cont;
  488. fh_.rsv1 = false;
  489. // Give up the write lock in between each frame
  490. // so that outgoing control frames might be sent.
  491. impl.wr_block.unlock(this);
  492. if( impl.op_close.maybe_invoke()
  493. || impl.op_idle_ping.maybe_invoke()
  494. || impl.op_rd.maybe_invoke()
  495. || impl.op_ping.maybe_invoke())
  496. {
  497. BOOST_ASSERT(impl.wr_block.is_locked());
  498. goto do_suspend;
  499. }
  500. impl.wr_block.lock(this);
  501. }
  502. else
  503. {
  504. if(fh_.fin)
  505. impl.do_context_takeover_write(impl.role);
  506. goto upcall;
  507. }
  508. }
  509. }
  510. //--------------------------------------------------------------------------
  511. upcall:
  512. impl.wr_block.unlock(this);
  513. impl.op_close.maybe_invoke()
  514. || impl.op_idle_ping.maybe_invoke()
  515. || impl.op_rd.maybe_invoke()
  516. || impl.op_ping.maybe_invoke();
  517. this->complete(cont, ec, bytes_transferred_);
  518. }
  519. }
  520. template<class NextLayer, bool deflateSupported>
  521. struct stream<NextLayer, deflateSupported>::
  522. run_write_some_op
  523. {
  524. boost::shared_ptr<impl_type> const& self;
  525. using executor_type = typename stream::executor_type;
  526. executor_type
  527. get_executor() const noexcept
  528. {
  529. return self->stream().get_executor();
  530. }
  531. template<
  532. class WriteHandler,
  533. class ConstBufferSequence>
  534. void
  535. operator()(
  536. WriteHandler&& h,
  537. bool fin,
  538. ConstBufferSequence const& b)
  539. {
  540. // If you get an error on the following line it means
  541. // that your handler does not meet the documented type
  542. // requirements for the handler.
  543. static_assert(
  544. beast::detail::is_invocable<WriteHandler,
  545. void(error_code, std::size_t)>::value,
  546. "WriteHandler type requirements not met");
  547. write_some_op<
  548. typename std::decay<WriteHandler>::type,
  549. ConstBufferSequence>(
  550. std::forward<WriteHandler>(h),
  551. self,
  552. fin,
  553. b);
  554. }
  555. };
  556. //------------------------------------------------------------------------------
  557. template<class NextLayer, bool deflateSupported>
  558. template<class ConstBufferSequence>
  559. std::size_t
  560. stream<NextLayer, deflateSupported>::
  561. write_some(bool fin, ConstBufferSequence const& buffers)
  562. {
  563. static_assert(is_sync_stream<next_layer_type>::value,
  564. "SyncStream type requirements not met");
  565. static_assert(net::is_const_buffer_sequence<
  566. ConstBufferSequence>::value,
  567. "ConstBufferSequence type requirements not met");
  568. error_code ec;
  569. auto const bytes_transferred =
  570. write_some(fin, buffers, ec);
  571. if(ec)
  572. BOOST_THROW_EXCEPTION(system_error{ec});
  573. return bytes_transferred;
  574. }
  575. template<class NextLayer, bool deflateSupported>
  576. template<class ConstBufferSequence>
  577. std::size_t
  578. stream<NextLayer, deflateSupported>::
  579. write_some(bool fin,
  580. ConstBufferSequence const& buffers, error_code& ec)
  581. {
  582. static_assert(is_sync_stream<next_layer_type>::value,
  583. "SyncStream type requirements not met");
  584. static_assert(net::is_const_buffer_sequence<
  585. ConstBufferSequence>::value,
  586. "ConstBufferSequence type requirements not met");
  587. using beast::detail::clamp;
  588. auto& impl = *impl_;
  589. std::size_t bytes_transferred = 0;
  590. ec = {};
  591. if(impl.check_stop_now(ec))
  592. return bytes_transferred;
  593. detail::frame_header fh;
  594. if(! impl.wr_cont)
  595. {
  596. impl.begin_msg(beast::buffer_bytes(buffers));
  597. fh.rsv1 = impl.wr_compress;
  598. }
  599. else
  600. {
  601. fh.rsv1 = false;
  602. }
  603. fh.rsv2 = false;
  604. fh.rsv3 = false;
  605. fh.op = impl.wr_cont ?
  606. detail::opcode::cont : impl.wr_opcode;
  607. fh.mask = impl.role == role_type::client;
  608. auto remain = beast::buffer_bytes(buffers);
  609. if(impl.wr_compress)
  610. {
  611. buffers_suffix<
  612. ConstBufferSequence> cb(buffers);
  613. for(;;)
  614. {
  615. auto b = net::buffer(
  616. impl.wr_buf.get(), impl.wr_buf_size);
  617. auto const more = impl.deflate(
  618. b, cb, fin, bytes_transferred, ec);
  619. if(impl.check_stop_now(ec))
  620. return bytes_transferred;
  621. auto const n = beast::buffer_bytes(b);
  622. if(n == 0)
  623. {
  624. // The input was consumed, but there
  625. // is no output due to compression
  626. // latency.
  627. BOOST_ASSERT(! fin);
  628. BOOST_ASSERT(beast::buffer_bytes(cb) == 0);
  629. fh.fin = false;
  630. break;
  631. }
  632. if(fh.mask)
  633. {
  634. fh.key = this->impl_->create_mask();
  635. detail::prepared_key key;
  636. detail::prepare_key(key, fh.key);
  637. detail::mask_inplace(b, key);
  638. }
  639. fh.fin = ! more;
  640. fh.len = n;
  641. detail::fh_buffer fh_buf;
  642. detail::write<
  643. flat_static_buffer_base>(fh_buf, fh);
  644. impl.wr_cont = ! fin;
  645. net::write(impl.stream(),
  646. buffers_cat(fh_buf.data(), b), ec);
  647. if(impl.check_stop_now(ec))
  648. return bytes_transferred;
  649. if(! more)
  650. break;
  651. fh.op = detail::opcode::cont;
  652. fh.rsv1 = false;
  653. }
  654. if(fh.fin)
  655. impl.do_context_takeover_write(impl.role);
  656. }
  657. else if(! fh.mask)
  658. {
  659. if(! impl.wr_frag)
  660. {
  661. // no mask, no autofrag
  662. fh.fin = fin;
  663. fh.len = remain;
  664. detail::fh_buffer fh_buf;
  665. detail::write<
  666. flat_static_buffer_base>(fh_buf, fh);
  667. impl.wr_cont = ! fin;
  668. net::write(impl.stream(),
  669. buffers_cat(fh_buf.data(), buffers), ec);
  670. if(impl.check_stop_now(ec))
  671. return bytes_transferred;
  672. bytes_transferred += remain;
  673. }
  674. else
  675. {
  676. // no mask, autofrag
  677. BOOST_ASSERT(impl.wr_buf_size != 0);
  678. buffers_suffix<
  679. ConstBufferSequence> cb{buffers};
  680. for(;;)
  681. {
  682. auto const n = clamp(remain, impl.wr_buf_size);
  683. remain -= n;
  684. fh.len = n;
  685. fh.fin = fin ? remain == 0 : false;
  686. detail::fh_buffer fh_buf;
  687. detail::write<
  688. flat_static_buffer_base>(fh_buf, fh);
  689. impl.wr_cont = ! fin;
  690. net::write(impl.stream(),
  691. beast::buffers_cat(fh_buf.data(),
  692. beast::buffers_prefix(n, cb)), ec);
  693. bytes_transferred += n;
  694. if(impl.check_stop_now(ec))
  695. return bytes_transferred;
  696. if(remain == 0)
  697. break;
  698. fh.op = detail::opcode::cont;
  699. cb.consume(n);
  700. }
  701. }
  702. }
  703. else if(! impl.wr_frag)
  704. {
  705. // mask, no autofrag
  706. fh.fin = fin;
  707. fh.len = remain;
  708. fh.key = this->impl_->create_mask();
  709. detail::prepared_key key;
  710. detail::prepare_key(key, fh.key);
  711. detail::fh_buffer fh_buf;
  712. detail::write<
  713. flat_static_buffer_base>(fh_buf, fh);
  714. buffers_suffix<
  715. ConstBufferSequence> cb{buffers};
  716. {
  717. auto const n =
  718. clamp(remain, impl.wr_buf_size);
  719. auto const b =
  720. net::buffer(impl.wr_buf.get(), n);
  721. net::buffer_copy(b, cb);
  722. cb.consume(n);
  723. remain -= n;
  724. detail::mask_inplace(b, key);
  725. impl.wr_cont = ! fin;
  726. net::write(impl.stream(),
  727. buffers_cat(fh_buf.data(), b), ec);
  728. bytes_transferred += n;
  729. if(impl.check_stop_now(ec))
  730. return bytes_transferred;
  731. }
  732. while(remain > 0)
  733. {
  734. auto const n =
  735. clamp(remain, impl.wr_buf_size);
  736. auto const b =
  737. net::buffer(impl.wr_buf.get(), n);
  738. net::buffer_copy(b, cb);
  739. cb.consume(n);
  740. remain -= n;
  741. detail::mask_inplace(b, key);
  742. net::write(impl.stream(), b, ec);
  743. bytes_transferred += n;
  744. if(impl.check_stop_now(ec))
  745. return bytes_transferred;
  746. }
  747. }
  748. else
  749. {
  750. // mask, autofrag
  751. BOOST_ASSERT(impl.wr_buf_size != 0);
  752. buffers_suffix<
  753. ConstBufferSequence> cb(buffers);
  754. for(;;)
  755. {
  756. fh.key = this->impl_->create_mask();
  757. detail::prepared_key key;
  758. detail::prepare_key(key, fh.key);
  759. auto const n =
  760. clamp(remain, impl.wr_buf_size);
  761. auto const b =
  762. net::buffer(impl.wr_buf.get(), n);
  763. net::buffer_copy(b, cb);
  764. detail::mask_inplace(b, key);
  765. fh.len = n;
  766. remain -= n;
  767. fh.fin = fin ? remain == 0 : false;
  768. impl.wr_cont = ! fh.fin;
  769. detail::fh_buffer fh_buf;
  770. detail::write<
  771. flat_static_buffer_base>(fh_buf, fh);
  772. net::write(impl.stream(),
  773. buffers_cat(fh_buf.data(), b), ec);
  774. bytes_transferred += n;
  775. if(impl.check_stop_now(ec))
  776. return bytes_transferred;
  777. if(remain == 0)
  778. break;
  779. fh.op = detail::opcode::cont;
  780. cb.consume(n);
  781. }
  782. }
  783. return bytes_transferred;
  784. }
  785. template<class NextLayer, bool deflateSupported>
  786. template<class ConstBufferSequence, BOOST_BEAST_ASYNC_TPARAM2 WriteHandler>
  787. BOOST_BEAST_ASYNC_RESULT2(WriteHandler)
  788. stream<NextLayer, deflateSupported>::
  789. async_write_some(bool fin,
  790. ConstBufferSequence const& bs, WriteHandler&& handler)
  791. {
  792. static_assert(is_async_stream<next_layer_type>::value,
  793. "AsyncStream type requirements not met");
  794. static_assert(net::is_const_buffer_sequence<
  795. ConstBufferSequence>::value,
  796. "ConstBufferSequence type requirements not met");
  797. return net::async_initiate<
  798. WriteHandler,
  799. void(error_code, std::size_t)>(
  800. run_write_some_op{impl_},
  801. handler,
  802. fin,
  803. bs);
  804. }
  805. //------------------------------------------------------------------------------
  806. template<class NextLayer, bool deflateSupported>
  807. template<class ConstBufferSequence>
  808. std::size_t
  809. stream<NextLayer, deflateSupported>::
  810. write(ConstBufferSequence const& buffers)
  811. {
  812. static_assert(is_sync_stream<next_layer_type>::value,
  813. "SyncStream type requirements not met");
  814. static_assert(net::is_const_buffer_sequence<
  815. ConstBufferSequence>::value,
  816. "ConstBufferSequence type requirements not met");
  817. error_code ec;
  818. auto const bytes_transferred = write(buffers, ec);
  819. if(ec)
  820. BOOST_THROW_EXCEPTION(system_error{ec});
  821. return bytes_transferred;
  822. }
  823. template<class NextLayer, bool deflateSupported>
  824. template<class ConstBufferSequence>
  825. std::size_t
  826. stream<NextLayer, deflateSupported>::
  827. write(ConstBufferSequence const& buffers, error_code& ec)
  828. {
  829. static_assert(is_sync_stream<next_layer_type>::value,
  830. "SyncStream type requirements not met");
  831. static_assert(net::is_const_buffer_sequence<
  832. ConstBufferSequence>::value,
  833. "ConstBufferSequence type requirements not met");
  834. return write_some(true, buffers, ec);
  835. }
  836. template<class NextLayer, bool deflateSupported>
  837. template<class ConstBufferSequence, BOOST_BEAST_ASYNC_TPARAM2 WriteHandler>
  838. BOOST_BEAST_ASYNC_RESULT2(WriteHandler)
  839. stream<NextLayer, deflateSupported>::
  840. async_write(
  841. ConstBufferSequence const& bs, WriteHandler&& handler)
  842. {
  843. static_assert(is_async_stream<next_layer_type>::value,
  844. "AsyncStream type requirements not met");
  845. static_assert(net::is_const_buffer_sequence<
  846. ConstBufferSequence>::value,
  847. "ConstBufferSequence type requirements not met");
  848. return net::async_initiate<
  849. WriteHandler,
  850. void(error_code, std::size_t)>(
  851. run_write_some_op{impl_},
  852. handler,
  853. true,
  854. bs);
  855. }
  856. } // websocket
  857. } // beast
  858. } // boost
  859. #endif