ping.hpp 10 KB

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