My Project
UrlLoaders.h
1 #pragma once
2 #include "IDataLoader.h"
3 #include <vector>
4 /* curl specific */
5 #include <curl/curl.h>
6 #include "util/mutex.h"
7 
8 namespace NPL
9 {
10  class NPLObjectProxy;
11 }
12 
13 namespace ParaEngine
14 {
15  class CUrlProcessor;
16  class IProcessorWorkerData;
17 
23  class CUrlLoader : public IDataLoader
24  {
25  public:
29  CUrlLoader();
30  CUrlLoader(const string& url);
31  ~CUrlLoader();
32 
33  void CleanUp();
34 
36  virtual void SetEstimatedSizeInBytes(int nSize);
37 
39  virtual int GetEstimatedSizeInBytes();
40  public:
42  const char* GetFileName();
43 
45  void SetUrl(const char* url);
46 
48  virtual HRESULT Decompress( void** ppData, int* pcBytes );
50  virtual HRESULT Destroy();
53  virtual HRESULT Load();
54 
55  virtual bool IsDeviceObject() { return false; };
56  private:
57  std::string m_url;
58  int m_nEstimatedSizeInBytes;
59  };
60 
63  {
64  public:
66  virtual ~CUrlProcessorUserData(){};
67  };
68 
69  typedef DWORD (*URL_LOADER_CALLBACK)(int nResult, CUrlProcessor* pRequest, CUrlProcessorUserData* pUserData);
70 
73  {
74  upload_context(const char* data, int nDataSize = 0);
75  const char* m_pData;
76  int m_nDataSize;
77  int m_nBytesSent;
78  };
79 
84  {
85  public:
86  // default time out in milliseconds
87  static const DWORD DEFAULT_TIME_OUT = 15000;
88  CUrlProcessor();
89  CUrlProcessor(const string& url, const string& npl_callback);
90  ~CUrlProcessor();
91 
92  void CleanUp();
93  // overrides
94  public:
95  virtual HRESULT LockDeviceObject();
96  virtual HRESULT UnLockDeviceObject();
97  virtual HRESULT Destroy();
98  virtual HRESULT Process( void* pData, int cBytes );
99  virtual HRESULT CopyToResource();
100  virtual void SetResourceError();
101 
103  virtual void SetProcessorWorkerData(IProcessorWorkerData * pThreadLocalData );
105  virtual IProcessorWorkerData * GetProcessorWorkerData();
106 
107  virtual bool IsDeviceObject() { return false; };
108  public:
111  {
112  URL_REQUEST_HTTP_AUTO,
113  URL_REQUEST_HTTP_GET,
114  URL_REQUEST_HTTP_POST,
115  URL_REQUEST_HTTP_GETFILE,
116  URL_REQUEST_HTTP_HEADERS_ONLY,
117  };
118 
121  {
122  // not yet started
123  URL_REQUEST_UNSTARTED,
124  URL_REQUEST_INCOMPLETE,
125  URL_REQUEST_COMPLETED,
126  };
127 
128  public:
130  void SetUrl(const char* url);
131 
133  void SetHeadersOnly();
134 
139  void SetScriptCallback(const char* sCallback);
140 
144  void SetSaveToFile(const char* filename);
145 
147  void AppendHTTPHeader(const char* text);
148 
150  CURLFORMcode AppendFormParam(const char* name, const char* value);
151 
159  CURLFORMcode AppendFormParam(const char* name, const char* type, const char* file, const char* data, int datalen, bool bCacheData = false);
160 
162  void SetCurlEasyOpt(CURL* handle);
163 
165  void CompleteTask();
166 
168  static size_t CUrl_write_data_callback(void *buffer, size_t size, size_t nmemb, void *stream);
169  static size_t CUrl_write_header_callback(void *buffer, size_t size, size_t nmemb, void *stream);
170  static int CUrl_progress_callback(void *clientp,double dltotal,double dlnow,double ultotal, double ulnow);
171  static size_t CUrl_read_email_payload(void *ptr, size_t size, size_t nmemb, void *userp);
172 
173  size_t write_data_callback(void *buffer, size_t size, size_t nmemb);
174  size_t write_header_callback(void *buffer, size_t size, size_t nmemb);
175  int progress_callback(double dltotal, double dlnow, double ultotal, double ulnow);
176 
178  void SetTimeOut(int nMilliSeconds);
180  int GetTimeOut();
181 
183  void SetForbidReuse(bool bForbidReuse);
184 
191  void SetCallBack(URL_LOADER_CALLBACK pFuncCallback=NULL, CUrlProcessorUserData* pUserData=NULL, bool bDeleteUserData=false);
192 
196  bool IsTimedOut(DWORD nCurrentTime);
197 
201  DWORD UpdateTime();
202 
204  void SafeDeleteUserData();
205 
207  CUrlProcessorUserData* GetUserData() {return m_pUserData;}
208 
210  inline vector<char>& GetData() {return m_data;}
211 
213  inline vector<char>& GetHeader() {return m_header;}
214 
216  int GetTotalBytes();
217 
219  int GetBytesReceived();
220 
222  void AddBytesReceived(int nBytesReceived);
223 
224  bool IsEnableProgressUpdate() const;
225 
226  void SetEnableProgressUpdate(bool val);
227 
228  const char* CopyRequestData(const char* pData, int nLength);
229 
231  NPL::NPLObjectProxy& GetOptions();
232 
234  bool IsSyncCallbackMode() const;
235  void SetSyncCallbackMode(bool val);
236  private:
237  int InvokeCallbackScript(const char* sCode, int nLength);
238  public:
240  string m_url;
245  struct curl_httppost* m_pFormLast;
246 
247  IProcessorWorkerData * m_pThreadLocalData;
248 
252  std::string m_sNPLCallback;
254  std::string m_sNPLStateName;
255 
257  URL_LOADER_CALLBACK m_pfuncCallBack;
262 
269 
274 
279 
290 
291  int m_nBytesReceived;
292  int m_nTotalBytes;
293 
294  vector<char> m_data;
295  vector<char> m_header;
296  std::string m_sResponseData;
297  std::string m_sResponseHeader;
298  // this is cache of the request data if any
299  std::string m_sRequestData;
300  // curl return code
301  CURLcode m_returnCode;
302  // the last received HTTP or FTP code. We will expect 200 for successful HTTP response
303  long m_responseCode;
304 
305  upload_context* m_pUploadContext;
306 
308  std::unique_ptr<NPL::NPLObjectProxy> m_options;
309 
311  //ParaEngine::mutex m_mutex;
312  };
313 }
bool m_bForbidReuse
whether to reuse connection.
Definition: UrlLoaders.h:285
int m_nUserDataType
default to 0.
Definition: UrlLoaders.h:266
CUrlProcessorUserData * m_pUserData
optional user data to be send along with the m_pfuncCallBack
Definition: UrlLoaders.h:268
CParaFile * m_pFile
the file to which to save the content to.
Definition: UrlLoaders.h:261
DWORD m_nTimeOutTime
for how long the request is considered timed out.
Definition: UrlLoaders.h:283
define this to enable debugging of NPL code in visual studio
Definition: INPL.h:9
Definition: curl.h:2049
NPL object proxy.
Definition: NPLTable.h:73
struct curl_httppost * m_pFormPost
CURLOPT_HTTPPOST.
Definition: UrlLoaders.h:244
URLREQUEST_STATUS m_nStatus
the current status of the request.
Definition: UrlLoaders.h:273
for read function
Definition: UrlLoaders.h:72
Definition: handle.hpp:33
string m_sSaveToFileName
the file name to save the response data to
Definition: UrlLoaders.h:259
one should derive from this class with virtual destructor.
Definition: UrlLoaders.h:62
different physics engine has different winding order.
Definition: EventBinding.h:32
string m_url
CURLOPT_URL.
Definition: UrlLoaders.h:240
std::unique_ptr< NPL::NPLObjectProxy > m_options
all lib curl options
Definition: UrlLoaders.h:308
URLREQUEST_STATUS
url request status
Definition: UrlLoaders.h:120
Definition: curl.h:126
interface of processor worker data
Definition: IDataLoader.h:44
DWORD m_nLastProgressTime
we send progress data at fixed 1 second interval, even when there are many or zero data...
Definition: UrlLoaders.h:278
bool m_bIsSyncCallbackMode
we will invoke callback immediately using NPL.call instead of NPL.activate.
Definition: UrlLoaders.h:289
int m_nPriority
task priority
Definition: UrlLoaders.h:250
std::string m_sNPLStateName
the NPL runtime state name in which the NPL call back will be invoked.
Definition: UrlLoaders.h:254
IDataProcessor is an interface that the AsyncLoader class uses to process and copy data into locked r...
Definition: IDataLoader.h:76
struct curl_slist * m_pHttpHeaders
http headers to send
Definition: UrlLoaders.h:242
CUrlProcessorUserData * GetUserData()
get user data.
Definition: UrlLoaders.h:207
vector< char > & GetHeader()
get request header.
Definition: UrlLoaders.h:213
Definition: enum_maker.hpp:46
bool m_bEnableProgressUpdate
whether to send progress update via callback
Definition: UrlLoaders.h:287
it presents a real or virtual file in ParaEngine.
Definition: ParaFile.h:31
IDataLoader is an interface that the AsyncLoader class uses to load data from disk.
Definition: IDataLoader.h:14
virtual bool IsDeviceObject()
default to true.
Definition: UrlLoaders.h:107
DWORD m_nStartTime
The time that this request is started or received any response.
Definition: UrlLoaders.h:276
CUrlProcessor implementation of IDataProcessor.
Definition: UrlLoaders.h:83
URLREQUEST_TYPE m_type
The type of the url request task.
Definition: UrlLoaders.h:271
virtual bool IsDeviceObject()
default to true.
Definition: UrlLoaders.h:55
URLREQUEST_TYPE
type of the url request task
Definition: UrlLoaders.h:110
CTextureLoader implementation of IDataLoader it will first search locally.
Definition: UrlLoaders.h:23
vector< char > & GetData()
get request data.
Definition: UrlLoaders.h:210
std::string m_sNPLCallback
the NPL function to call when task is finished.
Definition: UrlLoaders.h:252
URL_LOADER_CALLBACK m_pfuncCallBack
the C++ function to call when task is finished.
Definition: UrlLoaders.h:257