-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathproxy-conn.cpp
More file actions
299 lines (258 loc) · 9.56 KB
/
proxy-conn.cpp
File metadata and controls
299 lines (258 loc) · 9.56 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
287
288
289
290
291
292
293
294
295
296
297
298
299
#include "proxy-conn.hpp"
#include "target_mb_freertos.h"
#include "target_null.h"
/**
*
*
* @param io_service
*/
connection::connection(asio::io_service &io_service, std::string_view target, const std::vector<char *> &options,
std::string_view remote_host, int remote_port)
: io_service_(io_service),
m_client_socket(io_service),
m_target_socket(io_service),
resolver_(io_service),
m_requests_channel(io_service, m_client_socket, m_target_socket, channel::requests),
m_responses_channel(io_service, m_target_socket, m_client_socket, channel::responses),
fServer(remote_host),
fPort(remote_port)
{
// TBD
if (target == "mb_freertos") {
m_target = std::make_unique<target_mb_freertos>(*this, options);
} else if (target == "null") {
m_target = std::make_unique<target_null>();
} else {
throw std::system_error(std::make_error_code(std::errc::not_supported), "unknown target");
}
m_requests_channel.set_packet_handler([this](const gdb_packet& pkt) {
return on_request(pkt);
});
m_responses_channel.set_packet_handler([this](const gdb_packet& pkt) {
return on_response(pkt);
});
}
/**
* Start read data of request from browser
*
*/
void connection::start()
{
start_connect();
}
/**
* Start connecting to the web-server, initially to resolve the DNS-name of web-server into the IP address
*
*/
void connection::start_connect()
{
asio::ip::tcp::resolver::query query(fServer, std::to_string(fPort));
auto conn = shared_from_this();
resolver_.async_resolve(query, [conn](const std::error_code& err, asio::ip::tcp::resolver::iterator endpoint_iterator) {
std::clog << "resolve request handled\n";
conn->handle_resolve(err, endpoint_iterator);
});
}
/**
* If successful, after the resolved DNS-names of web-server into the IP addresses, try to connect
*
* @param err
* @param endpoint_iterator
*/
void connection::handle_resolve(const std::error_code& err,
asio::ip::tcp::resolver::iterator endpoint_iterator) {
// std::cout << "handle_resolve. Error: " << err.message() << "\n";
if (!err) {
const bool first_time = true;
handle_connect(std::error_code(), endpoint_iterator, first_time);
} else {
shutdown();
}
}
/**
* Try to connect to the web-server
*
* @param err
* @param endpoint_iterator
*/
void connection::handle_connect(const std::error_code& err,
asio::ip::tcp::resolver::iterator endpoint_iterator, const bool first_time) {
std::clog << "handle_connect. Error: " << err << ", " << err.message() << ", first=" << first_time << "\n";
if (!err && !first_time) {
// arm channels
m_requests_channel.start_read(shared_from_this());
m_responses_channel.start_read(shared_from_this());
} else if (endpoint_iterator != asio::ip::tcp::resolver::iterator()) {
m_target_socket.close();
asio::ip::tcp::endpoint endpoint = *endpoint_iterator;
std::clog << "endpoint: " << endpoint << std::endl;
auto conn = shared_from_this();
++endpoint_iterator;
m_target_socket.async_connect(endpoint, [this, conn, endpoint_iterator](const std::error_code& ec) {
handle_connect(ec, endpoint_iterator, false);
});
} else {
shutdown();
}
}
/**
* Close both sockets: for browser and web-server
*
*/
void connection::shutdown()
{
m_target_socket.close();
m_client_socket.close();
}
//
// TBD: rework
//
bool connection::on_request(const gdb_packet& pkt)
{
if (pkt.type() == gdb_packet_type::dat) {
++seq;
if (m_target) {
return m_target->process_request(pkt);
}
}
return false;
}
bool connection::on_response(const gdb_packet& pkt)
{
auto &requests = m_requests_channel.transfers_queue();
auto const& req = requests.front();
auto const& req_pkt = req.pkt;
std::clog << "rsp: " << "type=" << (int)pkt.type() << " internal=" << req.is_internal << std::endl;
if (pkt.type() == gdb_packet_type::dat) {
std::clog << "rsp: " << seq << " (count:" << m_requests_channel.transfers_queue().size() << ")" << std::endl;
std::clog << "rsp to the req: " << req_pkt.raw_data() << std::endl;
// Check ACK mode
if (req_pkt.data().substr(0, 15) == "QStartNoAckMode") {
if (pkt.data().substr(0, 2) == "OK") {
m_ack_mode = false;
}
}
if (req.on_response) {
req.on_response(req_pkt, pkt);
if (req.is_internal) {
if (m_ack_mode) {
//push_internal_request(gdb_packet_type::ack);
}
}
}
requests.pop_front();
if (req.is_internal)
return true;
} else if (pkt.type() == gdb_packet_type::ack) {
if (req.is_internal)
return true;
}
return false;
}
void connection::push_internal_request(gdb_packet &&req, transfer::on_response_cb cb)
{
m_requests_channel.start_write(shared_from_this(), {std::move(req), transfer::internal(), std::move(cb)});
if (m_ack_mode) {
// HACK: if we want to Nak internal request, we must not sent immediatelly another request
//m_requests_channel.start_write(shared_from_this(), {gdb_packet(gdb_packet_type::ack), transfer::internal()});
}
}
void connection::push_request(gdb_packet &&req, transfer::on_response_cb cb)
{
m_requests_channel.start_write(shared_from_this(), {std::move(req), std::move(cb)});
if (m_ack_mode) {
// HACK: if we want to Nak internal request, we must not sent immediatelly another request
//m_requests_channel.start_write(shared_from_this(), {gdb_packet(gdb_packet_type::ack)});
}
}
void connection::push_internal_response(gdb_packet &&req, transfer::on_response_cb cb)
{
m_responses_channel.start_write(shared_from_this(), {std::move(req), transfer::internal(), std::move(cb)});
}
//
// Channel
//
connection::channel::channel(asio::io_service &io, asio::ip::tcp::socket &src, asio::ip::tcp::socket &dst, direction dir)
: m_io(io), m_src(src), m_dst(dst), m_dir(dir)
{}
void connection::channel::start_read(std::shared_ptr<connection> con)
{
m_buffer.reset();
asio::async_read(m_src, asio::buffer(m_buffer.data), asio::transfer_at_least(1),
[con,this](const std::error_code& ec, size_t readed) {
// client read done
m_buffer.data[readed] = 0x00;
m_buffer.size = readed;
m_buffer.consumed = 0;
std::clog << (m_dir == requests ? "<- " : "-> ") << std::string_view(m_buffer.data.data(), readed) << std::endl;
if (!ec && readed) {
process_data(con);
} else {
con->shutdown();
}
});
}
void connection::channel::start_write(std::shared_ptr<connection> con, transfer &&req)
{
auto const pkt_type = req.pkt.type();
// TBD: hack. Need a more clean solution
if (pkt_type == gdb_packet_type::brk)
m_transfers.push_front(std::move(req));
else
m_transfers.push_back(std::move(req));
auto const& process_req = (pkt_type == gdb_packet_type::brk) ? m_transfers.front() : m_transfers.back();
auto const& pkt = process_req.pkt;
auto const total = pkt.raw_data().size();
#if ASIO_VERSION > 101200 // 1.12.0
auto data = pkt.raw_data();
#else
auto data = std::string(pkt.raw_data());
#endif
asio::async_write(m_dst, asio::buffer(data),
[con,this,total,&process_req](const std::error_code& ec, size_t transfered) {
std::clog << "transfered to " << (m_dir == requests ? "remote" : "client") << ": " << transfered << " / " << total << std::endl;
auto const& pkt = process_req.pkt;
// remote write done
assert(transfered == total && "Unhandled case, when server receive less data than was sent");
if (!ec) {
// Data packets must be cleaned at the Response handler
if (pkt.type() != gdb_packet_type::dat || m_dir == responses || !m_handler)
m_transfers.pop_front();
} else {
con->shutdown();
}
});
}
void connection::channel::set_packet_handler(connection::channel::on_packet_handler handler)
{
m_handler = std::move(handler);
}
std::deque<transfer> &connection::channel::transfers_queue()
{
return m_transfers;
}
void connection::channel::process_data(std::shared_ptr<connection> con)
{
do {
// process packets
auto const consumed = m_packet.parse(
m_buffer.data.data() + m_buffer.consumed,
m_buffer.size - m_buffer.consumed
);
m_buffer.consumed += consumed;
assert(consumed && "Invalid packet data?");
if (consumed == 0) {
// Invalid packet data?
}
if (m_packet.is_complete()) {
// TBD: handle complete handle, process it and if needed - sent to the target
auto const handled = m_handler ? m_handler(m_packet) : false;
if (!handled) {
start_write(con, std::move(m_packet));
}
m_packet.reset();
}
} while (!m_buffer.empty());
// Processed all client data, start client reading again
start_read(con);
}