From b7c3a6dfd8d10459d2400e5a0922c504e032393b Mon Sep 17 00:00:00 2001 From: def Date: Fri, 4 Sep 2020 19:56:30 +0200 Subject: [PATCH] Allow different timeouts for different downloads --- src/engine/client/client.cpp | 6 +++--- src/engine/client/http.cpp | 32 ++++++++++++-------------------- src/engine/client/http.h | 18 +++++++++++++----- src/engine/client/updater.cpp | 2 +- src/game/variables.h | 11 +++++------ 5 files changed, 34 insertions(+), 35 deletions(-) diff --git a/src/engine/client/client.cpp b/src/engine/client/client.cpp index 633faa76d..99f2f9a0f 100644 --- a/src/engine/client/client.cpp +++ b/src/engine/client/client.cpp @@ -1736,9 +1736,9 @@ void CClient::ProcessServerPacket(CNetChunk *pPacket) char aUrl[256]; char aEscaped[256]; EscapeUrl(aEscaped, sizeof(aEscaped), aFilename); - str_format(aUrl, sizeof(aUrl), "%s/%s", g_Config.m_ClDDNetMapDownloadUrl, aEscaped); + str_format(aUrl, sizeof(aUrl), "%s/%s", g_Config.m_ClMapDownloadUrl, aEscaped); - m_pMapdownloadTask = std::make_shared(Storage(), aUrl, m_aMapdownloadFilename, IStorage::TYPE_SAVE, true); + m_pMapdownloadTask = std::make_shared(Storage(), aUrl, m_aMapdownloadFilename, IStorage::TYPE_SAVE, CTimeout{g_Config.m_ClMapDownloadConnectTimeoutMs, g_Config.m_ClMapDownloadLowSpeedLimit, g_Config.m_ClMapDownloadLowSpeedTime}); Engine()->AddJob(m_pMapdownloadTask); } else @@ -4303,7 +4303,7 @@ void CClient::RequestDDNetInfo() str_append(aUrl, aEscaped, sizeof(aUrl)); } - m_pDDNetInfoTask = std::make_shared(Storage(), aUrl, m_aDDNetInfoTmp, IStorage::TYPE_SAVE, true); + m_pDDNetInfoTask = std::make_shared(Storage(), aUrl, m_aDDNetInfoTmp, IStorage::TYPE_SAVE, CTimeout{10000, 500, 10}); Engine()->AddJob(m_pDDNetInfoTask); } diff --git a/src/engine/client/http.cpp b/src/engine/client/http.cpp index f0d8eca16..5ccc65002 100644 --- a/src/engine/client/http.cpp +++ b/src/engine/client/http.cpp @@ -75,8 +75,8 @@ void EscapeUrl(char *pBuf, int Size, const char *pStr) curl_free(pEsc); } -CRequest::CRequest(const char *pUrl, bool CanTimeout) : - m_CanTimeout(CanTimeout), +CRequest::CRequest(const char *pUrl, CTimeout Timeout) : + m_Timeout(Timeout), m_Size(0), m_Progress(0), m_State(HTTP_QUEUED), @@ -115,18 +115,10 @@ int CRequest::RunImpl(CURL *pHandle) char aErr[CURL_ERROR_SIZE]; curl_easy_setopt(pHandle, CURLOPT_ERRORBUFFER, aErr); - if(m_CanTimeout) - { - curl_easy_setopt(pHandle, CURLOPT_CONNECTTIMEOUT_MS, (long)g_Config.m_ClHTTPConnectTimeoutMs); - curl_easy_setopt(pHandle, CURLOPT_LOW_SPEED_LIMIT, (long)g_Config.m_ClHTTPLowSpeedLimit); - curl_easy_setopt(pHandle, CURLOPT_LOW_SPEED_TIME, (long)g_Config.m_ClHTTPLowSpeedTime); - } - else - { - curl_easy_setopt(pHandle, CURLOPT_CONNECTTIMEOUT_MS, 0L); - curl_easy_setopt(pHandle, CURLOPT_LOW_SPEED_LIMIT, 0L); - curl_easy_setopt(pHandle, CURLOPT_LOW_SPEED_TIME, 0L); - } + curl_easy_setopt(pHandle, CURLOPT_CONNECTTIMEOUT_MS, m_Timeout.ConnectTimeoutMs); + curl_easy_setopt(pHandle, CURLOPT_LOW_SPEED_LIMIT, m_Timeout.LowSpeedLimit); + curl_easy_setopt(pHandle, CURLOPT_LOW_SPEED_TIME, m_Timeout.LowSpeedTime); + curl_easy_setopt(pHandle, CURLOPT_SHARE, gs_Share); curl_easy_setopt(pHandle, CURLOPT_PROTOCOLS, CURLPROTO_HTTP | CURLPROTO_HTTPS); curl_easy_setopt(pHandle, CURLOPT_FOLLOWLOCATION, 1L); @@ -181,8 +173,8 @@ int CRequest::ProgressCallback(void *pUser, double DlTotal, double DlCurr, doubl return pTask->m_Abort ? -1 : 0; } -CGet::CGet(const char *pUrl, bool CanTimeout) : - CRequest(pUrl, CanTimeout), +CGet::CGet(const char *pUrl, CTimeout Timeout) : + CRequest(pUrl, Timeout), m_BufferSize(0), m_BufferLength(0), m_pBuffer(NULL) @@ -254,8 +246,8 @@ size_t CGet::OnData(char *pData, size_t DataSize) return DataSize; } -CGetFile::CGetFile(IStorage *pStorage, const char *pUrl, const char *pDest, int StorageType, bool CanTimeout) : - CRequest(pUrl, CanTimeout), +CGetFile::CGetFile(IStorage *pStorage, const char *pUrl, const char *pDest, int StorageType, CTimeout Timeout) : + CRequest(pUrl, Timeout), m_pStorage(pStorage), m_StorageType(StorageType) { @@ -295,8 +287,8 @@ bool CGetFile::BeforeCompletion() return io_close(m_File) == 0; } -CPostJson::CPostJson(const char *pUrl, bool CanTimeout, const char *pJson) - : CRequest(pUrl, CanTimeout) +CPostJson::CPostJson(const char *pUrl, CTimeout Timeout, const char *pJson) + : CRequest(pUrl, Timeout) { str_copy(m_aJson, pJson, sizeof(m_aJson)); } diff --git a/src/engine/client/http.h b/src/engine/client/http.h index ddf98a7ca..e5369b2c2 100644 --- a/src/engine/client/http.h +++ b/src/engine/client/http.h @@ -17,6 +17,13 @@ enum HTTP_ABORTED, }; +struct CTimeout +{ + long ConnectTimeoutMs; + long LowSpeedLimit; + long LowSpeedTime; +}; + class CRequest : public IJob { // Abort the request with an error if `BeforeInit()` or `AfterInit()` @@ -31,7 +38,8 @@ class CRequest : public IJob virtual void OnCompletion() { } char m_aUrl[256]; - bool m_CanTimeout; + + CTimeout m_Timeout; double m_Size; double m_Current; @@ -47,7 +55,7 @@ class CRequest : public IJob int RunImpl(CURL *pHandle); public: - CRequest(const char *pUrl, bool CanTimeout); + CRequest(const char *pUrl, CTimeout Timeout); double Current() const { return m_Current; } double Size() const { return m_Size; } @@ -65,7 +73,7 @@ class CGet : public CRequest unsigned char *m_pBuffer; public: - CGet(const char *pUrl, bool CanTimeout); + CGet(const char *pUrl, CTimeout Timeout); ~CGet(); size_t ResultSize() const { if(!Result()) { return 0; } else { return m_BufferSize; } } @@ -87,7 +95,7 @@ class CGetFile : public CRequest IOHANDLE m_File; public: - CGetFile(IStorage *pStorage, const char *pUrl, const char *pDest, int StorageType = -2, bool CanTimeout = true); + CGetFile(IStorage *pStorage, const char *pUrl, const char *pDest, int StorageType = -2, CTimeout Timeout = CTimeout{4000, 500, 5}); const char *Dest() const { return m_aDest; } }; @@ -101,7 +109,7 @@ class CPostJson : public CRequest char m_aJson[1024]; public: - CPostJson(const char *pUrl, bool CanTimeout, const char *pJson); + CPostJson(const char *pUrl, CTimeout Timeout, const char *pJson); }; bool HttpInit(IStorage *pStorage); diff --git a/src/engine/client/updater.cpp b/src/engine/client/updater.cpp index 1966f0f27..72a346b97 100644 --- a/src/engine/client/updater.cpp +++ b/src/engine/client/updater.cpp @@ -42,7 +42,7 @@ static const char *GetUpdaterDestPath(char *pBuf, int BufSize, const char *pFile } CUpdaterFetchTask::CUpdaterFetchTask(CUpdater *pUpdater, const char *pFile, const char *pDestPath) : - CGetFile(pUpdater->m_pStorage, GetUpdaterUrl(m_aBuf, sizeof(m_aBuf), pFile), GetUpdaterDestPath(m_aBuf2, sizeof(m_aBuf), pFile, pDestPath), -2, false), + CGetFile(pUpdater->m_pStorage, GetUpdaterUrl(m_aBuf, sizeof(m_aBuf), pFile), GetUpdaterDestPath(m_aBuf2, sizeof(m_aBuf), pFile, pDestPath), -2, CTimeout{0, 0, 0}), m_pUpdater(pUpdater) { } diff --git a/src/game/variables.h b/src/game/variables.h index 5f95488e5..a70a24ba3 100644 --- a/src/game/variables.h +++ b/src/game/variables.h @@ -77,7 +77,11 @@ MACRO_CONFIG_INT(EdShowkeys, ed_showkeys, 0, 0, 1, CFGFLAG_CLIENT|CFGFLAG_SAVE, MACRO_CONFIG_INT(ClShowWelcome, cl_show_welcome, 1, 0, 1, CFGFLAG_CLIENT|CFGFLAG_SAVE, "") MACRO_CONFIG_INT(ClMotdTime, cl_motd_time, 10, 0, 100, CFGFLAG_CLIENT|CFGFLAG_SAVE, "How long to show the server message of the day") -MACRO_CONFIG_STR(ClDDNetMapDownloadUrl, cl_ddnet_map_download_url, 100, "https://maps2.ddnet.tw", CFGFLAG_CLIENT|CFGFLAG_SAVE, "URL to use to download maps (can start with http:// or https://)") +// http map download +MACRO_CONFIG_STR(ClMapDownloadUrl, cl_map_download_url, 100, "https://maps2.ddnet.tw", CFGFLAG_CLIENT|CFGFLAG_SAVE, "URL to use to download maps (can start with http:// or https://)") +MACRO_CONFIG_INT(ClMapDownloadConnectTimeoutMs, cl_map_download_connect_timeout_ms, 2000, 0, 100000, CFGFLAG_CLIENT|CFGFLAG_SAVE, "HTTP map downloads: timeout for the connect phase in milliseconds (0 to disable)") +MACRO_CONFIG_INT(ClMapDownloadLowSpeedLimit, cl_map_download_low_speed_limit, 500, 0, 100000, CFGFLAG_CLIENT|CFGFLAG_SAVE, "HTTP map downloads: Set low speed limit in bytes per second (0 to disable)") +MACRO_CONFIG_INT(ClMapDownloadLowSpeedTime, cl_map_download_low_speed_time, 5, 0, 100000, CFGFLAG_CLIENT|CFGFLAG_SAVE, "HTTP map downloads: Set low speed limit time period (0 to disable)") MACRO_CONFIG_STR(ClLanguagefile, cl_languagefile, 255, "", CFGFLAG_CLIENT|CFGFLAG_SAVE, "What language file to use") MACRO_CONFIG_INT(ClVanillaSkinsOnly, cl_vanilla_skins_only, 0, 0, 1, CFGFLAG_CLIENT|CFGFLAG_SAVE, "Only show skins available in Vanilla Teeworlds") @@ -131,11 +135,6 @@ MACRO_CONFIG_INT(ClDummyJump, cl_dummy_jump, 0, 0, 1, CFGFLAG_CLIENT, "Whether d MACRO_CONFIG_INT(ClDummyFire, cl_dummy_fire, 0, 0, 1, CFGFLAG_CLIENT, "Whether dummy is firing") MACRO_CONFIG_INT(ClDummyHook, cl_dummy_hook, 0, 0, 1, CFGFLAG_CLIENT, "Whether dummy is hooking") -// curl http download -MACRO_CONFIG_INT(ClHTTPConnectTimeoutMs, cl_http_connect_timeout_ms, 2000, 0, 100000, CFGFLAG_CLIENT|CFGFLAG_SAVE, "HTTP downloads: timeout for the connect phase in milliseconds (0 to disable)") -MACRO_CONFIG_INT(ClHTTPLowSpeedLimit, cl_http_low_speed_limit, 500, 0, 100000, CFGFLAG_CLIENT|CFGFLAG_SAVE, "HTTP downloads: Set low speed limit in bytes per second (0 to disable)") -MACRO_CONFIG_INT(ClHTTPLowSpeedTime, cl_http_low_speed_time, 5, 0, 100000, CFGFLAG_CLIENT|CFGFLAG_SAVE, "HTTP downloads: Set low speed limit time period (0 to disable)") - // server MACRO_CONFIG_INT(SvWarmup, sv_warmup, 0, 0, 0, CFGFLAG_SERVER, "Number of seconds to do warmup before round starts") MACRO_CONFIG_STR(SvMotd, sv_motd, 900, "", CFGFLAG_SERVER, "Message of the day to display for the clients")