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.

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