UFO: Alien Invasion
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
cl_http.cpp File Reference

HTTP downloading is used if the server provides a content server URL in the connect message. Any missing content the client needs will then use the HTTP server. CURL is used to enable multiple files to be downloaded in parallel to improve performance on high latency links when small files such as textures are needed. Since CURL natively supports gzip content encoding, any files on the HTTP server should ideally be gzipped to conserve bandwidth. More...

#include "client.h"
#include "cl_http.h"
#include "battlescape/cl_parse.h"

Go to the source code of this file.

Enumerations

enum  { HTTPDL_ABORT_NONE, HTTPDL_ABORT_SOFT, HTTPDL_ABORT_HARD }
 

Functions

static void StripHighBits (char *string)
 
static bool isvalidchar (int c)
 
static int CL_HTTP_Progress (void *clientp, double dltotal, double dlnow, double ultotal, double ulnow)
 libcurl callback to update progress info. Mainly just used as a way to cancel the transfer if required. More...
 
static void CL_EscapeHTTPPath (const char *filePath, char *escaped)
 Properly escapes a path with HTTP encoding. libcurl's function seems to treat '/' and such as illegal chars and encodes almost the entire URL... More...
 
static void CL_StartHTTPDownload (dlqueue_t *entry, dlhandle_t *dl)
 Actually starts a download by adding it to the curl multi handle. More...
 
void CL_SetHTTPServer (const char *URL)
 A new server is specified, so we nuke all our state. More...
 
void CL_CancelHTTPDownloads (bool permKill)
 Cancel all downloads and nuke the queue. More...
 
static dlhandle_tCL_GetFreeDLHandle (void)
 Find a free download handle to start another queue entry on. More...
 
bool CL_QueueHTTPDownload (const char *ufoPath)
 Called from the precache check to queue a download. More...
 
bool CL_PendingHTTPDownloads (void)
 See if we're still busy with some downloads. Called by precacher just before it loads the map since we could be downloading the map. If we're busy still, it'll wait and CL_FinishHTTPDownload will pick up from where it left. More...
 
bool CL_CheckOrDownloadFile (const char *filename)
 
static void CL_CheckAndQueueDownload (char *path)
 Validate a path supplied by a filelist. More...
 
static void CL_ParseFileList (dlhandle_t *dl)
 A filelist is in memory, scan and validate it and queue up the files. More...
 
static void CL_ReVerifyHTTPQueue (void)
 A pk3 file just downloaded, let's see if we can remove some stuff from the queue which is in the .pk3. More...
 
void CL_HTTP_Cleanup (void)
 UFO is exiting or we're changing servers. Clean up. More...
 
static void CL_FinishHTTPDownload (void)
 A download finished, find out what it was, whether there were any errors and if so, how severe. If none, rename file and other such stuff. More...
 
static void CL_StartNextHTTPDownload (void)
 Start another HTTP download if possible. More...
 
void CL_RunHTTPDownloads (void)
 This calls curl_multi_perform do actually do stuff. Called every frame while connecting to minimise latency. Also starts new downloads if we're not doing the maximum already. More...
 
void HTTP_InitStartup (void)
 

Variables

static cvar_tcl_http_downloads
 
static cvar_tcl_http_filelists
 
static cvar_tcl_http_max_connections
 
static CURLM * multi = nullptr
 
static int handleCount = 0
 
static int pendingCount = 0
 
static int abortDownloads = HTTPDL_ABORT_NONE
 
static bool downloadingPK3 = false
 

Detailed Description

HTTP downloading is used if the server provides a content server URL in the connect message. Any missing content the client needs will then use the HTTP server. CURL is used to enable multiple files to be downloaded in parallel to improve performance on high latency links when small files such as textures are needed. Since CURL natively supports gzip content encoding, any files on the HTTP server should ideally be gzipped to conserve bandwidth.

Author
R1CH
See also
CL_ConnectionlessPacket
SVC_DirectConnect

Definition in file cl_http.cpp.

Enumeration Type Documentation

anonymous enum
Enumerator
HTTPDL_ABORT_NONE 
HTTPDL_ABORT_SOFT 
HTTPDL_ABORT_HARD 

Definition at line 44 of file cl_http.cpp.

Function Documentation

void CL_CancelHTTPDownloads ( bool  permKill)
static void CL_CheckAndQueueDownload ( char *  path)
static

Validate a path supplied by a filelist.

Parameters
[in,out]pathPointer to file (path) to download (high bits will be stripped).
See also
CL_QueueHTTPDownload
CL_ParseFileList

Definition at line 391 of file cl_http.cpp.

References CL_CheckOrDownloadFile(), CL_QueueHTTPDownload(), cls, Com_GetExtension(), Com_Printf(), Com_sprintf(), client_static_s::downloadQueue, f, FILE, FS_CheckFile(), FS_Gamedir(), isvalidchar(), length, MAX_OSPATH, MAX_QPATH, dlqueue_s::next, Q_streq, StripHighBits(), and Sys_Fopen().

Referenced by CL_ParseFileList().

bool CL_CheckOrDownloadFile ( const char *  filename)
Returns
true if the file exists, otherwise it attempts to start a download via curl
See also
CL_CheckAndQueueDownload
CL_RequestNextDownload

Definition at line 341 of file cl_http.cpp.

References CL_QueueHTTPDownload(), Com_Printf(), FS_LoadFile(), MAX_OSPATH, Q_streq, Q_strncpyz(), and Q_strnull().

Referenced by CL_CheckAndQueueDownload(), CL_DownloadMap(), and CL_DownloadUMPMap().

static void CL_EscapeHTTPPath ( const char *  filePath,
char *  escaped 
)
static

Properly escapes a path with HTTP encoding. libcurl's function seems to treat '/' and such as illegal chars and encodes almost the entire URL...

Definition at line 106 of file cl_http.cpp.

References i, and len.

Referenced by CL_StartHTTPDownload().

static void CL_FinishHTTPDownload ( void  )
static

A download finished, find out what it was, whether there were any errors and if so, how severe. If none, rename file and other such stuff.

Todo:
technically i shouldn't need to do this as curl will auto reuse the existing handle when you change the URL. however, the handleCount goes all weird when reusing a download slot in this way. if you can figure out why, please let me know.

Definition at line 566 of file cl_http.cpp.

References abortDownloads, ca_connected, CL_CancelHTTPDownloads(), CL_ParseFileList(), CL_PendingHTTPDownloads(), CL_RequestNextDownload(), CL_ReVerifyHTTPQueue(), cls, Com_Error(), Com_GetExtension(), Com_Printf(), Com_sprintf(), dlhandle_s::curl, DLQ_STATE_DONE, downloadingPK3, client_static_s::downloadName, client_static_s::downloadPosition, client_static_s::downloadServer, ERR_DROP, dlhandle_s::file, dlhandle_s::filePath, FS_Gamedir(), FS_RemoveFile(), FS_RenameFile(), FS_RestartFilesystem(), handleCount, HTTPDL_ABORT_HARD, HTTPDL_ABORT_NONE, HTTPDL_ABORT_SOFT, client_static_s::HTTPHandles, i, MAX_OSPATH, multi, pendingCount, Q_streq, dlhandle_s::queueEntry, dlqueue_s::state, client_static_s::state, and dlqueue_s::ufoPath.

Referenced by CL_RunHTTPDownloads().

static dlhandle_t* CL_GetFreeDLHandle ( void  )
static

Find a free download handle to start another queue entry on.

Definition at line 271 of file cl_http.cpp.

References cls, DLQ_STATE_DONE, client_static_s::HTTPHandles, i, dlhandle_s::queueEntry, and dlqueue_s::state.

Referenced by CL_StartNextHTTPDownload().

void CL_HTTP_Cleanup ( void  )

UFO is exiting or we're changing servers. Clean up.

Definition at line 532 of file cl_http.cpp.

References cls, dlhandle_s::curl, dlhandle_s::file, dlhandle_s::filePath, client_static_s::HTTPHandles, i, Mem_Free, multi, dlhandle_s::queueEntry, Sys_Remove(), and dlhandle_s::tempBuffer.

Referenced by CL_SetHTTPServer(), and CL_Shutdown().

static int CL_HTTP_Progress ( void clientp,
double  dltotal,
double  dlnow,
double  ultotal,
double  ulnow 
)
static

libcurl callback to update progress info. Mainly just used as a way to cancel the transfer if required.

Definition at line 81 of file cl_http.cpp.

References abortDownloads, cls, client_static_s::downloadName, client_static_s::downloadPercent, client_static_s::downloadPosition, int(), dlhandle_s::position, Q_strncpyz(), dlhandle_s::queueEntry, and dlqueue_s::ufoPath.

Referenced by CL_StartHTTPDownload().

static void CL_ParseFileList ( dlhandle_t dl)
static

A filelist is in memory, scan and validate it and queue up the files.

Definition at line 486 of file cl_http.cpp.

References CL_CheckAndQueueDownload(), cvar_s::integer, Mem_Free, and dlhandle_s::tempBuffer.

Referenced by CL_FinishHTTPDownload().

bool CL_PendingHTTPDownloads ( void  )

See if we're still busy with some downloads. Called by precacher just before it loads the map since we could be downloading the map. If we're busy still, it'll wait and CL_FinishHTTPDownload will pick up from where it left.

Definition at line 328 of file cl_http.cpp.

References cls, client_static_s::downloadServer, handleCount, and pendingCount.

Referenced by CL_CanMultiplayerStart(), and CL_FinishHTTPDownload().

static void CL_ReVerifyHTTPQueue ( void  )
static

A pk3 file just downloaded, let's see if we can remove some stuff from the queue which is in the .pk3.

Definition at line 515 of file cl_http.cpp.

References cls, DLQ_STATE_DONE, DLQ_STATE_NOT_STARTED, client_static_s::downloadQueue, FS_LoadFile(), dlqueue_s::next, and pendingCount.

Referenced by CL_FinishHTTPDownload().

void CL_RunHTTPDownloads ( void  )

This calls curl_multi_perform do actually do stuff. Called every frame while connecting to minimise latency. Also starts new downloads if we're not doing the maximum already.

See also
CL_Frame

Definition at line 751 of file cl_http.cpp.

References abortDownloads, CL_CancelHTTPDownloads(), CL_FinishHTTPDownload(), CL_StartNextHTTPDownload(), cls, Com_Printf(), downloadingPK3, client_static_s::downloadServer, handleCount, HTTPDL_ABORT_NONE, multi, and pendingCount.

Referenced by CL_Frame().

void CL_SetHTTPServer ( const char *  URL)
static void CL_StartNextHTTPDownload ( void  )
static

Start another HTTP download if possible.

See also
CL_RunHTTPDownloads

Definition at line 725 of file cl_http.cpp.

References CL_GetFreeDLHandle(), CL_StartHTTPDownload(), cls, DLQ_STATE_NOT_STARTED, downloadingPK3, client_static_s::downloadQueue, len, dlqueue_s::next, and Q_strcasecmp.

Referenced by CL_RunHTTPDownloads().

void HTTP_InitStartup ( void  )

Definition at line 787 of file cl_http.cpp.

References Cvar_Get().

Referenced by CL_InitLocal().

static bool isvalidchar ( int  c)
inlinestatic

Definition at line 70 of file cl_http.cpp.

Referenced by CL_CheckAndQueueDownload().

static void StripHighBits ( char *  string)
static

Definition at line 56 of file cl_http.cpp.

Referenced by CL_CheckAndQueueDownload().

Variable Documentation

cvar_t* cl_http_downloads
static

Definition at line 40 of file cl_http.cpp.

cvar_t* cl_http_filelists
static

Definition at line 41 of file cl_http.cpp.

cvar_t* cl_http_max_connections
static

Definition at line 42 of file cl_http.cpp.

bool downloadingPK3 = false
static
CURLM* multi = nullptr
static