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 : public:
43 : // VFALCO This needs to be private
44 0 : void do_finish()
45 : {
46 0 : if(finish_)
47 : {
48 0 : auto f = std::move(finish_);
49 0 : finish_ = {};
50 0 : f();
51 0 : }
52 0 : }
53 :
54 : private:
55 :
56 : #ifdef BOOST_CAPY_HAS_CORO
57 : auto
58 : spawn(
59 : capy::task<http::route_result> t) ->
60 : http::route_result override
61 : {
62 : return this->suspend(
63 : [&](http::resumer resume)
64 : {
65 : beast2::spawn(
66 : stream.get_executor(),
67 : std::move(t),
68 : [resume](std::variant<
69 : std::exception_ptr,
70 : http::route_result> v)
71 : {
72 : if(v.index() == 0)
73 : {
74 : std::rethrow_exception(
75 : std::get<0>(v));
76 : }
77 : resume(std::get<1>(v));
78 : });
79 : });
80 : }
81 : #endif
82 :
83 : void do_post() override;
84 : };
85 :
86 : //-----------------------------------------------
87 :
88 : template<class AsyncStream>
89 : void
90 0 : asio_route_params<AsyncStream>::
91 : do_post()
92 : {
93 0 : asio::post(
94 0 : stream.get_executor(),
95 0 : [&]
96 : {
97 0 : if(task_->invoke())
98 0 : return;
99 0 : do_post();
100 : });
101 0 : }
102 :
103 : } // beast2
104 : } // boost
105 :
106 : #endif
|