Skip to content

Commit d5f0386

Browse files
committed
Store reference to resolver_impl instead of all result vars
1 parent 0f0daf1 commit d5f0386

File tree

4 files changed

+27
-24
lines changed

4 files changed

+27
-24
lines changed

src/resolve_attempt_udp.cpp

Lines changed: 18 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,11 @@
1313
using namespace lsl;
1414

1515
resolve_attempt_udp::resolve_attempt_udp(asio::io_context &io, const udp &protocol,
16-
const std::vector<udp::endpoint> &targets, const std::string &query, result_container &results,
17-
std::mutex &results_mut, double cancel_after, cancellable_registry *registry)
18-
: io_(io), results_(results), results_mut_(results_mut), cancel_after_(cancel_after),
19-
cancelled_(false), targets_(targets), query_(query), unicast_socket_(io),
20-
broadcast_socket_(io), multicast_socket_(io), recv_socket_(io), cancel_timer_(io) {
16+
const std::vector<udp::endpoint> &targets, const std::string &query, resolver_impl &resolver,
17+
double cancel_after)
18+
: io_(io), resolver_(resolver), cancel_after_(cancel_after), cancelled_(false),
19+
targets_(targets), query_(query), unicast_socket_(io), broadcast_socket_(io),
20+
multicast_socket_(io), recv_socket_(io), cancel_timer_(io) {
2121
// open the sockets that we might need
2222
recv_socket_.open(protocol);
2323
try {
@@ -57,7 +57,7 @@ resolve_attempt_udp::resolve_attempt_udp(asio::io_context &io, const udp &protoc
5757
query_msg_.c_str());
5858

5959
// register ourselves as a candidate for cancellation
60-
if (registry) register_at(registry);
60+
register_at(&resolver);
6161
}
6262

6363
resolve_attempt_udp::~resolve_attempt_udp() {
@@ -116,20 +116,24 @@ void resolve_attempt_udp::handle_receive_outcome(err_t err, std::size_t len) {
116116
std::string uid = info.uid();
117117
{
118118
// update the results
119-
std::lock_guard<std::mutex> lock(results_mut_);
120-
if (results_.find(uid) == results_.end())
121-
results_[uid] = std::make_pair(info, lsl_clock()); // insert new result
119+
std::lock_guard<std::mutex> lock(resolver_.results_mut_);
120+
auto it = resolver_.results_.find(uid);
121+
if (it == resolver_.results_.end())
122+
// insert new result, store iterator in it
123+
it = resolver_.results_.emplace(uid, std::make_pair(info, lsl_clock()))
124+
.first;
122125
else
123-
results_[uid].second = lsl_clock(); // update only the receive time
126+
it->second.second = lsl_clock(); // update only the receive time
127+
auto &stored_info = it->second.first;
124128
// ... also update the address associated with the result (but don't
125129
// override the address of an earlier record for this stream since this
126130
// would be the faster route)
127131
if (remote_endpoint_.address().is_v4()) {
128-
if (results_[uid].first.v4address().empty())
129-
results_[uid].first.v4address(remote_endpoint_.address().to_string());
132+
if (stored_info.v4address().empty())
133+
stored_info.v4address(remote_endpoint_.address().to_string());
130134
} else {
131-
if (results_[uid].first.v6address().empty())
132-
results_[uid].first.v6address(remote_endpoint_.address().to_string());
135+
if (stored_info.v6address().empty())
136+
stored_info.v6address(remote_endpoint_.address().to_string());
133137
}
134138
}
135139
}

src/resolve_attempt_udp.h

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -53,8 +53,7 @@ class resolve_attempt_udp : public cancellable_obj,
5353
*/
5454
resolve_attempt_udp(asio::io_context &io, const udp &protocol,
5555
const std::vector<udp::endpoint> &targets, const std::string &query,
56-
result_container &results, std::mutex &results_mut, double cancel_after = 5.0,
57-
cancellable_registry *registry = nullptr);
56+
resolver_impl &resolver, double cancel_after = 5.0);
5857

5958
/// Destructor
6059
~resolve_attempt_udp();
@@ -90,10 +89,8 @@ class resolve_attempt_udp : public cancellable_obj,
9089
// data shared with the resolver_impl
9190
/// reference to the IO service that executes our actions
9291
asio::io_context &io_;
93-
/// shared result container
94-
result_container &results_;
95-
/// shared mutex that protects the results
96-
std::mutex &results_mut_;
92+
/// the resolver associated with this attempt
93+
resolver_impl &resolver_;
9794

9895
// constant over the lifetime of this attempt
9996
/// the timeout for giving up

src/resolver_impl.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -206,8 +206,8 @@ void resolver_impl::udp_multicast_burst() {
206206
int failures = 0;
207207
for (auto protocol: udp_protocols_) {
208208
try {
209-
std::make_shared<resolve_attempt_udp>(*io_, protocol, mcast_endpoints_, query_,
210-
results_, results_mut_, cfg_->multicast_max_rtt(), this)
209+
std::make_shared<resolve_attempt_udp>(
210+
*io_, protocol, mcast_endpoints_, query_, *this, cfg_->multicast_max_rtt())
211211
->begin();
212212
} catch (std::exception &e) {
213213
if (++failures == udp_protocols_.size())
@@ -226,8 +226,8 @@ void resolver_impl::udp_unicast_burst(err_t err) {
226226
// start one per IP stack under consideration
227227
for (auto protocol: udp_protocols_) {
228228
try {
229-
std::make_shared<resolve_attempt_udp>(*io_, protocol, ucast_endpoints_, query_,
230-
results_, results_mut_, cfg_->unicast_max_rtt(), this)
229+
std::make_shared<resolve_attempt_udp>(
230+
*io_, protocol, ucast_endpoints_, query_, *this, cfg_->unicast_max_rtt())
231231
->begin();
232232
} catch (std::exception &e) {
233233
if (++failures == udp_protocols_.size())

src/resolver_impl.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,8 @@ class resolver_impl : public cancellable_registry {
129129
};
130130

131131
private:
132+
friend class resolve_attempt_udp;
133+
132134
/// This function starts a new wave of resolves.
133135
void next_resolve_wave();
134136

0 commit comments

Comments
 (0)