/
example.cpp
207 lines (165 loc) · 4.84 KB
/
example.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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
#include <cstdlib>
#include <cstdint>
#include <csignal>
#include <array>
#include <vector>
#include <iostream>
#include <stdexcept>
#include <boost/asio.hpp>
#include <boost/noncopyable.hpp>
#include <boost/thread.hpp>
#include <boost/thread/externally_locked_stream.hpp>
template<std::size_t BUFFER_SIZE = 0x10000>
class server : boost::noncopyable
{
public:
explicit server(unsigned short port)
:
signals_(io_), socket_(io_)
{
signals_.add(SIGINT);
signals_.add(SIGTERM);
signals_.add(SIGQUIT);
signals_.async_wait(
[this] (boost::system::error_code, int /* signo */)
{
io_.stop();
}
);
boost::asio::ip::udp::endpoint endpoint(boost::asio::ip::udp::v6(), port);
socket_.open(endpoint.protocol());
socket_.set_option(boost::asio::ip::udp::socket::reuse_address(true));
socket_.bind(endpoint);
receive();
}
void run()
{
io_.run();
}
private:
void receive()
{
socket_.async_receive_from(
boost::asio::buffer(buffer_), sender_endpoint_,
[this] (boost::system::error_code ec, std::size_t bytes_recvd)
{
if (!ec)
send(bytes_recvd);
else
receive();
}
);
}
void send(std::size_t length)
{
socket_.async_send_to(
boost::asio::buffer(buffer_, length), sender_endpoint_,
[this] (boost::system::error_code /*ec*/, std::size_t /*bytes_sent*/)
{
receive();
}
);
}
private:
boost::asio::io_service io_;
boost::asio::signal_set signals_;
boost::asio::ip::udp::socket socket_;
boost::asio::ip::udp::endpoint sender_endpoint_;
std::array<uint8_t, BUFFER_SIZE> buffer_;
};
class session : boost::noncopyable
{
public:
explicit session(boost::asio::ip::address const& address, unsigned short port);
void echo(std::vector<uint8_t> const& request);
private:
boost::asio::io_service io_;
boost::asio::ip::udp::socket socket_;
boost::asio::ip::udp::endpoint endpoint_;
};
class client : boost::noncopyable
{
public:
explicit client(std::ostream& ostream, boost::asio::ip::address const& address, unsigned short port)
: ostream_(ostream, terminal_mutex_), address_(address), port_(port)
{}
void run_test();
private:
void thread_func(unsigned n);
private:
boost::thread_group threads_;
boost::recursive_mutex terminal_mutex_;
boost::externally_locked_stream<std::ostream> ostream_;
boost::asio::ip::address address_;
unsigned short port_;
};
int main(int argc, char* argv[])
{
try
{
unsigned short port = 12122;
boost::asio::ip::address addr = boost::asio::ip::address::from_string("::1");
server<> serv(port);
client cli(std::cout, addr, port);
std::cout << "Run server" << std::endl;
boost::shared_ptr<boost::thread> th(new boost::thread([&serv] () { serv.run(); }));
cli.run_test();
// stop server
std::raise(SIGINT);
std::cout << "Stop server" << std::endl;
th->join();
}
catch (std::exception& e)
{
std::cerr << "Exception: " << e.what() << std::endl;
}
return 0;
}
void client::run_test()
{
unsigned optimal_threads_count = boost::thread::hardware_concurrency();
if (optimal_threads_count == 0)
optimal_threads_count = 1;
ostream_ << "Create " << optimal_threads_count << " threads for client" << std::endl;
for (unsigned i = 0; i < optimal_threads_count; ++i)
threads_.add_thread(new boost::thread([this, i] () { thread_func(i); }));
threads_.join_all();
ostream_ << "All client's threads done" << std::endl;
}
void client::thread_func(unsigned n)
{
session sess(address_, port_);
std::size_t message_sizes[] = { 0, 1, 3, 7, 234, 6432, 23221, 3311, 34, 4521, 333 };
for (auto s : message_sizes)
{
std::vector<uint8_t> mess(s, 'x');
try
{
const std::size_t cnt = 1024 * 10;
for (std::size_t i = 0; i < cnt; ++i)
sess.echo(mess);
ostream_ << "#" << n << ": " << cnt << " echos (size " << mess.size() << " bytes) received" << std::endl;
}
catch (std::exception& e)
{
ostream_ << "#" << n << ": " << "Exception: " << e.what() << std::endl;
}
}
}
session::session(boost::asio::ip::address const& address, unsigned short port)
: socket_(io_)
{
endpoint_.address(address);
endpoint_.port(port);
socket_.open(endpoint_.protocol());
}
void session::echo(std::vector<uint8_t> const& request)
{
auto reply = request;
socket_.send_to(boost::asio::buffer(request), endpoint_);
std::size_t reply_length = socket_.receive_from(boost::asio::buffer(reply), endpoint_);
if (reply_length != request.size())
throw std::logic_error("Different size of reply and request");
if (reply != request)
throw std::logic_error("Different data value of request and reply");
}