Skip to content

Commit 76896da

Browse files
committed
Basic internal requests support
1 parent df5f21f commit 76896da

File tree

2 files changed

+70
-15
lines changed

2 files changed

+70
-15
lines changed

proxy-conn.cpp

+56-10
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
#include "proxy-conn.hpp"
22

3-
//#define SYNC_TRANSFER
4-
53
/**
64
*
75
*
@@ -110,28 +108,76 @@ void connection::shutdown()
110108

111109
bool connection::on_request(const gdb_packet& pkt)
112110
{
113-
if (pkt.type() == gdb_packet_type::generic) {
111+
if (pkt.type() == gdb_packet_type::dat) {
114112
std::clog << "req: " << ++seq << std::endl;
113+
114+
#if 0
115+
//if (seq > 4)
116+
{
117+
const static char raw[] = R"#($qXfer:threads:read::0,1000#92)#";
118+
gdb_packet internal_pkt;
119+
internal_pkt.parse(raw, sizeof(raw));
120+
121+
push_internal_request(std::move(internal_pkt), [this](auto &req, auto& resp) {
122+
std::clog << "response to internal request:\n"
123+
<< " <- " << req.raw_data() << '\n'
124+
<< " -> " << resp.raw_data() << std::endl;
125+
});
126+
}
127+
#endif
115128
}
116129

117130
return false;
118131
}
119132

120133
bool connection::on_response(const gdb_packet& pkt)
121134
{
122-
if (pkt.type() == gdb_packet_type::generic) {
123-
std::clog << "rsp: " << seq << std::endl;
135+
auto &requests = m_requests_channel.transfers_queue();
136+
auto const& req = requests.front();
137+
auto const& req_pkt = req.pkt;
124138

125-
auto &queue = m_requests_channel.transfers_queue();
126-
auto const& req = queue.front();
127-
auto const& req_pkt = req.pkt;
139+
std::clog << "rsp: " << "type=" << (int)pkt.type() << " internal=" << req.is_internal << std::endl;
128140

141+
if (pkt.type() == gdb_packet_type::dat) {
142+
std::clog << "rsp: " << seq << " (count:" << m_requests_channel.transfers_queue().size() << ")" << std::endl;
129143
std::clog << "rsp to the req: " << req_pkt.raw_data() << std::endl;
130-
queue.pop_front();
144+
145+
// Check ACK mode
146+
if (req_pkt.data().substr(0, 15) == "QStartNoAckMode") {
147+
if (pkt.data().substr(0, 2) == "OK") {
148+
m_ack_mode = false;
149+
}
150+
}
151+
152+
if (req.on_response) {
153+
req.on_response(req_pkt, pkt);
154+
if (req.is_internal) {
155+
if (m_ack_mode) {
156+
push_internal_request(gdb_packet_type::ack);
157+
}
158+
}
159+
}
160+
161+
requests.pop_front();
162+
163+
if (req.is_internal)
164+
return true;
165+
} else if (pkt.type() == gdb_packet_type::ack) {
166+
if (req.is_internal)
167+
return true;
131168
}
132169
return false;
133170
}
134171

172+
void connection::push_internal_request(gdb_packet &&req, transfer::on_response_cb cb)
173+
{
174+
m_requests_channel.start_write(shared_from_this(), {std::move(req), transfer::internal(), std::move(cb)});
175+
if (m_ack_mode) {
176+
// HACK: if we want to Nak internal request, we must not sent immediatelly another request
177+
m_requests_channel.start_write(shared_from_this(), {gdb_packet(gdb_packet_type::ack), transfer::internal()});
178+
}
179+
}
180+
135181
//
136182
// Channel
137183
//
@@ -177,7 +223,7 @@ void connection::channel::start_write(std::shared_ptr<connection> con, transfer
177223
assert(transfered == total && "Unhandled case, when server receive less data than was sent");
178224
if (!ec) {
179225
// Data packets must be cleaned at the Response handler
180-
if (pkt.type() != gdb_packet_type::generic || m_dir == responses || !m_handler)
226+
if (pkt.type() != gdb_packet_type::dat || m_dir == responses || !m_handler)
181227
m_transfers.pop_front();
182228
} else {
183229
con->shutdown();

proxy-conn.hpp

+14-5
Original file line numberDiff line numberDiff line change
@@ -25,20 +25,25 @@ struct data_buffer
2525
};
2626

2727
// Only Data packets
28-
struct transfer_internal {};
2928
struct transfer
3029
{
31-
transfer(gdb_packet&& pkt)
32-
: pkt(std::move(pkt))
30+
struct internal {};
31+
using on_response_cb = std::function<void(const gdb_packet& request, const gdb_packet& response)>;
32+
33+
transfer(gdb_packet&& pkt, on_response_cb&& cb = on_response_cb())
34+
: pkt(std::move(pkt)),
35+
on_response(std::move(cb))
3336
{}
3437

35-
transfer(gdb_packet&& pkt, transfer_internal)
38+
transfer(gdb_packet&& pkt, internal, on_response_cb&& cb = on_response_cb())
3639
: pkt(std::move(pkt)),
37-
is_internal(true)
40+
is_internal(true),
41+
on_response(std::move(cb))
3842
{}
3943

4044
gdb_packet pkt;
4145
bool const is_internal = false;
46+
on_response_cb on_response;
4247
};
4348

4449

@@ -120,6 +125,8 @@ class connection : public std::enable_shared_from_this<connection> {
120125
bool on_request(const gdb_packet& pkt);
121126
bool on_response(const gdb_packet& pkt);
122127

128+
void push_internal_request(gdb_packet&& req, transfer::on_response_cb cb = transfer::on_response_cb());
129+
123130
asio::io_service& io_service_;
124131
asio::ip::tcp::socket m_client_socket;
125132
asio::ip::tcp::socket m_target_socket;
@@ -132,5 +139,7 @@ class connection : public std::enable_shared_from_this<connection> {
132139
std::string fPort = "3002";
133140

134141
size_t seq = 0;
142+
143+
bool m_ack_mode = true;
135144
};
136145

0 commit comments

Comments
 (0)