ping.hpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375
  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/stream_traits.hpp>
  13. #include <boost/beast/core/detail/bind_continuation.hpp>
  14. #include <boost/beast/websocket/detail/frame.hpp>
  15. #include <boost/beast/websocket/impl/stream_impl.hpp>
  16. #include <boost/asio/coroutine.hpp>
  17. #include <boost/asio/dispatch.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. const auto ex = this->get_immediate_executor();
  95. net::dispatch(ex, std::move(*this));
  96. }
  97. BOOST_ASSERT(impl.wr_block.is_locked(this));
  98. }
  99. if(impl.check_stop_now(ec))
  100. goto upcall;
  101. // Send ping frame
  102. BOOST_ASIO_CORO_YIELD
  103. {
  104. BOOST_ASIO_HANDLER_LOCATION((
  105. __FILE__, __LINE__,
  106. "websocket::async_ping"));
  107. net::async_write(impl.stream(), fb_.data(),
  108. beast::detail::bind_continuation(std::move(*this)));
  109. }
  110. if(impl.check_stop_now(ec))
  111. goto upcall;
  112. upcall:
  113. impl.wr_block.unlock(this);
  114. impl.op_close.maybe_invoke()
  115. || impl.op_idle_ping.maybe_invoke()
  116. || impl.op_rd.maybe_invoke()
  117. || impl.op_wr.maybe_invoke();
  118. this->complete(cont, ec);
  119. }
  120. }
  121. };
  122. //------------------------------------------------------------------------------
  123. // sends the idle ping
  124. template<class NextLayer, bool deflateSupported>
  125. template<class Executor>
  126. class stream<NextLayer, deflateSupported>::idle_ping_op
  127. : public asio::coroutine
  128. , public boost::empty_value<Executor>
  129. {
  130. boost::weak_ptr<impl_type> wp_;
  131. std::unique_ptr<detail::frame_buffer> fb_;
  132. public:
  133. static constexpr int id = 4; // for soft_mutex
  134. using executor_type = Executor;
  135. executor_type
  136. get_executor() const noexcept
  137. {
  138. return this->get();
  139. }
  140. idle_ping_op(
  141. boost::shared_ptr<impl_type> const& sp,
  142. Executor const& ex)
  143. : boost::empty_value<Executor>(
  144. boost::empty_init_t{}, ex)
  145. , wp_(sp)
  146. , fb_(new detail::frame_buffer)
  147. {
  148. if(! sp->idle_pinging)
  149. {
  150. // Create the ping frame
  151. ping_data payload; // empty for now
  152. sp->template write_ping<
  153. flat_static_buffer_base>(*fb_,
  154. detail::opcode::ping, payload);
  155. sp->idle_pinging = true;
  156. (*this)({}, 0);
  157. }
  158. else
  159. {
  160. // if we are already in the middle of sending
  161. // an idle ping, don't bother sending another.
  162. }
  163. }
  164. void operator()(
  165. error_code ec = {},
  166. std::size_t bytes_transferred = 0)
  167. {
  168. boost::ignore_unused(bytes_transferred);
  169. auto sp = wp_.lock();
  170. if(! sp)
  171. return;
  172. auto& impl = *sp;
  173. BOOST_ASIO_CORO_REENTER(*this)
  174. {
  175. // Acquire the write lock
  176. if(! impl.wr_block.try_lock(this))
  177. {
  178. BOOST_ASIO_CORO_YIELD
  179. {
  180. BOOST_ASIO_HANDLER_LOCATION((
  181. __FILE__, __LINE__,
  182. "websocket::async_ping"));
  183. impl.op_idle_ping.emplace(std::move(*this));
  184. }
  185. impl.wr_block.lock(this);
  186. BOOST_ASIO_CORO_YIELD
  187. {
  188. BOOST_ASIO_HANDLER_LOCATION((
  189. __FILE__, __LINE__,
  190. "websocket::async_ping"));
  191. net::post(sp->stream().get_executor(), std::move(*this));
  192. }
  193. BOOST_ASSERT(impl.wr_block.is_locked(this));
  194. }
  195. if(impl.check_stop_now(ec))
  196. goto upcall;
  197. // Send ping frame
  198. BOOST_ASIO_CORO_YIELD
  199. {
  200. BOOST_ASIO_HANDLER_LOCATION((
  201. __FILE__, __LINE__,
  202. "websocket::async_ping"));
  203. net::async_write(impl.stream(), fb_->data(),
  204. std::move(*this));
  205. }
  206. if(impl.check_stop_now(ec))
  207. goto upcall;
  208. upcall:
  209. BOOST_ASSERT(sp->idle_pinging);
  210. sp->idle_pinging = false;
  211. impl.wr_block.unlock(this);
  212. impl.op_close.maybe_invoke()
  213. || impl.op_ping.maybe_invoke()
  214. || impl.op_rd.maybe_invoke()
  215. || impl.op_wr.maybe_invoke();
  216. }
  217. }
  218. };
  219. template<class NextLayer, bool deflateSupported>
  220. struct stream<NextLayer, deflateSupported>::
  221. run_ping_op
  222. {
  223. boost::shared_ptr<impl_type> const& self;
  224. using executor_type = typename stream::executor_type;
  225. executor_type
  226. get_executor() const noexcept
  227. {
  228. return self->stream().get_executor();
  229. }
  230. template<class WriteHandler>
  231. void
  232. operator()(
  233. WriteHandler&& h,
  234. detail::opcode op,
  235. ping_data const& p)
  236. {
  237. // If you get an error on the following line it means
  238. // that your handler does not meet the documented type
  239. // requirements for the handler.
  240. static_assert(
  241. beast::detail::is_invocable<WriteHandler,
  242. void(error_code)>::value,
  243. "WriteHandler type requirements not met");
  244. ping_op<
  245. typename std::decay<WriteHandler>::type>(
  246. std::forward<WriteHandler>(h),
  247. self,
  248. op,
  249. p);
  250. }
  251. };
  252. //------------------------------------------------------------------------------
  253. template<class NextLayer, bool deflateSupported>
  254. void
  255. stream<NextLayer, deflateSupported>::
  256. ping(ping_data const& payload)
  257. {
  258. error_code ec;
  259. ping(payload, ec);
  260. if(ec)
  261. BOOST_THROW_EXCEPTION(system_error{ec});
  262. }
  263. template<class NextLayer, bool deflateSupported>
  264. void
  265. stream<NextLayer, deflateSupported>::
  266. ping(ping_data const& payload, error_code& ec)
  267. {
  268. if(impl_->check_stop_now(ec))
  269. return;
  270. detail::frame_buffer fb;
  271. impl_->template write_ping<flat_static_buffer_base>(
  272. fb, detail::opcode::ping, payload);
  273. net::write(impl_->stream(), fb.data(), ec);
  274. if(impl_->check_stop_now(ec))
  275. return;
  276. }
  277. template<class NextLayer, bool deflateSupported>
  278. void
  279. stream<NextLayer, deflateSupported>::
  280. pong(ping_data const& payload)
  281. {
  282. error_code ec;
  283. pong(payload, ec);
  284. if(ec)
  285. BOOST_THROW_EXCEPTION(system_error{ec});
  286. }
  287. template<class NextLayer, bool deflateSupported>
  288. void
  289. stream<NextLayer, deflateSupported>::
  290. pong(ping_data const& payload, error_code& ec)
  291. {
  292. if(impl_->check_stop_now(ec))
  293. return;
  294. detail::frame_buffer fb;
  295. impl_->template write_ping<flat_static_buffer_base>(
  296. fb, detail::opcode::pong, payload);
  297. net::write(impl_->stream(), fb.data(), ec);
  298. if(impl_->check_stop_now(ec))
  299. return;
  300. }
  301. template<class NextLayer, bool deflateSupported>
  302. template<BOOST_BEAST_ASYNC_TPARAM1 PingHandler>
  303. BOOST_BEAST_ASYNC_RESULT1(PingHandler)
  304. stream<NextLayer, deflateSupported>::
  305. async_ping(ping_data const& payload, PingHandler&& handler)
  306. {
  307. static_assert(is_async_stream<next_layer_type>::value,
  308. "AsyncStream type requirements not met");
  309. return net::async_initiate<
  310. PingHandler,
  311. void(error_code)>(
  312. run_ping_op{impl_},
  313. handler,
  314. detail::opcode::ping,
  315. payload);
  316. }
  317. template<class NextLayer, bool deflateSupported>
  318. template<BOOST_BEAST_ASYNC_TPARAM1 PongHandler>
  319. BOOST_BEAST_ASYNC_RESULT1(PongHandler)
  320. stream<NextLayer, deflateSupported>::
  321. async_pong(ping_data const& payload, PongHandler&& handler)
  322. {
  323. static_assert(is_async_stream<next_layer_type>::value,
  324. "AsyncStream type requirements not met");
  325. return net::async_initiate<
  326. PongHandler,
  327. void(error_code)>(
  328. run_ping_op{impl_},
  329. handler,
  330. detail::opcode::pong,
  331. payload);
  332. }
  333. } // websocket
  334. } // beast
  335. } // boost
  336. #endif