My Project
|
CUrlProcessor implementation of IDataProcessor. More...
#include <UrlLoaders.h>
Public Types | |
enum | URLREQUEST_TYPE { URL_REQUEST_HTTP_AUTO, URL_REQUEST_HTTP_GET, URL_REQUEST_HTTP_POST, URL_REQUEST_HTTP_GETFILE, URL_REQUEST_HTTP_HEADERS_ONLY } |
type of the url request task | |
enum | URLREQUEST_STATUS { URL_REQUEST_UNSTARTED, URL_REQUEST_INCOMPLETE, URL_REQUEST_COMPLETED } |
url request status | |
Public Member Functions | |
CUrlProcessor (const string &url, const string &npl_callback) | |
void | CleanUp () |
virtual HRESULT | LockDeviceObject () |
LockDeviceObject is called from the Graphics thread to lock the device object (D3D9), unless IsDeviceObject() is false. More... | |
virtual HRESULT | UnLockDeviceObject () |
UnLockDeviceObject is called from the Graphics thread to unlock the device object, or call updatesubresource for D3D10, unless IsDeviceObject() is false. More... | |
virtual HRESULT | Destroy () |
Destroy is called by the graphics thread when it has consumed the data, unless IsDeviceObject() is false. More... | |
virtual HRESULT | Process (void *pData, int cBytes) |
Process is called by one of the processing threads to process the data before it is consumed. More... | |
virtual HRESULT | CopyToResource () |
CopyToResource copies the data from memory to the locked device object (D3D9). More... | |
virtual void | SetResourceError () |
SetResourceError is called to set the resource pointer to an error code in the event that something went wrong. More... | |
virtual void | SetProcessorWorkerData (IProcessorWorkerData *pThreadLocalData) |
set thread local data. More... | |
virtual IProcessorWorkerData * | GetProcessorWorkerData () |
get thread local data. More... | |
virtual bool | IsDeviceObject () |
default to true. More... | |
void | SetUrl (const char *url) |
set the url to download | |
void | SetHeadersOnly () |
get only headers, without body | |
void | SetScriptCallback (const char *sCallback) |
set the npl callback script. More... | |
void | SetSaveToFile (const char *filename) |
to which file we shall save the url request to. More... | |
void | AppendHTTPHeader (const char *text) |
append http headers | |
CURLFORMcode | AppendFormParam (const char *name, const char *value) |
append form parameters | |
CURLFORMcode | AppendFormParam (const char *name, const char *type, const char *file, const char *data, int datalen, bool bCacheData=false) |
append a file, such as {name = {file="/tmp/test.txt", type="text/plain"}} {name = {file="dummy.html", data="<html><bold>bold</bold></html>, type="text/html"}} input can be NULL if not available. More... | |
void | SetCurlEasyOpt (CURL *handle) |
init the easy handle according to the settings. More... | |
void | CompleteTask () |
call the call back if any, this function must be called in the main game thread. More... | |
size_t | write_data_callback (void *buffer, size_t size, size_t nmemb) |
size_t | write_header_callback (void *buffer, size_t size, size_t nmemb) |
int | progress_callback (double dltotal, double dlnow, double ultotal, double ulnow) |
void | SetTimeOut (int nMilliSeconds) |
set the time out of the request. More... | |
int | GetTimeOut () |
Get the time out of the request. More... | |
void | SetForbidReuse (bool bForbidReuse) |
whether to close the connection when the request is completed. More... | |
void | SetCallBack (URL_LOADER_CALLBACK pFuncCallback=NULL, CUrlProcessorUserData *pUserData=NULL, bool bDeleteUserData=false) |
set the options for asset request. More... | |
bool | IsTimedOut (DWORD nCurrentTime) |
whether this request is timed out relative to a given time More... | |
DWORD | UpdateTime () |
this is to set the timer to current time. More... | |
void | SafeDeleteUserData () |
safe delete user data | |
CUrlProcessorUserData * | GetUserData () |
get user data. More... | |
vector< char > & | GetData () |
get request data. More... | |
vector< char > & | GetHeader () |
get request header. More... | |
int | GetTotalBytes () |
get the total number of bytes. More... | |
int | GetBytesReceived () |
get bytes received. More... | |
void | AddBytesReceived (int nBytesReceived) |
add bytes received. More... | |
bool | IsEnableProgressUpdate () const |
void | SetEnableProgressUpdate (bool val) |
const char * | CopyRequestData (const char *pData, int nLength) |
NPL::NPLObjectProxy & | GetOptions () |
get options as NPL table object. More... | |
bool | IsSyncCallbackMode () const |
whether we will invoke callback immediately using NPL.call instead of NPL.activate. More... | |
void | SetSyncCallbackMode (bool val) |
Static Public Member Functions | |
static size_t | CUrl_write_data_callback (void *buffer, size_t size, size_t nmemb, void *stream) |
curl call back. More... | |
static size_t | CUrl_write_header_callback (void *buffer, size_t size, size_t nmemb, void *stream) |
static int | CUrl_progress_callback (void *clientp, double dltotal, double dlnow, double ultotal, double ulnow) |
static size_t | CUrl_read_email_payload (void *ptr, size_t size, size_t nmemb, void *userp) |
Public Attributes | |
string | m_url |
CURLOPT_URL. | |
struct curl_slist * | m_pHttpHeaders |
http headers to send | |
struct curl_httppost * | m_pFormPost |
CURLOPT_HTTPPOST. | |
struct curl_httppost * | m_pFormLast |
IProcessorWorkerData * | m_pThreadLocalData |
int | m_nPriority |
task priority | |
std::string | m_sNPLCallback |
the NPL function to call when task is finished. More... | |
std::string | m_sNPLStateName |
the NPL runtime state name in which the NPL call back will be invoked. More... | |
URL_LOADER_CALLBACK | m_pfuncCallBack |
the C++ function to call when task is finished. More... | |
string | m_sSaveToFileName |
the file name to save the response data to | |
CParaFile * | m_pFile |
the file to which to save the content to. More... | |
int | m_nUserDataType |
default to 0. More... | |
CUrlProcessorUserData * | m_pUserData |
optional user data to be send along with the m_pfuncCallBack | |
URLREQUEST_TYPE | m_type |
The type of the url request task. | |
URLREQUEST_STATUS | m_nStatus |
the current status of the request. More... | |
DWORD | m_nStartTime |
The time that this request is started or received any response. More... | |
DWORD | m_nLastProgressTime |
we send progress data at fixed 1 second interval, even when there are many or zero data. More... | |
DWORD | m_nTimeOutTime |
for how long the request is considered timed out. More... | |
bool | m_bForbidReuse |
whether to reuse connection. More... | |
bool | m_bEnableProgressUpdate |
whether to send progress update via callback | |
bool | m_bIsSyncCallbackMode |
we will invoke callback immediately using NPL.call instead of NPL.activate. More... | |
int | m_nBytesReceived |
int | m_nTotalBytes |
vector< char > | m_data |
vector< char > | m_header |
std::string | m_sResponseData |
std::string | m_sResponseHeader |
std::string | m_sRequestData |
CURLcode | m_returnCode |
long | m_responseCode |
upload_context * | m_pUploadContext |
std::unique_ptr< NPL::NPLObjectProxy > | m_options |
all lib curl options | |
Static Public Attributes | |
static const DWORD | DEFAULT_TIME_OUT = 15000 |
CUrlProcessor implementation of IDataProcessor.
void ParaEngine::CUrlProcessor::AddBytesReceived | ( | int | nBytesReceived | ) |
add bytes received.
only for statistics
CURLFORMcode ParaEngine::CUrlProcessor::AppendFormParam | ( | const char * | name, |
const char * | type, | ||
const char * | file, | ||
const char * | data, | ||
int | datalen, | ||
bool | bCacheData = false |
||
) |
append a file, such as {name = {file="/tmp/test.txt", type="text/plain"}} {name = {file="dummy.html", data="<html><bold>bold</bold></html>, type="text/html"}} input can be NULL if not available.
bCacheData | if false, data must be valid until the request is complete (in other thread), otherwise it may be rubbish. if true, we will copy the data and the caller can release the data buffer as soon as the function returns |
void ParaEngine::CUrlProcessor::CompleteTask | ( | ) |
call the call back if any, this function must be called in the main game thread.
|
virtual |
CopyToResource copies the data from memory to the locked device object (D3D9).
Also by the IO thread.
Implements ParaEngine::IDataProcessor.
|
static |
curl call back.
|
virtual |
Destroy is called by the graphics thread when it has consumed the data, unless IsDeviceObject() is false.
Implements ParaEngine::IDataProcessor.
int ParaEngine::CUrlProcessor::GetBytesReceived | ( | ) |
get bytes received.
only for statistics
|
inline |
get request data.
|
inline |
get request header.
NPL::NPLObjectProxy & ParaEngine::CUrlProcessor::GetOptions | ( | ) |
get options as NPL table object.
|
virtual |
get thread local data.
It may return NULL if the processor does not support thread local data.
Reimplemented from ParaEngine::IDataProcessor.
int ParaEngine::CUrlProcessor::GetTimeOut | ( | ) |
Get the time out of the request.
default is 15000 milliseconds.
int ParaEngine::CUrlProcessor::GetTotalBytes | ( | ) |
get the total number of bytes.
only for statistics.
|
inline |
get user data.
|
inlinevirtual |
default to true.
If not true, LockDeviceObject, UnLockDeviceObject are not called and Destroy will be called in the worker thread instead of render thread.
Reimplemented from ParaEngine::IDataProcessor.
bool ParaEngine::CUrlProcessor::IsSyncCallbackMode | ( | ) | const |
whether we will invoke callback immediately using NPL.call instead of NPL.activate.
This is only enabled in sync-mode api.
bool ParaEngine::CUrlProcessor::IsTimedOut | ( | DWORD | nCurrentTime | ) |
whether this request is timed out relative to a given time
nCurrentTime | it must be a value returned from GetTickCount(); |
|
virtual |
LockDeviceObject is called from the Graphics thread to lock the device object (D3D9), unless IsDeviceObject() is false.
Implements ParaEngine::IDataProcessor.
|
virtual |
Process is called by one of the processing threads to process the data before it is consumed.
Implements ParaEngine::IDataProcessor.
void ParaEngine::CUrlProcessor::SetCallBack | ( | URL_LOADER_CALLBACK | pFuncCallback = NULL , |
CUrlProcessorUserData * | pUserData = NULL , |
||
bool | bDeleteUserData = false |
||
) |
set the options for asset request.
pFuncCallback | the call back function to use. if none is specified, it will pick a default one to use according to pRequestData->m_nAssetType; this function is always called by the main thread. |
lpUserData | is expected to be classes derived from CUrlProcessorUserData, such as CAssetRequestData |
bDeleteUserData | if true, we will delete lpUserData. |
void ParaEngine::CUrlProcessor::SetCurlEasyOpt | ( | CURL * | handle | ) |
init the easy handle according to the settings.
It will also prepare the task for the curl interface by clearing data, etc.
Pass a long. It should contain the maximum time in seconds that you allow the connection to the server to take. This only limits the connection phase, once it has connected, this option is of no more use. Set to zero to disable connection timeout (it will then only timeout on the system's internal timeouts). See also the CURLOPT_TIMEOUT option
void ParaEngine::CUrlProcessor::SetForbidReuse | ( | bool | bForbidReuse | ) |
whether to close the connection when the request is completed.
|
virtual |
set thread local data.
Reimplemented from ParaEngine::IDataProcessor.
|
virtual |
SetResourceError is called to set the resource pointer to an error code in the event that something went wrong.
Implements ParaEngine::IDataProcessor.
void ParaEngine::CUrlProcessor::SetSaveToFile | ( | const char * | filename | ) |
to which file we shall save the url request to.
This is usually for downloading large web file. if we specify a file to save to. the callback script will no longer contain the response body.
void ParaEngine::CUrlProcessor::SetScriptCallback | ( | const char * | sCallback | ) |
set the npl callback script.
sCallback | a string callback function. it may begin with (runtime_state_name) such as "(main)my_function()", if no runtime state is provided, it is the main state(Not the calling thread). This prevents the user to use multiple threads to download to the same file location by mistake. |
void ParaEngine::CUrlProcessor::SetTimeOut | ( | int | nMilliSeconds | ) |
set the time out of the request.
default is 15000 milliseconds.
|
virtual |
UnLockDeviceObject is called from the Graphics thread to unlock the device object, or call updatesubresource for D3D10, unless IsDeviceObject() is false.
Implements ParaEngine::IDataProcessor.
DWORD ParaEngine::CUrlProcessor::UpdateTime | ( | ) |
this is to set the timer to current time.
it is used to check for TimedOut()
bool ParaEngine::CUrlProcessor::m_bForbidReuse |
whether to reuse connection.
CURLOPT_FORBID_REUSE in libcurl
bool ParaEngine::CUrlProcessor::m_bIsSyncCallbackMode |
we will invoke callback immediately using NPL.call instead of NPL.activate.
This is only enabled in sync-mode api.
DWORD ParaEngine::CUrlProcessor::m_nLastProgressTime |
we send progress data at fixed 1 second interval, even when there are many or zero data.
DWORD ParaEngine::CUrlProcessor::m_nStartTime |
The time that this request is started or received any response.
in milliseconds
URLREQUEST_STATUS ParaEngine::CUrlProcessor::m_nStatus |
the current status of the request.
DWORD ParaEngine::CUrlProcessor::m_nTimeOutTime |
for how long the request is considered timed out.
If a request was not responding, either because of DNS resolving timeout or connection timed out. we will manually remove the request and free the slot for next request. This is usually not needed. However, in windows vista, DNS time out is not working as expected, in windows XP the libcurl can detect time out by itself.
int ParaEngine::CUrlProcessor::m_nUserDataType |
default to 0.
if 0, the request will not SAFE_DELETE the user data. the caller is responsible for the task. if it is 1, the destructor will try to SAFE_DELETE(m_pAssetData)
CParaFile* ParaEngine::CUrlProcessor::m_pFile |
the file to which to save the content to.
usually we only save to memory.
URL_LOADER_CALLBACK ParaEngine::CUrlProcessor::m_pfuncCallBack |
the C++ function to call when task is finished.
std::string ParaEngine::CUrlProcessor::m_sNPLCallback |
the NPL function to call when task is finished.
std::string ParaEngine::CUrlProcessor::m_sNPLStateName |