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.

187 lines
5.0 KiB

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( "curlRequestsManager", this, X )
  3. #define LOG_ERROR( X ) m_pLog->Log( "curlRequestsManager", this, _LOG_ERROR, X )
  4. //Thread Logging
  5. #define SLOG( X ) mThis->m_pLog->Log( "curlRequestsManagerS", mThis, X )
  6. #define SLOG_ERROR( X ) mThis->m_pLog->Log( "curlRequestsManagerS", mThis, _LOG_ERROR, X )
  7. #ifdef _WIN32
  8. #define SHORT_SLEEP Sleep(100)
  9. #else
  10. #define SHORT_SLEEP usleep(100000)
  11. #endif
  12. curlRequestsManager* curlRequestsManager::_instance = 0;
  13. /**
  14. * Returns the single instance of the object.
  15. */
  16. curlRequestsManager* curlRequestsManager::getInstance() {
  17. // check if the instance has been created yet
  18. if (0 == _instance) {
  19. // if not, then create it
  20. _instance = new curlRequestsManager;
  21. }
  22. // return the single instance
  23. return _instance;
  24. };
  25. #ifdef __linux__
  26. void *curlRequestsManager::m_fReaperEntryPoint(void *ptr){
  27. #else
  28. DWORD WINAPI curlRequestsManager::m_fReaperEntryPoint( LPVOID ptr){
  29. #endif
  30. curlRequestsManager *mThis;
  31. CURLMsg *mMsg; /* for picking up messages with the transfer status */
  32. int nMsgsLeft; /* how many messages are left */
  33. struct timeval mTimeOut;
  34. int RetVal; /* select() return code */
  35. CURLMcode mRetCode; /* curl_multi_fdset() return code */
  36. fd_set mReadFD;
  37. fd_set mWriteFD;
  38. fd_set mExcepFD;
  39. int mMaxFD = -1;
  40. long mCURLTimeOut;
  41. int mStillRunning;
  42. mThis = (curlRequestsManager *)ptr;
  43. SLOG("Starting Finished Requests Reaper");
  44. mThis->m_bThreadStatus = TH_WORKING;
  45. while (mThis->m_bThreadStatus < TH_ENDED ){
  46. /* we start some action by calling perform right away */
  47. curl_multi_perform( mThis->m_pCURLM, &mStillRunning );
  48. do{
  49. curl_multi_timeout( mThis->m_pCURLM, &mCURLTimeOut );
  50. if ( mCURLTimeOut < 0 ) mCURLTimeOut = 1000;
  51. mTimeOut.tv_sec = mCURLTimeOut / 1000;
  52. mTimeOut.tv_usec = (mCURLTimeOut % 1000) * 1000;
  53. FD_ZERO(&mReadFD);
  54. FD_ZERO(&mWriteFD);
  55. FD_ZERO(&mExcepFD);
  56. /* get file descriptors from the transfers */
  57. mRetCode = curl_multi_fdset(mThis->m_pCURLM, &mReadFD, &mWriteFD, &mExcepFD, &mMaxFD);
  58. if( mMaxFD == -1 ){
  59. SHORT_SLEEP;
  60. RetVal = 0;
  61. } else {
  62. RetVal = select( mMaxFD + 1, &mReadFD, &mWriteFD, &mExcepFD, &mTimeOut );
  63. };
  64. switch (RetVal) {
  65. case -1:
  66. /* select error */
  67. break;
  68. case 0:
  69. default:
  70. /* timeout or readable/writable sockets */
  71. curl_multi_perform( mThis->m_pCURLM, &mStillRunning );
  72. break;
  73. };
  74. }while( mStillRunning ); /* if there are still transfers, loop! */
  75. /* call curl_multi_perform or curl_multi_socket_action first, then loop
  76. through and check if there are any transfers that have completed */
  77. /* See how the transfers went */
  78. while( ( mMsg = curl_multi_info_read( mThis->m_pCURLM, &nMsgsLeft ) ) ){
  79. if( mMsg->msg == CURLMSG_DONE ){
  80. /* Find out which handle this message is about */
  81. for (RequestPool::iterator index = mThis->m_vRequestPool.begin(); index < mThis->m_vRequestPool.end(); index++){
  82. if ((*index)->getEH() == mMsg->easy_handle){
  83. mRetCode = curl_multi_remove_handle(mThis->m_pCURLM, (*index)->getEH());
  84. (*index)->issue();
  85. delete( (*index) );
  86. mThis->m_vRequestPool.erase( index );
  87. break;
  88. };
  89. };
  90. };
  91. };
  92. };
  93. SLOG("Ending Finished Requests Reaper");
  94. #ifdef __linux__
  95. pthread_exit(NULL);
  96. return NULL;
  97. #else
  98. return 0;
  99. #endif
  100. };
  101. #ifdef __linux__
  102. void *curlRequestsManager::m_fRequestEntryPoint(void *ptr){
  103. #else
  104. DWORD WINAPI curlRequestsManager::m_fRequestEntryPoint(LPVOID ptr){
  105. #endif
  106. curlRequest *mThis;
  107. mThis = (curlRequest *)ptr;
  108. //SLOG("Request Thread Entry Point Enter.");
  109. //mThis->issue();
  110. //SLOG("Request Thread Entry Point Exit.");
  111. #ifdef __linux__
  112. pthread_exit(NULL);
  113. return NULL;
  114. #else
  115. return 0;
  116. #endif
  117. };
  118. bool curlRequestsManager::get(curlRequest *RC){
  119. if (RC != NULL){
  120. CURLMcode RetCode;
  121. LOG("New Request");
  122. m_vRequestPool.push_back(RC);
  123. RetCode = curl_multi_add_handle( m_pCURLM, RC->getEH());
  124. /*
  125. #ifdef __linux__
  126. int RetVal = pthread_create(&m_sThreadHandler, NULL, m_fThreadEntryPoint, (void *) this);
  127. if (RetVal == -1) {
  128. LOG_ERROR("Thread Creation Faled.");
  129. return false;
  130. };
  131. #else
  132. DWORD tThreadID;
  133. HANDLE tThreadHandle = CreateThread(
  134. NULL,
  135. 0,
  136. (LPTHREAD_START_ROUTINE)m_fRequestEntryPoint,
  137. (LPVOID)RC,
  138. 0,
  139. &tThreadID);
  140. if (tThreadHandle == NULL) {
  141. LOG_ERROR("Thread Creation Faled.");
  142. return false;
  143. };
  144. LOG("Thread to be started.");
  145. m_vRequestThreadPool.push_back(tThreadHandle);
  146. #endif
  147. */
  148. return true;
  149. };
  150. return false;
  151. };
  152. bool curlRequestsManager::get(CSTR URL, CSTR contentType ){
  153. curlRequest *tRC;
  154. //LOG("Get URL.");
  155. tRC = new curlRequest(URL, contentType );
  156. if (tRC != NULL){
  157. return get(tRC);
  158. };
  159. return false;
  160. };
  161. #undef LOG
  162. #undef LOG_ERROR
  163. #undef SLOG
  164. #undef SLOG_ERROR