Merge pull request #7841 from infclass/kaffeine/http-state

Make http states a enum class and use it across the codebase
This commit is contained in:
Dennis Felsing 2024-01-21 22:35:27 +00:00 committed by GitHub
commit 24612a003e
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
9 changed files with 44 additions and 44 deletions

View file

@ -2579,9 +2579,9 @@ void CClient::Update()
if(m_pMapdownloadTask)
{
if(m_pMapdownloadTask->State() == HTTP_DONE)
if(m_pMapdownloadTask->State() == EHttpState::DONE)
FinishMapDownload();
else if(m_pMapdownloadTask->State() == HTTP_ERROR || m_pMapdownloadTask->State() == HTTP_ABORTED)
else if(m_pMapdownloadTask->State() == EHttpState::ERROR || m_pMapdownloadTask->State() == EHttpState::ABORTED)
{
dbg_msg("webdl", "http failed, falling back to gameserver");
ResetMapDownload();
@ -2591,14 +2591,14 @@ void CClient::Update()
if(m_pDDNetInfoTask)
{
if(m_pDDNetInfoTask->State() == HTTP_DONE)
if(m_pDDNetInfoTask->State() == EHttpState::DONE)
FinishDDNetInfo();
else if(m_pDDNetInfoTask->State() == HTTP_ERROR)
else if(m_pDDNetInfoTask->State() == EHttpState::ERROR)
{
Storage()->RemoveFile(m_aDDNetInfoTmp, IStorage::TYPE_SAVE);
ResetDDNetInfo();
}
else if(m_pDDNetInfoTask->State() == HTTP_ABORTED)
else if(m_pDDNetInfoTask->State() == EHttpState::ABORTED)
{
Storage()->RemoveFile(m_aDDNetInfoTmp, IStorage::TYPE_SAVE);
m_pDDNetInfoTask = NULL;

View file

@ -184,12 +184,12 @@ void CChooseMaster::CJob::Run()
m_pParent->m_pHttp->Run(pHead);
pHead->Wait();
if(pHead->State() == HTTP_ABORTED)
if(pHead->State() == EHttpState::ABORTED)
{
dbg_msg("serverbrowse_http", "master chooser aborted");
return;
}
if(pHead->State() != HTTP_DONE)
if(pHead->State() != EHttpState::DONE)
{
continue;
}
@ -207,12 +207,12 @@ void CChooseMaster::CJob::Run()
pGet->Wait();
auto Time = std::chrono::duration_cast<std::chrono::milliseconds>(time_get_nanoseconds() - StartTime);
if(pHead->State() == HTTP_ABORTED)
if(pHead->State() == EHttpState::ABORTED)
{
dbg_msg("serverbrowse_http", "master chooser aborted");
return;
}
if(pGet->State() != HTTP_DONE)
if(pGet->State() != EHttpState::DONE)
{
continue;
}
@ -345,7 +345,7 @@ void CServerBrowserHttp::Update()
}
else if(m_State == STATE_REFRESHING)
{
if(m_pGetServers->State() == HTTP_QUEUED || m_pGetServers->State() == HTTP_RUNNING)
if(m_pGetServers->State() == EHttpState::QUEUED || m_pGetServers->State() == EHttpState::RUNNING)
{
return;
}

View file

@ -24,7 +24,7 @@ class CUpdaterFetchTask : public CHttpRequest
void OnProgress() override;
protected:
void OnCompletion(int State) override;
void OnCompletion(EHttpState State) override;
public:
CUpdaterFetchTask(CUpdater *pUpdater, const char *pFile, const char *pDestPath);
@ -59,7 +59,7 @@ void CUpdaterFetchTask::OnProgress()
m_pUpdater->m_Percent = Progress();
}
void CUpdaterFetchTask::OnCompletion(int State)
void CUpdaterFetchTask::OnCompletion(EHttpState State)
{
const char *pFileName = 0;
for(const char *pPath = Dest(); *pPath; pPath++)
@ -68,9 +68,9 @@ void CUpdaterFetchTask::OnCompletion(int State)
pFileName = pFileName ? pFileName : Dest();
if(!str_comp(pFileName, "update.json"))
{
if(State == HTTP_DONE)
if(State == EHttpState::DONE)
m_pUpdater->SetCurrentState(IUpdater::GOT_MANIFEST);
else if(State == HTTP_ERROR)
else if(State == EHttpState::ERROR)
m_pUpdater->SetCurrentState(IUpdater::FAIL);
}
}
@ -297,7 +297,7 @@ void CUpdater::RunningUpdate()
{
return;
}
else if(m_pCurrentTask->State() == HTTP_ERROR || m_pCurrentTask->State() == HTTP_ABORTED)
else if(m_pCurrentTask->State() == EHttpState::ERROR || m_pCurrentTask->State() == EHttpState::ABORTED)
{
SetCurrentState(IUpdater::FAIL);
}

View file

@ -411,7 +411,7 @@ void CRegister::CProtocol::CJob::Run()
{
m_pHttp->Run(m_pRegister);
m_pRegister->Wait();
if(m_pRegister->State() != HTTP_DONE)
if(m_pRegister->State() != EHttpState::DONE)
{
// TODO: log the error response content from master
// TODO: exponential backoff

View file

@ -270,7 +270,7 @@ int CHttpRequest::ProgressCallback(void *pUser, double DlTotal, double DlCurr, d
void CHttpRequest::OnCompletionInternal(std::optional<unsigned int> Result)
{
int State;
EHttpState State;
if(Result.has_value())
{
CURLcode Code = static_cast<CURLcode>(Result.value());
@ -278,22 +278,22 @@ void CHttpRequest::OnCompletionInternal(std::optional<unsigned int> Result)
{
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;
State = (Code == CURLE_ABORTED_BY_CALLBACK) ? EHttpState::ABORTED : EHttpState::ERROR;
}
else
{
if(g_Config.m_DbgCurl || m_LogProgress >= HTTPLOG::ALL)
dbg_msg("http", "task done: %s", m_aUrl);
State = HTTP_DONE;
State = EHttpState::DONE;
}
}
else
{
dbg_msg("http", "%s failed. internal error: %s", m_aUrl, m_aErr);
State = HTTP_ERROR;
State = EHttpState::ERROR;
}
if(State == HTTP_DONE && m_ExpectedSha256 != SHA256_ZEROED)
if(State == EHttpState::DONE && m_ExpectedSha256 != SHA256_ZEROED)
{
const SHA256_DIGEST ActualSha256 = sha256_finish(&m_ActualSha256);
if(ActualSha256 != m_ExpectedSha256)
@ -306,7 +306,7 @@ void CHttpRequest::OnCompletionInternal(std::optional<unsigned int> Result)
sha256_str(m_ExpectedSha256, aExpectedSha256, sizeof(aExpectedSha256));
dbg_msg("http", "SHA256 mismatch: got=%s, expected=%s, url=%s", aActualSha256, aExpectedSha256, m_aUrl);
}
State = HTTP_ERROR;
State = EHttpState::ERROR;
}
}
@ -315,10 +315,10 @@ void CHttpRequest::OnCompletionInternal(std::optional<unsigned int> Result)
if(m_File && io_close(m_File) != 0)
{
dbg_msg("http", "i/o error, cannot close file: %s", m_aDest);
State = HTTP_ERROR;
State = EHttpState::ERROR;
}
if(State == HTTP_ERROR || State == HTTP_ABORTED)
if(State == EHttpState::ERROR || State == EHttpState::ABORTED)
{
fs_remove(m_aDestAbsolute);
}
@ -357,8 +357,8 @@ void CHttpRequest::Wait()
// This is so uncommon that polling just might work
for(;;)
{
int State = m_State.load(std::memory_order_seq_cst);
if(State != HTTP_QUEUED && State != HTTP_RUNNING)
EHttpState State = m_State.load(std::memory_order_seq_cst);
if(State != EHttpState::QUEUED && State != EHttpState::RUNNING)
{
return;
}
@ -368,7 +368,7 @@ void CHttpRequest::Wait()
void CHttpRequest::Result(unsigned char **ppResult, size_t *pResultLength) const
{
if(m_WriteToFile || State() != HTTP_DONE)
if(m_WriteToFile || State() != EHttpState::DONE)
{
*ppResult = nullptr;
*pResultLength = 0;

View file

@ -18,13 +18,13 @@
typedef struct _json_value json_value;
class IStorage;
enum
enum class EHttpState
{
HTTP_ERROR = -1,
HTTP_QUEUED,
HTTP_RUNNING,
HTTP_DONE,
HTTP_ABORTED,
ERROR = -1,
QUEUED,
RUNNING,
DONE,
ABORTED,
};
enum class HTTPLOG
@ -111,7 +111,7 @@ class CHttpRequest : public IHttpRequest
IPRESOLVE m_IpResolve = IPRESOLVE::WHATEVER;
char m_aErr[256]; // 256 == CURL_ERROR_SIZE
std::atomic<int> m_State{HTTP_QUEUED};
std::atomic<EHttpState> m_State{EHttpState::QUEUED};
std::atomic<bool> m_Abort{false};
// Abort the request with an error if `BeforeInit()` returns false.
@ -129,7 +129,7 @@ class CHttpRequest : public IHttpRequest
protected:
// These run on the curl thread now, DO NOT STALL THE THREAD
virtual void OnProgress() {}
virtual void OnCompletion(int State) {}
virtual void OnCompletion(EHttpState State) {}
public:
CHttpRequest(const char *pUrl);
@ -185,11 +185,11 @@ public:
double Current() const { return m_Current.load(std::memory_order_relaxed); }
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; }
EHttpState State() const { return m_State; }
bool Done() const
{
int State = m_State;
return State != HTTP_QUEUED && State != HTTP_RUNNING;
EHttpState State = m_State;
return State != EHttpState::QUEUED && State != EHttpState::RUNNING;
}
void Abort() { m_Abort = true; }

View file

@ -1953,7 +1953,7 @@ void CMenus::UpdateCommunityIcons()
std::shared_ptr<CCommunityIconDownloadJob> pJob = m_CommunityIconDownloadJobs.front();
if(pJob->Done())
{
if(pJob->State() == HTTP_DONE)
if(pJob->State() == EHttpState::DONE)
{
std::shared_ptr<CCommunityIconLoadJob> pLoadJob = std::make_shared<CCommunityIconLoadJob>(this, pJob->CommunityId(), IStorage::TYPE_SAVE);
Engine()->AddJob(pLoadJob);

View file

@ -21,10 +21,10 @@ bool CSkins::IsVanillaSkin(const char *pName)
return std::any_of(std::begin(VANILLA_SKINS), std::end(VANILLA_SKINS), [pName](const char *pVanillaSkin) { return str_comp(pName, pVanillaSkin) == 0; });
}
void CSkins::CGetPngFile::OnCompletion(int State)
void CSkins::CGetPngFile::OnCompletion(EHttpState State)
{
// Maybe this should start another thread to load the png in instead of stalling the curl thread
if(State == HTTP_DONE)
if(State == EHttpState::DONE)
{
m_pSkins->LoadSkinPNG(m_Info, Dest(), Dest(), IStorage::TYPE_SAVE);
}
@ -410,7 +410,7 @@ const CSkin *CSkins::FindImpl(const char *pName)
const auto SkinDownloadIt = m_DownloadSkins.find(pName);
if(SkinDownloadIt != m_DownloadSkins.end())
{
if(SkinDownloadIt->second->m_pTask && SkinDownloadIt->second->m_pTask->State() == HTTP_DONE && SkinDownloadIt->second->m_pTask->m_Info.m_pData)
if(SkinDownloadIt->second->m_pTask && SkinDownloadIt->second->m_pTask->State() == EHttpState::DONE && SkinDownloadIt->second->m_pTask->m_Info.m_pData)
{
char aPath[IO_MAX_PATH_LENGTH];
str_format(aPath, sizeof(aPath), "downloadedskins/%s.png", SkinDownloadIt->second->GetName());
@ -420,7 +420,7 @@ const CSkin *CSkins::FindImpl(const char *pName)
--m_DownloadingSkins;
return pSkin;
}
if(SkinDownloadIt->second->m_pTask && (SkinDownloadIt->second->m_pTask->State() == HTTP_ERROR || SkinDownloadIt->second->m_pTask->State() == HTTP_ABORTED))
if(SkinDownloadIt->second->m_pTask && (SkinDownloadIt->second->m_pTask->State() == EHttpState::ERROR || SkinDownloadIt->second->m_pTask->State() == EHttpState::ABORTED))
{
SkinDownloadIt->second->m_pTask = nullptr;
--m_DownloadingSkins;

View file

@ -20,7 +20,7 @@ public:
CSkins *m_pSkins;
protected:
virtual void OnCompletion(int State) override;
virtual void OnCompletion(EHttpState State) override;
public:
CGetPngFile(CSkins *pSkins, const char *pUrl, IStorage *pStorage, const char *pDest);