From 01728fe1f40c9ce49516174ad0fdae5b7de4b099 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tam=C3=A1s=20B=C3=A1lint=20Misius?= Date: Tue, 24 Oct 2023 06:49:24 +0200 Subject: [PATCH] Make curl_multi_wakeup/curl_multi_poll usage optional Thus dropping the minimum required libcurl version to at least 7.64, possibly further. We have compatibility macros all the way to 7.55 so yeah. Also fix RequestManagerImpl::~RequestManagerImpl not doing a wakeup after setting running = false. This meant that in the worst case the worker would wake up 1000ms later and only then notice running = false and exit, making the game take overall longer to exit. --- src/client/http/requestmanager/Libcurl.cpp | 36 ++++++++++++++++++++-- 1 file changed, 33 insertions(+), 3 deletions(-) diff --git a/src/client/http/requestmanager/Libcurl.cpp b/src/client/http/requestmanager/Libcurl.cpp index 828daf23c..657b2085e 100644 --- a/src/client/http/requestmanager/Libcurl.cpp +++ b/src/client/http/requestmanager/Libcurl.cpp @@ -14,6 +14,9 @@ #if defined(CURL_AT_LEAST_VERSION) && CURL_AT_LEAST_VERSION(7, 61, 0) # define REQUEST_USE_CURL_TLSV13CL #endif +#if defined(CURL_AT_LEAST_VERSION) && CURL_AT_LEAST_VERSION(7, 68, 0) +# define REQUEST_USE_CURL_MULTI_POLL +#endif constexpr long curlMaxHostConnections = 1; constexpr long curlMaxConcurrentStreams = httpMaxConcurrentStreams; @@ -137,6 +140,32 @@ namespace http bool curlGlobalInit = false; CURLM *curlMulti = NULL; + + void Wake() + { +#ifdef REQUEST_USE_CURL_MULTI_POLL + curl_multi_wakeup(curlMulti); +#endif + } + + void Wait() + { + int dontcare; +#ifdef REQUEST_USE_CURL_MULTI_POLL + HandleCURLMcode(curl_multi_poll(curlMulti, NULL, 0, 100000, &dontcare)); +#else + constexpr auto TickMs = 100; + if (requestHandles.size()) + { + HandleCURLMcode(curl_multi_wait(curlMulti, NULL, 0, TickMs, &dontcare)); + } + else + { + std::this_thread::sleep_for(std::chrono::milliseconds(TickMs)); + return; + } +#endif + } }; RequestManagerImpl::RequestManagerImpl(ByteString newProxy, ByteString newCafile, ByteString newCapath, bool newDisableNetwork) : @@ -153,6 +182,7 @@ namespace http std::lock_guard lk(sharedStateMx); running = false; } + Wake(); worker.join(); } @@ -175,8 +205,8 @@ namespace http void RequestManagerImpl::WorkerPerform() { auto manager = static_cast(this); + manager->Wait(); int dontcare; - HandleCURLMcode(curl_multi_poll(manager->curlMulti, NULL, 0, 1000, &dontcare)); HandleCURLMcode(curl_multi_perform(manager->curlMulti, &dontcare)); while (auto msg = curl_multi_info_read(manager->curlMulti, &dontcare)) { @@ -313,7 +343,7 @@ namespace http std::lock_guard lk(manager->sharedStateMx); manager->requestHandlesToRegister.push_back(request.handle); } - curl_multi_wakeup(manager->curlMulti); + manager->Wake(); } void RequestManager::UnregisterRequestImpl(Request &request) @@ -323,7 +353,7 @@ namespace http std::lock_guard lk(manager->sharedStateMx); manager->requestHandlesToUnregister.push_back(request.handle); } - curl_multi_wakeup(manager->curlMulti); + manager->Wake(); } void RequestManagerImpl::RegisterRequestHandle(std::shared_ptr requestHandle)