-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcoroutines.cpp
86 lines (74 loc) · 1.96 KB
/
coroutines.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
#include <boost/asio.hpp>
#include <thread>
#include <iostream>
#include <cstdlib>
namespace net = boost::asio;
namespace this_coro = boost::asio::this_coro;
using net::ip::tcp;
using boost::asio::awaitable;
using boost::asio::co_spawn;
using boost::asio::detached;
using boost::asio::use_awaitable;
using net::dynamic_buffer;
awaitable<void> session(tcp::socket socket)
{
try
{
for (;;)
{
std::string command;
boost::system::error_code error;
auto length = co_await net::async_read_until(socket, dynamic_buffer(command), '\n', use_awaitable);
if (error == net::error::eof)
{
break;
}
else if (error)
{
throw boost::system::system_error(error);
}
if (command == "quit\n")
{
break;
}
co_await net::async_write(socket, net::buffer(command, length), use_awaitable);
}
}
catch (std::exception& e)
{
std::cerr << "Error in session: " << e.what() << '\n';
}
}
awaitable<void> server(unsigned short port)
{
auto executor = co_await this_coro::executor;
tcp::acceptor acceptor {executor, tcp::endpoint{tcp::v4(), port}};
for (;;)
{
auto socket = co_await acceptor.async_accept(use_awaitable);
co_spawn(executor, session(std::move(socket)), detached);
}
}
int main(int argc, char* argv[])
{
if (argc != 2)
{
std::cerr << "Binding port must be specified\n";
return 1;
}
try
{
net::io_context io_context;
co_spawn(io_context, server(std::atoi(argv[1])), detached);
std::vector<std::jthread> threads;
for (int i = 0; i < 10; ++i)
{
threads.emplace_back([&io_context]{ io_context.run(); });
}
}
catch (std::exception& e)
{
std::cerr << "Exception: " << e.what() << '\n';
}
return 0;
}