File indexing completed on 2025-01-18 09:29:34
0001
0002
0003
0004
0005
0006
0007
0008
0009
0010 #ifndef BOOST_BEAST_WEBSOCKET_IMPL_PING_HPP
0011 #define BOOST_BEAST_WEBSOCKET_IMPL_PING_HPP
0012
0013 #include <boost/beast/core/async_base.hpp>
0014 #include <boost/beast/core/bind_handler.hpp>
0015 #include <boost/beast/core/stream_traits.hpp>
0016 #include <boost/beast/core/detail/bind_continuation.hpp>
0017 #include <boost/beast/websocket/detail/frame.hpp>
0018 #include <boost/beast/websocket/impl/stream_impl.hpp>
0019 #include <boost/asio/coroutine.hpp>
0020 #include <boost/asio/dispatch.hpp>
0021 #include <boost/asio/post.hpp>
0022 #include <boost/throw_exception.hpp>
0023 #include <memory>
0024
0025 namespace boost {
0026 namespace beast {
0027 namespace websocket {
0028
0029
0030
0031
0032
0033
0034 template<class NextLayer, bool deflateSupported>
0035 template<class Handler>
0036 class stream<NextLayer, deflateSupported>::ping_op
0037 : public beast::stable_async_base<
0038 Handler, beast::executor_type<stream>>
0039 , public asio::coroutine
0040 {
0041 boost::weak_ptr<impl_type> wp_;
0042 detail::frame_buffer& fb_;
0043
0044 public:
0045 static constexpr int id = 3;
0046
0047 template<class Handler_>
0048 ping_op(
0049 Handler_&& h,
0050 boost::shared_ptr<impl_type> const& sp,
0051 detail::opcode op,
0052 ping_data const& payload)
0053 : stable_async_base<Handler,
0054 beast::executor_type<stream>>(
0055 std::forward<Handler_>(h),
0056 sp->stream().get_executor())
0057 , wp_(sp)
0058 , fb_(beast::allocate_stable<
0059 detail::frame_buffer>(*this))
0060 {
0061
0062 sp->template write_ping<
0063 flat_static_buffer_base>(fb_, op, payload);
0064 (*this)({}, 0, false);
0065 }
0066
0067 void operator()(
0068 error_code ec = {},
0069 std::size_t bytes_transferred = 0,
0070 bool cont = true)
0071 {
0072 boost::ignore_unused(bytes_transferred);
0073 auto sp = wp_.lock();
0074 if(! sp)
0075 {
0076 BOOST_BEAST_ASSIGN_EC(ec, net::error::operation_aborted);
0077 return this->complete(cont, ec);
0078 }
0079 auto& impl = *sp;
0080 BOOST_ASIO_CORO_REENTER(*this)
0081 {
0082
0083 if(! impl.wr_block.try_lock(this))
0084 {
0085 BOOST_ASIO_CORO_YIELD
0086 {
0087 BOOST_ASIO_HANDLER_LOCATION((
0088 __FILE__, __LINE__,
0089 "websocket::async_ping"));
0090 this->set_allowed_cancellation(net::cancellation_type::all);
0091 impl.op_ping.emplace(std::move(*this), net::cancellation_type::all);
0092 }
0093 if (ec)
0094 return this->complete(cont, ec);
0095 this->set_allowed_cancellation(net::cancellation_type::terminal);
0096 impl.wr_block.lock(this);
0097 BOOST_ASIO_CORO_YIELD
0098 {
0099 BOOST_ASIO_HANDLER_LOCATION((
0100 __FILE__, __LINE__,
0101 "websocket::async_ping"));
0102
0103 const auto ex = this->get_immediate_executor();
0104 net::dispatch(ex, std::move(*this));
0105 }
0106 BOOST_ASSERT(impl.wr_block.is_locked(this));
0107 }
0108 if(impl.check_stop_now(ec))
0109 goto upcall;
0110
0111
0112 BOOST_ASIO_CORO_YIELD
0113 {
0114 BOOST_ASIO_HANDLER_LOCATION((
0115 __FILE__, __LINE__,
0116 "websocket::async_ping"));
0117
0118 net::async_write(impl.stream(), fb_.data(),
0119 beast::detail::bind_continuation(std::move(*this)));
0120 }
0121 if(impl.check_stop_now(ec))
0122 goto upcall;
0123
0124 upcall:
0125 impl.wr_block.unlock(this);
0126 impl.op_close.maybe_invoke()
0127 || impl.op_idle_ping.maybe_invoke()
0128 || impl.op_rd.maybe_invoke()
0129 || impl.op_wr.maybe_invoke();
0130 this->complete(cont, ec);
0131 }
0132 }
0133 };
0134
0135
0136
0137
0138 template<class NextLayer, bool deflateSupported>
0139 template<class Executor>
0140 class stream<NextLayer, deflateSupported>::idle_ping_op
0141 : public asio::coroutine
0142 , public boost::empty_value<Executor>
0143 {
0144 boost::weak_ptr<impl_type> wp_;
0145 std::unique_ptr<detail::frame_buffer> fb_;
0146
0147 public:
0148 static constexpr int id = 4;
0149
0150 using executor_type = Executor;
0151
0152 executor_type
0153 get_executor() const noexcept
0154 {
0155 return this->get();
0156 }
0157
0158 idle_ping_op(
0159 boost::shared_ptr<impl_type> const& sp,
0160 Executor const& ex)
0161 : boost::empty_value<Executor>(
0162 boost::empty_init_t{}, ex)
0163 , wp_(sp)
0164 , fb_(new detail::frame_buffer)
0165 {
0166 if(! sp->idle_pinging)
0167 {
0168
0169 ping_data payload;
0170 sp->template write_ping<
0171 flat_static_buffer_base>(*fb_,
0172 detail::opcode::ping, payload);
0173
0174 sp->idle_pinging = true;
0175 (*this)({}, 0);
0176 }
0177 else
0178 {
0179
0180
0181 }
0182 }
0183
0184 void operator()(
0185 error_code ec = {},
0186 std::size_t bytes_transferred = 0)
0187 {
0188 boost::ignore_unused(bytes_transferred);
0189 auto sp = wp_.lock();
0190 if(! sp)
0191 return;
0192 auto& impl = *sp;
0193 BOOST_ASIO_CORO_REENTER(*this)
0194 {
0195
0196 if(! impl.wr_block.try_lock(this))
0197 {
0198 BOOST_ASIO_CORO_YIELD
0199 {
0200 BOOST_ASIO_HANDLER_LOCATION((
0201 __FILE__, __LINE__,
0202 "websocket::async_ping"));
0203
0204 impl.op_idle_ping.emplace(std::move(*this));
0205 }
0206 impl.wr_block.lock(this);
0207 BOOST_ASIO_CORO_YIELD
0208 {
0209 BOOST_ASIO_HANDLER_LOCATION((
0210 __FILE__, __LINE__,
0211 "websocket::async_ping"));
0212
0213 net::post(sp->stream().get_executor(), std::move(*this));
0214 }
0215 BOOST_ASSERT(impl.wr_block.is_locked(this));
0216 }
0217 if(impl.check_stop_now(ec))
0218 goto upcall;
0219
0220
0221 BOOST_ASIO_CORO_YIELD
0222 {
0223 BOOST_ASIO_HANDLER_LOCATION((
0224 __FILE__, __LINE__,
0225 "websocket::async_ping"));
0226
0227 net::async_write(impl.stream(), fb_->data(),
0228 std::move(*this));
0229 }
0230 if(impl.check_stop_now(ec))
0231 goto upcall;
0232
0233 upcall:
0234 BOOST_ASSERT(sp->idle_pinging);
0235 sp->idle_pinging = false;
0236 impl.wr_block.unlock(this);
0237 impl.op_close.maybe_invoke()
0238 || impl.op_ping.maybe_invoke()
0239 || impl.op_rd.maybe_invoke()
0240 || impl.op_wr.maybe_invoke();
0241 }
0242 }
0243 };
0244
0245 template<class NextLayer, bool deflateSupported>
0246 struct stream<NextLayer, deflateSupported>::
0247 run_ping_op
0248 {
0249 template<class WriteHandler>
0250 void
0251 operator()(
0252 WriteHandler&& h,
0253 boost::shared_ptr<impl_type> const& sp,
0254 detail::opcode op,
0255 ping_data const& p)
0256 {
0257
0258
0259
0260
0261 static_assert(
0262 beast::detail::is_invocable<WriteHandler,
0263 void(error_code)>::value,
0264 "WriteHandler type requirements not met");
0265
0266 ping_op<
0267 typename std::decay<WriteHandler>::type>(
0268 std::forward<WriteHandler>(h),
0269 sp,
0270 op,
0271 p);
0272 }
0273 };
0274
0275
0276
0277 template<class NextLayer, bool deflateSupported>
0278 void
0279 stream<NextLayer, deflateSupported>::
0280 ping(ping_data const& payload)
0281 {
0282 error_code ec;
0283 ping(payload, ec);
0284 if(ec)
0285 BOOST_THROW_EXCEPTION(system_error{ec});
0286 }
0287
0288 template<class NextLayer, bool deflateSupported>
0289 void
0290 stream<NextLayer, deflateSupported>::
0291 ping(ping_data const& payload, error_code& ec)
0292 {
0293 if(impl_->check_stop_now(ec))
0294 return;
0295 detail::frame_buffer fb;
0296 impl_->template write_ping<flat_static_buffer_base>(
0297 fb, detail::opcode::ping, payload);
0298 net::write(impl_->stream(), fb.data(), ec);
0299 if(impl_->check_stop_now(ec))
0300 return;
0301 }
0302
0303 template<class NextLayer, bool deflateSupported>
0304 void
0305 stream<NextLayer, deflateSupported>::
0306 pong(ping_data const& payload)
0307 {
0308 error_code ec;
0309 pong(payload, ec);
0310 if(ec)
0311 BOOST_THROW_EXCEPTION(system_error{ec});
0312 }
0313
0314 template<class NextLayer, bool deflateSupported>
0315 void
0316 stream<NextLayer, deflateSupported>::
0317 pong(ping_data const& payload, error_code& ec)
0318 {
0319 if(impl_->check_stop_now(ec))
0320 return;
0321 detail::frame_buffer fb;
0322 impl_->template write_ping<flat_static_buffer_base>(
0323 fb, detail::opcode::pong, payload);
0324 net::write(impl_->stream(), fb.data(), ec);
0325 if(impl_->check_stop_now(ec))
0326 return;
0327 }
0328
0329 template<class NextLayer, bool deflateSupported>
0330 template<BOOST_BEAST_ASYNC_TPARAM1 PingHandler>
0331 BOOST_BEAST_ASYNC_RESULT1(PingHandler)
0332 stream<NextLayer, deflateSupported>::
0333 async_ping(ping_data const& payload, PingHandler&& handler)
0334 {
0335 static_assert(is_async_stream<next_layer_type>::value,
0336 "AsyncStream type requirements not met");
0337 return net::async_initiate<
0338 PingHandler,
0339 void(error_code)>(
0340 run_ping_op{},
0341 handler,
0342 impl_,
0343 detail::opcode::ping,
0344 payload);
0345 }
0346
0347 template<class NextLayer, bool deflateSupported>
0348 template<BOOST_BEAST_ASYNC_TPARAM1 PongHandler>
0349 BOOST_BEAST_ASYNC_RESULT1(PongHandler)
0350 stream<NextLayer, deflateSupported>::
0351 async_pong(ping_data const& payload, PongHandler&& handler)
0352 {
0353 static_assert(is_async_stream<next_layer_type>::value,
0354 "AsyncStream type requirements not met");
0355 return net::async_initiate<
0356 PongHandler,
0357 void(error_code)>(
0358 run_ping_op{},
0359 handler,
0360 impl_,
0361 detail::opcode::pong,
0362 payload);
0363 }
0364
0365 }
0366 }
0367 }
0368
0369 #endif