client.cpp
7.26 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
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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
//
// client.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.hpp"
#include <algorithm>
#include <boost/bind/bind.hpp>
#include <boost/mem_fn.hpp>
#include <iostream>
#include <list>
#include <string>
#include "handler_allocator.hpp"
class stats
{
public:
stats()
: mutex_(),
total_bytes_written_(0),
total_bytes_read_(0)
{
}
void add(size_t bytes_written, size_t bytes_read)
{
asio::detail::mutex::scoped_lock lock(mutex_);
total_bytes_written_ += bytes_written;
total_bytes_read_ += bytes_read;
}
void print()
{
asio::detail::mutex::scoped_lock lock(mutex_);
std::cout << total_bytes_written_ << " total bytes written\n";
std::cout << total_bytes_read_ << " total bytes read\n";
}
private:
asio::detail::mutex mutex_;
size_t total_bytes_written_;
size_t total_bytes_read_;
};
class session
{
public:
session(asio::io_context& ioc, size_t block_size, stats& s)
: strand_(ioc.get_executor()),
socket_(ioc),
block_size_(block_size),
read_data_(new char[block_size]),
read_data_length_(0),
write_data_(new char[block_size]),
unwritten_count_(0),
bytes_written_(0),
bytes_read_(0),
stats_(s)
{
for (size_t i = 0; i < block_size_; ++i)
write_data_[i] = static_cast<char>(i % 128);
}
~session()
{
stats_.add(bytes_written_, bytes_read_);
delete[] read_data_;
delete[] write_data_;
}
void start(asio::ip::tcp::resolver::results_type endpoints)
{
asio::async_connect(socket_, endpoints,
asio::bind_executor(strand_,
boost::bind(&session::handle_connect, this,
asio::placeholders::error)));
}
void stop()
{
asio::post(strand_, boost::bind(&session::close_socket, this));
}
private:
void handle_connect(const asio::error_code& err)
{
if (!err)
{
asio::error_code set_option_err;
asio::ip::tcp::no_delay no_delay(true);
socket_.set_option(no_delay, set_option_err);
if (!set_option_err)
{
++unwritten_count_;
async_write(socket_, asio::buffer(write_data_, block_size_),
asio::bind_executor(strand_,
make_custom_alloc_handler(write_allocator_,
boost::bind(&session::handle_write, this,
asio::placeholders::error,
asio::placeholders::bytes_transferred))));
socket_.async_read_some(asio::buffer(read_data_, block_size_),
asio::bind_executor(strand_,
make_custom_alloc_handler(read_allocator_,
boost::bind(&session::handle_read, this,
asio::placeholders::error,
asio::placeholders::bytes_transferred))));
}
}
}
void handle_read(const asio::error_code& err, size_t length)
{
if (!err)
{
bytes_read_ += length;
read_data_length_ = length;
++unwritten_count_;
if (unwritten_count_ == 1)
{
std::swap(read_data_, write_data_);
async_write(socket_, asio::buffer(write_data_, read_data_length_),
asio::bind_executor(strand_,
make_custom_alloc_handler(write_allocator_,
boost::bind(&session::handle_write, this,
asio::placeholders::error,
asio::placeholders::bytes_transferred))));
socket_.async_read_some(asio::buffer(read_data_, block_size_),
asio::bind_executor(strand_,
make_custom_alloc_handler(read_allocator_,
boost::bind(&session::handle_read, this,
asio::placeholders::error,
asio::placeholders::bytes_transferred))));
}
}
}
void handle_write(const asio::error_code& err, size_t length)
{
if (!err && length > 0)
{
bytes_written_ += length;
--unwritten_count_;
if (unwritten_count_ == 1)
{
std::swap(read_data_, write_data_);
async_write(socket_, asio::buffer(write_data_, read_data_length_),
asio::bind_executor(strand_,
make_custom_alloc_handler(write_allocator_,
boost::bind(&session::handle_write, this,
asio::placeholders::error,
asio::placeholders::bytes_transferred))));
socket_.async_read_some(asio::buffer(read_data_, block_size_),
asio::bind_executor(strand_,
make_custom_alloc_handler(read_allocator_,
boost::bind(&session::handle_read, this,
asio::placeholders::error,
asio::placeholders::bytes_transferred))));
}
}
}
void close_socket()
{
socket_.close();
}
private:
asio::strand<asio::io_context::executor_type> strand_;
asio::ip::tcp::socket socket_;
size_t block_size_;
char* read_data_;
size_t read_data_length_;
char* write_data_;
int unwritten_count_;
size_t bytes_written_;
size_t bytes_read_;
stats& stats_;
handler_allocator read_allocator_;
handler_allocator write_allocator_;
};
class client
{
public:
client(asio::io_context& ioc,
const asio::ip::tcp::resolver::results_type endpoints,
size_t block_size, size_t session_count, int timeout)
: io_context_(ioc),
stop_timer_(ioc),
sessions_(),
stats_()
{
stop_timer_.expires_after(asio::chrono::seconds(timeout));
stop_timer_.async_wait(boost::bind(&client::handle_timeout, this));
for (size_t i = 0; i < session_count; ++i)
{
session* new_session = new session(io_context_, block_size, stats_);
new_session->start(endpoints);
sessions_.push_back(new_session);
}
}
~client()
{
while (!sessions_.empty())
{
delete sessions_.front();
sessions_.pop_front();
}
stats_.print();
}
void handle_timeout()
{
std::for_each(sessions_.begin(), sessions_.end(),
boost::mem_fn(&session::stop));
}
private:
asio::io_context& io_context_;
asio::steady_timer stop_timer_;
std::list<session*> sessions_;
stats stats_;
};
int main(int argc, char* argv[])
{
try
{
if (argc != 7)
{
std::cerr << "Usage: client <host> <port> <threads> <blocksize> ";
std::cerr << "<sessions> <time>\n";
return 1;
}
using namespace std; // For atoi.
const char* host = argv[1];
const char* port = argv[2];
int thread_count = atoi(argv[3]);
size_t block_size = atoi(argv[4]);
size_t session_count = atoi(argv[5]);
int timeout = atoi(argv[6]);
asio::io_context ioc;
asio::ip::tcp::resolver r(ioc);
asio::ip::tcp::resolver::results_type endpoints =
r.resolve(host, port);
client c(ioc, endpoints, block_size, session_count, timeout);
std::list<asio::thread*> threads;
while (--thread_count > 0)
{
asio::thread* new_thread = new asio::thread(
boost::bind(&asio::io_context::run, &ioc));
threads.push_back(new_thread);
}
ioc.run();
while (!threads.empty())
{
threads.front()->join();
delete threads.front();
threads.pop_front();
}
}
catch (std::exception& e)
{
std::cerr << "Exception: " << e.what() << "\n";
}
return 0;
}