ping.hpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367
  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_PING_HPP
  10. #define BOOST_BEAST_WEBSOCKET_IMPL_PING_HPP
  11. #include <boost/beast/core/async_base.hpp>
  12. #include <boost/beast/core/bind_handler.hpp>
  13. #include <boost/beast/core/stream_traits.hpp>
  14. #include <boost/beast/core/detail/bind_continuation.hpp>
  15. #include <boost/beast/websocket/detail/frame.hpp>
  16. #include <boost/beast/websocket/impl/stream_impl.hpp>
  17. #include <boost/asio/coroutine.hpp>
  18. #include <boost/asio/post.hpp>
  19. #include <boost/throw_exception.hpp>
  20. #include <memory>
  21. namespace boost {
  22. namespace beast {
  23. namespace websocket {
  24. /*
  25. This composed operation handles sending ping and pong frames.
  26. It only sends the frames it does not make attempts to read
  27. any frame data.
  28. */
  29. template<class NextLayer, bool deflateSupported>
  30. template<class Handler>
  31. class stream<NextLayer, deflateSupported>::ping_op
  32. : public beast::stable_async_base<
  33. Handler, beast::executor_type<stream>>
  34. , public asio::coroutine
  35. {
  36. boost::weak_ptr<impl_type> wp_;
  37. detail::frame_buffer& fb_;
  38. public:
  39. static constexpr int id = 3; // for soft_mutex
  40. template<class Handler_>
  41. ping_op(
  42. Handler_&& h,
  43. boost::shared_ptr<impl_type> const& sp,
  44. detail::opcode op,
  45. ping_data const& payload)
  46. : stable_async_base<Handler,
  47. beast::executor_type<stream>>(
  48. std::forward<Handler_>(h),
  49. sp->stream().get_executor())
  50. , wp_(sp)
  51. , fb_(beast::allocate_stable<
  52. detail::frame_buffer>(*this))
  53. {
  54. // Serialize the ping or pong frame
  55. sp->template write_ping<
  56. flat_static_buffer_base>(fb_, op, payload);
  57. (*this)({}, 0, false);
  58. }
  59. void operator()(
  60. error_code ec = {},
  61. std::size_t bytes_transferred = 0,
  62. bool cont = true)
  63. {
  64. boost::ignore_unused(bytes_transferred);
  65. auto sp = wp_.lock();
  66. if(! sp)
  67. {
  68. BOOST_BEAST_ASSIGN_EC(ec, net::error::operation_aborted);
  69. return this->complete(cont, ec);
  70. }
  71. auto& impl = *sp;
  72. BOOST_ASIO_CORO_REENTER(*this)
  73. {
  74. // Acquire the write lock
  75. if(! impl.wr_block.try_lock(this))
  76. {
  77. BOOST_ASIO_CORO_YIELD
  78. {
  79. BOOST_ASIO_HANDLER_LOCATION((
  80. __FILE__, __LINE__,
  81. "websocket::async_ping"));
  82. this->set_allowed_cancellation(net::cancellation_type::all);
  83. impl.op_ping.emplace(std::move(*this), net::cancellation_type::all);
  84. }
  85. if (ec)
  86. return this->complete(cont, ec);
  87. this->set_allowed_cancellation(net::cancellation_type::terminal);
  88. impl.wr_block.lock(this);
  89. BOOST_ASIO_CORO_YIELD
  90. {
  91. BOOST_ASIO_HANDLER_LOCATION((
  92. __FILE__, __LINE__,
  93. "websocket::async_ping"));
  94. net::post(sp->stream().get_executor(), std::move(*this));
  95. }
  96. BOOST_ASSERT(impl.wr_block.is_locked(this));
  97. }
  98. if(impl.check_stop_now(ec))
  99. goto upcall;
  100. // Send ping frame
  101. BOOST_ASIO_CORO_YIELD
  102. {
  103. BOOST_ASIO_HANDLER_LOCATION((
  104. __FILE__, __LINE__,
  105. "websocket::async_ping"));
  106. net::async_write(impl.stream(), fb_.data(),
  107. beast::detail::bind_continuation(std::move(*this)));
  108. }
  109. if(impl.check_stop_now(ec))
  110. goto upcall;
  111. upcall:
  112. impl.wr_block.unlock(this);
  113. impl.op_close.maybe_invoke()
  114. || impl.op_idle_ping.maybe_invoke()
  115. || impl.op_rd.maybe_invoke()
  116. || impl.op_wr.maybe_invoke();
  117. this->complete(cont, ec);
  118. }
  119. }
  120. };
  121. //------------------------------------------------------------------------------
  122. // sends the idle ping
  123. template<class NextLayer, bool deflateSupported>
  124. template<class Executor>
  125. class stream<NextLayer, deflateSupported>::idle_ping_op
  126. : public asio::coroutine
  127. , public boost::empty_value<Executor>
  128. {
  129. boost::weak_ptr<impl_type> wp_;
  130. std::unique_ptr<detail::frame_buffer> fb_;
  131. public:
  132. static constexpr int id = 4; // for soft_mutex
  133. using executor_type = Executor;
  134. executor_type
  135. get_executor() const noexcept
  136. {
  137. return this->get();
  138. }
  139. idle_ping_op(
  140. boost::shared_ptr<impl_type> const& sp,
  141. Executor const& ex)
  142. : boost::empty_value<Executor>(
  143. boost::empty_init_t{}, ex)
  144. , wp_(sp)
  145. , fb_(new detail::frame_buffer)
  146. {
  147. if(! sp->idle_pinging)
  148. {
  149. // Create the ping frame
  150. ping_data payload; // empty for now
  151. sp->template write_ping<
  152. flat_static_buffer_base>(*fb_,
  153. detail::opcode::ping, payload);
  154. sp->idle_pinging = true;
  155. (*this)({}, 0);
  156. }
  157. else
  158. {
  159. // if we are already in the middle of sending
  160. // an idle ping, don't bother sending another.
  161. }
  162. }
  163. void operator()(
  164. error_code ec = {},
  165. std::size_t bytes_transferred = 0)
  166. {
  167. boost::ignore_unused(bytes_transferred);
  168. auto sp = wp_.lock();
  169. if(! sp)
  170. return;
  171. auto& impl = *sp;
  172. BOOST_ASIO_CORO_REENTER(*this)
  173. {
  174. // Acquire the write lock
  175. if(! impl.wr_block.try_lock(this))
  176. {
  177. BOOST_ASIO_CORO_YIELD
  178. {
  179. BOOST_ASIO_HANDLER_LOCATION((
  180. __FILE__, __LINE__,
  181. "websocket::async_ping"));
  182. impl.op_idle_ping.emplace(std::move(*this));
  183. }
  184. impl.wr_block.lock(this);
  185. BOOST_ASIO_CORO_YIELD
  186. {
  187. BOOST_ASIO_HANDLER_LOCATION((
  188. __FILE__, __LINE__,
  189. "websocket::async_ping"));
  190. net::post(sp->stream().get_executor(), std::move(*this));
  191. }
  192. BOOST_ASSERT(impl.wr_block.is_locked(this));
  193. }
  194. if(impl.check_stop_now(ec))
  195. goto upcall;
  196. // Send ping frame
  197. BOOST_ASIO_CORO_YIELD
  198. {
  199. BOOST_ASIO_HANDLER_LOCATION((
  200. __FILE__, __LINE__,
  201. "websocket::async_ping"));
  202. net::async_write(impl.stream(), fb_->data(),
  203. std::move(*this));
  204. }
  205. if(impl.check_stop_now(ec))
  206. goto upcall;
  207. upcall:
  208. BOOST_ASSERT(sp->idle_pinging);
  209. sp->idle_pinging = false;
  210. impl.wr_block.unlock(this);
  211. impl.op_close.maybe_invoke()
  212. || impl.op_ping.maybe_invoke()
  213. || impl.op_rd.maybe_invoke()
  214. || impl.op_wr.maybe_invoke();
  215. }
  216. }
  217. };
  218. template<class NextLayer, bool deflateSupported>
  219. struct stream<NextLayer, deflateSupported>::
  220. run_ping_op
  221. {
  222. template<class WriteHandler>
  223. void
  224. operator()(
  225. WriteHandler&& h,
  226. boost::shared_ptr<impl_type> const& sp,
  227. detail::opcode op,
  228. ping_data const& p)
  229. {
  230. // If you get an error on the following line it means
  231. // that your handler does not meet the documented type
  232. // requirements for the handler.
  233. static_assert(
  234. beast::detail::is_invocable<WriteHandler,
  235. void(error_code)>::value,
  236. "WriteHandler type requirements not met");
  237. ping_op<
  238. typename std::decay<WriteHandler>::type>(
  239. std::forward<WriteHandler>(h),
  240. sp,
  241. op,
  242. p);
  243. }
  244. };
  245. //------------------------------------------------------------------------------
  246. template<class NextLayer, bool deflateSupported>
  247. void
  248. stream<NextLayer, deflateSupported>::
  249. ping(ping_data const& payload)
  250. {
  251. error_code ec;
  252. ping(payload, ec);
  253. if(ec)
  254. BOOST_THROW_EXCEPTION(system_error{ec});
  255. }
  256. template<class NextLayer, bool deflateSupported>
  257. void
  258. stream<NextLayer, deflateSupported>::
  259. ping(ping_data const& payload, error_code& ec)
  260. {
  261. if(impl_->check_stop_now(ec))
  262. return;
  263. detail::frame_buffer fb;
  264. impl_->template write_ping<flat_static_buffer_base>(
  265. fb, detail::opcode::ping, payload);
  266. net::write(impl_->stream(), fb.data(), ec);
  267. if(impl_->check_stop_now(ec))
  268. return;
  269. }
  270. template<class NextLayer, bool deflateSupported>
  271. void
  272. stream<NextLayer, deflateSupported>::
  273. pong(ping_data const& payload)
  274. {
  275. error_code ec;
  276. pong(payload, ec);
  277. if(ec)
  278. BOOST_THROW_EXCEPTION(system_error{ec});
  279. }
  280. template<class NextLayer, bool deflateSupported>
  281. void
  282. stream<NextLayer, deflateSupported>::
  283. pong(ping_data const& payload, error_code& ec)
  284. {
  285. if(impl_->check_stop_now(ec))
  286. return;
  287. detail::frame_buffer fb;
  288. impl_->template write_ping<flat_static_buffer_base>(
  289. fb, detail::opcode::pong, payload);
  290. net::write(impl_->stream(), fb.data(), ec);
  291. if(impl_->check_stop_now(ec))
  292. return;
  293. }
  294. template<class NextLayer, bool deflateSupported>
  295. template<BOOST_BEAST_ASYNC_TPARAM1 WriteHandler>
  296. BOOST_BEAST_ASYNC_RESULT1(WriteHandler)
  297. stream<NextLayer, deflateSupported>::
  298. async_ping(ping_data const& payload, WriteHandler&& handler)
  299. {
  300. static_assert(is_async_stream<next_layer_type>::value,
  301. "AsyncStream type requirements not met");
  302. return net::async_initiate<
  303. WriteHandler,
  304. void(error_code)>(
  305. run_ping_op{},
  306. handler,
  307. impl_,
  308. detail::opcode::ping,
  309. payload);
  310. }
  311. template<class NextLayer, bool deflateSupported>
  312. template<BOOST_BEAST_ASYNC_TPARAM1 WriteHandler>
  313. BOOST_BEAST_ASYNC_RESULT1(WriteHandler)
  314. stream<NextLayer, deflateSupported>::
  315. async_pong(ping_data const& payload, WriteHandler&& handler)
  316. {
  317. static_assert(is_async_stream<next_layer_type>::value,
  318. "AsyncStream type requirements not met");
  319. return net::async_initiate<
  320. WriteHandler,
  321. void(error_code)>(
  322. run_ping_op{},
  323. handler,
  324. impl_,
  325. detail::opcode::pong,
  326. payload);
  327. }
  328. } // websocket
  329. } // beast
  330. } // boost
  331. #endif