ping.hpp 9.3 KB

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