- Attempt to use libcurl in a thread safe manner

This commit is contained in:
Mark Vejvoda
2010-06-23 14:49:20 +00:00
parent e0b9089eeb
commit 87b4706940
14 changed files with 220 additions and 158 deletions

View File

@@ -33,7 +33,7 @@ BaseThread::BaseThread() : Thread() {
BaseThread::~BaseThread() {
SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s Line: %d] uniqueID [%s]\n",__FILE__,__FUNCTION__,__LINE__,uniqueID.c_str());
shutdownAndWait();
SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s Line: %d] uniqueID [%s]\n",__FILE__,__FUNCTION__,__LINE__,uniqueID.c_str());
SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s Line: %d] uniqueID [%s] END\n",__FILE__,__FUNCTION__,__LINE__,uniqueID.c_str());
}
void BaseThread::signalQuit() {
@@ -98,20 +98,20 @@ void BaseThread::shutdownAndWait(BaseThread *pThread) {
SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s Line: %d] uniqueID [%s]\n",__FILE__,__FUNCTION__,__LINE__,uniqueID.c_str());
pThread->signalQuit();
sleep(0);
//sleep(0);
for( time_t elapsed = time(NULL); difftime(time(NULL),elapsed) <= 7; ) {
for( time_t elapsed = time(NULL); difftime(time(NULL),elapsed) <= 5; ) {
if(pThread->getRunningStatus() == false) {
break;
}
sleep(0);
sleep(1);
//SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s Line: %d]\n",__FILE__,__FUNCTION__,__LINE__);
}
//sleep(0);
SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s Line: %d] uniqueID [%s]\n",__FILE__,__FUNCTION__,__LINE__,uniqueID.c_str());
}
//sleep(0);
SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s Line: %d] uniqueID [%s]\n",__FILE__,__FUNCTION__,__LINE__,uniqueID.c_str());
SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s Line: %d] uniqueID [%s] END\n",__FILE__,__FUNCTION__,__LINE__,uniqueID.c_str());
}
void BaseThread::shutdownAndWait() {

View File

@@ -145,7 +145,7 @@ void SimpleTaskThread::execute() {
}
SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s Line: %d] uniqueID [%s]\n",__FILE__,__FUNCTION__,__LINE__,this->getUniqueID().c_str());
setRunningStatus(false);
SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s Line: %d] uniqueID [%s]\n",__FILE__,__FUNCTION__,__LINE__,this->getUniqueID().c_str());
SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s Line: %d] uniqueID [%s] END\n",__FILE__,__FUNCTION__,__LINE__,this->getUniqueID().c_str());
}
void SimpleTaskThread::setTaskSignalled(bool value) {

View File

@@ -708,6 +708,7 @@ float Socket::getThreadedPingMS(std::string host) {
if(pingThread == NULL) {
lastThreadedPing = 0;
pingThread = new SimpleTaskThread(this,0,50);
pingThread->setUniqueID(__FILE__ + "_" + __FUNCTION);
pingThread->start();
}
@@ -1216,6 +1217,7 @@ void ClientSocket::startBroadCastClientThread(DiscoveredServersInterface *cb) {
ClientSocket::stopBroadCastClientThread();
broadCastClientThread = new BroadCastClientSocketThread(cb);
broadCastClientThread->setUniqueID(string(__FILE__) + string("_") + string(__FUNCTION__));
broadCastClientThread->start();
SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s Line: %d]\n",__FILE__,__FUNCTION__,__LINE__);
@@ -1495,6 +1497,7 @@ void ServerSocket::startBroadCastThread() {
stopBroadCastThread();
broadCastThread = new BroadCastSocketThread();
broadCastThread->setUniqueID(string(__FILE__) + string("_") + string(__FUNCTION__));
broadCastThread->start();
SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s Line: %d]\n",__FILE__,__FUNCTION__,__LINE__);

View File

@@ -69,11 +69,13 @@ size_t SystemFlags::httpWriteMemoryCallback(void *ptr, size_t size, size_t nmemb
return realsize;
}
std::string SystemFlags::escapeURL(std::string URL)
{
std::string SystemFlags::escapeURL(std::string URL, CURL *handle) {
string result = URL;
char *escaped=curl_easy_escape(SystemFlags::curl_handle,URL.c_str(),0);
if(handle == NULL) {
handle = SystemFlags::curl_handle;
}
char *escaped=curl_easy_escape(handle,URL.c_str(),0);
if(escaped != NULL) {
result = escaped;
curl_free(escaped);
@@ -81,33 +83,56 @@ std::string SystemFlags::escapeURL(std::string URL)
return result;
}
std::string SystemFlags::getHTTP(std::string URL) {
curl_easy_setopt(SystemFlags::curl_handle, CURLOPT_URL, URL.c_str());
std::string SystemFlags::getHTTP(std::string URL,CURL *handle) {
if(handle == NULL) {
handle = SystemFlags::curl_handle;
}
SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line %d]\n",__FILE__,__FUNCTION__,__LINE__);
curl_easy_setopt(handle, CURLOPT_URL, URL.c_str());
/* send all data to this function */
curl_easy_setopt(SystemFlags::curl_handle, CURLOPT_WRITEFUNCTION, SystemFlags::httpWriteMemoryCallback);
curl_easy_setopt(handle, CURLOPT_WRITEFUNCTION, SystemFlags::httpWriteMemoryCallback);
struct SystemFlags::httpMemoryStruct chunk;
chunk.memory=NULL; /* we expect realloc(NULL, size) to work */
chunk.size = 0; /* no data at this point */
/* we pass our 'chunk' struct to the callback function */
curl_easy_setopt(SystemFlags::curl_handle, CURLOPT_WRITEDATA, (void *)&chunk);
curl_easy_setopt(handle, CURLOPT_WRITEDATA, (void *)&chunk);
/* some servers don't like requests that are made without a user-agent
field, so we provide one */
curl_easy_setopt(SystemFlags::curl_handle, CURLOPT_USERAGENT, "mega-glest-agent/1.0");
curl_easy_setopt(handle, CURLOPT_USERAGENT, "mega-glest-agent/1.0");
SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line %d] handle = %p\n",__FILE__,__FUNCTION__,__LINE__,handle);
//curl_easy_setopt(handle, CURLOPT_VERBOSE, 1);
SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line %d]\n",__FILE__,__FUNCTION__,__LINE__);
/* get contents from the URL */
curl_easy_perform(SystemFlags::curl_handle);
curl_easy_perform(handle);
SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line %d]\n",__FILE__,__FUNCTION__,__LINE__);
std::string serverResponse = (chunk.memory != NULL ? chunk.memory : "");
if(chunk.memory) {
free(chunk.memory);
}
SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line %d] serverResponse [%s]\n",__FILE__,__FUNCTION__,__LINE__,serverResponse.c_str());
return serverResponse;
}
CURL *SystemFlags::initHTTP() {
curl_global_init(CURL_GLOBAL_ALL);
CURL *handle = curl_easy_init();
return handle;
}
void SystemFlags::init() {
if(SystemFlags::debugLogFileList.size() == 0) {
SystemFlags::debugLogFileList[SystemFlags::debugSystem] = SystemFlags::SystemFlagsType(SystemFlags::debugSystem);
@@ -117,9 +142,15 @@ void SystemFlags::init() {
SystemFlags::debugLogFileList[SystemFlags::debugUnitCommands] = SystemFlags::SystemFlagsType(SystemFlags::debugUnitCommands);
}
SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line %d]\n",__FILE__,__FUNCTION__,__LINE__);
if(curl_handle == NULL) {
curl_global_init(CURL_GLOBAL_ALL);
curl_handle = curl_easy_init();
SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line %d]\n",__FILE__,__FUNCTION__,__LINE__);
curl_handle = SystemFlags::initHTTP();
SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line %d] curl_handle = %p\n",__FILE__,__FUNCTION__,__LINE__,curl_handle);
}
}
@@ -141,13 +172,21 @@ inline bool acquire_file_lock(int hnd)
SystemFlags::SystemFlags() {
}
void SystemFlags::cleanupHTTP(CURL **handle) {
if(handle != NULL && *handle != NULL) {
curl_easy_cleanup(*handle);
*handle = NULL;
curl_global_cleanup();
}
}
SystemFlags::~SystemFlags() {
SystemFlags::Close();
if(curl_handle != NULL) {
curl_easy_cleanup(curl_handle);
SystemFlags::cleanupHTTP(&curl_handle);
curl_handle = NULL;
curl_global_cleanup();
}
}