-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.cpp
84 lines (69 loc) · 2.34 KB
/
server.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
#include <async/accept.hpp>
#include <async/write.hpp>
#include <conduit/coroutine.hpp>
#include <atomic>
#include <iostream>
#include <thread>
using boost::asio::ip::tcp;
namespace asio = boost::asio;
using namespace conduit;
std::string make_daytime_string() {
using namespace std; // For time_t, time and ctime;
time_t now = time(0);
return ctime(&now);
}
coroutine do_write(tcp::socket socket, std::string message) {
auto&& [status, write_size] = co_await async::write(socket, message);
if (status) {
std::cerr << ("Write error: " + status.message() + '\n');
co_return;
}
}
coroutine start_server(asio::io_context& context, tcp::acceptor& acceptor) {
while (true) {
// Accept an incoming connection
auto socket = tcp::socket(context);
auto& status = co_await async::accept(acceptor, socket);
if (status) {
std::cerr << ("Server error: " + status.message() + '\n');
continue;
}
// Transfer ownership of the socket to the write command
do_write(std::move(socket), make_daytime_string());
}
context.stop();
}
int main(int argc, char** argv) {
try {
boost::asio::io_context context;
auto endpoint = tcp::endpoint(tcp::v4(), 13);
auto acceptor = tcp::acceptor(context, endpoint);
int num_threads = 8;
// jthreads automatically join on destruction
// so they're preferrable to std::thread
std::vector<std::jthread> threads(num_threads);
for (auto& t : threads) {
t = std::jthread([&] {
start_server(context, acceptor);
context.run();
});
}
std::cout << "Type 'halt' to halt the server or press Crtl+D." << std::endl;
std::string line;
// Wait until EOF from std::cin
while(std::cin) {
std::cout << "> " << std::flush;
std::getline(std::cin, line);
if(line == "halt")
break;
}
std::cerr << "Stopping" << '\n';
// Cancel all coroutines
context.stop();
} catch (std::exception& e) {
std::cerr << e.what() << std::endl;
std::cerr << "A daytime server runs on port 13, so it needs to be run with sudo. Try:\n";
std::cerr << "\n $ sudo " << argv[0] << '\n';
}
return 0;
}