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.

219 lines
6.9 KiB

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