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.0 KiB
188 lines
5.0 KiB
#include "curlRequestsManager.h"
|
|
|
|
#define LOG( X ) m_pLog->Log( "curlRequestsManager", this, X )
|
|
#define LOG_ERROR( X ) m_pLog->Log( "curlRequestsManager", this, _LOG_ERROR, X )
|
|
//Thread Logging
|
|
#define SLOG( X ) mThis->m_pLog->Log( "curlRequestsManagerS", mThis, X )
|
|
#define SLOG_ERROR( X ) mThis->m_pLog->Log( "curlRequestsManagerS", mThis, _LOG_ERROR, X )
|
|
|
|
#ifdef _WIN32
|
|
#define SHORT_SLEEP Sleep(100)
|
|
#else
|
|
#define SHORT_SLEEP usleep(100000)
|
|
#endif
|
|
|
|
curlRequestsManager* curlRequestsManager::_instance = 0;
|
|
|
|
/**
|
|
* Returns the single instance of the object.
|
|
*/
|
|
curlRequestsManager* curlRequestsManager::getInstance() {
|
|
// check if the instance has been created yet
|
|
if (0 == _instance) {
|
|
// if not, then create it
|
|
_instance = new curlRequestsManager;
|
|
}
|
|
// return the single instance
|
|
return _instance;
|
|
};
|
|
|
|
|
|
#ifdef __linux__
|
|
void *curlRequestsManager::m_fReaperEntryPoint(void *ptr){
|
|
#else
|
|
DWORD WINAPI curlRequestsManager::m_fReaperEntryPoint( LPVOID ptr){
|
|
#endif
|
|
curlRequestsManager *mThis;
|
|
CURLMsg *mMsg; /* for picking up messages with the transfer status */
|
|
int nMsgsLeft; /* how many messages are left */
|
|
struct timeval mTimeOut;
|
|
int RetVal; /* select() return code */
|
|
CURLMcode mRetCode; /* curl_multi_fdset() return code */
|
|
fd_set mReadFD;
|
|
fd_set mWriteFD;
|
|
fd_set mExcepFD;
|
|
int mMaxFD = -1;
|
|
long mCURLTimeOut;
|
|
int mStillRunning;
|
|
|
|
mThis = (curlRequestsManager *)ptr;
|
|
SLOG("Starting Finished Requests Reaper");
|
|
mThis->m_bThreadStatus = TH_WORKING;
|
|
while (mThis->m_bThreadStatus < TH_ENDED ){
|
|
|
|
/* we start some action by calling perform right away */
|
|
curl_multi_perform( mThis->m_pCURLM, &mStillRunning );
|
|
|
|
do{
|
|
|
|
curl_multi_timeout( mThis->m_pCURLM, &mCURLTimeOut );
|
|
|
|
if ( mCURLTimeOut < 0 ) mCURLTimeOut = 1000;
|
|
|
|
mTimeOut.tv_sec = mCURLTimeOut / 1000;
|
|
mTimeOut.tv_usec = (mCURLTimeOut % 1000) * 1000;
|
|
|
|
FD_ZERO(&mReadFD);
|
|
FD_ZERO(&mWriteFD);
|
|
FD_ZERO(&mExcepFD);
|
|
|
|
/* get file descriptors from the transfers */
|
|
mRetCode = curl_multi_fdset(mThis->m_pCURLM, &mReadFD, &mWriteFD, &mExcepFD, &mMaxFD);
|
|
|
|
if( mMaxFD == -1 ){
|
|
SHORT_SLEEP;
|
|
RetVal = 0;
|
|
} else {
|
|
RetVal = select( mMaxFD + 1, &mReadFD, &mWriteFD, &mExcepFD, &mTimeOut );
|
|
};
|
|
|
|
switch (RetVal) {
|
|
case -1:
|
|
/* select error */
|
|
break;
|
|
case 0:
|
|
default:
|
|
/* timeout or readable/writable sockets */
|
|
curl_multi_perform( mThis->m_pCURLM, &mStillRunning );
|
|
break;
|
|
};
|
|
|
|
}while( mStillRunning ); /* if there are still transfers, loop! */
|
|
|
|
/* call curl_multi_perform or curl_multi_socket_action first, then loop
|
|
through and check if there are any transfers that have completed */
|
|
|
|
/* See how the transfers went */
|
|
while( ( mMsg = curl_multi_info_read( mThis->m_pCURLM, &nMsgsLeft ) ) ){
|
|
if( mMsg->msg == CURLMSG_DONE ){
|
|
/* Find out which handle this message is about */
|
|
for (RequestPool::iterator index = mThis->m_vRequestPool.begin(); index < mThis->m_vRequestPool.end(); index++){
|
|
if ((*index)->getEH() == mMsg->easy_handle){
|
|
mRetCode = curl_multi_remove_handle(mThis->m_pCURLM, (*index)->getEH());
|
|
(*index)->issue();
|
|
delete( (*index) );
|
|
mThis->m_vRequestPool.erase( index );
|
|
break;
|
|
};
|
|
};
|
|
};
|
|
};
|
|
};
|
|
SLOG("Ending Finished Requests Reaper");
|
|
#ifdef __linux__
|
|
pthread_exit(NULL);
|
|
return NULL;
|
|
#else
|
|
return 0;
|
|
#endif
|
|
};
|
|
|
|
|
|
#ifdef __linux__
|
|
void *curlRequestsManager::m_fRequestEntryPoint(void *ptr){
|
|
#else
|
|
DWORD WINAPI curlRequestsManager::m_fRequestEntryPoint(LPVOID ptr){
|
|
#endif
|
|
curlRequest *mThis;
|
|
mThis = (curlRequest *)ptr;
|
|
//SLOG("Request Thread Entry Point Enter.");
|
|
//mThis->issue();
|
|
//SLOG("Request Thread Entry Point Exit.");
|
|
#ifdef __linux__
|
|
pthread_exit(NULL);
|
|
return NULL;
|
|
#else
|
|
return 0;
|
|
#endif
|
|
};
|
|
|
|
bool curlRequestsManager::get(curlRequest *RC){
|
|
if (RC != NULL){
|
|
CURLMcode RetCode;
|
|
LOG("New Request");
|
|
m_vRequestPool.push_back(RC);
|
|
RetCode = curl_multi_add_handle( m_pCURLM, RC->getEH());
|
|
/*
|
|
#ifdef __linux__
|
|
int RetVal = pthread_create(&m_sThreadHandler, NULL, m_fThreadEntryPoint, (void *) this);
|
|
if (RetVal == -1) {
|
|
LOG_ERROR("Thread Creation Faled.");
|
|
return false;
|
|
};
|
|
#else
|
|
DWORD tThreadID;
|
|
HANDLE tThreadHandle = CreateThread(
|
|
NULL,
|
|
0,
|
|
(LPTHREAD_START_ROUTINE)m_fRequestEntryPoint,
|
|
(LPVOID)RC,
|
|
0,
|
|
&tThreadID);
|
|
if (tThreadHandle == NULL) {
|
|
LOG_ERROR("Thread Creation Faled.");
|
|
return false;
|
|
};
|
|
LOG("Thread to be started.");
|
|
m_vRequestThreadPool.push_back(tThreadHandle);
|
|
#endif
|
|
*/
|
|
return true;
|
|
};
|
|
return false;
|
|
};
|
|
|
|
bool curlRequestsManager::get(CSTR URL, CSTR contentType ){
|
|
curlRequest *tRC;
|
|
//LOG("Get URL.");
|
|
tRC = new curlRequest(URL, contentType );
|
|
if (tRC != NULL){
|
|
return get(tRC);
|
|
};
|
|
return false;
|
|
};
|
|
|
|
#undef LOG
|
|
#undef LOG_ERROR
|
|
#undef SLOG
|
|
#undef SLOG_ERROR
|