From bd8558804f8004745be647fc929785be8bb7f9a7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=A9bastien=20Blin?= Date: Tue, 13 Aug 2019 09:43:51 -0400 Subject: [PATCH] p2p: answer to correct file transfer If multiple transfers occurs at the same time, responses can come with another order. So, we must check the id to get the correct response. Change-Id: Ica5d2f78fee6cfd385accc8cce6fff462dd4c4f8 --- src/jamidht/p2p.cpp | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/src/jamidht/p2p.cpp b/src/jamidht/p2p.cpp index c46656bb5..c264a5a57 100644 --- a/src/jamidht/p2p.cpp +++ b/src/jamidht/p2p.cpp @@ -341,6 +341,10 @@ public: return responseReceived_; } + bool waitId(uint64_t id) { + return waitId_ == id; + } + void addListener(const ListenerFunction& cb) { if (!connected_) { std::lock_guard lk {listenersMutex_}; @@ -391,6 +395,7 @@ private: // Prepare connection request as a DHT message PeerConnectionMsg request; request.id = ValueIdDist()(parent_.account.rand); /* Random id for the message unicity */ + waitId_ = request.id; request.addresses = {icemsg.str()}; request.addresses.insert(request.addresses.end(), publicAddresses_.begin(), publicAddresses_.end()); @@ -512,6 +517,7 @@ private: std::atomic_bool responseReceived_ {false}; std::condition_variable responseCV_{}; PeerConnectionMsg response_; + uint64_t waitId_ {0}; std::shared_ptr peerCertificate_; std::shared_ptr peer_ep_; std::unique_ptr connection_; @@ -857,9 +863,10 @@ DhtPeerConnector::Impl::onResponseMsg(PeerConnectionMsg&& response) JAMI_DBG() << account << "[CNX] rx DHT reply from " << response.from; std::lock_guard lock(clientsMutex_); for (auto& client: clients_) { - // NOTE We can receives multiple files from one peer. So fill unanswered clients. + // NOTE We can receives multiple files from one peer. So fill unanswered clients with linked id. if (client.first.first == response.from - && client.second && !client.second->hasAlreadyAResponse()) { + && client.second && !client.second->hasAlreadyAResponse() + && client.second->waitId(response.id)) { client.second->onDhtResponse(std::move(response)); break; } -- 2.22.0