http: add Request::await

This commit is contained in:
Adrien Béraud committed 2020-10-28 12:23:53 -04:00
1 parent 1778f64048
commit c84fac6823
2 files changed
+19

No files matched your search

+3
View File
@@ -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.
*/
+16
View File
@@ -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