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.

206 lines
6.4 KiB

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