diff --git a/include/opendht/http.h b/include/opendht/http.h index 46b722cef448448fe4ea4400dccd24a60fe6da07..85284185c701026901a729415eda292d6b1647fe 100644 --- a/include/opendht/http.h +++ b/include/opendht/http.h @@ -290,6 +290,9 @@ public: void send(); + /** Send and block for response */ + const Response& await(); + /** * User action to cancel the Request and call the completion callbacks. */ diff --git a/src/http.cpp b/src/http.cpp index b878aa31b7b29aab0874db81e35000a8cd7bf081..ad1981a99395018626c4886314ee81a7b521ec1a 100644 --- a/src/http.cpp +++ b/src/http.cpp @@ -1096,5 +1096,21 @@ Request::getRelativePath(const Url& origin, const std::string& path) return newPath.toString(); } +const Response& +Request::await() +{ + std::mutex mtx; + std::unique_lock<std::mutex> lock(mtx); + std::condition_variable cv; + bool ok {false}; + add_on_done_callback([&](const Response& resp){ + std::lock_guard<std::mutex> lk(mtx); + ok = true; + cv.notify_all(); + }); + cv.wait(lock, [&]{ return ok; }); + return response_; +} + } // namespace http } // namespace dht