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 net::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 net::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(this->get(), std::move(*this));
  163. BOOST_ASSERT(impl.wr_block.is_locked(this));
  164. }
  165. if(impl.check_stop_now(ec))
  166. goto upcall;
  167. // Send ping frame
  168. BOOST_ASIO_CORO_YIELD
  169. net::async_write(impl.stream(), fb_->data(),
  170. //beast::detail::bind_continuation(std::move(*this)));
  171. std::move(*this));
  172. if(impl.check_stop_now(ec))
  173. goto upcall;
  174. upcall:
  175. BOOST_ASSERT(sp->idle_pinging);
  176. sp->idle_pinging = false;
  177. impl.wr_block.unlock(this);
  178. impl.op_close.maybe_invoke()
  179. || impl.op_ping.maybe_invoke()
  180. || impl.op_rd.maybe_invoke()
  181. || impl.op_wr.maybe_invoke();
  182. }
  183. }
  184. };
  185. template<class NextLayer, bool deflateSupported>
  186. struct stream<NextLayer, deflateSupported>::
  187. run_ping_op
  188. {
  189. template<class WriteHandler>
  190. void
  191. operator()(
  192. WriteHandler&& h,
  193. boost::shared_ptr<impl_type> const& sp,
  194. detail::opcode op,
  195. ping_data const& p)
  196. {
  197. // If you get an error on the following line it means
  198. // that your handler does not meet the documented type
  199. // requirements for the handler.
  200. static_assert(
  201. beast::detail::is_invocable<WriteHandler,
  202. void(error_code)>::value,
  203. "WriteHandler type requirements not met");
  204. ping_op<
  205. typename std::decay<WriteHandler>::type>(
  206. std::forward<WriteHandler>(h),
  207. sp,
  208. op,
  209. p);
  210. }
  211. };
  212. //------------------------------------------------------------------------------
  213. template<class NextLayer, bool deflateSupported>
  214. void
  215. stream<NextLayer, deflateSupported>::
  216. ping(ping_data const& payload)
  217. {
  218. error_code ec;
  219. ping(payload, ec);
  220. if(ec)
  221. BOOST_THROW_EXCEPTION(system_error{ec});
  222. }
  223. template<class NextLayer, bool deflateSupported>
  224. void
  225. stream<NextLayer, deflateSupported>::
  226. ping(ping_data const& payload, error_code& ec)
  227. {
  228. if(impl_->check_stop_now(ec))
  229. return;
  230. detail::frame_buffer fb;
  231. impl_->template write_ping<flat_static_buffer_base>(
  232. fb, detail::opcode::ping, payload);
  233. net::write(impl_->stream(), fb.data(), ec);
  234. if(impl_->check_stop_now(ec))
  235. return;
  236. }
  237. template<class NextLayer, bool deflateSupported>
  238. void
  239. stream<NextLayer, deflateSupported>::
  240. pong(ping_data const& payload)
  241. {
  242. error_code ec;
  243. pong(payload, ec);
  244. if(ec)
  245. BOOST_THROW_EXCEPTION(system_error{ec});
  246. }
  247. template<class NextLayer, bool deflateSupported>
  248. void
  249. stream<NextLayer, deflateSupported>::
  250. pong(ping_data const& payload, error_code& ec)
  251. {
  252. if(impl_->check_stop_now(ec))
  253. return;
  254. detail::frame_buffer fb;
  255. impl_->template write_ping<flat_static_buffer_base>(
  256. fb, detail::opcode::pong, payload);
  257. net::write(impl_->stream(), fb.data(), ec);
  258. if(impl_->check_stop_now(ec))
  259. return;
  260. }
  261. template<class NextLayer, bool deflateSupported>
  262. template<class WriteHandler>
  263. BOOST_BEAST_ASYNC_RESULT1(WriteHandler)
  264. stream<NextLayer, deflateSupported>::
  265. async_ping(ping_data const& payload, WriteHandler&& handler)
  266. {
  267. static_assert(is_async_stream<next_layer_type>::value,
  268. "AsyncStream type requirements not met");
  269. return net::async_initiate<
  270. WriteHandler,
  271. void(error_code)>(
  272. run_ping_op{},
  273. handler,
  274. impl_,
  275. detail::opcode::ping,
  276. payload);
  277. }
  278. template<class NextLayer, bool deflateSupported>
  279. template<class WriteHandler>
  280. BOOST_BEAST_ASYNC_RESULT1(WriteHandler)
  281. stream<NextLayer, deflateSupported>::
  282. async_pong(ping_data const& payload, WriteHandler&& handler)
  283. {
  284. static_assert(is_async_stream<next_layer_type>::value,
  285. "AsyncStream type requirements not met");
  286. return net::async_initiate<
  287. WriteHandler,
  288. void(error_code)>(
  289. run_ping_op{},
  290. handler,
  291. impl_,
  292. detail::opcode::pong,
  293. payload);
  294. }
  295. } // websocket
  296. } // beast
  297. } // boost
  298. #endif