My Project
Public Types | Public Member Functions | Static Public Member Functions | Public Attributes | Static Public Attributes | List of all members
ParaEngine::CUrlProcessor Class Reference

CUrlProcessor implementation of IDataProcessor. More...

#include <UrlLoaders.h>

Inheritance diagram for ParaEngine::CUrlProcessor:
ParaEngine::IDataProcessor

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 IProcessorWorkerDataGetProcessorWorkerData ()
 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
 
CUrlProcessorUserDataGetUserData ()
 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::NPLObjectProxyGetOptions ()
 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_slistm_pHttpHeaders
 http headers to send
 
struct curl_httppostm_pFormPost
 CURLOPT_HTTPPOST.
 
struct curl_httppostm_pFormLast
 
IProcessorWorkerDatam_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
 
CParaFilem_pFile
 the file to which to save the content to. More...
 
int m_nUserDataType
 default to 0. More...
 
CUrlProcessorUserDatam_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_contextm_pUploadContext
 
std::unique_ptr< NPL::NPLObjectProxym_options
 all lib curl options
 

Static Public Attributes

static const DWORD DEFAULT_TIME_OUT = 15000
 

Detailed Description

CUrlProcessor implementation of IDataProcessor.

Member Function Documentation

§ AddBytesReceived()

void ParaEngine::CUrlProcessor::AddBytesReceived ( int  nBytesReceived)

add bytes received.

only for statistics

§ AppendFormParam()

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.

Parameters
bCacheDataif 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

§ CompleteTask()

void ParaEngine::CUrlProcessor::CompleteTask ( )

call the call back if any, this function must be called in the main game thread.

§ CopyToResource()

HRESULT ParaEngine::CUrlProcessor::CopyToResource ( )
virtual

CopyToResource copies the data from memory to the locked device object (D3D9).

Also by the IO thread.

Implements ParaEngine::IDataProcessor.

§ CUrl_write_data_callback()

size_t ParaEngine::CUrlProcessor::CUrl_write_data_callback ( void *  buffer,
size_t  size,
size_t  nmemb,
void *  stream 
)
static

curl call back.

§ Destroy()

HRESULT ParaEngine::CUrlProcessor::Destroy ( )
virtual

Destroy is called by the graphics thread when it has consumed the data, unless IsDeviceObject() is false.

Implements ParaEngine::IDataProcessor.

§ GetBytesReceived()

int ParaEngine::CUrlProcessor::GetBytesReceived ( )

get bytes received.

only for statistics

§ GetData()

vector<char>& ParaEngine::CUrlProcessor::GetData ( )
inline

get request data.

§ GetHeader()

vector<char>& ParaEngine::CUrlProcessor::GetHeader ( )
inline

get request header.

§ GetOptions()

NPL::NPLObjectProxy & ParaEngine::CUrlProcessor::GetOptions ( )

get options as NPL table object.

§ GetProcessorWorkerData()

ParaEngine::IProcessorWorkerData * CUrlProcessor::GetProcessorWorkerData ( )
virtual

get thread local data.

It may return NULL if the processor does not support thread local data.

Reimplemented from ParaEngine::IDataProcessor.

§ GetTimeOut()

int ParaEngine::CUrlProcessor::GetTimeOut ( )

Get the time out of the request.

default is 15000 milliseconds.

§ GetTotalBytes()

int ParaEngine::CUrlProcessor::GetTotalBytes ( )

get the total number of bytes.

only for statistics.

§ GetUserData()

CUrlProcessorUserData* ParaEngine::CUrlProcessor::GetUserData ( )
inline

get user data.

§ IsDeviceObject()

virtual bool ParaEngine::CUrlProcessor::IsDeviceObject ( )
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.

§ IsSyncCallbackMode()

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.

§ IsTimedOut()

bool ParaEngine::CUrlProcessor::IsTimedOut ( DWORD  nCurrentTime)

whether this request is timed out relative to a given time

Parameters
nCurrentTimeit must be a value returned from GetTickCount();

§ LockDeviceObject()

HRESULT ParaEngine::CUrlProcessor::LockDeviceObject ( )
virtual

LockDeviceObject is called from the Graphics thread to lock the device object (D3D9), unless IsDeviceObject() is false.

Implements ParaEngine::IDataProcessor.

§ Process()

HRESULT ParaEngine::CUrlProcessor::Process ( void *  pData,
int  cBytes 
)
virtual

Process is called by one of the processing threads to process the data before it is consumed.

Implements ParaEngine::IDataProcessor.

§ SetCallBack()

void ParaEngine::CUrlProcessor::SetCallBack ( URL_LOADER_CALLBACK  pFuncCallback = NULL,
CUrlProcessorUserData pUserData = NULL,
bool  bDeleteUserData = false 
)

set the options for asset request.

Parameters
pFuncCallbackthe 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.
lpUserDatais expected to be classes derived from CUrlProcessorUserData, such as CAssetRequestData
bDeleteUserDataif true, we will delete lpUserData.

§ SetCurlEasyOpt()

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

§ SetForbidReuse()

void ParaEngine::CUrlProcessor::SetForbidReuse ( bool  bForbidReuse)

whether to close the connection when the request is completed.

§ SetProcessorWorkerData()

void CUrlProcessor::SetProcessorWorkerData ( IProcessorWorkerData pThreadLocalData)
virtual

set thread local data.

Reimplemented from ParaEngine::IDataProcessor.

§ SetResourceError()

void ParaEngine::CUrlProcessor::SetResourceError ( )
virtual

SetResourceError is called to set the resource pointer to an error code in the event that something went wrong.

Implements ParaEngine::IDataProcessor.

§ SetSaveToFile()

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.

§ SetScriptCallback()

void ParaEngine::CUrlProcessor::SetScriptCallback ( const char *  sCallback)

set the npl callback script.

Parameters
sCallbacka 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.

§ SetTimeOut()

void ParaEngine::CUrlProcessor::SetTimeOut ( int  nMilliSeconds)

set the time out of the request.

default is 15000 milliseconds.

§ UnLockDeviceObject()

HRESULT ParaEngine::CUrlProcessor::UnLockDeviceObject ( )
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.

§ UpdateTime()

DWORD ParaEngine::CUrlProcessor::UpdateTime ( )

this is to set the timer to current time.

it is used to check for TimedOut()

Returns
: return the current time.

Member Data Documentation

§ m_bForbidReuse

bool ParaEngine::CUrlProcessor::m_bForbidReuse

whether to reuse connection.

CURLOPT_FORBID_REUSE in libcurl

§ m_bIsSyncCallbackMode

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.

§ m_nLastProgressTime

DWORD ParaEngine::CUrlProcessor::m_nLastProgressTime

we send progress data at fixed 1 second interval, even when there are many or zero data.

§ m_nStartTime

DWORD ParaEngine::CUrlProcessor::m_nStartTime

The time that this request is started or received any response.

in milliseconds

§ m_nStatus

URLREQUEST_STATUS ParaEngine::CUrlProcessor::m_nStatus

the current status of the request.

§ m_nTimeOutTime

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.

§ m_nUserDataType

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)

§ m_pFile

CParaFile* ParaEngine::CUrlProcessor::m_pFile

the file to which to save the content to.

usually we only save to memory.

§ m_pfuncCallBack

URL_LOADER_CALLBACK ParaEngine::CUrlProcessor::m_pfuncCallBack

the C++ function to call when task is finished.

§ m_sNPLCallback

std::string ParaEngine::CUrlProcessor::m_sNPLCallback

the NPL function to call when task is finished.

§ m_sNPLStateName

std::string ParaEngine::CUrlProcessor::m_sNPLStateName

the NPL runtime state name in which the NPL call back will be invoked.

if empty, it will be the default main state.


The documentation for this class was generated from the following files: