Skip to content

Commit

Permalink
Make http states a enum class and use it across the codebase
Browse files Browse the repository at this point in the history
  • Loading branch information
Kaffeine committed Jan 21, 2024
1 parent c2ecf1a commit 3966020
Show file tree
Hide file tree
Showing 9 changed files with 39 additions and 39 deletions.
10 changes: 5 additions & 5 deletions src/engine/client/client.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2579,9 +2579,9 @@ void CClient::Update()

if(m_pMapdownloadTask)
{
if(m_pMapdownloadTask->State() == HTTP_DONE)
if(m_pMapdownloadTask->State() == EHttpState::HTTP_DONE)
FinishMapDownload();
else if(m_pMapdownloadTask->State() == HTTP_ERROR || m_pMapdownloadTask->State() == HTTP_ABORTED)
else if(m_pMapdownloadTask->State() == EHttpState::HTTP_ERROR || m_pMapdownloadTask->State() == EHttpState::HTTP_ABORTED)
{
dbg_msg("webdl", "http failed, falling back to gameserver");
ResetMapDownload();
Expand All @@ -2591,14 +2591,14 @@ void CClient::Update()

if(m_pDDNetInfoTask)
{
if(m_pDDNetInfoTask->State() == HTTP_DONE)
if(m_pDDNetInfoTask->State() == EHttpState::HTTP_DONE)
FinishDDNetInfo();
else if(m_pDDNetInfoTask->State() == HTTP_ERROR)
else if(m_pDDNetInfoTask->State() == EHttpState::HTTP_ERROR)
{
Storage()->RemoveFile(m_aDDNetInfoTmp, IStorage::TYPE_SAVE);
ResetDDNetInfo();
}
else if(m_pDDNetInfoTask->State() == HTTP_ABORTED)
else if(m_pDDNetInfoTask->State() == EHttpState::HTTP_ABORTED)
{
Storage()->RemoveFile(m_aDDNetInfoTmp, IStorage::TYPE_SAVE);
m_pDDNetInfoTask = NULL;
Expand Down
10 changes: 5 additions & 5 deletions src/engine/client/serverbrowser_http.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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::HTTP_ABORTED)
{
dbg_msg("serverbrowse_http", "master chooser aborted");
return;
}
if(pHead->State() != HTTP_DONE)
if(pHead->State() != EHttpState::HTTP_DONE)
{
continue;
}
Expand All @@ -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::HTTP_ABORTED)
{
dbg_msg("serverbrowse_http", "master chooser aborted");
return;
}
if(pGet->State() != HTTP_DONE)
if(pGet->State() != EHttpState::HTTP_DONE)
{
continue;
}
Expand Down Expand Up @@ -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::HTTP_QUEUED || m_pGetServers->State() == EHttpState::HTTP_RUNNING)
{
return;
}
Expand Down
10 changes: 5 additions & 5 deletions src/engine/client/updater.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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++)
Expand All @@ -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::HTTP_DONE)
m_pUpdater->SetCurrentState(IUpdater::GOT_MANIFEST);
else if(State == HTTP_ERROR)
else if(State == EHttpState::HTTP_ERROR)
m_pUpdater->SetCurrentState(IUpdater::FAIL);
}
}
Expand Down Expand Up @@ -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::HTTP_ERROR || m_pCurrentTask->State() == EHttpState::HTTP_ABORTED)
{
SetCurrentState(IUpdater::FAIL);
}
Expand Down
2 changes: 1 addition & 1 deletion src/engine/server/register.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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::HTTP_DONE)
{
// TODO: log the error response content from master
// TODO: exponential backoff
Expand Down
22 changes: 11 additions & 11 deletions src/engine/shared/http.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -270,30 +270,30 @@ 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());
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;
State = (Code == CURLE_ABORTED_BY_CALLBACK) ? EHttpState::HTTP_ABORTED : EHttpState::HTTP_ERROR;
}
else
{
if(g_Config.m_DbgCurl || m_LogProgress >= HTTPLOG::ALL)
dbg_msg("http", "task done: %s", m_aUrl);
State = HTTP_DONE;
State = EHttpState::HTTP_DONE;
}
}
else
{
dbg_msg("http", "%s failed. internal error: %s", m_aUrl, m_aErr);
State = HTTP_ERROR;
State = EHttpState::HTTP_ERROR;
}

if(State == HTTP_DONE && m_ExpectedSha256 != SHA256_ZEROED)
if(State == EHttpState::HTTP_DONE && m_ExpectedSha256 != SHA256_ZEROED)
{
const SHA256_DIGEST ActualSha256 = sha256_finish(&m_ActualSha256);
if(ActualSha256 != m_ExpectedSha256)
Expand All @@ -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::HTTP_ERROR;
}
}

Expand All @@ -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::HTTP_ERROR;
}

if(State == HTTP_ERROR || State == HTTP_ABORTED)
if(State == EHttpState::HTTP_ERROR || State == EHttpState::HTTP_ABORTED)
{
fs_remove(m_aDestAbsolute);
}
Expand Down Expand Up @@ -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::HTTP_QUEUED && State != EHttpState::HTTP_RUNNING)
{
return;
}
Expand All @@ -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::HTTP_DONE)
{
*ppResult = nullptr;
*pResultLength = 0;
Expand Down
12 changes: 6 additions & 6 deletions src/engine/shared/http.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
typedef struct _json_value json_value;
class IStorage;

enum
enum class EHttpState
{
HTTP_ERROR = -1,
HTTP_QUEUED,
Expand Down Expand Up @@ -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::HTTP_QUEUED};
std::atomic<bool> m_Abort{false};

// Abort the request with an error if `BeforeInit()` returns false.
Expand All @@ -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);
Expand Down Expand Up @@ -185,11 +185,11 @@ class CHttpRequest : public IHttpRequest
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::HTTP_QUEUED && State != EHttpState::HTTP_RUNNING;
}
void Abort() { m_Abort = true; }

Expand Down
2 changes: 1 addition & 1 deletion src/game/client/components/menus_browser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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::HTTP_DONE)
{
std::shared_ptr<CCommunityIconLoadJob> pLoadJob = std::make_shared<CCommunityIconLoadJob>(this, pJob->CommunityId(), IStorage::TYPE_SAVE);
Engine()->AddJob(pLoadJob);
Expand Down
8 changes: 4 additions & 4 deletions src/game/client/components/skins.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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::HTTP_DONE)
{
m_pSkins->LoadSkinPNG(m_Info, Dest(), Dest(), IStorage::TYPE_SAVE);
}
Expand Down Expand Up @@ -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::HTTP_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());
Expand All @@ -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::HTTP_ERROR || SkinDownloadIt->second->m_pTask->State() == EHttpState::HTTP_ABORTED))
{
SkinDownloadIt->second->m_pTask = nullptr;
--m_DownloadingSkins;
Expand Down
2 changes: 1 addition & 1 deletion src/game/client/components/skins.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ class CSkins : public CComponent
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);
Expand Down

0 comments on commit 3966020

Please sign in to comment.