cleanup http download stuff a little

unglobalise a bunch of stuffand put into a struct
also hopefully made the progressbar less funky
This commit is contained in:
Alug committed 2025-04-19 08:10:09 +02:00
1 parent 59b1d66c39
commit 4de86a1bad
4 files changed
+102 -102

No files matched your search

+43 -47
View File
@@ -1194,11 +1194,6 @@ typedef enum
static void GetPackets(void);
static cl_mode_t cl_mode = CL_SEARCHING;
#ifdef HAVE_CURL
char http_source[MAX_MIRROR_LENGTH+1];
#endif
static UINT16 cl_lastcheckedfilecount = 0; // used for full file list
// Player name send/load
@@ -1272,10 +1267,10 @@ static inline void CL_DrawConnectionStatus(void)
{
#ifdef JOININGAME
case CL_DOWNLOADSAVEGAME:
if (lastfilenum != -1)
if (filedownload.current != -1)
{
UINT32 currentsize = fileneeded[lastfilenum].currentsize;
UINT32 totalsize = fileneeded[lastfilenum].totalsize;
UINT32 currentsize = fileneeded[filedownload.current].currentsize;
UINT32 totalsize = fileneeded[filedownload.current].totalsize;
INT32 dldlength;
cltext = M_GetText("Downloading game state...");
@@ -1370,13 +1365,13 @@ static inline void CL_DrawConnectionStatus(void)
V_DrawCenteredString(BASEVIDWIDTH/2, BASEVIDHEIGHT-24, V_20TRANS|V_MONOSPACE|MENUCAPS,
va(" %2u/%2u Files",loadcompletednum,fileneedednum));
}
else if (lastfilenum != -1)
else if (filedownload.current != -1)
{
INT32 dldlength;
INT32 totalfileslength;
UINT32 totaldldsize;
static char tempname[28];
fileneeded_t *file = &fileneeded[lastfilenum];
fileneeded_t *file = &fileneeded[filedownload.current];
char *filename = file->filename;
// Draw the bottom box.
@@ -1415,32 +1410,32 @@ static inline void CL_DrawConnectionStatus(void)
V_DrawCenteredString(BASEVIDWIDTH/2, BASEVIDHEIGHT-58-22, V_YELLOWMAP|MENUCAPS,
va(M_GetText("\"%s\""), tempname));
V_DrawString(BASEVIDWIDTH/2-128, BASEVIDHEIGHT-58, V_20TRANS|V_MONOSPACE|MENUCAPS,
va(" %4uK/%4uK",fileneeded[lastfilenum].currentsize>>10,file->totalsize>>10));
va(" %4uK/%4uK",file->currentsize>>10,file->totalsize>>10));
V_DrawRightAlignedString(BASEVIDWIDTH/2+128, BASEVIDHEIGHT-58, V_20TRANS|V_MONOSPACE|MENUCAPS,
va("%3.1fK/s ", ((double)getbps)/1024));
// Download progress
if (fileneeded[lastfilenum].currentsize != fileneeded[lastfilenum].totalsize)
totaldldsize = downloadcompletedsize+fileneeded[lastfilenum].currentsize; //Add in single file progress download if applicable
if (file->currentsize != file->totalsize)
totaldldsize = filedownload.completedsize+file->currentsize; //Add in single file progress download if applicable
else
totaldldsize = downloadcompletedsize;
totaldldsize = filedownload.completedsize;
V_DrawCenteredString(BASEVIDWIDTH/2, BASEVIDHEIGHT-24-14, V_YELLOWMAP|MENUCAPS, "Overall Download Progress");
totalfileslength = (INT32)((totaldldsize/(double)totalfilesrequestedsize) * 256);
totalfileslength = (INT32)((totaldldsize/(double)filedownload.totalsize) * 256);
M_DrawTextBox(BASEVIDWIDTH/2-128-8, BASEVIDHEIGHT-24-8, 32, 1);
V_DrawFill(BASEVIDWIDTH/2-128, BASEVIDHEIGHT-24, 256, 8, 175);
V_DrawFill(BASEVIDWIDTH/2-128, BASEVIDHEIGHT-24, totalfileslength, 8, 160);
if (totalfilesrequestedsize>>20 >= 10) //display in MB if over 10MB
if (filedownload.totalsize>>20 >= 10) //display in MB if over 10MB
V_DrawString(BASEVIDWIDTH/2-128, BASEVIDHEIGHT-24, V_20TRANS|V_MONOSPACE|MENUCAPS,
va(" %4uM/%4uM",totaldldsize>>20,totalfilesrequestedsize>>20));
va(" %4uM/%4uM",totaldldsize>>20,filedownload.totalsize>>20));
else
V_DrawString(BASEVIDWIDTH/2-128, BASEVIDHEIGHT-24, V_20TRANS|V_MONOSPACE|MENUCAPS,
va(" %4uK/%4uK",totaldldsize>>10,totalfilesrequestedsize>>10));
va(" %4uK/%4uK",totaldldsize>>10,filedownload.totalsize>>10));
V_DrawRightAlignedString(BASEVIDWIDTH/2+128, BASEVIDHEIGHT-24, V_20TRANS|V_MONOSPACE|MENUCAPS,
va("%2u/%2u Files ",downloadcompletednum,totalfilesrequestednum));
va("%2u/%2u Files ", filedownload.completednum, filedownload.totalnum));
}
else
{
@@ -2260,10 +2255,10 @@ void CL_TimeoutServerList(void)
static void CL_ConfirmConnect(void)
{
if (totalfilesrequestednum > 0)
if (filedownload.totalnum > 0)
{
#ifdef HAVE_CURL
if (http_source[0] == '\0' || curl_failedwebdownload)
if (filedownload.http_source[0] == '\0' || filedownload.http_failed)
#endif
{
if (CL_SendRequestFile())
@@ -2376,7 +2371,7 @@ static boolean CL_FinishedFileList(void)
// must download something
// can we, though?
#ifdef HAVE_CURL
if (http_source[0] == '\0' || curl_failedwebdownload)
if (filedownload.http_source[0] == '\0' || filedownload.http_failed)
#endif
{
if (!CL_CheckDownloadable()) // nope!
@@ -2396,30 +2391,31 @@ static boolean CL_FinishedFileList(void)
}
#ifdef HAVE_CURL
if (!curl_failedwebdownload)
if (!filedownload.http_failed)
#endif
{
#ifndef NONET
downloadcompletednum = 0;
downloadcompletedsize = 0;
totalfilesrequestednum = 0;
totalfilesrequestedsize = 0;
filedownload.completednum = 0;
filedownload.completedsize = 0;
filedownload.totalnum = 0;
filedownload.totalsize = 0;
#endif
for (i = 0; i < fileneedednum; i++)
if (fileneeded[i].status == FS_NOTFOUND || fileneeded[i].status == FS_MD5SUMBAD)
{
#ifndef NONET
totalfilesrequestednum++;
totalfilesrequestedsize += fileneeded[i].totalsize;
filedownload.totalnum++;
filedownload.totalsize += fileneeded[i].totalsize;
#endif
}
#ifndef NONET
if (totalfilesrequestedsize>>20 >= 10)
downloadsize = Z_StrDup(va("%uM",totalfilesrequestedsize>>20));
if (filedownload.totalsize>>20 >= 10)
downloadsize = Z_StrDup(va("%uM",filedownload.totalsize>>20));
else
downloadsize = Z_StrDup(va("%uK",totalfilesrequestedsize>>10));
downloadsize = Z_StrDup(va("%uK",filedownload.totalsize>>10));
#endif
if (cv_showdownloadprompt.value)
@@ -2504,9 +2500,9 @@ static boolean CL_ServerConnectionSearchTicker(tic_t *asksent)
{
#ifdef HAVE_CURL
if (serverlist[i].info.httpsource[0])
strncpy(http_source, serverlist[i].info.httpsource, MAX_MIRROR_LENGTH);
strncpy(filedownload.http_source, serverlist[i].info.httpsource, MAX_MIRROR_LENGTH);
else
http_source[0] = '\0';
filedownload.http_source[0] = '\0';
#else
if (serverlist[i].info.httpsource[0])
CONS_Printf("We received a http url from the server, however it will not be used as this build lacks curl support (%s)\n", serverlist[i].info.httpsource);
@@ -2589,12 +2585,12 @@ static boolean CL_ServerConnectionTicker(const char *tmpsave, tic_t *oldtic, tic
break;
#ifdef HAVE_CURL
case CL_PREPAREHTTPFILES:
if (http_source[0])
if (filedownload.http_source[0])
{
for (i = 0; i < fileneedednum; i++)
if (fileneeded[i].status == FS_NOTFOUND || fileneeded[i].status == FS_MD5SUMBAD)
{
curl_transfers++;
filedownload.remaining++;
}
cl_mode = CL_DOWNLOADHTTPFILES;
@@ -2606,8 +2602,8 @@ static boolean CL_ServerConnectionTicker(const char *tmpsave, tic_t *oldtic, tic
for (i = 0; i < fileneedednum; i++)
if (fileneeded[i].status == FS_NOTFOUND || fileneeded[i].status == FS_MD5SUMBAD)
{
if (!curl_running)
CURLPrepareFile(http_source, i);
if (!filedownload.http_running)
CURLPrepareFile(filedownload.http_source, i);
waitmore = true;
break;
}
@@ -2615,14 +2611,14 @@ static boolean CL_ServerConnectionTicker(const char *tmpsave, tic_t *oldtic, tic
if (waitmore)
break; // exit the case
if (curl_failedwebdownload && !curl_transfers)
if (filedownload.http_failed && !filedownload.remaining)
{
CONS_Printf("One or more files failed to download, falling back to internal downloader\n");
cl_mode = CL_CHECKFILES;
break;
}
if (!curl_transfers)
if (!filedownload.remaining)
cl_mode = CL_LOADFILES;
break;
@@ -2799,7 +2795,7 @@ static void CL_ConnectToServer(void)
#endif
#ifdef CLIENT_LOADINGSCREEN
lastfilenum = -1;
filedownload.current = -1;
#endif
cl_mode = CL_SEARCHING;
@@ -3440,18 +3436,18 @@ void CL_Reset(void)
memset(packetstat, 0, sizeof(packetstat));
#ifndef NONET
totalfilesrequestednum = 0;
totalfilesrequestedsize = 0;
filedownload.totalnum = 0;
filedownload.totalsize = 0;
#endif
firstconnectattempttime = 0;
serverisfull = false;
connectiontimeout = (tic_t)cv_nettimeout.value; //reset this temporary hack
#ifdef HAVE_CURL
curl_failedwebdownload = false;
curl_transfers = 0;
curl_running = false;
http_source[0] = '\0';
filedownload.remaining = 0;
filedownload.http_failed = false;
filedownload.http_running = false;
filedownload.http_source[0] = '\0';
#endif
G_ResetAllDeviceRumbles();
+1
View File
@@ -393,6 +393,7 @@ typedef struct
#define MAXSERVERNAME 32
#define MAXFILENEEDED 915
#define MAX_MIRROR_LENGTH 256
// This packet is too large
typedef struct
{
+38 -47
View File
@@ -64,11 +64,11 @@ static boolean SV_SendFile(INT32 node, const char *filename, UINT8 fileid);
#ifdef HAVE_CURL
size_t curlwrite_data(void *ptr, size_t size, size_t nmemb, FILE *stream);
#if defined(CURL_AT_LEAST_VERSION) && CURL_AT_LEAST_VERSION(7, 35, 0)
#if (LIBCURL_VERSION_MAJOR <= 7) && (LIBCURL_VERSION_MINOR < 35)
static int curlprogress_callback(void *clientp, double dltotal, double dlnow, double ultotal, double ulnow);
#else
static int curlprogress_callbackx(void *clientp, curl_off_t dltotal, curl_off_t dlnow, curl_off_t ultotal, curl_off_t ulnow);
#define XFERINFOFUNCTION
#else
static int curlprogress_callback(void *clientp, double dltotal, double dlnow, double ultotal, double ulnow);
#endif
#endif
@@ -114,24 +114,14 @@ fileneeded_t fileneeded[MAX_WADFILES]; // List of needed files
static I_mutex downloadmutex;
char downloaddir[512] = "DOWNLOAD";
#ifdef CLIENT_LOADINGSCREEN
// for cl loading screen
INT32 lastfilenum = -1;
INT32 downloadcompletednum = 0;
UINT32 downloadcompletedsize = 0;
INT32 totalfilesrequestednum = 0;
UINT32 totalfilesrequestedsize = 0;
#endif
file_download_t filedownload;
#ifdef HAVE_CURL
static CURL *http_handle;
static CURLM *multi_handle;
boolean curl_running = false;
boolean curl_failedwebdownload = false;
static double curl_dlnow;
static double curl_dltotal;
static UINT32 curl_dlnow;
static UINT32 curl_dltotal;
static time_t curl_starttime;
INT32 curl_transfers = 0;
static int curl_runninghandles = 0;
static UINT32 curl_origfilesize;
static UINT32 curl_origtotalfilesize;
@@ -236,7 +226,7 @@ void D_ParseFileneeded(INT32 fileneedednum_parm, UINT8 *fileneededstr, UINT16 fi
void CL_PrepareDownloadSaveGame(const char *tmpsave)
{
#ifdef CLIENT_LOADINGSCREEN
lastfilenum = -1;
filedownload.current = -1;
#endif
fileneedednum = 1;
fileneeded[0].status = FS_REQUESTED;
@@ -1031,8 +1021,8 @@ void Got_Filetxpak(void)
CONS_Printf(M_GetText("Downloading %s...(done)\n"),
filename);
#ifndef NONET
downloadcompletednum++;
downloadcompletedsize += file->totalsize;
filedownload.completednum++;
filedownload.completedsize += file->totalsize;
#endif
}
}
@@ -1068,7 +1058,7 @@ void Got_Filetxpak(void)
}
#ifdef CLIENT_LOADINGSCREEN
lastfilenum = filenum;
filedownload.current = filenum;
#endif
}
@@ -1217,9 +1207,7 @@ filestatus_t findfile(char *filename, const UINT8 *wantedmd5sum, boolean complet
#ifdef HAVE_CURL
size_t curlwrite_data(void *ptr, size_t size, size_t nmemb, FILE *stream)
{
size_t written;
written = fwrite(ptr, size, nmemb, stream);
return written;
return fwrite(ptr, size, nmemb, stream);
}
#ifdef XFERINFOFUNCTION
@@ -1233,12 +1221,12 @@ static int curlprogress_callbackx(void *clientp, curl_off_t dltotal, curl_off_t
curtime = time(NULL);
curl_dlnow = dlnow;
curl_dltotal = dltotal;
curl_dlnow = (UINT32)dlnow;
curl_dltotal = (UINT32)dltotal;
if (curtime > curl_starttime)
{
getbytes = curl_dlnow / (curtime - curl_starttime); // To-do: Make this more accurate???
getbytes = ((double)dlnow) / (curtime - curl_starttime); // To-do: Make this more accurate???
}
else
{
@@ -1258,12 +1246,12 @@ static int curlprogress_callback(void *clientp, double dltotal, double dlnow, do
curtime = time(NULL);
curl_dlnow = dlnow;
curl_dltotal = dltotal;
curl_dlnow = (UINT32)dlnow;
curl_dltotal = (UINT32)dltotal;
if (curtime > curl_starttime)
{
getbytes = curl_dlnow / (curtime - curl_starttime); // To-do: Make this more accurate???
getbytes = ((double)dlnow) / (curtime - curl_starttime); // To-do: Make this more accurate???
}
else
{
@@ -1305,10 +1293,10 @@ void CURLPrepareFile(const char* url, int dfilenum)
curl_easy_setopt(http_handle, CURLOPT_URL, va("%s/%s", url, curl_realname));
// Only allow HTTP and HTTPS
#if defined(CURL_AT_LEAST_VERSION) && CURL_AT_LEAST_VERSION(7, 85, 0)
curl_easy_setopt(http_handle, CURLOPT_PROTOCOLS_STR, "http,https");
#else
#if (LIBCURL_VERSION_MAJOR <= 7) && (LIBCURL_VERSION_MINOR < 85)
curl_easy_setopt(http_handle, CURLOPT_PROTOCOLS, CURLPROTO_HTTP|CURLPROTO_HTTPS);
#else
curl_easy_setopt(http_handle, CURLOPT_PROTOCOLS_STR, "http,https");
#endif
curl_easy_setopt(http_handle, CURLOPT_USERAGENT, va("SRB2Kart/v%d.%d", VERSION, SUBVERSION)); // Set user agent as some servers won't accept invalid user agents.
@@ -1340,23 +1328,25 @@ void CURLPrepareFile(const char* url, int dfilenum)
#endif
curl_curfile->status = FS_DOWNLOADING;
lastfilenum = dfilenum;
curl_multi_add_handle(multi_handle, http_handle);
curl_multi_perform(multi_handle, &curl_runninghandles);
curl_starttime = time(NULL);
curl_running = true;
filedownload.current = dfilenum;
filedownload.http_running = true;
#ifdef HAVE_THREADS
I_spawn_thread("http-download", (I_thread_fn)CURLGetFile, NULL);
#endif
}
else
filedownload.http_running = false;
}
void CURLAbortFile(void)
{
curl_running = false;
filedownload.http_running = false;
// lock and unlock to wait for the download thread to exit
I_lock_mutex(&downloadmutex);
@@ -1373,10 +1363,8 @@ void CURLGetFile(void)
int msgs_left; /* how many messages are left */
const char *easy_handle_error;
boolean running = true;
long response_code = 0;
static char *filename;
while (running && curl_running)
while (running && filedownload.http_running)
{
if (curl_runninghandles)
{
@@ -1403,11 +1391,13 @@ void CURLGetFile(void)
e = m->easy_handle;
easyres = m->data.result;
filename = Z_StrDup(curl_realname);
char *filename = Z_StrDup(curl_realname);
nameonly(filename);
if (easyres != CURLE_OK)
{
long response_code = 0;
if (easyres == CURLE_HTTP_RETURNED_ERROR)
curl_easy_getinfo(e, CURLINFO_RESPONSE_CODE, &response_code);
@@ -1415,7 +1405,7 @@ void CURLGetFile(void)
curl_curfile->status = FS_FALLBACK;
curl_curfile->currentsize = curl_origfilesize;
curl_curfile->totalsize = curl_origtotalfilesize;
curl_failedwebdownload = true;
filedownload.http_failed = true;
fclose(curl_curfile->file);
remove(curl_curfile->filename);
CONS_Printf(M_GetText("Failed to download %s (%s)\n"), filename, easy_handle_error);
@@ -1428,36 +1418,37 @@ void CURLGetFile(void)
{
CONS_Alert(CONS_ERROR, M_GetText("HTTP Download of %s finished but is corrupt or has been modified\n"), filename);
curl_curfile->status = FS_FALLBACK;
curl_failedwebdownload = true;
filedownload.http_failed = true;
}
else
{
CONS_Printf(M_GetText("Finished HTTP download of %s\n"), filename);
downloadcompletednum++;
downloadcompletedsize += curl_curfile->totalsize;
filedownload.completednum++;
filedownload.completedsize += curl_curfile->totalsize;
curl_curfile->status = FS_FOUND;
}
}
Z_Free(filename);
curl_curfile->file = NULL;
curl_transfers--;
filedownload.remaining--;
curl_multi_remove_handle(multi_handle, e);
curl_easy_cleanup(e);
if (!curl_transfers)
if (!filedownload.remaining)
break;
}
}
}
if (!curl_transfers || !curl_running)
if (!filedownload.remaining || !filedownload.http_running)
{
curl_multi_cleanup(multi_handle);
curl_global_cleanup();
multi_handle = NULL;
}
curl_running = false;
filedownload.http_running = false;
I_unlock_mutex(downloadmutex);
}
+20 -8
View File
@@ -13,6 +13,7 @@
#ifndef __D_NETFIL__
#define __D_NETFIL__
#include "d_clisrv.h"
#include "w_wad.h"
typedef enum
@@ -52,18 +53,29 @@ extern INT32 fileneedednum;
extern fileneeded_t fileneeded[MAX_WADFILES];
extern char downloaddir[512];
typedef struct
{
#ifdef CLIENT_LOADINGSCREEN
extern INT32 lastfilenum;
extern INT32 downloadcompletednum;
extern UINT32 downloadcompletedsize;
extern INT32 totalfilesrequestednum;
extern UINT32 totalfilesrequestedsize;
INT32 current;
INT32 remaining;
INT32 completednum;
UINT32 completedsize;
INT32 totalnum;
UINT32 totalsize;
#endif
#ifdef HAVE_CURL
extern boolean curl_failedwebdownload;
extern boolean curl_running;
extern INT32 curl_transfers;
boolean http_failed;
boolean http_running;
char http_source[MAX_MIRROR_LENGTH+1];
#endif
} file_download_t;
extern file_download_t filedownload;
#ifdef HAVE_CURL
typedef struct HTTP_login HTTP_login;