From dfb97916376c5930a938ea2842c4efd74526085f Mon Sep 17 00:00:00 2001
From: Guillaume Roguez <guillaume.roguez@savoirfairelinux.com>
Date: Tue, 1 Mar 2016 17:24:50 -0500
Subject: [PATCH] sips: fix send ack

send ack message was queued in a queue unstacked in
the main thread, but this last is busy by application
waiting for the ack... deadloop!
This patch fixes this by calling the application ack
confirmation callback immediatly when the TLS acknowledge
the ack.

Change-Id: Ic8fe6102acca184d78f86e3e8524382e9b257f4b
Tuleap: #433
---
 src/ringdht/sips_transport_ice.cpp | 22 +++++-----------------
 src/ringdht/sips_transport_ice.h   |  3 ---
 2 files changed, 5 insertions(+), 20 deletions(-)

diff --git a/src/ringdht/sips_transport_ice.cpp b/src/ringdht/sips_transport_ice.cpp
index 08ea4b1703..a571aaef4c 100644
--- a/src/ringdht/sips_transport_ice.cpp
+++ b/src/ringdht/sips_transport_ice.cpp
@@ -328,20 +328,6 @@ SipsIceTransport::handleEvents()
             }
         }
     }
-
-    // Acknowledged SIP -> TLS packet
-    decltype(txAckQueue_) ack_queue;
-    {
-        std::lock_guard<std::mutex> l(txAckQueueMutex_);
-        ack_queue = std::move(txAckQueue_);
-        txAckQueue_.clear();
-    }
-    for (const auto& pair: ack_queue) {
-        auto tdata = pair.first;
-        tdata->op_key.tdata = nullptr;
-        if (tdata->op_key.callback)
-            tdata->op_key.callback(&trData_.base, tdata->op_key.token, pair.second);
-    }
 }
 
 void
@@ -637,11 +623,13 @@ SipsIceTransport::send(pjsip_tx_data *tdata, const pj_sockaddr_t *rem_addr,
 
     // Asynchronous send
     const std::size_t size = tdata->buf.cur - tdata->buf.start;
-    auto ret = tls_->async_send(tdata->buf.start, size, [=](std::size_t bytes_sent){
+    auto ret = tls_->async_send(tdata->buf.start, size, [=](std::size_t bytes_sent) {
+            // WARN: This code is called in the context of the TlsSession thread
             if (bytes_sent == 0)
                 bytes_sent = -PJ_RETURN_OS_ERROR(OSERR_ENOTCONN);
-            std::lock_guard<std::mutex> l(txAckQueueMutex_);
-            txAckQueue_.emplace_back(std::make_pair(tdata, bytes_sent));
+            tdata->op_key.tdata = nullptr;
+            if (tdata->op_key.callback)
+                tdata->op_key.callback(&trData_.base, tdata->op_key.token, bytes_sent);
         });
 
     // Shutdown on fatal errors
diff --git a/src/ringdht/sips_transport_ice.h b/src/ringdht/sips_transport_ice.h
index 27b1d65a51..6433dd62e3 100644
--- a/src/ringdht/sips_transport_ice.h
+++ b/src/ringdht/sips_transport_ice.h
@@ -115,9 +115,6 @@ private:
     std::mutex rxMtx_;
     std::list<std::vector<uint8_t>> rxPending_;
 
-    std::mutex txAckQueueMutex_;
-    std::list<std::pair<pjsip_tx_data*, ssize_t>> txAckQueue_;
-
     pj_status_t send(pjsip_tx_data*, const pj_sockaddr_t*, int, void*, pjsip_transport_callback);
     void handleEvents();
     void pushChangeStateEvent(ChangeStateEventData&&);
-- 
GitLab