Skip to content

Commit a7b3684

Browse files
committed
Pull out non curl specific code from the curl backend
1 parent 6b101cb commit a7b3684

3 files changed

Lines changed: 78 additions & 64 deletions

File tree

code/client/cl_http.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
2828
qboolean CL_HTTP_Init( void );
2929
qboolean CL_HTTP_Available( void );
3030
void CL_HTTP_Shutdown( void );
31-
void CL_HTTP_BeginDownload( const char *localName, const char *remoteURL );
32-
void CL_HTTP_PerformDownload( void );
31+
void CL_HTTP_BeginDownload( const char *remoteURL );
32+
qboolean CL_HTTP_PerformDownload( void );
3333

3434
#endif // __CL_HTTP_H__

code/client/cl_http_curl.c

Lines changed: 7 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -264,47 +264,22 @@ CURLcode qcurl_easy_setopt_warn(CURL *curl, CURLoption option, ...)
264264
return result;
265265
}
266266

267-
void CL_HTTP_BeginDownload( const char *localName, const char *remoteURL )
267+
void CL_HTTP_BeginDownload( const char *remoteURL )
268268
{
269269
CURLMcode result;
270270

271-
clc.httpUsed = qtrue;
272-
Com_Printf("URL: %s\n", remoteURL);
273-
Com_DPrintf("***** CL_HTTP_BeginDownload *****\n"
274-
"Localname: %s\n"
275-
"RemoteURL: %s\n"
276-
"****************************\n", localName, remoteURL);
277271
CL_cURL_Cleanup();
278-
Q_strncpyz(clc.downloadURL, remoteURL, sizeof(clc.downloadURL));
279-
Q_strncpyz(clc.downloadName, localName, sizeof(clc.downloadName));
280-
Com_sprintf(clc.downloadTempName, sizeof(clc.downloadTempName),
281-
"%s.tmp", localName);
282-
283-
// Set so UI gets access to it
284-
Cvar_Set("cl_downloadName", localName);
285-
Cvar_Set("cl_downloadSize", "0");
286-
Cvar_Set("cl_downloadCount", "0");
287-
Cvar_SetValue("cl_downloadTime", cls.realtime);
288-
289-
clc.downloadBlock = 0; // Starting new file
290-
clc.downloadCount = 0;
291272

292273
downloadCURL = qcurl_easy_init();
293274
if(!downloadCURL) {
294275
Com_Error(ERR_DROP, "CL_HTTP_BeginDownload: qcurl_easy_init() "
295276
"failed");
296277
return;
297278
}
298-
clc.download = FS_SV_FOpenFileWrite(clc.downloadTempName);
299-
if(!clc.download) {
300-
Com_Error(ERR_DROP, "CL_HTTP_BeginDownload: failed to open "
301-
"%s for writing", clc.downloadTempName);
302-
return;
303-
}
304279

305280
if(com_developer->integer)
306281
qcurl_easy_setopt_warn(downloadCURL, CURLOPT_VERBOSE, 1);
307-
qcurl_easy_setopt_warn(downloadCURL, CURLOPT_URL, clc.downloadURL);
282+
qcurl_easy_setopt_warn(downloadCURL, CURLOPT_URL, remoteURL);
308283
qcurl_easy_setopt_warn(downloadCURL, CURLOPT_TRANSFERTEXT, 0);
309284
qcurl_easy_setopt_warn(downloadCURL, CURLOPT_REFERER, va("ioQ3://%s",
310285
NET_AdrToString(clc.serverAddress)));
@@ -337,19 +312,9 @@ void CL_HTTP_BeginDownload( const char *localName, const char *remoteURL )
337312
Com_Error(ERR_DROP,"CL_HTTP_BeginDownload: qcurl_multi_add_handle() failed: %s", qcurl_multi_strerror(result));
338313
return;
339314
}
340-
341-
if(!(clc.sv_allowDownload & DLF_NO_DISCONNECT) &&
342-
!clc.disconnectedForHttpDownload) {
343-
344-
CL_AddReliableCommand("disconnect", qtrue);
345-
CL_WritePacket();
346-
CL_WritePacket();
347-
CL_WritePacket();
348-
clc.disconnectedForHttpDownload = qtrue;
349-
}
350315
}
351316

352-
void CL_HTTP_PerformDownload(void)
317+
qboolean CL_HTTP_PerformDownload(void)
353318
{
354319
CURLMcode res;
355320
CURLMsg *msg;
@@ -362,17 +327,12 @@ void CL_HTTP_PerformDownload(void)
362327
i++;
363328
}
364329
if(res == CURLM_CALL_MULTI_PERFORM)
365-
return;
330+
return qfalse;
366331
msg = qcurl_multi_info_read(downloadCURLM, &c);
367332
if(msg == NULL) {
368-
return;
369-
}
370-
FS_FCloseFile(clc.download);
371-
if(msg->msg == CURLMSG_DONE && msg->data.result == CURLE_OK) {
372-
FS_SV_Rename(clc.downloadTempName, clc.downloadName, qfalse);
373-
clc.downloadRestart = qtrue;
333+
return qfalse;
374334
}
375-
else {
335+
if(msg->msg != CURLMSG_DONE || msg->data.result != CURLE_OK) {
376336
long code;
377337

378338
qcurl_easy_getinfo(msg->easy_handle, CURLINFO_RESPONSE_CODE,
@@ -382,7 +342,7 @@ void CL_HTTP_PerformDownload(void)
382342
code, clc.downloadURL);
383343
}
384344

385-
CL_NextDownload();
345+
return qtrue;
386346
}
387347

388348
#endif /* USE_HTTP */

code/client/cl_main.c

Lines changed: 69 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -2154,34 +2154,73 @@ void CL_DownloadsComplete( void ) {
21542154

21552155
/*
21562156
=================
2157-
CL_BeginDownload
2158-
2159-
Requests a file to download from the server. Stores it in the current
2160-
game directory.
2157+
CL_InitDownload
21612158
=================
21622159
*/
2163-
void CL_BeginDownload( const char *localName, const char *remoteName ) {
2164-
2165-
Com_DPrintf("***** CL_BeginDownload *****\n"
2166-
"Localname: %s\n"
2167-
"Remotename: %s\n"
2168-
"****************************\n", localName, remoteName);
2169-
2160+
static void CL_InitDownload( const char *localName ) {
21702161
Q_strncpyz ( clc.downloadName, localName, sizeof(clc.downloadName) );
21712162
Com_sprintf( clc.downloadTempName, sizeof(clc.downloadTempName), "%s.tmp", localName );
21722163

21732164
// Set so UI gets access to it
2174-
Cvar_Set( "cl_downloadName", remoteName );
2165+
Cvar_Set( "cl_downloadName", localName );
21752166
Cvar_Set( "cl_downloadSize", "0" );
21762167
Cvar_Set( "cl_downloadCount", "0" );
21772168
Cvar_SetValue( "cl_downloadTime", cls.realtime );
21782169

21792170
clc.downloadBlock = 0; // Starting new file
21802171
clc.downloadCount = 0;
2172+
}
2173+
2174+
/*
2175+
=================
2176+
CL_BeginDownload
21812177
2178+
Requests a file to download from the server. Stores it in the current
2179+
game directory.
2180+
=================
2181+
*/
2182+
static void CL_BeginDownload( const char *remoteName ) {
21822183
CL_AddReliableCommand(va("download %s", remoteName), qfalse);
21832184
}
21842185

2186+
#ifdef USE_HTTP
2187+
/*
2188+
=================
2189+
CL_BeginHttpDownload
2190+
=================
2191+
*/
2192+
static void CL_BeginHttpDownload( const char *remoteURL ) {
2193+
if(Q_strncmp(remoteURL, "http://", strlen("http://")) != 0 &&
2194+
Q_strncmp(remoteURL, "https://", strlen("https://")) != 0) {
2195+
Com_Error(ERR_DROP, "Download Error: %s is a malformed/"
2196+
"unsupported URL", remoteURL);
2197+
}
2198+
2199+
Com_Printf("URL: %s\n", remoteURL);
2200+
2201+
CL_HTTP_BeginDownload(remoteURL);
2202+
Q_strncpyz(clc.downloadURL, remoteURL, sizeof(clc.downloadURL));
2203+
2204+
clc.download = FS_SV_FOpenFileWrite(clc.downloadTempName);
2205+
if(!clc.download) {
2206+
Com_Error(ERR_DROP, "CL_BeginHTTPDownload: failed to open "
2207+
"%s for writing", clc.downloadTempName);
2208+
}
2209+
2210+
if(!(clc.sv_allowDownload & DLF_NO_DISCONNECT) &&
2211+
!clc.disconnectedForHttpDownload) {
2212+
2213+
CL_AddReliableCommand("disconnect", qtrue);
2214+
CL_WritePacket();
2215+
CL_WritePacket();
2216+
CL_WritePacket();
2217+
clc.disconnectedForHttpDownload = qtrue;
2218+
}
2219+
2220+
clc.httpUsed = qtrue;
2221+
}
2222+
#endif /* USE_HTTP */
2223+
21852224
/*
21862225
=================
21872226
CL_NextDownload
@@ -2244,8 +2283,10 @@ void CL_NextDownload(void)
22442283
"have sv_dlURL set\n");
22452284
}
22462285
else if(CL_HTTP_Available()) {
2247-
CL_HTTP_BeginDownload(localName, va("%s/%s",
2286+
CL_InitDownload(localName);
2287+
CL_BeginHttpDownload(va("%s/%s",
22482288
clc.sv_dlURL, remoteName));
2289+
22492290
usedHTTP = qtrue;
22502291
}
22512292
}
@@ -2265,7 +2306,8 @@ void CL_NextDownload(void)
22652306
return;
22662307
}
22672308
else {
2268-
CL_BeginDownload( localName, remoteName );
2309+
CL_InitDownload( localName );
2310+
CL_BeginDownload( remoteName );
22692311
}
22702312
}
22712313
clc.downloadRestart = qtrue;
@@ -2932,7 +2974,19 @@ void CL_Frame ( int msec ) {
29322974

29332975
#ifdef USE_HTTP
29342976
if(clc.httpUsed) {
2935-
CL_HTTP_PerformDownload();
2977+
qboolean finished = CL_HTTP_PerformDownload();
2978+
2979+
if(finished) {
2980+
if(clc.download) {
2981+
FS_FCloseFile(clc.download);
2982+
clc.download = 0;
2983+
}
2984+
2985+
FS_SV_Rename(clc.downloadTempName, clc.downloadName, qfalse);
2986+
clc.downloadRestart = qtrue;
2987+
CL_NextDownload();
2988+
}
2989+
29362990
// we can't process frames normally when in disconnected
29372991
// download mode since the ui vm expects clc.state to be
29382992
// CA_CONNECTED

0 commit comments

Comments
 (0)