ping.hpp 11 KB

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