server.cpp
5.68 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
//
// 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.hpp"
#include <algorithm>
#include <boost/bind/bind.hpp>
#include <iostream>
#include <list>
#include "handler_allocator.hpp"
class session
{
public:
session(asio::io_context& ioc, size_t block_size)
: io_context_(ioc),
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]),
unsent_count_(0),
op_count_(0)
{
}
~session()
{
delete[] read_data_;
delete[] write_data_;
}
asio::ip::tcp::socket& socket()
{
return socket_;
}
void start()
{
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)
{
++op_count_;
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))));
}
else
{
asio::post(io_context_, boost::bind(&session::destroy, this));
}
}
void handle_read(const asio::error_code& err, size_t length)
{
--op_count_;
if (!err)
{
read_data_length_ = length;
++unsent_count_;
if (unsent_count_ == 1)
{
op_count_ += 2;
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))));
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))));
}
}
if (op_count_ == 0)
asio::post(io_context_, boost::bind(&session::destroy, this));
}
void handle_write(const asio::error_code& err)
{
--op_count_;
if (!err)
{
--unsent_count_;
if (unsent_count_ == 1)
{
op_count_ += 2;
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))));
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))));
}
}
if (op_count_ == 0)
asio::post(io_context_, boost::bind(&session::destroy, this));
}
static void destroy(session* s)
{
delete s;
}
private:
asio::io_context& io_context_;
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 unsent_count_;
int op_count_;
handler_allocator read_allocator_;
handler_allocator write_allocator_;
};
class server
{
public:
server(asio::io_context& ioc, const asio::ip::tcp::endpoint& endpoint,
size_t block_size)
: io_context_(ioc),
acceptor_(ioc),
block_size_(block_size)
{
acceptor_.open(endpoint.protocol());
acceptor_.set_option(asio::ip::tcp::acceptor::reuse_address(1));
acceptor_.bind(endpoint);
acceptor_.listen();
start_accept();
}
void start_accept()
{
session* new_session = new session(io_context_, block_size_);
acceptor_.async_accept(new_session->socket(),
boost::bind(&server::handle_accept, this, new_session,
asio::placeholders::error));
}
void handle_accept(session* new_session, const asio::error_code& err)
{
if (!err)
{
new_session->start();
}
else
{
delete new_session;
}
start_accept();
}
private:
asio::io_context& io_context_;
asio::ip::tcp::acceptor acceptor_;
size_t block_size_;
};
int main(int argc, char* argv[])
{
try
{
if (argc != 5)
{
std::cerr << "Usage: server <address> <port> <threads> <blocksize>\n";
return 1;
}
using namespace std; // For atoi.
asio::ip::address address = asio::ip::make_address(argv[1]);
short port = atoi(argv[2]);
int thread_count = atoi(argv[3]);
size_t block_size = atoi(argv[4]);
asio::io_context ioc;
server s(ioc, asio::ip::tcp::endpoint(address, port), block_size);
// Threads not currently supported in this test.
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;
}