Line data Source code
1 : //
2 : // Copyright (c) 2025 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/cppalliance/beast2
8 : //
9 :
10 : #ifndef BOOST_BEAST2_SERVER_ROUTE_HANDLER_ASIO_HPP
11 : #define BOOST_BEAST2_SERVER_ROUTE_HANDLER_ASIO_HPP
12 :
13 : #include <boost/beast2/detail/config.hpp>
14 : #include <boost/beast2/spawn.hpp>
15 : #include <boost/http_proto/server/route_handler.hpp>
16 : #include <boost/asio/post.hpp>
17 : #include <type_traits>
18 :
19 : namespace boost {
20 : namespace beast2 {
21 :
22 : /** Route parameters object for Asio HTTP route handlers
23 : */
24 : template<class AsyncStream>
25 : class asio_route_params
26 : : public http::route_params
27 : {
28 : public:
29 : using stream_type = typename std::decay<AsyncStream>::type;
30 :
31 : AsyncStream stream;
32 :
33 : template<class... Args>
34 : explicit
35 1 : asio_route_params(
36 : Args&&... args)
37 1 : : stream(std::forward<Args>(args)...)
38 : {
39 1 : }
40 :
41 : private:
42 : #ifdef BOOST_CAPY_HAS_CORO
43 : auto
44 : spawn(
45 : capy::task<http::route_result> t) ->
46 : http::route_result override
47 : {
48 : return this->suspend(
49 : [&](http::resumer resume)
50 : {
51 : beast2::spawn(
52 : stream.get_executor(),
53 : std::move(t),
54 : [resume](std::variant<
55 : std::exception_ptr,
56 : http::route_result> v)
57 : {
58 : if(v.index() == 0)
59 : {
60 : std::rethrow_exception(
61 : std::get<0>(v));
62 : }
63 : resume(std::get<1>(v));
64 : });
65 : });
66 : }
67 : #endif
68 :
69 : void do_post() override;
70 : };
71 :
72 : //-----------------------------------------------
73 :
74 : template<class AsyncStream>
75 : void
76 0 : asio_route_params<AsyncStream>::
77 : do_post()
78 : {
79 0 : asio::post(
80 0 : stream.get_executor(),
81 0 : [&]
82 : {
83 0 : if(task_->invoke())
84 0 : return;
85 0 : do_post();
86 : });
87 0 : }
88 :
89 : } // beast2
90 : } // boost
91 :
92 : #endif
|