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_PLAIN_WORKER_HPP
11 : #define BOOST_BEAST2_SERVER_PLAIN_WORKER_HPP
12 :
13 : #include <boost/beast2/server/http_session.hpp>
14 : #include <boost/beast2/server/basic_router.hpp>
15 : #include <boost/beast2/server/call_mf.hpp>
16 : #include <boost/beast2/server/workers.hpp>
17 : #include <boost/beast2/application.hpp>
18 : #include <boost/beast2/logger.hpp>
19 : #include <boost/beast2/read.hpp>
20 : #include <boost/asio/basic_socket_acceptor.hpp>
21 : #include <boost/asio/basic_stream_socket.hpp>
22 : #include <boost/asio/prepend.hpp>
23 :
24 : namespace boost {
25 : namespace beast2 {
26 :
27 : template<class Executor, class Protocol>
28 : class plain_worker
29 : : public http_session<
30 : asio::basic_stream_socket<Protocol, Executor>
31 : >
32 : {
33 : public:
34 : using executor_type = Executor;
35 : using protocol_type = Protocol;
36 : using socket_type =
37 : asio::basic_stream_socket<Protocol, Executor>;
38 : using stream_type = socket_type;
39 : using acceptor_config = beast2::acceptor_config;
40 :
41 : template<class Executor0>
42 : plain_worker(
43 : workers_base& wb,
44 : Executor0 const& ex,
45 : router_asio<stream_type> rr);
46 :
47 : application& app() noexcept
48 : {
49 : return wb_.app();
50 : }
51 :
52 0 : socket_type& socket() noexcept
53 : {
54 0 : return stream_;
55 : }
56 :
57 : typename Protocol::endpoint&
58 0 : endpoint() noexcept
59 : {
60 0 : return ep_;
61 : }
62 :
63 : /** Cancel all outstanding I/O
64 : */
65 : void cancel();
66 :
67 : // Called when an incoming connection is accepted
68 : void on_accept(acceptor_config const* pconfig);
69 :
70 : void do_fail(
71 : core::string_view s, system::error_code const& ec);
72 :
73 : void reset();
74 :
75 : private:
76 : /** Called when the logical session ends
77 : */
78 : void do_close(system::error_code const& ec);
79 :
80 : workers_base& wb_;
81 : stream_type stream_;
82 : typename Protocol::endpoint ep_;
83 : };
84 :
85 : //------------------------------------------------
86 :
87 : template<class Executor, class Protocol>
88 : template<class Executor0>
89 0 : plain_worker<Executor, Protocol>::
90 : plain_worker(
91 : workers_base& wb,
92 : Executor0 const& ex,
93 : router_asio<stream_type> rr)
94 : : http_session<stream_type>(
95 0 : wb.app(),
96 0 : stream_,
97 0 : std::move(rr),
98 0 : [this](system::error_code const& ec)
99 : {
100 0 : this->do_close(ec);
101 : })
102 0 : , wb_(wb)
103 0 : , stream_(Executor(ex))
104 : {
105 0 : }
106 :
107 : template<class Executor, class Protocol>
108 : void
109 0 : plain_worker<Executor, Protocol>::
110 : cancel()
111 : {
112 0 : system::error_code ec;
113 0 : stream_.cancel(ec);
114 0 : }
115 :
116 : //--------------------------------------------
117 :
118 : // Called when an incoming connection is accepted
119 : template<class Executor, class Protocol>
120 : void
121 0 : plain_worker<Executor, Protocol>::
122 : on_accept(acceptor_config const* pconfig)
123 : {
124 0 : BOOST_ASSERT(stream_.get_executor().running_in_this_thread());
125 : // VFALCO TODO timeout
126 0 : this->do_session(*pconfig);
127 0 : }
128 :
129 : template<class Executor, class Protocol>
130 : void
131 0 : plain_worker<Executor, Protocol>::
132 : do_fail(
133 : core::string_view s, system::error_code const& ec)
134 : {
135 0 : reset();
136 :
137 0 : if(ec == asio::error::operation_aborted)
138 : {
139 0 : LOG_TRC(this->sect_)(
140 : "{} {}: {}",
141 : this->id(), s, ec.message());
142 : // this means the worker was stopped, don't submit new work
143 0 : return;
144 : }
145 :
146 0 : LOG_DBG(this->sect_)(
147 : "{} {}: {}",
148 : this->id(), s, ec.message());
149 0 : wb_.do_idle(this);
150 : }
151 :
152 : template<class Executor, class Protocol>
153 : void
154 0 : plain_worker<Executor, Protocol>::
155 : reset()
156 : {
157 : // Clean up any previous connection.
158 0 : system::error_code ec;
159 0 : stream_.close(ec);
160 0 : }
161 :
162 : /** Close the connection to end the session
163 : */
164 : template<class Executor, class Protocol>
165 : void
166 0 : plain_worker<Executor, Protocol>::
167 : do_close(system::error_code const& ec)
168 : {
169 0 : if(! ec.failed())
170 : {
171 0 : reset();
172 0 : wb_.do_idle(this);
173 0 : return;
174 : }
175 :
176 0 : do_fail("plain_worker::do_close", ec);
177 : }
178 :
179 : } // beast2
180 : } // boost
181 :
182 : #endif
|