Format and bump fake curl

This commit is contained in:
Learath 2023-12-18 22:53:23 +01:00
parent d847b0f60c
commit 1f224344ec
6 changed files with 61 additions and 33 deletions

@ -1 +1 @@
Subproject commit 59d64dbb36ade02607ad20a7f3a45605ab1de80d
Subproject commit 4d796ea119b52c8901286e14ab96faf7353a5d59

View file

@ -3,14 +3,16 @@
#include "kernel.h"
class IHttpRequest {};
class IHttpRequest
{
};
class IHttp : public IInterface
{
MACRO_INTERFACE("http")
MACRO_INTERFACE("http")
public:
virtual void Run(std::shared_ptr<IHttpRequest> pRequest) = 0;
virtual void Run(std::shared_ptr<IHttpRequest> pRequest) = 0;
};
#endif

View file

@ -8,8 +8,8 @@
#include <engine/storage.h>
#include <game/version.h>
#include <thread>
#include <limits>
#include <thread>
#if !defined(CONF_FAMILY_WINDOWS)
#include <csignal>
@ -272,8 +272,8 @@ void CHttpRequest::OnCompletionInternal(std::optional<unsigned int> Result)
if(Code != CURLE_OK)
{
if(g_Config.m_DbgCurl || m_LogProgress >= HTTPLOG::FAILURE)
dbg_msg("http", "%s failed. libcurl error (%u): %s", m_aUrl, Code, m_aErr);
State = (Code == CURLE_ABORTED_BY_CALLBACK) ? HTTP_ABORTED : HTTP_ERROR;
dbg_msg("http", "%s failed. libcurl error (%u): %s", m_aUrl, Code, m_aErr);
State = (Code == CURLE_ABORTED_BY_CALLBACK) ? HTTP_ABORTED : HTTP_ERROR;
}
else
{
@ -282,7 +282,8 @@ void CHttpRequest::OnCompletionInternal(std::optional<unsigned int> Result)
State = HTTP_DONE;
}
}
else {
else
{
dbg_msg("http", "%s failed. internal error: %s", m_aUrl, m_aErr);
State = HTTP_ERROR;
}
@ -346,9 +347,11 @@ void CHttpRequest::Wait()
using namespace std::chrono_literals;
// This is so uncommon that polling just might work
for(;;) {
for(;;)
{
int State = m_State.load(std::memory_order_seq_cst);
if(State != HTTP_QUEUED && State != HTTP_RUNNING) {
if(State != HTTP_QUEUED && State != HTTP_RUNNING)
{
return;
}
std::this_thread::sleep_for(10ms);
@ -391,7 +394,7 @@ bool CHttp::Init(std::chrono::milliseconds ShutdownDelay)
m_pThread = thread_init(CHttp::ThreadMain, this, "http");
std::unique_lock Lock(m_Lock);
m_Cv.wait(Lock, [this](){ return m_State != UNINITIALIZED; });
m_Cv.wait(Lock, [this]() { return m_State != UNINITIALIZED; });
if(m_State != RUNNING)
{
return false;
@ -437,24 +440,29 @@ void CHttp::RunLoop()
dbg_msg("http", "running");
Lock.unlock();
while(m_State == RUNNING) {
while(m_State == RUNNING)
{
static int NextTimeout = std::numeric_limits<int>::max();
int Events = 0;
CURLMcode mc = curl_multi_poll(m_pMultiH, NULL, 0, NextTimeout, &Events);
// We may have been woken up for a shutdown
if(m_Shutdown) {
if(m_Shutdown)
{
auto Now = std::chrono::steady_clock::now();
if(!m_ShutdownTime.has_value()) {
if(!m_ShutdownTime.has_value())
{
m_ShutdownTime = Now + m_ShutdownDelay;
NextTimeout = m_ShutdownDelay.count();
}
else if(m_ShutdownTime < Now || m_RunningRequests.empty()) {
else if(m_ShutdownTime < Now || m_RunningRequests.empty())
{
break;
}
}
if(mc != CURLM_OK) {
if(mc != CURLM_OK)
{
Lock.lock();
dbg_msg("http", "Failed multi wait: %s", curl_multi_strerror(mc));
m_State = ERROR;
@ -462,7 +470,8 @@ void CHttp::RunLoop()
}
mc = curl_multi_perform(m_pMultiH, &Events);
if(mc != CURLM_OK) {
if(mc != CURLM_OK)
{
Lock.lock();
dbg_msg("http", "Failed multi perform: %s", curl_multi_strerror(mc));
m_State = ERROR;
@ -470,8 +479,10 @@ void CHttp::RunLoop()
}
struct CURLMsg *m;
while((m = curl_multi_info_read(m_pMultiH, &Events))) {
if(m->msg == CURLMSG_DONE) {
while((m = curl_multi_info_read(m_pMultiH, &Events)))
{
if(m->msg == CURLMSG_DONE)
{
auto RequestIt = m_RunningRequests.find(m->easy_handle);
dbg_assert(RequestIt != m_RunningRequests.end(), "Running handle not added to map");
auto pRequest = std::move(RequestIt->second);
@ -488,7 +499,8 @@ void CHttp::RunLoop()
std::swap(m_PendingRequests, NewRequests);
Lock.unlock();
while(!NewRequests.empty()) {
while(!NewRequests.empty())
{
auto &pRequest = NewRequests.front();
dbg_msg("http", "task: %s %s", CHttpRequest::GetRequestType(pRequest->m_Type), pRequest->m_aUrl);
@ -518,7 +530,8 @@ void CHttp::RunLoop()
}
// Only happens if m_State == ERROR, thus we already hold the lock
if(!NewRequests.empty()) {
if(!NewRequests.empty())
{
m_PendingRequests.insert(m_PendingRequests.end(), std::make_move_iterator(NewRequests.begin()), std::make_move_iterator(NewRequests.end()));
break;
}
@ -528,14 +541,17 @@ void CHttp::RunLoop()
Lock.lock();
bool Cleanup = m_State != ERROR;
for(auto &pRequest : m_PendingRequests) {
for(auto &pRequest : m_PendingRequests)
{
str_copy(pRequest->m_aErr, "Shutting down");
pRequest->OnCompletionInternal(std::nullopt);
}
for(auto &ReqPair : m_RunningRequests) {
for(auto &ReqPair : m_RunningRequests)
{
auto &[pHandle, pRequest] = ReqPair;
if(Cleanup) {
if(Cleanup)
{
curl_multi_remove_handle(m_pMultiH, pHandle);
curl_easy_cleanup(pHandle);
}
@ -544,7 +560,8 @@ void CHttp::RunLoop()
pRequest->OnCompletionInternal(std::nullopt);
}
if(Cleanup) {
if(Cleanup)
{
curl_multi_cleanup(m_pMultiH);
curl_global_cleanup();
}
@ -553,7 +570,7 @@ void CHttp::RunLoop()
void CHttp::Run(std::shared_ptr<IHttpRequest> pRequest)
{
std::unique_lock Lock(m_Lock);
m_Cv.wait(Lock, [this](){ return m_State != UNINITIALIZED; });
m_Cv.wait(Lock, [this]() { return m_State != UNINITIALIZED; });
m_PendingRequests.emplace_back(std::move(std::static_pointer_cast<CHttpRequest>(pRequest)));
curl_multi_wakeup(m_pMultiH);
}

View file

@ -7,9 +7,9 @@
#include <algorithm>
#include <atomic>
#include <mutex>
#include <condition_variable>
#include <deque>
#include <mutex>
#include <optional>
#include <engine/http.h>
@ -127,8 +127,8 @@ class CHttpRequest : public IHttpRequest
protected:
// These run on the curl thread now, DO NOT STALL THE THREAD
virtual void OnProgress() {};
virtual void OnCompletion() {};
virtual void OnProgress(){};
virtual void OnCompletion(){};
public:
CHttpRequest(const char *pUrl);
@ -185,7 +185,11 @@ public:
double Size() const { return m_Size.load(std::memory_order_relaxed); }
int Progress() const { return m_Progress.load(std::memory_order_relaxed); }
int State() const { return m_State; }
bool Done() const { int State = m_State; return State != HTTP_QUEUED && State != HTTP_DONE; }
bool Done() const
{
int State = m_State;
return State != HTTP_QUEUED && State != HTTP_DONE;
}
void Abort() { m_Abort = true; }
void Wait();
@ -237,7 +241,8 @@ bool HttpHasIpresolveBug();
// In an ideal world this would be a kernel interface
class CHttp : public IHttp
{
enum EState {
enum EState
{
UNINITIALIZED,
RUNNING,
STOPPING,

View file

@ -519,7 +519,7 @@ protected:
SHA256_DIGEST m_Sha256;
CAbstractCommunityIconJob(CMenus *pMenus, const char *pCommunityId, int StorageType);
virtual ~CAbstractCommunityIconJob() {};
virtual ~CAbstractCommunityIconJob(){};
public:
const char *CommunityId() const { return m_aCommunityId; }
@ -530,6 +530,7 @@ protected:
class CCommunityIconLoadJob : public IJob, public CAbstractCommunityIconJob
{
CImageInfo m_ImageInfo;
protected:
void Run() override;

View file

@ -252,7 +252,10 @@ public:
return m_pUpdater;
}
#endif
class IHttp *Http() { return m_pHttp; }
class IHttp *Http()
{
return m_pHttp;
}
int NetobjNumCorrections()
{