This repository has been archived by the owner on Jul 19, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
175 lines (145 loc) · 4.74 KB
/
main.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
/**
* @file
* @brief This file contains complete example of Boost.Beast HTTP
* server based on C++ stackless coroutines
*/
#include <fmt/core.h>
#include <boost/asio/awaitable.hpp>
#include <boost/asio/co_spawn.hpp>
#include <boost/asio/detached.hpp>
#include <boost/asio/experimental/as_tuple.hpp>
#include <boost/asio/ip/tcp.hpp>
#include <boost/asio/this_coro.hpp>
#include <boost/asio/use_awaitable.hpp>
#include <boost/beast/core.hpp>
#include <boost/beast/http.hpp>
#include <coroutine>
#include <ranges>
#include <thread>
namespace beast = boost::beast;
namespace asio {
using namespace boost::asio;
using boost::asio::experimental::as_tuple;
}
namespace ip = asio::ip;
using tcp = ip::tcp;
namespace http = beast::http;
/**
* @brief Send response in TCP stream
* @tparam [in] Code status code to respond with
* @tparam [in, optional] Body response body type
* @param [out] stream stream to which response will be sent
* @param [in, optional] Args optional arg such as string when the
* Body type is present
* @return Awaitable coroutine object
*/
template <http::status Code, typename Body = http::empty_body>
asio::awaitable<void> respond(beast::tcp_stream& stream,
auto... Args) {
http::response<Body> response;
response.result(Code);
if constexpr (std::same_as<Body, http::string_body>) {
response.body() = std::get<0>(std::forward_as_tuple(Args...));
}
response.prepare_payload();
co_await http::async_write(stream, response, asio::use_awaitable);
}
/**
* @brief Parse request and await on `respond()`
* @param [out] stream stream to which response will be sent
* @param [in] request incoming request to parse
* @return Awaitable coroutine object
*/
asio::awaitable<void> handle_request(
beast::tcp_stream& stream,
http::request<http::string_body> request) {
if (request.method() == http::verb::get) {
co_return co_await respond<http::status::ok, http::string_body>(
stream, request.body());
}
co_await respond<http::status::bad_request>(stream);
}
/**
* @brief Poll socket for new requests
* @param socket socket to poll
* @return Awaitable coroutine object
*/
asio::awaitable<void> poll_socket(tcp::socket socket) {
http::request<http::string_body> request;
beast::tcp_stream stream{std::move(socket)};
beast::flat_buffer buffer;
for (;;) {
auto [ec, size] = co_await http::async_read(
stream, buffer, request, asio::as_tuple(asio::use_awaitable));
if (ec) {
if (ec == http::error::end_of_stream) {
break;
}
fmt::print("{}\n", ec.message());
break;
}
bool close{request.need_eof()};
co_await handle_request(stream, std::move(request));
if (close) {
break;
}
request = {};
}
stream.socket().shutdown(tcp::socket::shutdown_send);
}
/**
* @brief Poll for new server connections
* @param address address on which clients will attempt to connect
* @param port port to listen to
* @return Awaitable coroutine object
*/
asio::awaitable<void> poll_connections(asio::ip::address address,
uint16_t port) {
auto executor = co_await asio::this_coro::executor;
tcp::endpoint endpoint{address, port};
tcp::acceptor acceptor{executor};
acceptor.open(endpoint.protocol());
acceptor.bind(endpoint);
acceptor.listen();
tcp::socket socket{executor};
for (;;) {
co_await acceptor.async_accept(socket, asio::use_awaitable);
asio::co_spawn(executor, poll_socket(std::move(socket)),
asio::detached);
socket = tcp::socket{executor};
}
}
/**
* @brief Start primary server poll
* @param address address on which clients will attempt to connect
* @param port port to listen to
* @param worker_threads amount of worker threads that will process
* new connections and requests
*/
void start_polling(std::string_view address, uint16_t port,
uint32_t worker_threads) {
int concurrency_hint{std::max(1, static_cast<int>(worker_threads))};
asio::io_context ioc{concurrency_hint};
asio::co_spawn(ioc.get_executor(),
poll_connections(ip::make_address(address), port),
asio::detached);
// Run the I/O service on the requested number of threads
std::vector<std::thread> v;
v.reserve(concurrency_hint - 1);
for (int i : std::views::iota(1, concurrency_hint)) {
v.emplace_back([&ioc] { ioc.run(); });
}
ioc.run();
}
int main(int argc, char* argv[]) {
// Check command line arguments
if (argc != 4) {
fmt::print(
"Usage: coroutine-server <address> <port> <threads>\n"
"Example:\n"
" coroutine-server 127.0.0.1 80 1\n");
return EXIT_FAILURE;
}
start_polling(argv[1], static_cast<uint16_t>(std::stoi(argv[2])),
std::stoi(argv[3]));
}