You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

257 lines
6.2 KiB

​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
12 months ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. #include "curlRequestsManager.h"
  2. #define LOG(X) m_pLog->Log("CURL_RQM", this, X)
  3. #define LOG_ERROR(X) \
  4. { \
  5. char _tBfrErrMSG2w34[1024]; \
  6. sprintf(_tBfrErrMSG2w34, "In file %s[%d] in func:%s %s", __FILE__, __LINE__, __func__, X); \
  7. m_pLog->Log("CURL_RQM", this, _LOG_ERROR, _tBfrErrMSG2w34); \
  8. }
  9. // Constructor implementation
  10. curlRequestsManager::curlRequestsManager() :
  11. m_mPoolProtector()
  12. {
  13. m_pLog = basicLogCollection::getInstance();
  14. curl_global_init(CURL_GLOBAL_ALL);
  15. m_pCURLM = curl_multi_init();
  16. if (m_pCURLM)
  17. {
  18. init();
  19. }
  20. else
  21. {
  22. LOG_ERROR("Curl Muti Handle Cannot be created!!");
  23. };
  24. }
  25. curlRequestsManager::~curlRequestsManager()
  26. {
  27. waitThreadEnd(true);
  28. curl_multi_cleanup(m_pCURLM);
  29. }
  30. curlRequestsManager * curlRequestsManager::_instance = 0;
  31. /**
  32. * Returns the single instance of the object.
  33. */
  34. curlRequestsManager * curlRequestsManager::getInstance()
  35. {
  36. // check if the instance has been created yet
  37. if (0 == _instance)
  38. {
  39. // if not, then create it
  40. _instance = new curlRequestsManager();
  41. }
  42. // return the single instance
  43. return _instance;
  44. }
  45. void curlRequestsManager::m_fThreadProc(void)
  46. {
  47. CURLMsg * mMsg = NULL; /* for picking up messages with the transfer status */
  48. int nMsgsLeft = 0; /* how many messages are left */
  49. struct timeval mTimeOut;
  50. int RetVal; /* select() return code */
  51. CURLMcode mRetCode; /* curl_multi_fdset() return code */
  52. fd_set mReadFD;
  53. fd_set mWriteFD;
  54. fd_set mExcepFD;
  55. int mMaxFD = -1;
  56. long mCURLTimeOut;
  57. int mStillRunning;
  58. bool bProceedThread = false;
  59. #ifdef DEBUG
  60. LOG("Starting Finished Requests Reaper");
  61. #endif
  62. // BugCurl Multi cannot starty if MR = empty ....
  63. m_bThreadStatus = TH_WORKING;
  64. while ((m_bThreadStatus < TH_ENDED) && ! bProceedThread)
  65. {
  66. SLEEP(10);
  67. m_mPoolProtector.lock();
  68. if (m_vRequestPool.size())
  69. {
  70. bProceedThread = true;
  71. }
  72. m_mPoolProtector.unlock();
  73. };
  74. #ifdef DEBUG
  75. LOG("First Req Enqueued");
  76. #endif
  77. while (m_bThreadStatus < TH_ENDED)
  78. {
  79. /* we start some action by calling perform right away */
  80. curl_multi_perform(m_pCURLM, &mStillRunning);
  81. do
  82. {
  83. curl_multi_timeout(m_pCURLM, &mCURLTimeOut);
  84. if (mCURLTimeOut < 0)
  85. {
  86. mCURLTimeOut = 1000;
  87. }
  88. mTimeOut.tv_sec = mCURLTimeOut / 1000;
  89. mTimeOut.tv_usec = (mCURLTimeOut % 1000) * 1000;
  90. FD_ZERO(&mReadFD);
  91. FD_ZERO(&mWriteFD);
  92. FD_ZERO(&mExcepFD);
  93. /* get file descriptors from the transfers */
  94. mRetCode = curl_multi_fdset(m_pCURLM, &mReadFD, &mWriteFD, &mExcepFD, &mMaxFD);
  95. if (mRetCode != CURLM_OK)
  96. {
  97. char tT[256];
  98. sprintf(tT, "curl_multi_fdset() failed, code %d!", mRetCode);
  99. LOG_ERROR(tT);
  100. break;
  101. };
  102. if (mMaxFD == -1)
  103. {
  104. SLEEP(10);
  105. RetVal = 0;
  106. break;
  107. }
  108. else
  109. {
  110. RetVal = select(mMaxFD + 1, &mReadFD, &mWriteFD, &mExcepFD, &mTimeOut);
  111. };
  112. switch (RetVal)
  113. {
  114. case -1:
  115. /* select error */
  116. break;
  117. case 0:
  118. default:
  119. /* timeout or readable/writable sockets */
  120. curl_multi_perform(m_pCURLM, &mStillRunning);
  121. break;
  122. };
  123. /* See how the transfers went */
  124. while ((mMsg = curl_multi_info_read(m_pCURLM, &nMsgsLeft)) && (m_bThreadStatus < TH_ENDED))
  125. {
  126. if (mMsg->msg == CURLMSG_DONE)
  127. {
  128. CURLcode rC = mMsg->data.result;
  129. #ifdef DEBUG_EXTRA
  130. char tB[256];
  131. sprintf(tB, "Request Done with Code: %d", rC);
  132. LOG(tB);
  133. #endif
  134. /* Find out which handle this message is about */
  135. m_mPoolProtector.lock();
  136. for (RequestPool::iterator index = m_vRequestPool.begin(); index < m_vRequestPool.end();)
  137. {
  138. if ((*index)->getEH() == mMsg->easy_handle)
  139. {
  140. mRetCode = curl_multi_remove_handle(m_pCURLM, (*index)->getEH());
  141. if (rC == CURLE_OK)
  142. {
  143. (*index)->issue();
  144. };
  145. // CURLE_OPERATION_TIMEDOUT
  146. delete(*index);
  147. m_vRequestPool.erase(index);
  148. break;
  149. }
  150. else
  151. {
  152. index++;
  153. };
  154. };
  155. m_mPoolProtector.unlock();
  156. };
  157. };
  158. }
  159. while (mStillRunning && (m_bThreadStatus < TH_ENDED)); /* if there are still transfers, loop! */
  160. /* call curl_multi_perform or curl_multi_socket_action first, then loop
  161. through and check if there are any transfers that have completed */
  162. /* See how the transfers went */
  163. /*
  164. while( ( mMsg = curl_multi_info_read( m_pCURLM, &nMsgsLeft ) ) && ( m_bThreadStatus < TH_ENDED ) ){
  165. char tB[129];
  166. printf( tB, "[%d] Msgs Left", nMsgsLeft);
  167. LOG( tB );
  168. if( mMsg->msg == CURLMSG_DONE ){
  169. // Find out which handle this message is about
  170. LOG("REQ -> Done!");
  171. m_mPoolProtector.lock();
  172. for (RequestPool::iterator index = m_vRequestPool.begin(); index < m_vRequestPool.end(); index++){
  173. if ((*index)->getEH() == mMsg->easy_handle){
  174. mRetCode = curl_multi_remove_handle( m_pCURLM, (*index)->getEH());
  175. (*index)->issue();
  176. delete( (*index) );
  177. m_vRequestPool.erase( index );
  178. break;
  179. };
  180. };
  181. m_mPoolProtector.unlock();
  182. };
  183. };
  184. */
  185. SLEEP(10);
  186. };
  187. #ifdef DEBUG
  188. LOG("Ending Finished Requests Reaper");
  189. #endif
  190. }
  191. bool curlRequestsManager::accomplish(curlRequest * RC)
  192. {
  193. if (RC)
  194. {
  195. #ifdef DEBUG_EXTRA
  196. LOG("New Request");
  197. #endif
  198. CURLMcode RetCode = curl_multi_add_handle(m_pCURLM, RC->getEH());
  199. if (RetCode == CURLM_OK)
  200. {
  201. m_mPoolProtector.lock();
  202. m_vRequestPool.push_back(RC);
  203. m_mPoolProtector.unlock();
  204. #ifdef DEBUG_EXTRA
  205. LOG("Added OK");
  206. #endif
  207. return true;
  208. }
  209. else
  210. {
  211. LOG_ERROR(curl_multi_strerror(RetCode));
  212. };
  213. };
  214. return false;
  215. }
  216. bool curlRequestsManager::get(CSTR URL, CSTR contentType)
  217. {
  218. curlRequest * tRC;
  219. tRC = new curlRequest(URL, contentType);
  220. if (tRC != NULL)
  221. {
  222. #ifdef DEBUG_EXTRA
  223. LOG("Adding GET RQ");
  224. #endif
  225. return accomplish(tRC);
  226. };
  227. return false;
  228. }
  229. bool curlRequestsManager::post(CSTR URL, CSTR postData, CSTR contentType)
  230. {
  231. curlRequest * tRC;
  232. tRC = new curlRequest(URL, contentType);
  233. if (tRC != NULL)
  234. {
  235. #ifdef DEBUG_EXTRA
  236. LOG("Adding POST RQ");
  237. #endif
  238. tRC->setPOST(postData, strlen(postData));
  239. return accomplish(tRC);
  240. };
  241. return false;
  242. }
  243. #undef LOG
  244. #undef LOG_ERROR