tcp_server.cpp
2.38 KB
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
//
// tcp_server.cpp
// ~~~~~~~~~~~~~~
//
// Copyright (c) 2003-2020 Christopher M. Kohlhoff (chris at kohlhoff dot com)
//
// Distributed under the Boost Software License, Version 1.0. (See accompanying
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
//
#include <asio/io_context.hpp>
#include <asio/ip/tcp.hpp>
#include <asio/read.hpp>
#include <asio/write.hpp>
#include <boost/shared_ptr.hpp>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <vector>
using asio::ip::tcp;
#include <asio/yield.hpp>
class tcp_server : asio::coroutine
{
public:
tcp_server(tcp::acceptor& acceptor, std::size_t buf_size) :
acceptor_(acceptor),
socket_(acceptor_.get_executor()),
buffer_(buf_size)
{
}
void operator()(asio::error_code ec, std::size_t n = 0)
{
reenter (this) for (;;)
{
yield acceptor_.async_accept(socket_, ref(this));
while (!ec)
{
yield asio::async_read(socket_,
asio::buffer(buffer_), ref(this));
if (!ec)
{
for (std::size_t i = 0; i < n; ++i) buffer_[i] = ~buffer_[i];
yield asio::async_write(socket_,
asio::buffer(buffer_), ref(this));
}
}
socket_.close();
}
}
struct ref
{
explicit ref(tcp_server* p)
: p_(p)
{
}
void operator()(asio::error_code ec, std::size_t n = 0)
{
(*p_)(ec, n);
}
private:
tcp_server* p_;
};
private:
tcp::acceptor& acceptor_;
tcp::socket socket_;
std::vector<unsigned char> buffer_;
tcp::endpoint sender_;
};
#include <asio/unyield.hpp>
int main(int argc, char* argv[])
{
if (argc != 5)
{
std::fprintf(stderr,
"Usage: tcp_server <port> <nconns> "
"<bufsize> {spin|block}\n");
return 1;
}
unsigned short port = static_cast<unsigned short>(std::atoi(argv[1]));
int max_connections = std::atoi(argv[2]);
std::size_t buf_size = std::atoi(argv[3]);
bool spin = (std::strcmp(argv[4], "spin") == 0);
asio::io_context io_context(1);
tcp::acceptor acceptor(io_context, tcp::endpoint(tcp::v4(), port));
std::vector<boost::shared_ptr<tcp_server> > servers;
for (int i = 0; i < max_connections; ++i)
{
boost::shared_ptr<tcp_server> s(new tcp_server(acceptor, buf_size));
servers.push_back(s);
(*s)(asio::error_code());
}
if (spin)
for (;;) io_context.poll();
else
io_context.run();
}