-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathasio-test.cpp
177 lines (139 loc) · 5.67 KB
/
asio-test.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
#include <iostream>
#include <memory>
#include <boost/asio.hpp>
#include <boost/fiber/all.hpp>
namespace asio = boost::asio;
namespace fibers = boost::fibers;
const char request[] = "GET / HTTP/1.1\r\n\r\n";
struct my_interface {
template<typename R>
struct CompletionCallback {
template<typename... Args, std::enable_if_t<std::is_constructible_v<R, Args...>, int> = 0>
void operator()(Args&& ... args) const noexcept(std::is_nothrow_constructible_v<R, Args...>) {}
};
virtual void foo(int, CompletionCallback<int> callback) = 0;
};
struct my_interface_service : my_interface {
void foo(int, CompletionCallback<int> callback) override {}
};
struct client_base : std::enable_shared_from_this<client_base> {
virtual ~client_base() = default;
};
template <typename socket_type>
struct client : client_base {
explicit client(asio::ip::tcp::socket sock) : _sock(std::move(sock)) {
async_read_next_header();
}
struct request {
std::size_t request_id = 0;
std::size_t service_length = 0;
std::size_t method_length = 0;
std::size_t body_length = 0;
std::string service_name;
std::string method_name;
std::unique_ptr<std::byte[]> body;
};
void async_read_next_header() {
auto req = std::make_shared<request>();
std::array<asio::mutable_buffer, 4> buffers = {
asio::buffer(&req->request_id, sizeof(std::size_t)),
asio::buffer(&req->service_length, sizeof(std::size_t)),
asio::buffer(&req->method_length, sizeof(std::size_t)),
asio::buffer(&req->body_length, sizeof(std::size_t)),
};
asio::async_read(_sock, buffers, asio::transfer_at_least(4 * sizeof(std::size_t)),
[this, self = shared_from_this(),
req = std::move(req)](boost::system::error_code ec,
std::size_t bytesRead) mutable {
if (ec.failed()) {
std::cerr << "async read failed: " << ec << std::endl;
} else {
async_read_next_request(std::move(req));
}
});
}
void async_read_next_request(std::shared_ptr<request> req) {
req->service_name.reserve(req->service_length);
req->method_name.reserve(req->method_length);
req->body = std::make_unique<std::byte[]>(req->body_length);
std::array<asio::mutable_buffer, 3> buffers = {
asio::buffer(req->service_name.data(), req->service_length),
asio::buffer(req->method_name.data(), req->method_length),
asio::buffer(req->body.get(), req->body_length),
};
asio::async_read(_sock, buffers,
asio::transfer_exactly(req->service_length +
req->method_length + req->body_length),
[this, self = shared_from_this(),
req = std::move(req)](boost::system::error_code ec,
std::size_t bytesRead) mutable {
std::cout << "received request " << req->request_id << std::endl;
std::cout << "call to " << req->service_name << "::" << req->method_name << std::endl;
});
}
asio::ip::tcp::socket _sock;
};
int main(int argc, char* argv[]) {
auto io = std::make_shared<asio::io_context>();
asio::ip::tcp::acceptor acceptor(*io);
acceptor.async_accept([&](boost::system::error_code ec, asio::ip::tcp::socket sock) {
if (ec.failed()) {
std::cerr << "accept failed: " << ec << std::endl;
} else {
auto c = std::make_shared<client<decltype(sock)>>(std::move(sock));
}
});
/*asio::ip::tcp::socket s(*io);
asio::ip::tcp::resolver r(*io);
char buffer[1024];
auto const async_read_completion = [&](boost::system::error_code ec, std::size_t bytesRead){
if (ec.failed()) {
std::cerr << "read failed: " << ec << std::endl;
} else {
std::cout << "read completed " << bytesRead << std::endl;
std::cout << buffer << std::endl;
}
};
auto const async_write_completio
auto const async_read_completion = [&](boost::system::error_code ec, std::size_t bytesRead){
if (ec.failed()) {n = [&](boost::system::error_code ec,
std::size_t bytesWritten) {
if (ec.failed()) {
std::cerr << "write failed: " << ec << std::endl;
} else {
std::cout << "write completed " << bytesWritten << std::endl;
asio::async_read(s, asio::buffer(buffer), async_read_completion);
}
};
auto const async_connect_handler = [&](boost::system::error_code const& ec,
asio::ip::tcp::endpoint const& endpoint) {
if (ec.failed()) {
std::cerr << "connect failed: " << ec << std::endl;
} else {
std::cout << "connect completed " << endpoint << std::endl;
asio::async_write(s, asio::buffer(request), async_write_completion);
}
};
auto const async_resolve_handler = [&](boost::system::error_code ec,
asio::ip::tcp::resolver::results_type const& result) {
if (ec.failed()) {
std::cerr << "resolution failed: " << ec << std::endl;
} else {
asio::async_connect(s, result, async_connect_handler);
}
};
r.async_resolve("google.de", "http", async_resolve_handler);
asio::deadline_timer timer(*io);
timer.expires_from_now(boost::posix_time::milliseconds {1});
timer.async_wait([&](boost::system::error_code ec) {
std::cout << "cancel async socket" << std::endl;
r.cancel();
if (s.is_open()) {
std::cout << "calling cancel on socket" << std::endl;
s.cancel();
}
});*/
io->run();
std::cout << "io.run() returned" << std::endl;
return EXIT_SUCCESS;
}