My Project
NPLNetClient.h
1 #pragma once
2 #include "IParaWebService.h"
3 #include <string>
4 #include <map>
5 #include <set>
6 #include <boost/asio.hpp>
7 #include <boost/thread.hpp>
8 #include <boost/scoped_ptr.hpp>
9 
10 /* curl specific */
11 #include <curl/curl.h>
12 
13 
14 namespace ParaEngine
15 {
16 #ifdef HAS_JABBER_CLIENT
17  class CNPLJabberClient;
18 #endif
19  struct AssetEntity;
20 
21  using namespace std;
22 
25  {
26  public:
27  CAssetRequestData():m_nAssetType(-1), m_bLazyLoading(false){};
29  public:
33  string m_sAssetKey;
36  };
37 
38  class CURLRequestTask;
39  typedef DWORD (*URL_REQUEST_TASK_CALLBACK)(int nResult, CURLRequestTask* pRequest, LPVOID lpUserData);
40 
46  {
47  public:
50  {
51  URL_REQUEST_HTTP_AUTO,
52  URL_REQUEST_HTTP_GET,
53  URL_REQUEST_HTTP_POST,
54  URL_REQUEST_HTTP_GETFILE,
55  };
56 
59  {
60  // not yet started
61  URL_REQUEST_UNSTARTED,
62  URL_REQUEST_INCOMPLETE,
63  URL_REQUEST_COMPLETED,
64  };
65  // default time out in milliseconds
66  static const DWORD DEFAULT_TIME_OUT = 15000;
67 
68  CURLRequestTask():m_pFormPost(0), m_nTimeOutTime(DEFAULT_TIME_OUT), m_nStartTime(0),m_responseCode(0), m_pFormLast(0), m_pUserData(0), m_returnCode(CURLE_OK), m_type(URL_REQUEST_HTTP_AUTO), m_nPriority(0), m_nStatus(URL_REQUEST_UNSTARTED), m_pfuncCallBack(0), m_nBytesReceived(0), m_nTotalBytes(0), m_nUserDataType(0) {};
69  ~CURLRequestTask();
70 
71  public:
73  CURLFORMcode AppendFormParam(const char* name, const char* value);
74 
80  CURLFORMcode AppendFormParam(const char* name, const char* type, const char* file, const char* data, int datalen);
81 
83  void SetCurlEasyOpt(CURL* handle);
84 
86  void CompleteTask();
87 
89  static size_t CUrl_write_data_callback(void *buffer, size_t size, size_t nmemb, void *stream);
90  static size_t CUrl_write_header_callback(void *buffer, size_t size, size_t nmemb, void *stream);
91 
93  void SetTimeOut(int nMilliSeconds);
95  int GetTimeOut();
96 
101  void SetAssetRequestOpt(CAssetRequestData* pRequestData, URL_REQUEST_TASK_CALLBACK pFuncCallback=NULL);
102 
106  static DWORD Asset_HTTP_request_callback(int nResult, CURLRequestTask* pRequest, LPVOID lpUserData);
107 
109  void SafeDeleteUserData();
110 
114  bool IsTimedOut(DWORD nCurrentTime);
115 
119  DWORD UpdateTime();
120 
121  public:
123  string m_url;
126  struct curl_httppost* m_pFormLast;
127 
132 
134  URL_REQUEST_TASK_CALLBACK m_pfuncCallBack;
137 
143  union {
144  LPVOID m_pUserData;
145  CAssetRequestData* m_pAssetData;
146  };
147 
148 
153 
160 
161  int m_nBytesReceived;
162  int m_nTotalBytes;
163  vector<char> m_data;
164  vector<char> m_header;
165  string m_sResponseData;
166  string m_sResponseHeader;
167  // curl return code
168  CURLcode m_returnCode;
169  // the last received HTTP or FTP code. We will expect 200 for successful HTTP response
170  long m_responseCode;
171 
172  };
173 
189  {
190  public:
193  {
194  public:
195  CUrlWorkerState(): m_easy_handle(NULL), m_pCurrentTask(NULL), m_returnCode(CURLE_OK), m_bIsCompleted(true) {};
196  CURL* m_easy_handle;
197  /* return code for last transfer */
198  CURLcode m_returnCode;
201  CURLRequestTask* m_pCurrentTask;
202  };
203 
204  CRequestTaskPool():m_nRunningTaskCount(0), m_nMaxWorkerThreads(1), m_nMaxQueuedTask(65535), m_multi_handle(NULL){}
205  ~CRequestTaskPool();
206  public:
210  bool AppendURLRequest(CURLRequestTask* pUrlTask);
211 
215  int DoProcess();
216 
218  void SetMaxTaskSlotsCount(int nCount);
219  protected:
222  CUrlWorkerState* GetFreeWorkerSlot();
223 
228  int CURL_MultiPerform();
229 
230  private:
231 
233  std::list <CURLRequestTask*> m_task_pool;
234 
236  int m_nMaxWorkerThreads;
238  std::list <CUrlWorkerState> m_easy_handles;
239 
241  int m_nMaxQueuedTask;
243  CURLM * m_multi_handle;
245  int m_nRunningTaskCount;
246  };
247 
248 
253  {
254  public:
255  CNPLNetClient();
256  virtual ~CNPLNetClient();
257 
259  static CNPLNetClient* GetInstance();
261  static void ReleaseInstance();
262  public:
263 
268  virtual INPLWebService* GetWebService(const char* sURL);
276  virtual INPLWebService* OpenWebService(const char* sURL, const char* sUserName, const char* sPassword, const char* sDomain);
277 
282  virtual bool CloseWebService(const char* sURL);
283 
289  virtual INPLJabberClient* GetJabberClient(const char* sJID);
294  virtual INPLJabberClient* CreateJabberClient(const char* sJID);
295 
301  virtual bool CloseJabberClient(const char* sJID);
302 
308  virtual void AsyncDownload(const char* url, const char* destFolder, const char* callbackScript, const char* DownloaderName);
309 
310 
315  virtual void CancelDownload(const char* DownloaderName);
316 
322  virtual int Download(const char* url, const char* destFolder, const char* callbackScript, const char* DownloaderName);
323 
328  virtual int ProcessResults();
329 
334  virtual int ProcessDownloaderResults();
335 
337  virtual void DeleteThis();
338 
340  //
341  // Do url request
342  //
344 
355  virtual bool AppendURLRequest(CURLRequestTask* pUrlTask, const char* sPoolName = NULL);
356 
361  virtual bool ChangeRequestPoolSize(const char* sPoolName, int nCount);
362 
367  virtual int ProcessUrlRequests();
368 
372  void AddPendingRequest(const char* sURL);
373 
377  bool HasPendingRequest(const char* sURL);
378 
380  void RemovePendingRequest(const char* sURL);
381 
383  void ClearAllPendingRequests();
384 
386  string GetCachePath(const char* sFileUrl);
387 
389  void Cleanup();
390  protected:
391 
393  CRequestTaskPool* CreateGetRequestTaskPool(const char* sPoolName = NULL);
394 
395  private:
396 #ifdef HAS_JABBER_CLIENT
397  std::map <std::string, CNPLJabberClient*> m_jabberClients;
398 #endif
399  std::map <std::string, CRequestTaskPool*> m_request_pools;
400 
401  std::set <std::string> m_pending_requests;
402 
404  boost::asio::io_service m_dispatcher_io_service;
405 
407  boost::scoped_ptr<boost::thread> m_dispatcherThread;
408 
411  boost::scoped_ptr<boost::asio::io_service::work> m_work_lifetime;
412  };
413 }
bool m_bLazyLoading
default to true.
Definition: NPLNetClient.h:35
Definition: handle.hpp:33
NPL interface of a XML Web service client proxy.
Definition: IParaWebService.h:50
different physics engine has different winding order.
Definition: EventBinding.h:32
a task pool is a collection of requests of which only a specified limited number of worker threads ar...
Definition: NPLNetClient.h:188
DWORD m_nStartTime
The time that this request is started or received any response.
Definition: NPLNetClient.h:155
Definition: curl.h:126
URLREQUEST_TYPE m_type
The type of the url request task.
Definition: NPLNetClient.h:150
struct curl_httppost * m_pFormPost
CURLOPT_HTTPPOST.
Definition: NPLNetClient.h:125
URL request can be a HTTP get/post request etc.
Definition: NPLNetClient.h:45
URL_REQUEST_TASK_CALLBACK m_pfuncCallBack
the C++ function to call when task is finished.
Definition: NPLNetClient.h:134
Option names.
Definition: NPLNetClient.h:252
int m_nAssetType
it is of AssetEntity::AssetType
Definition: NPLNetClient.h:31
an asset request data to be sent via the CURLRequestTask
Definition: NPLNetClient.h:24
NPL interface of a Jabber-XMPP client proxy.
Definition: IParaWebService.h:108
URLREQUEST_STATUS m_nStatus
the current status of the request.
Definition: NPLNetClient.h:152
Definition: enum_maker.hpp:46
URLREQUEST_TYPE
type of the url request task
Definition: NPLNetClient.h:49
int m_nPriority
task priority
Definition: NPLNetClient.h:129
url worker state.
Definition: NPLNetClient.h:192
string m_sSaveToFileName
the file name to save the response data to
Definition: NPLNetClient.h:136
bool m_bIsCompleted
what this message means
Definition: NPLNetClient.h:200
string m_sNPLCallback
the NPL function to call when task is finished.
Definition: NPLNetClient.h:131
interface of NPL web service client.
Definition: IParaWebService.h:466
int m_nUserDataType
default to 0.
Definition: NPLNetClient.h:141
string m_url
CURLOPT_URL.
Definition: NPLNetClient.h:123
URLREQUEST_STATUS
url request status
Definition: NPLNetClient.h:58
Base class for managed asset entity in ParaEngine.
Definition: AssetEntity.h:25
DWORD m_nTimeOutTime
for how long the request is considered timed out.
Definition: NPLNetClient.h:159
string m_sAssetKey
this is usually used to locate the asset entity once request is finished.
Definition: NPLNetClient.h:33