forked from eidheim/Simple-Web-Server
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsimple_client_tcp.hpp
311 lines (269 loc) · 8.9 KB
/
simple_client_tcp.hpp
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
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
#pragma once
#include <memory>
#include <utility>
#include <mutex>
#include <string>
#include <unordered_map>
#include <cstdint>
#include <cstdlib>
#include <boost/asio.hpp>
#include "net_utils.hpp"
namespace SimpleTCP {
class DefaultTransporter {
public:
typedef boost::asio::ip::tcp::socket socket_type;
template <typename Client>
void init(Client&) const {}
template <typename Client>
void connect(Client& client) const {
if (!client.socket || !client.socket->is_open()) {
std::unique_ptr<boost::asio::ip::tcp::resolver::query> query;
if (!client.config.has_proxy_server()) {
query = std::make_unique<boost::asio::ip::tcp::resolver::query>(client.host, std::to_string(client.port));
}
else {
auto proxy_host_port = parse_host_port(client.config.proxy_server(), 8080);
query = std::make_unique<boost::asio::ip::tcp::resolver::query>(proxy_host_port.first, std::to_string(proxy_host_port.second));
}
boost::asio::deadline_timer timer(client.io_service);
client.resolver.async_resolve(*query, [&client, &timer](const boost::system::error_code &ec, boost::asio::ip::tcp::resolver::iterator it) {
if (!ec) {
{
std::lock_guard<std::mutex> lock(client.socket_mutex);
client.socket = std::make_unique<socket_type>(client.io_service);
}
client.make_and_start_timeout_connect_timer(timer);
boost::asio::async_connect(*(client.socket), it, [&client, &timer](const boost::system::error_code &ec, boost::asio::ip::tcp::resolver::iterator /*it*/) {
timer.cancel();
if (!ec) {
boost::asio::ip::tcp::no_delay option(true);
client.socket->set_option(option);
}
else {
std::lock_guard<std::mutex> lock(client.socket_mutex);
client.socket = nullptr;
throw boost::system::system_error(ec);
}
});
}
else {
std::lock_guard<std::mutex> lock(client.socket_mutex);
client.socket = nullptr;
throw boost::system::system_error(ec);
}
});
client.io_service.reset();
client.io_service.run();
}
}
};
template <typename Transporter = DefaultTransporter>
class DefaultConfig {
public:
/// Set timeout on requests in seconds. Default value: 0 (no timeout).
constexpr size_t timeout() const noexcept {
return 10;
}
/// Set connect timeout in seconds. Default value: 0 (Config::timeout is then used instead).
constexpr size_t timeout_connect() const noexcept {
return 10;
}
/// Set proxy server (server:port)
constexpr const char* proxy_server() const noexcept {
return nullptr;
}
constexpr bool has_proxy_server() const noexcept {
return false;
}
typedef Transporter transporter_type;
const transporter_type& get_transporter() const noexcept {
return transporter_type{};
}
constexpr std::uint16_t default_port() const noexcept {
return 0;
}
};
template <typename Config = DefaultConfig>
class TCPClient {
friend class Config::transporter_type;
public:
typedef typename Config::transporter_type transporter_type;
typedef typename transporter_type::socket_type socket_type;
typedef Config config_type;
protected:
Config config;
boost::asio::io_service io_service;
boost::asio::ip::tcp::resolver resolver;
std::unique_ptr<socket_type> socket;
std::mutex socket_mutex;
std::string host;
std::uint16_t port;
public:
const Config& get_config() const {
return config;
}
const std::string& get_host() const {
return host;
}
const std::uint16_t& get_port() const {
return port;
}
public:
TCPClient(const std::string& host_port, Config config = Config{}) :resolver(io_service), config(std::move(config)) {
std::tie(host, port) = parse_host_port(host_port, config.default_port());
config.get_transporter().init(*this);
}
TCPClient(std::string&& host_port, Config config = Config{}) :resolver(io_service), config(std::move(config)) {
std::tie(host, port) = parse_host_port(std::move(host_port), config.default_port());
config.get_transporter().init(*this);
}
TCPClient(const TCPClient& client) = delete;
TCPClient(TCPClient&& client) = default;
TCPClient& operator=(const TCPClient& client) = delete;
TCPClient& operator=(TCPClient&& client) = default;
protected:
void make_and_start_timeout_timer(boost::asio::deadline_timer& timer) {
make_timeout_timer(timer, config.timeout(), [this](const boost::system::error_code& ec) {
if (!ec) {
std::lock_guard<std::mutex> lock(socket_mutex);
if (socket) {
boost::system::error_code ec;
socket->lowest_layer().shutdown(boost::asio::ip::tcp::socket::shutdown_both, ec);
socket->lowest_layer().close();
}
}
});
}
void make_and_start_timeout_connect_timer(boost::asio::deadline_timer& timer) {
make_timeout_timer(timer, config.timeout_connect(), [this](const boost::system::error_code& ec) {
if (!ec) {
std::lock_guard<std::mutex> lock(socket_mutex);
if (socket) {
boost::system::error_code ec;
socket->lowest_layer().shutdown(boost::asio::ip::tcp::socket::shutdown_both, ec);
socket->lowest_layer().close();
}
}
});
}
void connect() {
config.get_transporter().connect(*this);
}
template <typename Buffer, typename Socket>
void send_noconnect(Buffer&& buffer, Socket& w_socket) {
boost::asio::deadline_timer timer(io_service);
make_and_start_timeout_timer(timer);
boost::asio::async_write(w_socket, std::forward<Buffer>(buffer),
[this, &timer](const boost::system::error_code &ec, size_t /*bytes_transferred*/) {
timer.cancel();
if (ec) {
std::lock_guard<std::mutex> lock(socket_mutex);
this->socket = nullptr;
throw boost::system::system_error(ec);
}
});
io_service.reset();
io_service.run();
}
template <typename Buffer, typename Socket>
size_t receive_until_noconnect(Buffer& buffer, const std::string& delim, Socket& w_socket) {
boost::asio::deadline_timer timer(io_service);
make_and_start_timeout_timer(timer);
size_t ret = 0;
boost::asio::async_read_until(w_socket, buffer, delim,
[this, &timer, &ret](const boost::system::error_code& ec, size_t bytes_transferred) {
timer.cancel();
ret = bytes_transferred;
if (ec) {
std::lock_guard<std::mutex> lock(socket_mutex);
this->socket = nullptr;
throw boost::system::system_error(ec);
}
});
io_service.reset();
io_service.run();
return ret;
}
public:
template <typename Buffer>
void send(Buffer&& buffer) {
connect();
boost::asio::deadline_timer timer(io_service);
make_and_start_timeout_timer(timer);
boost::asio::async_write(*socket, std::forward<Buffer>(buffer),
[this, &timer](const boost::system::error_code &ec, size_t /*bytes_transferred*/) {
timer.cancel();
if (ec) {
std::lock_guard<std::mutex> lock(socket_mutex);
this->socket = nullptr;
throw boost::system::system_error(ec);
}
});
io_service.reset();
io_service.run();
}
template <typename Buffer>
void receive(Buffer& buffer) {
connect();
boost::asio::deadline_timer timer(io_service);
make_and_start_timeout_timer(timer);
boost::asio::async_read(*socket, buffer,
[this, &timer](const boost::system::error_code& ec, size_t /*bytes_transferred*/) {
timer.cancel();
if (ec) {
std::lock_guard<std::mutex> lock(socket_mutex);
this->socket = nullptr;
if (ec != boost::asio::error::eof) throw boost::system::system_error(ec);
}
});
io_service.reset();
io_service.run();
}
template <typename Buffer>
size_t receive_until(Buffer& buffer, const std::string& delim) {
connect();
boost::asio::deadline_timer timer(io_service);
make_and_start_timeout_timer(timer);
size_t ret = 0;
boost::asio::async_read_until(*socket, buffer, delim,
[this, &timer, &ret](const boost::system::error_code& ec, size_t bytes_transferred) {
timer.cancel();
ret = bytes_transferred;
if (ec) {
std::lock_guard<std::mutex> lock(socket_mutex);
this->socket = nullptr;
throw boost::system::system_error(ec);
}
});
io_service.reset();
io_service.run();
return ret;
}
template <typename Buffer>
size_t receive_exactly(Buffer& buffer, const size_t count) {
connect();
boost::asio::deadline_timer timer(io_service);
make_and_start_timeout_timer(timer);
size_t ret = 0;
boost::asio::async_read(*socket, buffer, boost::asio::transfer_exactly(count),
[this, &timer, &ret](const boost::system::error_code& ec, size_t bytes_transferred) {
timer.cancel();
ret = bytes_transferred;
if (ec) {
std::lock_guard<std::mutex> lock(socket_mutex);
this->socket = nullptr;
throw boost::system::system_error(ec);
}
});
io_service.reset();
io_service.run();
return ret;
}
/*boost::asio::streambuf receive() {
boost::asio::streambuf buffer;
receive(buffer);
return std::move(buffer);
}*/
};
typedef TCPClient<DefaultConfig<>> DefaultTCPClient;
}