From 9e6d7b2f8d41ce74a5aa25e43fdad2478deeab8a Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?S=C3=A9bastien=20Blin?=
 <sebastien.blin@savoirfairelinux.com>
Date: Wed, 11 Jul 2018 15:50:25 -0400
Subject: [PATCH] data transfer: correctly set parameters for cancelling
 transfer

a CANCEL message takes two parameters:
1. The peer device
2. The transfer id to cancel
Only one was given in some cases

Change-Id: I4f73b3d64be90bdb4d90fcb6bb935eb460840df5
Gitlab: #18
---
 src/ringdht/p2p.cpp | 14 ++++++++++----
 1 file changed, 10 insertions(+), 4 deletions(-)

diff --git a/src/ringdht/p2p.cpp b/src/ringdht/p2p.cpp
index 016fbce7e8..b8f738768d 100644
--- a/src/ringdht/p2p.cpp
+++ b/src/ringdht/p2p.cpp
@@ -256,11 +256,13 @@ public:
     using ListenerFunction = std::function<void(PeerConnection*)>;
 
     ClientConnector(Impl& parent,
+                    const DRing::DataTransferId& tid,
                     const dht::InfoHash& peer_h,
                     const std::shared_ptr<dht::crypto::Certificate>& peer_cert,
                     const std::vector<std::string>& public_addresses,
                     const ListenerFunction& connect_cb)
         : parent_ {parent}
+        , tid_ {tid}
         , peer_ {peer_h}
         , publicAddresses_ {public_addresses}
         , peerCertificate_ {peer_cert} {
@@ -296,7 +298,7 @@ public:
     }
 
     void cancel() {
-        parent_.ctrl << makeMsg<CtrlMsgType::CANCEL>(peer_);
+        parent_.ctrl << makeMsg<CtrlMsgType::CANCEL>(peer_, tid_);
     }
 
     void onDhtResponse(PeerConnectionMsg&& response) {
@@ -321,8 +323,11 @@ private:
         Timeout<Clock> dhtMsgTimeout {DHT_MSG_TIMEOUT};
         dhtMsgTimeout.start();
         while (!responseReceived_) {
-            if (dhtMsgTimeout)
-                throw std::runtime_error("no response from DHT to E2E request");
+            if (dhtMsgTimeout) {
+                RING_ERR("no response from DHT to E2E request. Cancel transfer");
+                cancel();
+                return;
+            }
             std::this_thread::sleep_for(std::chrono::milliseconds(10));
         }
 
@@ -370,6 +375,7 @@ private:
     }
 
     Impl& parent_;
+    const DRing::DataTransferId tid_;
     const dht::InfoHash peer_;
 
     std::vector<std::string> publicAddresses_;
@@ -609,7 +615,7 @@ DhtPeerConnector::Impl::onAddDevice(const dht::InfoHash& dev_h,
     if (iter == std::end(clients_)) {
         clients_.emplace(
             client,
-            std::make_unique<Impl::ClientConnector>(*this, dev_h, peer_cert, public_addresses, connect_cb));
+            std::make_unique<Impl::ClientConnector>(*this, tid, dev_h, peer_cert, public_addresses, connect_cb));
     } else {
         iter->second->addListener(connect_cb);
     }
-- 
GitLab