| 1 |
|
|
1 |
|
|
| 2 |
|
// Copyright (c) 2026 Vinnie Falco (vinnie dot falco at gmail dot com)
|
2 |
|
// Copyright (c) 2026 Vinnie Falco (vinnie dot falco at gmail dot com)
|
| 3 |
|
|
3 |
|
|
| 4 |
|
// Distributed under the Boost Software License, Version 1.0. (See accompanying
|
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)
|
5 |
|
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
|
| 6 |
|
|
6 |
|
|
| 7 |
|
// Official repository: https://github.com/cppalliance/corosio
|
7 |
|
// Official repository: https://github.com/cppalliance/corosio
|
| 8 |
|
|
8 |
|
|
| 9 |
|
|
9 |
|
|
| 10 |
|
#ifndef BOOST_COROSIO_TCP_SERVER_HPP
|
10 |
|
#ifndef BOOST_COROSIO_TCP_SERVER_HPP
|
| 11 |
|
#define BOOST_COROSIO_TCP_SERVER_HPP
|
11 |
|
#define BOOST_COROSIO_TCP_SERVER_HPP
|
| 12 |
|
|
12 |
|
|
| 13 |
|
#include <boost/corosio/detail/config.hpp>
|
13 |
|
#include <boost/corosio/detail/config.hpp>
|
| 14 |
|
#include <boost/corosio/detail/except.hpp>
|
14 |
|
#include <boost/corosio/detail/except.hpp>
|
| 15 |
|
#include <boost/corosio/tcp_acceptor.hpp>
|
15 |
|
#include <boost/corosio/tcp_acceptor.hpp>
|
| 16 |
|
#include <boost/corosio/tcp_socket.hpp>
|
16 |
|
#include <boost/corosio/tcp_socket.hpp>
|
| 17 |
|
#include <boost/corosio/io_context.hpp>
|
17 |
|
#include <boost/corosio/io_context.hpp>
|
| 18 |
|
#include <boost/corosio/endpoint.hpp>
|
18 |
|
#include <boost/corosio/endpoint.hpp>
|
| 19 |
|
#include <boost/capy/task.hpp>
|
19 |
|
#include <boost/capy/task.hpp>
|
| 20 |
|
#include <boost/capy/concept/execution_context.hpp>
|
20 |
|
#include <boost/capy/concept/execution_context.hpp>
|
| 21 |
|
#include <boost/capy/concept/io_awaitable.hpp>
|
21 |
|
#include <boost/capy/concept/io_awaitable.hpp>
|
| 22 |
|
#include <boost/capy/concept/executor.hpp>
|
22 |
|
#include <boost/capy/concept/executor.hpp>
|
| 23 |
|
#include <boost/capy/ex/any_executor.hpp>
|
23 |
|
#include <boost/capy/ex/any_executor.hpp>
|
| 24 |
|
#include <boost/capy/ex/run_async.hpp>
|
24 |
|
#include <boost/capy/ex/run_async.hpp>
|
| 25 |
|
|
25 |
|
|
| 26 |
|
|
26 |
|
|
| 27 |
|
|
27 |
|
|
| 28 |
|
|
28 |
|
|
| 29 |
|
|
29 |
|
|
| 30 |
|
|
30 |
|
|
| 31 |
|
namespace boost::corosio {
|
31 |
|
namespace boost::corosio {
|
| 32 |
|
|
32 |
|
|
| 33 |
|
|
33 |
|
|
| 34 |
|
|
34 |
|
|
| 35 |
|
#pragma warning(disable: 4251) // class needs to have dll-interface
|
35 |
|
#pragma warning(disable: 4251) // class needs to have dll-interface
|
| 36 |
|
|
36 |
|
|
| 37 |
|
|
37 |
|
|
| 38 |
|
/** TCP server with pooled workers.
|
38 |
|
/** TCP server with pooled workers.
|
| 39 |
|
|
39 |
|
|
| 40 |
|
This class manages a pool of reusable worker objects that handle
|
40 |
|
This class manages a pool of reusable worker objects that handle
|
| 41 |
|
incoming connections. When a connection arrives, an idle worker
|
41 |
|
incoming connections. When a connection arrives, an idle worker
|
| 42 |
|
is dispatched to handle it. After the connection completes, the
|
42 |
|
is dispatched to handle it. After the connection completes, the
|
| 43 |
|
worker returns to the pool for reuse, avoiding allocation overhead
|
43 |
|
worker returns to the pool for reuse, avoiding allocation overhead
|
| 44 |
|
|
44 |
|
|
| 45 |
|
|
45 |
|
|
| 46 |
|
Workers are set via @ref set_workers as a forward range of
|
46 |
|
Workers are set via @ref set_workers as a forward range of
|
| 47 |
|
pointer-like objects (e.g., `unique_ptr<worker_base>`). The server
|
47 |
|
pointer-like objects (e.g., `unique_ptr<worker_base>`). The server
|
| 48 |
|
takes ownership of the container via type erasure.
|
48 |
|
takes ownership of the container via type erasure.
|
| 49 |
|
|
49 |
|
|
| 50 |
|
|
50 |
|
|
| 51 |
|
|
51 |
|
|
| 52 |
|
|
52 |
|
|
| 53 |
|
|
53 |
|
|
| 54 |
|
|
54 |
|
|
| 55 |
|
The server operates in three states:
|
55 |
|
The server operates in three states:
|
| 56 |
|
|
56 |
|
|
| 57 |
|
- **Stopped**: Initial state, or after @ref join completes.
|
57 |
|
- **Stopped**: Initial state, or after @ref join completes.
|
| 58 |
|
- **Running**: After @ref start, actively accepting connections.
|
58 |
|
- **Running**: After @ref start, actively accepting connections.
|
| 59 |
|
- **Stopping**: After @ref stop, draining active work.
|
59 |
|
- **Stopping**: After @ref stop, draining active work.
|
| 60 |
|
|
60 |
|
|
| 61 |
|
|
61 |
|
|
| 62 |
|
|
62 |
|
|
| 63 |
|
[Stopped] --start()--> [Running] --stop()--> [Stopping] --join()--> [Stopped]
|
63 |
|
[Stopped] --start()--> [Running] --stop()--> [Stopping] --join()--> [Stopped]
|
| 64 |
|
|
64 |
|
|
| 65 |
|
|
65 |
|
|
| 66 |
|
|
66 |
|
|
| 67 |
|
|
67 |
|
|
| 68 |
|
|
68 |
|
|
| 69 |
|
tcp_server srv(ioc, ioc.get_executor());
|
69 |
|
tcp_server srv(ioc, ioc.get_executor());
|
| 70 |
|
srv.set_workers(make_workers(ioc, 100));
|
70 |
|
srv.set_workers(make_workers(ioc, 100));
|
| 71 |
|
srv.bind(endpoint{address_v4::any(), 8080});
|
71 |
|
srv.bind(endpoint{address_v4::any(), 8080});
|
| 72 |
|
|
72 |
|
|
| 73 |
|
ioc.run(); // Blocks until all work completes
|
73 |
|
ioc.run(); // Blocks until all work completes
|
| 74 |
|
|
74 |
|
|
| 75 |
|
|
75 |
|
|
| 76 |
|
|
76 |
|
|
| 77 |
|
To shut down gracefully, call @ref stop then drain the io_context:
|
77 |
|
To shut down gracefully, call @ref stop then drain the io_context:
|
| 78 |
|
|
78 |
|
|
| 79 |
|
// From a signal handler or timer callback:
|
79 |
|
// From a signal handler or timer callback:
|
| 80 |
|
|
80 |
|
|
| 81 |
|
|
81 |
|
|
| 82 |
|
// ioc.run() returns after pending work drains.
|
82 |
|
// ioc.run() returns after pending work drains.
|
| 83 |
|
// Then from the thread that called ioc.run():
|
83 |
|
// Then from the thread that called ioc.run():
|
| 84 |
|
srv.join(); // Wait for accept loops to finish
|
84 |
|
srv.join(); // Wait for accept loops to finish
|
| 85 |
|
|
85 |
|
|
| 86 |
|
|
86 |
|
|
| 87 |
|
|
87 |
|
|
| 88 |
|
The server can be restarted after a complete shutdown cycle.
|
88 |
|
The server can be restarted after a complete shutdown cycle.
|
| 89 |
|
You must drain the io_context and call @ref join before restarting:
|
89 |
|
You must drain the io_context and call @ref join before restarting:
|
| 90 |
|
|
90 |
|
|
| 91 |
|
|
91 |
|
|
| 92 |
|
ioc.run_for( 10s ); // Run for a while
|
92 |
|
ioc.run_for( 10s ); // Run for a while
|
| 93 |
|
srv.stop(); // Signal shutdown
|
93 |
|
srv.stop(); // Signal shutdown
|
| 94 |
|
ioc.run(); // REQUIRED: drain pending completions
|
94 |
|
ioc.run(); // REQUIRED: drain pending completions
|
| 95 |
|
srv.join(); // REQUIRED: wait for accept loops
|
95 |
|
srv.join(); // REQUIRED: wait for accept loops
|
| 96 |
|
|
96 |
|
|
| 97 |
|
|
97 |
|
|
| 98 |
|
|
98 |
|
|
| 99 |
|
|
99 |
|
|
| 100 |
|
|
100 |
|
|
| 101 |
|
|
101 |
|
|
| 102 |
|
@par WARNING: What NOT to Do
|
102 |
|
@par WARNING: What NOT to Do
|
| 103 |
|
- Do NOT call @ref join from inside a worker coroutine (deadlock).
|
103 |
|
- Do NOT call @ref join from inside a worker coroutine (deadlock).
|
| 104 |
|
- Do NOT call @ref join from a thread running `ioc.run()` (deadlock).
|
104 |
|
- Do NOT call @ref join from a thread running `ioc.run()` (deadlock).
|
| 105 |
|
- Do NOT call @ref start without completing @ref join after @ref stop.
|
105 |
|
- Do NOT call @ref start without completing @ref join after @ref stop.
|
| 106 |
|
- Do NOT call `ioc.stop()` for graceful shutdown; use @ref stop instead.
|
106 |
|
- Do NOT call `ioc.stop()` for graceful shutdown; use @ref stop instead.
|
| 107 |
|
|
107 |
|
|
| 108 |
|
|
108 |
|
|
| 109 |
|
|
109 |
|
|
| 110 |
|
class my_worker : public tcp_server::worker_base
|
110 |
|
class my_worker : public tcp_server::worker_base
|
| 111 |
|
|
111 |
|
|
| 112 |
|
corosio::tcp_socket sock_;
|
112 |
|
corosio::tcp_socket sock_;
|
| 113 |
|
|
113 |
|
|
| 114 |
|
|
114 |
|
|
| 115 |
|
my_worker(io_context& ctx)
|
115 |
|
my_worker(io_context& ctx)
|
| 116 |
|
|
116 |
|
|
| 117 |
|
, ex_(ctx.get_executor())
|
117 |
|
, ex_(ctx.get_executor())
|
| 118 |
|
|
118 |
|
|
| 119 |
|
|
119 |
|
|
| 120 |
|
|
120 |
|
|
| 121 |
|
corosio::tcp_socket& socket() override { return sock_; }
|
121 |
|
corosio::tcp_socket& socket() override { return sock_; }
|
| 122 |
|
|
122 |
|
|
| 123 |
|
void run(launcher launch) override
|
123 |
|
void run(launcher launch) override
|
| 124 |
|
|
124 |
|
|
| 125 |
|
launch(ex_, [](corosio::tcp_socket* sock) -> capy::task<>
|
125 |
|
launch(ex_, [](corosio::tcp_socket* sock) -> capy::task<>
|
| 126 |
|
|
126 |
|
|
| 127 |
|
// handle connection using sock
|
127 |
|
// handle connection using sock
|
| 128 |
|
|
128 |
|
|
| 129 |
|
|
129 |
|
|
| 130 |
|
|
130 |
|
|
| 131 |
|
|
131 |
|
|
| 132 |
|
|
132 |
|
|
| 133 |
|
auto make_workers(io_context& ctx, int n)
|
133 |
|
auto make_workers(io_context& ctx, int n)
|
| 134 |
|
|
134 |
|
|
| 135 |
|
std::vector<std::unique_ptr<tcp_server::worker_base>> v;
|
135 |
|
std::vector<std::unique_ptr<tcp_server::worker_base>> v;
|
| 136 |
|
|
136 |
|
|
| 137 |
|
for(int i = 0; i < n; ++i)
|
137 |
|
for(int i = 0; i < n; ++i)
|
| 138 |
|
v.push_back(std::make_unique<my_worker>(ctx));
|
138 |
|
v.push_back(std::make_unique<my_worker>(ctx));
|
| 139 |
|
|
139 |
|
|
| 140 |
|
|
140 |
|
|
| 141 |
|
|
141 |
|
|
| 142 |
|
|
142 |
|
|
| 143 |
|
tcp_server srv(ioc, ioc.get_executor());
|
143 |
|
tcp_server srv(ioc, ioc.get_executor());
|
| 144 |
|
srv.set_workers(make_workers(ioc, 100));
|
144 |
|
srv.set_workers(make_workers(ioc, 100));
|
| 145 |
|
|
145 |
|
|
| 146 |
|
|
146 |
|
|
| 147 |
|
@see worker_base, set_workers, launcher
|
147 |
|
@see worker_base, set_workers, launcher
|
| 148 |
|
|
148 |
|
|
| 149 |
|
|
149 |
|
|
| 150 |
|
|
150 |
|
|
| 151 |
|
|
151 |
|
|
| 152 |
|
|
152 |
|
|
| 153 |
|
class worker_base; ///< Abstract base for connection handlers.
|
153 |
|
class worker_base; ///< Abstract base for connection handlers.
|
| 154 |
|
class launcher; ///< Move-only handle to launch worker coroutines.
|
154 |
|
class launcher; ///< Move-only handle to launch worker coroutines.
|
| 155 |
|
|
155 |
|
|
| 156 |
|
|
156 |
|
|
| 157 |
|
|
157 |
|
|
| 158 |
|
|
158 |
|
|
| 159 |
|
|
159 |
|
|
| 160 |
|
std::coroutine_handle<> h;
|
160 |
|
std::coroutine_handle<> h;
|
| 161 |
|
|
161 |
|
|
| 162 |
|
|
162 |
|
|
| 163 |
|
|
163 |
|
|
| 164 |
|
|
164 |
|
|
| 165 |
|
|
165 |
|
|
| 166 |
|
static impl* make_impl(capy::execution_context& ctx);
|
166 |
|
static impl* make_impl(capy::execution_context& ctx);
|
| 167 |
|
|
167 |
|
|
| 168 |
|
|
168 |
|
|
| 169 |
|
|
169 |
|
|
| 170 |
|
waiter* waiters_ = nullptr;
|
170 |
|
waiter* waiters_ = nullptr;
|
| 171 |
|
worker_base* idle_head_ = nullptr; // Forward list: available workers
|
171 |
|
worker_base* idle_head_ = nullptr; // Forward list: available workers
|
| 172 |
|
worker_base* active_head_ = nullptr; // Doubly linked: workers handling connections
|
172 |
|
worker_base* active_head_ = nullptr; // Doubly linked: workers handling connections
|
| 173 |
|
worker_base* active_tail_ = nullptr; // Tail for O(1) push_back
|
173 |
|
worker_base* active_tail_ = nullptr; // Tail for O(1) push_back
|
| 174 |
|
std::size_t active_accepts_ = 0; // Number of active do_accept coroutines
|
174 |
|
std::size_t active_accepts_ = 0; // Number of active do_accept coroutines
|
| 175 |
|
std::shared_ptr<void> storage_; // Owns the worker container (type-erased)
|
175 |
|
std::shared_ptr<void> storage_; // Owns the worker container (type-erased)
|
| 176 |
|
|
176 |
|
|
| 177 |
|
|
177 |
|
|
| 178 |
|
// Idle list (forward/singly linked) - push front, pop front
|
178 |
|
// Idle list (forward/singly linked) - push front, pop front
|
| 179 |
|
void idle_push(worker_base* w) noexcept
|
179 |
|
void idle_push(worker_base* w) noexcept
|
| 180 |
|
|
180 |
|
|
| 181 |
|
|
181 |
|
|
| 182 |
|
|
182 |
|
|
| 183 |
|
|
183 |
|
|
| 184 |
|
|
184 |
|
|
| 185 |
|
worker_base* idle_pop() noexcept
|
185 |
|
worker_base* idle_pop() noexcept
|
| 186 |
|
|
186 |
|
|
| 187 |
|
|
187 |
|
|
| 188 |
|
if(w) idle_head_ = w->next_;
|
188 |
|
if(w) idle_head_ = w->next_;
|
| 189 |
|
|
189 |
|
|
| 190 |
|
|
190 |
|
|
| 191 |
|
|
191 |
|
|
| 192 |
|
bool idle_empty() const noexcept { return idle_head_ == nullptr; }
|
192 |
|
bool idle_empty() const noexcept { return idle_head_ == nullptr; }
|
| 193 |
|
|
193 |
|
|
| 194 |
|
// Active list (doubly linked) - push back, remove anywhere
|
194 |
|
// Active list (doubly linked) - push back, remove anywhere
|
| 195 |
|
void active_push(worker_base* w) noexcept
|
195 |
|
void active_push(worker_base* w) noexcept
|
| 196 |
|
|
196 |
|
|
| 197 |
|
|
197 |
|
|
| 198 |
|
|
198 |
|
|
| 199 |
|
|
199 |
|
|
| 200 |
|
|
200 |
|
|
| 201 |
|
|
201 |
|
|
| 202 |
|
|
202 |
|
|
| 203 |
|
|
203 |
|
|
| 204 |
|
|
204 |
|
|
| 205 |
|
|
205 |
|
|
| 206 |
|
void active_remove(worker_base* w) noexcept
|
206 |
|
void active_remove(worker_base* w) noexcept
|
| 207 |
|
|
207 |
|
|
| 208 |
|
// Skip if not in active list (e.g., after failed accept)
|
208 |
|
// Skip if not in active list (e.g., after failed accept)
|
| 209 |
|
if(w != active_head_ && w->prev_ == nullptr)
|
209 |
|
if(w != active_head_ && w->prev_ == nullptr)
|
| 210 |
|
|
210 |
|
|
| 211 |
|
|
211 |
|
|
| 212 |
|
w->prev_->next_ = w->next_;
|
212 |
|
w->prev_->next_ = w->next_;
|
| 213 |
|
|
213 |
|
|
| 214 |
|
|
214 |
|
|
| 215 |
|
|
215 |
|
|
| 216 |
|
w->next_->prev_ = w->prev_;
|
216 |
|
w->next_->prev_ = w->prev_;
|
| 217 |
|
|
217 |
|
|
| 218 |
|
|
218 |
|
|
| 219 |
|
w->prev_ = nullptr; // Mark as not in active list
|
219 |
|
w->prev_ = nullptr; // Mark as not in active list
|
| 220 |
|
|
220 |
|
|
| 221 |
|
|
221 |
|
|
| 222 |
|
template<capy::Executor Ex>
|
222 |
|
template<capy::Executor Ex>
|
| 223 |
|
|
223 |
|
|
| 224 |
|
|
224 |
|
|
| 225 |
|
|
225 |
|
|
| 226 |
|
|
226 |
|
|
| 227 |
|
Ex ex; // Stored directly in frame, no allocation
|
227 |
|
Ex ex; // Stored directly in frame, no allocation
|
| 228 |
|
|
228 |
|
|
| 229 |
|
|
229 |
|
|
| 230 |
|
// For regular coroutines: first arg is executor, second is stop token
|
230 |
|
// For regular coroutines: first arg is executor, second is stop token
|
| 231 |
|
template<class E, class S, class... Args>
|
231 |
|
template<class E, class S, class... Args>
|
| 232 |
|
requires capy::Executor<std::decay_t<E>>
|
232 |
|
requires capy::Executor<std::decay_t<E>>
|
| 233 |
|
promise_type(E e, S s, Args&&...)
|
233 |
|
promise_type(E e, S s, Args&&...)
|
| 234 |
|
|
234 |
|
|
| 235 |
|
|
235 |
|
|
| 236 |
|
|
236 |
|
|
| 237 |
|
|
237 |
|
|
| 238 |
|
|
238 |
|
|
| 239 |
|
// For lambda coroutines: first arg is closure, second is executor, third is stop token
|
239 |
|
// For lambda coroutines: first arg is closure, second is executor, third is stop token
|
| 240 |
|
template<class Closure, class E, class S, class... Args>
|
240 |
|
template<class Closure, class E, class S, class... Args>
|
| 241 |
|
requires (!capy::Executor<std::decay_t<Closure>> &&
|
241 |
|
requires (!capy::Executor<std::decay_t<Closure>> &&
|
| 242 |
|
capy::Executor<std::decay_t<E>>)
|
242 |
|
capy::Executor<std::decay_t<E>>)
|
| 243 |
|
promise_type(Closure&&, E e, S s, Args&&...)
|
243 |
|
promise_type(Closure&&, E e, S s, Args&&...)
|
| 244 |
|
|
244 |
|
|
| 245 |
|
|
245 |
|
|
| 246 |
|
|
246 |
|
|
| 247 |
|
|
247 |
|
|
| 248 |
|
|
248 |
|
|
| 249 |
|
launch_wrapper get_return_object() noexcept {
|
249 |
|
launch_wrapper get_return_object() noexcept {
|
| 250 |
|
return {std::coroutine_handle<promise_type>::from_promise(*this)};
|
250 |
|
return {std::coroutine_handle<promise_type>::from_promise(*this)};
|
| 251 |
|
|
251 |
|
|
| 252 |
|
std::suspend_always initial_suspend() noexcept { return {}; }
|
252 |
|
std::suspend_always initial_suspend() noexcept { return {}; }
|
| 253 |
|
std::suspend_never final_suspend() noexcept { return {}; }
|
253 |
|
std::suspend_never final_suspend() noexcept { return {}; }
|
| 254 |
|
void return_void() noexcept {}
|
254 |
|
void return_void() noexcept {}
|
| 255 |
|
void unhandled_exception() { std::terminate(); }
|
255 |
|
void unhandled_exception() { std::terminate(); }
|
| 256 |
|
|
256 |
|
|
| 257 |
|
// Pass through simple awaitables, inject executor/stop_token for IoAwaitable
|
257 |
|
// Pass through simple awaitables, inject executor/stop_token for IoAwaitable
|
| 258 |
|
template<class Awaitable>
|
258 |
|
template<class Awaitable>
|
| 259 |
|
auto await_transform(Awaitable&& a)
|
259 |
|
auto await_transform(Awaitable&& a)
|
| 260 |
|
|
260 |
|
|
| 261 |
|
using AwaitableT = std::decay_t<Awaitable>;
|
261 |
|
using AwaitableT = std::decay_t<Awaitable>;
|
| 262 |
|
// Simple awaitable: has await_suspend(coroutine_handle<>) but not IoAwaitable
|
262 |
|
// Simple awaitable: has await_suspend(coroutine_handle<>) but not IoAwaitable
|
| 263 |
|
|
263 |
|
|
| 264 |
|
requires { a.await_suspend(std::declval<std::coroutine_handle<>>()); } &&
|
264 |
|
requires { a.await_suspend(std::declval<std::coroutine_handle<>>()); } &&
|
| 265 |
|
!capy::IoAwaitable<AwaitableT>)
|
265 |
|
!capy::IoAwaitable<AwaitableT>)
|
| 266 |
|
|
266 |
|
|
| 267 |
|
return std::forward<Awaitable>(a);
|
267 |
|
return std::forward<Awaitable>(a);
|
| 268 |
|
|
268 |
|
|
| 269 |
|
|
269 |
|
|
| 270 |
|
|
270 |
|
|
| 271 |
|
|
271 |
|
|
| 272 |
|
|
272 |
|
|
| 273 |
|
|
273 |
|
|
| 274 |
|
|
274 |
|
|
| 275 |
|
|
275 |
|
|
| 276 |
|
|
276 |
|
|
| 277 |
|
bool await_ready() { return aw.await_ready(); }
|
277 |
|
bool await_ready() { return aw.await_ready(); }
|
| 278 |
|
decltype(auto) await_resume() { return aw.await_resume(); }
|
278 |
|
decltype(auto) await_resume() { return aw.await_resume(); }
|
| 279 |
|
|
279 |
|
|
| 280 |
|
auto await_suspend(std::coroutine_handle<promise_type> h)
|
280 |
|
auto await_suspend(std::coroutine_handle<promise_type> h)
|
| 281 |
|
|
281 |
|
|
| 282 |
|
static_assert(capy::IoAwaitable<AwaitableT>);
|
282 |
|
static_assert(capy::IoAwaitable<AwaitableT>);
|
| 283 |
|
return aw.await_suspend(h, *ex_ptr, *st_ptr);
|
283 |
|
return aw.await_suspend(h, *ex_ptr, *st_ptr);
|
| 284 |
|
|
284 |
|
|
| 285 |
|
|
285 |
|
|
| 286 |
|
return adapter{std::forward<Awaitable>(a), &ex, &st};
|
286 |
|
return adapter{std::forward<Awaitable>(a), &ex, &st};
|
| 287 |
|
|
287 |
|
|
| 288 |
|
|
288 |
|
|
| 289 |
|
|
289 |
|
|
| 290 |
|
|
290 |
|
|
| 291 |
|
std::coroutine_handle<promise_type> h;
|
291 |
|
std::coroutine_handle<promise_type> h;
|
| 292 |
|
|
292 |
|
|
| 293 |
|
launch_wrapper(std::coroutine_handle<promise_type> handle) noexcept
|
293 |
|
launch_wrapper(std::coroutine_handle<promise_type> handle) noexcept
|
| 294 |
|
|
294 |
|
|
| 295 |
|
|
295 |
|
|
| 296 |
|
|
296 |
|
|
| 297 |
|
|
297 |
|
|
| 298 |
|
|
298 |
|
|
| 299 |
|
|
299 |
|
|
| 300 |
|
|
300 |
|
|
| 301 |
|
|
301 |
|
|
| 302 |
|
|
302 |
|
|
| 303 |
|
|
303 |
|
|
| 304 |
|
launch_wrapper(launch_wrapper&& o) noexcept
|
304 |
|
launch_wrapper(launch_wrapper&& o) noexcept
|
| 305 |
|
: h(std::exchange(o.h, nullptr))
|
305 |
|
: h(std::exchange(o.h, nullptr))
|
| 306 |
|
|
306 |
|
|
| 307 |
|
|
307 |
|
|
| 308 |
|
|
308 |
|
|
| 309 |
|
launch_wrapper(launch_wrapper const&) = delete;
|
309 |
|
launch_wrapper(launch_wrapper const&) = delete;
|
| 310 |
|
launch_wrapper& operator=(launch_wrapper const&) = delete;
|
310 |
|
launch_wrapper& operator=(launch_wrapper const&) = delete;
|
| 311 |
|
launch_wrapper& operator=(launch_wrapper&&) = delete;
|
311 |
|
launch_wrapper& operator=(launch_wrapper&&) = delete;
|
| 312 |
|
|
312 |
|
|
| 313 |
|
|
313 |
|
|
| 314 |
|
// Named functor to avoid incomplete lambda type in coroutine promise
|
314 |
|
// Named functor to avoid incomplete lambda type in coroutine promise
|
| 315 |
|
|
315 |
|
|
| 316 |
|
|
316 |
|
|
| 317 |
|
|
317 |
|
|
| 318 |
|
launch_wrapper<Executor> operator()(
|
318 |
|
launch_wrapper<Executor> operator()(
|
| 319 |
|
|
319 |
|
|
| 320 |
|
|
320 |
|
|
| 321 |
|
|
321 |
|
|
| 322 |
|
|
322 |
|
|
| 323 |
|
|
323 |
|
|
| 324 |
|
|
324 |
|
|
| 325 |
|
// Executor and stop token stored in promise via constructor
|
325 |
|
// Executor and stop token stored in promise via constructor
|
| 326 |
|
|
326 |
|
|
| 327 |
|
co_await self->push(*wp);
|
327 |
|
co_await self->push(*wp);
|
| 328 |
|
|
328 |
|
|
| 329 |
|
|
329 |
|
|
| 330 |
|
|
330 |
|
|
| 331 |
|
|
331 |
|
|
| 332 |
|
|
332 |
|
|
| 333 |
|
|
333 |
|
|
| 334 |
|
|
334 |
|
|
| 335 |
|
|
335 |
|
|
| 336 |
|
|
336 |
|
|
| 337 |
|
|
337 |
|
|
| 338 |
|
|
338 |
|
|
| 339 |
|
|
339 |
|
|
| 340 |
|
|
340 |
|
|
| 341 |
|
|
341 |
|
|
| 342 |
|
|
342 |
|
|
| 343 |
|
|
343 |
|
|
| 344 |
|
|
344 |
|
|
| 345 |
|
bool await_ready() const noexcept
|
345 |
|
bool await_ready() const noexcept
|
| 346 |
|
|
346 |
|
|
| 347 |
|
|
347 |
|
|
| 348 |
|
|
348 |
|
|
| 349 |
|
|
349 |
|
|
| 350 |
|
|
350 |
|
|
| 351 |
|
|
351 |
|
|
| 352 |
|
|
352 |
|
|
| 353 |
|
std::coroutine_handle<> h,
|
353 |
|
std::coroutine_handle<> h,
|
| 354 |
|
Ex const&, std::stop_token) noexcept
|
354 |
|
Ex const&, std::stop_token) noexcept
|
| 355 |
|
|
355 |
|
|
| 356 |
|
// Dispatch to server's executor before touching shared state
|
356 |
|
// Dispatch to server's executor before touching shared state
|
| 357 |
|
|
357 |
|
|
| 358 |
|
return std::noop_coroutine();
|
358 |
|
return std::noop_coroutine();
|
| 359 |
|
|
359 |
|
|
| 360 |
|
|
360 |
|
|
| 361 |
|
void await_resume() noexcept
|
361 |
|
void await_resume() noexcept
|
| 362 |
|
|
362 |
|
|
| 363 |
|
// Running on server executor - safe to modify lists
|
363 |
|
// Running on server executor - safe to modify lists
|
| 364 |
|
// Remove from active (if present), then wake waiter or add to idle
|
364 |
|
// Remove from active (if present), then wake waiter or add to idle
|
| 365 |
|
self_.active_remove(&w_);
|
365 |
|
self_.active_remove(&w_);
|
| 366 |
|
|
366 |
|
|
| 367 |
|
|
367 |
|
|
| 368 |
|
auto* wait = self_.waiters_;
|
368 |
|
auto* wait = self_.waiters_;
|
| 369 |
|
self_.waiters_ = wait->next;
|
369 |
|
self_.waiters_ = wait->next;
|
| 370 |
|
|
370 |
|
|
| 371 |
|
|
371 |
|
|
| 372 |
|
|
372 |
|
|
| 373 |
|
|
373 |
|
|
| 374 |
|
|
374 |
|
|
| 375 |
|
|
375 |
|
|
| 376 |
|
|
376 |
|
|
| 377 |
|
|
377 |
|
|
| 378 |
|
|
378 |
|
|
| 379 |
|
|
379 |
|
|
| 380 |
|
|
380 |
|
|
| 381 |
|
|
381 |
|
|
| 382 |
|
|
382 |
|
|
| 383 |
|
|
383 |
|
|
| 384 |
|
|
384 |
|
|
| 385 |
|
|
385 |
|
|
| 386 |
|
pop_awaitable(tcp_server& self) noexcept
|
386 |
|
pop_awaitable(tcp_server& self) noexcept
|
| 387 |
|
|
387 |
|
|
| 388 |
|
|
388 |
|
|
| 389 |
|
|
389 |
|
|
| 390 |
|
|
390 |
|
|
| 391 |
|
|
391 |
|
|
| 392 |
|
bool await_ready() const noexcept
|
392 |
|
bool await_ready() const noexcept
|
| 393 |
|
|
393 |
|
|
| 394 |
|
return !self_.idle_empty();
|
394 |
|
return !self_.idle_empty();
|
| 395 |
|
|
395 |
|
|
| 396 |
|
|
396 |
|
|
| 397 |
|
|
397 |
|
|
| 398 |
|
|
398 |
|
|
| 399 |
|
|
399 |
|
|
| 400 |
|
std::coroutine_handle<> h,
|
400 |
|
std::coroutine_handle<> h,
|
| 401 |
|
Ex const&, std::stop_token) noexcept
|
401 |
|
Ex const&, std::stop_token) noexcept
|
| 402 |
|
|
402 |
|
|
| 403 |
|
// Running on server executor (do_accept runs there)
|
403 |
|
// Running on server executor (do_accept runs there)
|
| 404 |
|
|
404 |
|
|
| 405 |
|
|
405 |
|
|
| 406 |
|
wait_.next = self_.waiters_;
|
406 |
|
wait_.next = self_.waiters_;
|
| 407 |
|
|
407 |
|
|
| 408 |
|
|
408 |
|
|
| 409 |
|
|
409 |
|
|
| 410 |
|
|
410 |
|
|
| 411 |
|
worker_base& await_resume() noexcept
|
411 |
|
worker_base& await_resume() noexcept
|
| 412 |
|
|
412 |
|
|
| 413 |
|
// Running on server executor
|
413 |
|
// Running on server executor
|
| 414 |
|
|
414 |
|
|
| 415 |
|
return *wait_.w; // Woken by push_awaitable
|
415 |
|
return *wait_.w; // Woken by push_awaitable
|
| 416 |
|
return *self_.idle_pop();
|
416 |
|
return *self_.idle_pop();
|
| 417 |
|
|
417 |
|
|
| 418 |
|
|
418 |
|
|
| 419 |
|
|
419 |
|
|
| 420 |
|
push_awaitable push(worker_base& w)
|
420 |
|
push_awaitable push(worker_base& w)
|
| 421 |
|
|
421 |
|
|
| 422 |
|
return push_awaitable{*this, w};
|
422 |
|
return push_awaitable{*this, w};
|
| 423 |
|
|
423 |
|
|
| 424 |
|
|
424 |
|
|
| 425 |
|
// Synchronous version for destructor/guard paths
|
425 |
|
// Synchronous version for destructor/guard paths
|
| 426 |
|
// Must be called from server executor context
|
426 |
|
// Must be called from server executor context
|
| 427 |
|
void push_sync(worker_base& w) noexcept
|
427 |
|
void push_sync(worker_base& w) noexcept
|
| 428 |
|
|
428 |
|
|
| 429 |
|
|
429 |
|
|
| 430 |
|
|
430 |
|
|
| 431 |
|
|
431 |
|
|
| 432 |
|
|
432 |
|
|
| 433 |
|
|
433 |
|
|
| 434 |
|
|
434 |
|
|
| 435 |
|
|
435 |
|
|
| 436 |
|
|
436 |
|
|
| 437 |
|
|
437 |
|
|
| 438 |
|
|
438 |
|
|
| 439 |
|
|
439 |
|
|
| 440 |
|
|
440 |
|
|
| 441 |
|
|
441 |
|
|
| 442 |
|
|
442 |
|
|
| 443 |
|
|
443 |
|
|
| 444 |
|
|
444 |
|
|
| 445 |
|
return pop_awaitable{*this};
|
445 |
|
return pop_awaitable{*this};
|
| 446 |
|
|
446 |
|
|
| 447 |
|
|
447 |
|
|
| 448 |
|
capy::task<void> do_accept(tcp_acceptor& acc);
|
448 |
|
capy::task<void> do_accept(tcp_acceptor& acc);
|
| 449 |
|
|
449 |
|
|
| 450 |
|
|
450 |
|
|
| 451 |
|
/** Abstract base class for connection handlers.
|
451 |
|
/** Abstract base class for connection handlers.
|
| 452 |
|
|
452 |
|
|
| 453 |
|
Derive from this class to implement custom connection handling.
|
453 |
|
Derive from this class to implement custom connection handling.
|
| 454 |
|
Each worker owns a socket and is reused across multiple
|
454 |
|
Each worker owns a socket and is reused across multiple
|
| 455 |
|
connections to avoid per-connection allocation.
|
455 |
|
connections to avoid per-connection allocation.
|
| 456 |
|
|
456 |
|
|
| 457 |
|
@see tcp_server, launcher
|
457 |
|
@see tcp_server, launcher
|
| 458 |
|
|
458 |
|
|
| 459 |
|
|
459 |
|
|
| 460 |
|
|
460 |
|
|
| 461 |
|
|
461 |
|
|
| 462 |
|
// Ordered largest to smallest for optimal packing
|
462 |
|
// Ordered largest to smallest for optimal packing
|
| 463 |
|
std::stop_source stop_; // ~16 bytes
|
463 |
|
std::stop_source stop_; // ~16 bytes
|
| 464 |
|
worker_base* next_ = nullptr; // 8 bytes - used by idle and active lists
|
464 |
|
worker_base* next_ = nullptr; // 8 bytes - used by idle and active lists
|
| 465 |
|
worker_base* prev_ = nullptr; // 8 bytes - used only by active list
|
465 |
|
worker_base* prev_ = nullptr; // 8 bytes - used only by active list
|
| 466 |
|
|
466 |
|
|
| 467 |
|
|
467 |
|
|
| 468 |
|
|
468 |
|
|
| 469 |
|
|
469 |
|
|
| 470 |
|
|
470 |
|
|
| 471 |
|
virtual ~worker_base() = default;
|
471 |
|
virtual ~worker_base() = default;
|
| 472 |
|
|
472 |
|
|
| 473 |
|
/** Handle an accepted connection.
|
473 |
|
/** Handle an accepted connection.
|
| 474 |
|
|
474 |
|
|
| 475 |
|
Called when this worker is dispatched to handle a new
|
475 |
|
Called when this worker is dispatched to handle a new
|
| 476 |
|
connection. The implementation must invoke the launcher
|
476 |
|
connection. The implementation must invoke the launcher
|
| 477 |
|
exactly once to start the handling coroutine.
|
477 |
|
exactly once to start the handling coroutine.
|
| 478 |
|
|
478 |
|
|
| 479 |
|
@param launch Handle to launch the connection coroutine.
|
479 |
|
@param launch Handle to launch the connection coroutine.
|
| 480 |
|
|
480 |
|
|
| 481 |
|
virtual void run(launcher launch) = 0;
|
481 |
|
virtual void run(launcher launch) = 0;
|
| 482 |
|
|
482 |
|
|
| 483 |
|
/// Return the socket used for connections.
|
483 |
|
/// Return the socket used for connections.
|
| 484 |
|
virtual corosio::tcp_socket& socket() = 0;
|
484 |
|
virtual corosio::tcp_socket& socket() = 0;
|
| 485 |
|
|
485 |
|
|
| 486 |
|
|
486 |
|
|
| 487 |
|
/** Move-only handle to launch a worker coroutine.
|
487 |
|
/** Move-only handle to launch a worker coroutine.
|
| 488 |
|
|
488 |
|
|
| 489 |
|
Passed to @ref worker_base::run to start the connection-handling
|
489 |
|
Passed to @ref worker_base::run to start the connection-handling
|
| 490 |
|
coroutine. The launcher ensures the worker returns to the idle
|
490 |
|
coroutine. The launcher ensures the worker returns to the idle
|
| 491 |
|
pool when the coroutine completes or if launching fails.
|
491 |
|
pool when the coroutine completes or if launching fails.
|
| 492 |
|
|
492 |
|
|
| 493 |
|
The launcher must be invoked exactly once via `operator()`.
|
493 |
|
The launcher must be invoked exactly once via `operator()`.
|
| 494 |
|
If destroyed without invoking, the worker is returned to the
|
494 |
|
If destroyed without invoking, the worker is returned to the
|
| 495 |
|
|
495 |
|
|
| 496 |
|
|
496 |
|
|
| 497 |
|
|
497 |
|
|
| 498 |
|
|
498 |
|
|
| 499 |
|
|
499 |
|
|
| 500 |
|
|
500 |
|
|
| 501 |
|
|
501 |
|
|
| 502 |
|
|
502 |
|
|
| 503 |
|
|
503 |
|
|
| 504 |
|
|
504 |
|
|
| 505 |
|
|
505 |
|
|
| 506 |
|
|
506 |
|
|
| 507 |
|
launcher(tcp_server& srv, worker_base& w) noexcept
|
507 |
|
launcher(tcp_server& srv, worker_base& w) noexcept
|
| 508 |
|
|
508 |
|
|
| 509 |
|
|
509 |
|
|
| 510 |
|
|
510 |
|
|
| 511 |
|
|
511 |
|
|
| 512 |
|
|
512 |
|
|
| 513 |
|
|
513 |
|
|
| 514 |
|
/// Return the worker to the pool if not launched.
|
514 |
|
/// Return the worker to the pool if not launched.
|
| 515 |
|
|
515 |
|
|
| 516 |
|
|
516 |
|
|
| 517 |
|
|
517 |
|
|
| 518 |
|
|
518 |
|
|
| 519 |
|
|
519 |
|
|
| 520 |
|
|
520 |
|
|
| 521 |
|
launcher(launcher&& o) noexcept
|
521 |
|
launcher(launcher&& o) noexcept
|
| 522 |
|
|
522 |
|
|
| 523 |
|
, w_(std::exchange(o.w_, nullptr))
|
523 |
|
, w_(std::exchange(o.w_, nullptr))
|
| 524 |
|
|
524 |
|
|
| 525 |
|
|
525 |
|
|
| 526 |
|
launcher(launcher const&) = delete;
|
526 |
|
launcher(launcher const&) = delete;
|
| 527 |
|
launcher& operator=(launcher const&) = delete;
|
527 |
|
launcher& operator=(launcher const&) = delete;
|
| 528 |
|
launcher& operator=(launcher&&) = delete;
|
528 |
|
launcher& operator=(launcher&&) = delete;
|
| 529 |
|
|
529 |
|
|
| 530 |
|
/** Launch the connection-handling coroutine.
|
530 |
|
/** Launch the connection-handling coroutine.
|
| 531 |
|
|
531 |
|
|
| 532 |
|
Starts the given coroutine on the specified executor. When
|
532 |
|
Starts the given coroutine on the specified executor. When
|
| 533 |
|
the coroutine completes, the worker is automatically returned
|
533 |
|
the coroutine completes, the worker is automatically returned
|
| 534 |
|
|
534 |
|
|
| 535 |
|
|
535 |
|
|
| 536 |
|
@param ex The executor to run the coroutine on.
|
536 |
|
@param ex The executor to run the coroutine on.
|
| 537 |
|
@param task The coroutine to execute.
|
537 |
|
@param task The coroutine to execute.
|
| 538 |
|
|
538 |
|
|
| 539 |
|
@throws std::logic_error If this launcher was already invoked.
|
539 |
|
@throws std::logic_error If this launcher was already invoked.
|
| 540 |
|
|
540 |
|
|
| 541 |
|
|
541 |
|
|
| 542 |
|
void operator()(Executor const& ex, capy::task<void> task)
|
542 |
|
void operator()(Executor const& ex, capy::task<void> task)
|
| 543 |
|
|
543 |
|
|
| 544 |
|
|
544 |
|
|
| 545 |
|
detail::throw_logic_error(); // launcher already invoked
|
545 |
|
detail::throw_logic_error(); // launcher already invoked
|
| 546 |
|
|
546 |
|
|
| 547 |
|
auto* w = std::exchange(w_, nullptr);
|
547 |
|
auto* w = std::exchange(w_, nullptr);
|
| 548 |
|
|
548 |
|
|
| 549 |
|
// Worker is being dispatched - add to active list
|
549 |
|
// Worker is being dispatched - add to active list
|
| 550 |
|
|
550 |
|
|
| 551 |
|
|
551 |
|
|
| 552 |
|
// Return worker to pool if coroutine setup throws
|
552 |
|
// Return worker to pool if coroutine setup throws
|
| 553 |
|
|
553 |
|
|
| 554 |
|
|
554 |
|
|
| 555 |
|
|
555 |
|
|
| 556 |
|
~guard_t() { if(w) srv->push_sync(*w); }
|
556 |
|
~guard_t() { if(w) srv->push_sync(*w); }
|
| 557 |
|
|
557 |
|
|
| 558 |
|
|
558 |
|
|
| 559 |
|
// Reset worker's stop source for this connection
|
559 |
|
// Reset worker's stop source for this connection
|
| 560 |
|
|
560 |
|
|
| 561 |
|
auto st = w->stop_.get_token();
|
561 |
|
auto st = w->stop_.get_token();
|
| 562 |
|
|
562 |
|
|
| 563 |
|
auto wrapper = launch_coro<Executor>{}(
|
563 |
|
auto wrapper = launch_coro<Executor>{}(
|
| 564 |
|
ex, st, srv_, std::move(task), w);
|
564 |
|
ex, st, srv_, std::move(task), w);
|
| 565 |
|
|
565 |
|
|
| 566 |
|
// Executor and stop token stored in promise via constructor
|
566 |
|
// Executor and stop token stored in promise via constructor
|
| 567 |
|
ex.post(std::exchange(wrapper.h, nullptr)); // Release before post
|
567 |
|
ex.post(std::exchange(wrapper.h, nullptr)); // Release before post
|
| 568 |
|
guard.w = nullptr; // Success - dismiss guard
|
568 |
|
guard.w = nullptr; // Success - dismiss guard
|
| 569 |
|
|
569 |
|
|
| 570 |
|
|
570 |
|
|
| 571 |
|
|
571 |
|
|
| 572 |
|
/** Construct a TCP server.
|
572 |
|
/** Construct a TCP server.
|
| 573 |
|
|
573 |
|
|
| 574 |
|
@tparam Ctx Execution context type satisfying ExecutionContext.
|
574 |
|
@tparam Ctx Execution context type satisfying ExecutionContext.
|
| 575 |
|
@tparam Ex Executor type satisfying Executor.
|
575 |
|
@tparam Ex Executor type satisfying Executor.
|
| 576 |
|
|
576 |
|
|
| 577 |
|
@param ctx The execution context for socket operations.
|
577 |
|
@param ctx The execution context for socket operations.
|
| 578 |
|
@param ex The executor for dispatching coroutines.
|
578 |
|
@param ex The executor for dispatching coroutines.
|
| 579 |
|
|
579 |
|
|
| 580 |
|
|
580 |
|
|
| 581 |
|
|
581 |
|
|
| 582 |
|
tcp_server srv(ctx, ctx.get_executor());
|
582 |
|
tcp_server srv(ctx, ctx.get_executor());
|
| 583 |
|
srv.set_workers(make_workers(ctx, 100));
|
583 |
|
srv.set_workers(make_workers(ctx, 100));
|
| 584 |
|
|
584 |
|
|
| 585 |
|
|
585 |
|
|
| 586 |
|
|
586 |
|
|
| 587 |
|
|
587 |
|
|
| 588 |
|
|
588 |
|
|
| 589 |
|
capy::ExecutionContext Ctx,
|
589 |
|
capy::ExecutionContext Ctx,
|
| 590 |
|
|
590 |
|
|
| 591 |
|
tcp_server(Ctx& ctx, Ex ex)
|
591 |
|
tcp_server(Ctx& ctx, Ex ex)
|
| 592 |
|
|
592 |
|
|
| 593 |
|
|
593 |
|
|
| 594 |
|
|
594 |
|
|
| 595 |
|
|
595 |
|
|
| 596 |
|
|
596 |
|
|
| 597 |
|
|
597 |
|
|
| 598 |
|
|
598 |
|
|
| 599 |
|
tcp_server(tcp_server const&) = delete;
|
599 |
|
tcp_server(tcp_server const&) = delete;
|
| 600 |
|
tcp_server& operator=(tcp_server const&) = delete;
|
600 |
|
tcp_server& operator=(tcp_server const&) = delete;
|
| 601 |
|
tcp_server(tcp_server&& o) noexcept;
|
601 |
|
tcp_server(tcp_server&& o) noexcept;
|
| 602 |
|
tcp_server& operator=(tcp_server&& o) noexcept;
|
602 |
|
tcp_server& operator=(tcp_server&& o) noexcept;
|
| 603 |
|
|
603 |
|
|
| 604 |
|
/** Bind to a local endpoint.
|
604 |
|
/** Bind to a local endpoint.
|
| 605 |
|
|
605 |
|
|
| 606 |
|
Creates an acceptor listening on the specified endpoint.
|
606 |
|
Creates an acceptor listening on the specified endpoint.
|
| 607 |
|
Multiple endpoints can be bound by calling this method
|
607 |
|
Multiple endpoints can be bound by calling this method
|
| 608 |
|
multiple times before @ref start.
|
608 |
|
multiple times before @ref start.
|
| 609 |
|
|
609 |
|
|
| 610 |
|
@param ep The local endpoint to bind to.
|
610 |
|
@param ep The local endpoint to bind to.
|
| 611 |
|
|
611 |
|
|
| 612 |
|
@return The error code if binding fails.
|
612 |
|
@return The error code if binding fails.
|
| 613 |
|
|
613 |
|
|
| 614 |
|
|
614 |
|
|
| 615 |
|
|
615 |
|
|
| 616 |
|
|
616 |
|
|
| 617 |
|
|
617 |
|
|
| 618 |
|
|
618 |
|
|
| 619 |
|
Replaces any existing workers with the given range. Any
|
619 |
|
Replaces any existing workers with the given range. Any
|
| 620 |
|
previous workers are released and the idle/active lists
|
620 |
|
previous workers are released and the idle/active lists
|
| 621 |
|
are cleared before populating with new workers.
|
621 |
|
are cleared before populating with new workers.
|
| 622 |
|
|
622 |
|
|
| 623 |
|
@tparam Range Forward range of pointer-like objects to worker_base.
|
623 |
|
@tparam Range Forward range of pointer-like objects to worker_base.
|
| 624 |
|
|
624 |
|
|
| 625 |
|
@param workers Range of workers to manage. Each element must
|
625 |
|
@param workers Range of workers to manage. Each element must
|
| 626 |
|
support `std::to_address()` yielding `worker_base*`.
|
626 |
|
support `std::to_address()` yielding `worker_base*`.
|
| 627 |
|
|
627 |
|
|
| 628 |
|
|
628 |
|
|
| 629 |
|
|
629 |
|
|
| 630 |
|
std::vector<std::unique_ptr<my_worker>> workers;
|
630 |
|
std::vector<std::unique_ptr<my_worker>> workers;
|
| 631 |
|
for(int i = 0; i < 100; ++i)
|
631 |
|
for(int i = 0; i < 100; ++i)
|
| 632 |
|
workers.push_back(std::make_unique<my_worker>(ctx));
|
632 |
|
workers.push_back(std::make_unique<my_worker>(ctx));
|
| 633 |
|
srv.set_workers(std::move(workers));
|
633 |
|
srv.set_workers(std::move(workers));
|
| 634 |
|
|
634 |
|
|
| 635 |
|
|
635 |
|
|
| 636 |
|
template<std::ranges::forward_range Range>
|
636 |
|
template<std::ranges::forward_range Range>
|
| 637 |
|
requires std::convertible_to<
|
637 |
|
requires std::convertible_to<
|
| 638 |
|
decltype(std::to_address(
|
638 |
|
decltype(std::to_address(
|
| 639 |
|
std::declval<std::ranges::range_value_t<Range>&>())),
|
639 |
|
std::declval<std::ranges::range_value_t<Range>&>())),
|
| 640 |
|
|
640 |
|
|
| 641 |
|
|
641 |
|
|
| 642 |
|
set_workers(Range&& workers)
|
642 |
|
set_workers(Range&& workers)
|
| 643 |
|
|
643 |
|
|
| 644 |
|
|
644 |
|
|
| 645 |
|
|
645 |
|
|
| 646 |
|
|
646 |
|
|
| 647 |
|
|
647 |
|
|
| 648 |
|
|
648 |
|
|
| 649 |
|
|
649 |
|
|
| 650 |
|
// Take ownership and populate idle list
|
650 |
|
// Take ownership and populate idle list
|
| 651 |
|
using StorageType = std::decay_t<Range>;
|
651 |
|
using StorageType = std::decay_t<Range>;
|
| 652 |
|
auto* p = new StorageType(std::forward<Range>(workers));
|
652 |
|
auto* p = new StorageType(std::forward<Range>(workers));
|
| 653 |
|
storage_ = std::shared_ptr<void>(p, [](void* ptr) {
|
653 |
|
storage_ = std::shared_ptr<void>(p, [](void* ptr) {
|
| 654 |
|
delete static_cast<StorageType*>(ptr);
|
654 |
|
delete static_cast<StorageType*>(ptr);
|
| 655 |
|
|
655 |
|
|
| 656 |
|
for(auto&& elem : *static_cast<StorageType*>(p))
|
656 |
|
for(auto&& elem : *static_cast<StorageType*>(p))
|
| 657 |
|
idle_push(std::to_address(elem));
|
657 |
|
idle_push(std::to_address(elem));
|
| 658 |
|
|
658 |
|
|
| 659 |
|
|
659 |
|
|
| 660 |
|
/** Start accepting connections.
|
660 |
|
/** Start accepting connections.
|
| 661 |
|
|
661 |
|
|
| 662 |
|
Launches accept loops for all bound endpoints. Incoming
|
662 |
|
Launches accept loops for all bound endpoints. Incoming
|
| 663 |
|
connections are dispatched to idle workers from the pool.
|
663 |
|
connections are dispatched to idle workers from the pool.
|
| 664 |
|
|
664 |
|
|
| 665 |
|
Calling `start()` on an already-running server has no effect.
|
665 |
|
Calling `start()` on an already-running server has no effect.
|
| 666 |
|
|
666 |
|
|
| 667 |
|
|
667 |
|
|
| 668 |
|
- At least one endpoint bound via @ref bind.
|
668 |
|
- At least one endpoint bound via @ref bind.
|
| 669 |
|
- Workers provided to the constructor.
|
669 |
|
- Workers provided to the constructor.
|
| 670 |
|
- If restarting, @ref join must have completed first.
|
670 |
|
- If restarting, @ref join must have completed first.
|
| 671 |
|
|
671 |
|
|
| 672 |
|
|
672 |
|
|
| 673 |
|
Creates one accept coroutine per bound endpoint. Each coroutine
|
673 |
|
Creates one accept coroutine per bound endpoint. Each coroutine
|
| 674 |
|
runs on the server's executor, waiting for connections and
|
674 |
|
runs on the server's executor, waiting for connections and
|
| 675 |
|
dispatching them to idle workers.
|
675 |
|
dispatching them to idle workers.
|
| 676 |
|
|
676 |
|
|
| 677 |
|
|
677 |
|
|
| 678 |
|
To restart after stopping, complete the full shutdown cycle:
|
678 |
|
To restart after stopping, complete the full shutdown cycle:
|
| 679 |
|
|
679 |
|
|
| 680 |
|
|
680 |
|
|
| 681 |
|
|
681 |
|
|
| 682 |
|
srv.stop(); // 1. Signal shutdown
|
682 |
|
srv.stop(); // 1. Signal shutdown
|
| 683 |
|
ioc.run(); // 2. Drain remaining completions
|
683 |
|
ioc.run(); // 2. Drain remaining completions
|
| 684 |
|
srv.join(); // 3. Wait for accept loops
|
684 |
|
srv.join(); // 3. Wait for accept loops
|
| 685 |
|
|
685 |
|
|
| 686 |
|
|
686 |
|
|
| 687 |
|
|
687 |
|
|
| 688 |
|
|
688 |
|
|
| 689 |
|
|
689 |
|
|
| 690 |
|
|
690 |
|
|
| 691 |
|
|
691 |
|
|
| 692 |
|
|
692 |
|
|
| 693 |
|
|
693 |
|
|
| 694 |
|
@throws std::logic_error If a previous session has not been
|
694 |
|
@throws std::logic_error If a previous session has not been
|
| 695 |
|
joined (accept loops still active).
|
695 |
|
joined (accept loops still active).
|
| 696 |
|
|
696 |
|
|
| 697 |
|
|
697 |
|
|
| 698 |
|
|
698 |
|
|
| 699 |
|
/** Stop accepting connections.
|
699 |
|
/** Stop accepting connections.
|
| 700 |
|
|
700 |
|
|
| 701 |
|
Signals all listening ports to stop accepting new connections
|
701 |
|
Signals all listening ports to stop accepting new connections
|
| 702 |
|
and requests cancellation of active workers via their stop tokens.
|
702 |
|
and requests cancellation of active workers via their stop tokens.
|
| 703 |
|
|
703 |
|
|
| 704 |
|
This function returns immediately; it does not wait for workers
|
704 |
|
This function returns immediately; it does not wait for workers
|
| 705 |
|
to finish. Pending I/O operations complete asynchronously.
|
705 |
|
to finish. Pending I/O operations complete asynchronously.
|
| 706 |
|
|
706 |
|
|
| 707 |
|
Calling `stop()` on a non-running server has no effect.
|
707 |
|
Calling `stop()` on a non-running server has no effect.
|
| 708 |
|
|
708 |
|
|
| 709 |
|
|
709 |
|
|
| 710 |
|
- Closes all acceptors (pending accepts complete with error).
|
710 |
|
- Closes all acceptors (pending accepts complete with error).
|
| 711 |
|
- Requests stop on each active worker's stop token.
|
711 |
|
- Requests stop on each active worker's stop token.
|
| 712 |
|
- Workers observing their stop token should exit promptly.
|
712 |
|
- Workers observing their stop token should exit promptly.
|
| 713 |
|
|
713 |
|
|
| 714 |
|
|
714 |
|
|
| 715 |
|
No new connections will be accepted. Active workers continue
|
715 |
|
No new connections will be accepted. Active workers continue
|
| 716 |
|
until they observe their stop token or complete naturally.
|
716 |
|
until they observe their stop token or complete naturally.
|
| 717 |
|
|
717 |
|
|
| 718 |
|
|
718 |
|
|
| 719 |
|
|
719 |
|
|
| 720 |
|
1. Let `ioc.run()` return (drains pending completions).
|
720 |
|
1. Let `ioc.run()` return (drains pending completions).
|
| 721 |
|
2. Call @ref join to wait for accept loops to finish.
|
721 |
|
2. Call @ref join to wait for accept loops to finish.
|
| 722 |
|
3. Only then is it safe to restart or destroy the server.
|
722 |
|
3. Only then is it safe to restart or destroy the server.
|
| 723 |
|
|
723 |
|
|
| 724 |
|
|
724 |
|
|
| 725 |
|
|
725 |
|
|
| 726 |
|
|
726 |
|
|
| 727 |
|
|
727 |
|
|
| 728 |
|
|
728 |
|
|
| 729 |
|
|
729 |
|
|
| 730 |
|
|
730 |
|
|
| 731 |
|
/** Block until all accept loops complete.
|
731 |
|
/** Block until all accept loops complete.
|
| 732 |
|
|
732 |
|
|
| 733 |
|
Blocks the calling thread until all accept coroutines launched
|
733 |
|
Blocks the calling thread until all accept coroutines launched
|
| 734 |
|
by @ref start have finished executing. This synchronizes the
|
734 |
|
by @ref start have finished executing. This synchronizes the
|
| 735 |
|
shutdown sequence, ensuring the server is fully stopped before
|
735 |
|
shutdown sequence, ensuring the server is fully stopped before
|
| 736 |
|
restarting or destroying it.
|
736 |
|
restarting or destroying it.
|
| 737 |
|
|
737 |
|
|
| 738 |
|
|
738 |
|
|
| 739 |
|
@ref stop has been called and `ioc.run()` has returned.
|
739 |
|
@ref stop has been called and `ioc.run()` has returned.
|
| 740 |
|
|
740 |
|
|
| 741 |
|
|
741 |
|
|
| 742 |
|
All accept loops have completed. The server is in the stopped
|
742 |
|
All accept loops have completed. The server is in the stopped
|
| 743 |
|
state and may be restarted via @ref start.
|
743 |
|
state and may be restarted via @ref start.
|
| 744 |
|
|
744 |
|
|
| 745 |
|
@par Example (Correct Usage)
|
745 |
|
@par Example (Correct Usage)
|
| 746 |
|
|
746 |
|
|
| 747 |
|
|
747 |
|
|
| 748 |
|
|
748 |
|
|
| 749 |
|
ioc.run(); // Blocks until work completes
|
749 |
|
ioc.run(); // Blocks until work completes
|
| 750 |
|
srv.join(); // Safe: called after ioc.run() returns
|
750 |
|
srv.join(); // Safe: called after ioc.run() returns
|
| 751 |
|
|
751 |
|
|
| 752 |
|
|
752 |
|
|
| 753 |
|
@par WARNING: Deadlock Scenarios
|
753 |
|
@par WARNING: Deadlock Scenarios
|
| 754 |
|
Calling `join()` from the wrong context causes deadlock:
|
754 |
|
Calling `join()` from the wrong context causes deadlock:
|
| 755 |
|
|
755 |
|
|
| 756 |
|
|
756 |
|
|
| 757 |
|
// WRONG: calling join() from inside a worker coroutine
|
757 |
|
// WRONG: calling join() from inside a worker coroutine
|
| 758 |
|
void run( launcher launch ) override
|
758 |
|
void run( launcher launch ) override
|
| 759 |
|
|
759 |
|
|
| 760 |
|
launch( ex, [this]() -> capy::task<>
|
760 |
|
launch( ex, [this]() -> capy::task<>
|
| 761 |
|
|
761 |
|
|
| 762 |
|
srv_.join(); // DEADLOCK: blocks the executor
|
762 |
|
srv_.join(); // DEADLOCK: blocks the executor
|
| 763 |
|
|
763 |
|
|
| 764 |
|
|
764 |
|
|
| 765 |
|
|
765 |
|
|
| 766 |
|
|
766 |
|
|
| 767 |
|
// WRONG: calling join() while ioc.run() is still active
|
767 |
|
// WRONG: calling join() while ioc.run() is still active
|
| 768 |
|
std::thread t( [&]{ ioc.run(); } );
|
768 |
|
std::thread t( [&]{ ioc.run(); } );
|
| 769 |
|
|
769 |
|
|
| 770 |
|
srv.join(); // DEADLOCK: ioc.run() still running in thread t
|
770 |
|
srv.join(); // DEADLOCK: ioc.run() still running in thread t
|
| 771 |
|
|
771 |
|
|
| 772 |
|
|
772 |
|
|
| 773 |
|
|
773 |
|
|
| 774 |
|
May be called from any thread, but will deadlock if called
|
774 |
|
May be called from any thread, but will deadlock if called
|
| 775 |
|
from within the io_context event loop or from a worker coroutine.
|
775 |
|
from within the io_context event loop or from a worker coroutine.
|
| 776 |
|
|
776 |
|
|
| 777 |
|
|
777 |
|
|
| 778 |
|
|
778 |
|
|
| 779 |
|
|
779 |
|
|
| 780 |
|
|
780 |
|
|
| 781 |
|
|
781 |
|
|
| 782 |
|
|
782 |
|
|
| 783 |
|
|
783 |
|
|
| 784 |
|
|
784 |
|
|
| 785 |
|
|
785 |
|
|
| 786 |
|
|
786 |
|
|
| 787 |
|
|
787 |
|
|
| 788 |
|
|
788 |
|
|
| 789 |
|
} // namespace boost::corosio
|
789 |
|
} // namespace boost::corosio
|
| 790 |
|
|
790 |
|
|
| 791 |
|
|
791 |
|
|