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.

214 lines
6.6 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():m_mPoolProtector(){
  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
  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. #ifdef DEBUG
  57. LOG("First Req Enqueued");
  58. #endif
  59. while( m_bThreadStatus < TH_ENDED ){
  60. /* we start some action by calling perform right away */
  61. curl_multi_perform( m_pCURLM, &mStillRunning );
  62. do{
  63. curl_multi_timeout( m_pCURLM, &mCURLTimeOut );
  64. if( mCURLTimeOut < 0 ) mCURLTimeOut = 1000;
  65. mTimeOut.tv_sec = mCURLTimeOut / 1000;
  66. mTimeOut.tv_usec = (mCURLTimeOut % 1000) * 1000;
  67. FD_ZERO(&mReadFD);
  68. FD_ZERO(&mWriteFD);
  69. FD_ZERO(&mExcepFD);
  70. /* get file descriptors from the transfers */
  71. mRetCode = curl_multi_fdset( m_pCURLM, &mReadFD, &mWriteFD, &mExcepFD, &mMaxFD );
  72. if( mRetCode != CURLM_OK) {
  73. char tT[256];
  74. sprintf( tT, "curl_multi_fdset() failed, code %d!", mRetCode );
  75. LOG_ERROR( tT );
  76. break;
  77. };
  78. if( mMaxFD == -1 ){
  79. SLEEP( 10 );
  80. RetVal = 0;
  81. break;
  82. } else {
  83. RetVal = select( mMaxFD + 1, &mReadFD, &mWriteFD, &mExcepFD, &mTimeOut );
  84. };
  85. switch( RetVal ){
  86. case -1:
  87. /* select error */
  88. break;
  89. case 0:
  90. default:
  91. /* timeout or readable/writable sockets */
  92. curl_multi_perform( m_pCURLM, &mStillRunning );
  93. break;
  94. };
  95. /* See how the transfers went */
  96. while( ( mMsg = curl_multi_info_read( m_pCURLM, &nMsgsLeft ) ) && ( m_bThreadStatus < TH_ENDED ) ){
  97. if( mMsg->msg == CURLMSG_DONE ){
  98. CURLcode rC = mMsg->data.result;
  99. #ifdef DEBUG_EXTRA
  100. char tB[256];
  101. sprintf( tB, "Request Done with Code: %d", rC );
  102. LOG( tB );
  103. #endif
  104. /* Find out which handle this message is about */
  105. m_mPoolProtector.lock();
  106. for( RequestPool::iterator index = m_vRequestPool.begin(); index < m_vRequestPool.end(); index++ ){
  107. if( (*index)->getEH() == mMsg->easy_handle ){
  108. mRetCode = curl_multi_remove_handle( m_pCURLM, (*index)->getEH());
  109. if( rC == CURLE_OK ){
  110. (*index)->issue();
  111. };
  112. //CURLE_OPERATION_TIMEDOUT
  113. delete( (*index) );
  114. m_vRequestPool.erase( index );
  115. break;
  116. };
  117. };
  118. m_mPoolProtector.unlock();
  119. };
  120. };
  121. } while ( mStillRunning && ( m_bThreadStatus < TH_ENDED ) ); /* if there are still transfers, loop! */
  122. /* call curl_multi_perform or curl_multi_socket_action first, then loop
  123. through and check if there are any transfers that have completed */
  124. /* See how the transfers went */
  125. /*
  126. while( ( mMsg = curl_multi_info_read( m_pCURLM, &nMsgsLeft ) ) && ( m_bThreadStatus < TH_ENDED ) ){
  127. char tB[129];
  128. printf( tB, "[%d] Msgs Left", nMsgsLeft);
  129. LOG( tB );
  130. if( mMsg->msg == CURLMSG_DONE ){
  131. // Find out which handle this message is about
  132. LOG("REQ -> Done!");
  133. m_mPoolProtector.lock();
  134. for (RequestPool::iterator index = m_vRequestPool.begin(); index < m_vRequestPool.end(); index++){
  135. if ((*index)->getEH() == mMsg->easy_handle){
  136. mRetCode = curl_multi_remove_handle( m_pCURLM, (*index)->getEH());
  137. (*index)->issue();
  138. delete( (*index) );
  139. m_vRequestPool.erase( index );
  140. break;
  141. };
  142. };
  143. m_mPoolProtector.unlock();
  144. };
  145. };
  146. */
  147. SLEEP( 10 );
  148. };
  149. #ifdef DEBUG
  150. LOG("Ending Finished Requests Reaper");
  151. #endif
  152. };
  153. bool curlRequestsManager::accomplish( curlRequest *RC ){
  154. if( RC ){
  155. #ifdef DEBUG_EXTRA
  156. LOG("New Request");
  157. #endif
  158. CURLMcode RetCode = curl_multi_add_handle( m_pCURLM, RC->getEH() );
  159. if( RetCode == CURLM_OK ) {
  160. m_mPoolProtector.lock();
  161. m_vRequestPool.push_back( RC );
  162. m_mPoolProtector.unlock();
  163. #ifdef DEBUG_EXTRA
  164. LOG("Added OK");
  165. #endif
  166. return true;
  167. } else {
  168. LOG_ERROR(curl_multi_strerror(RetCode));
  169. };
  170. };
  171. return false;
  172. };
  173. bool curlRequestsManager::get( CSTR URL, CSTR contentType ){
  174. curlRequest *tRC;
  175. tRC = new curlRequest( URL, contentType );
  176. if( tRC != NULL ){
  177. #ifdef DEBUG_EXTRA
  178. LOG("Adding GET RQ");
  179. #endif
  180. return accomplish(tRC);
  181. };
  182. return false;
  183. };
  184. bool curlRequestsManager::post(CSTR URL, CSTR postData, CSTR contentType ){
  185. curlRequest *tRC;
  186. tRC = new curlRequest( URL, contentType );
  187. if( tRC != NULL ){
  188. #ifdef DEBUG_EXTRA
  189. LOG("Adding POST RQ");
  190. #endif
  191. tRC->setPOST( postData, strlen(postData) );
  192. return accomplish( tRC );
  193. };
  194. return false;
  195. };
  196. #undef LOG
  197. #undef LOG_ERROR