Hello,
I wrote a little class for the communication via curl.
If I call
curl_handle = curl_easy_init();
and
curl_easy_cleanup(curl_handle);
for every single request, everything works fine.
But I recognized that I can perform a large number of requests much quicker, if I call curl_handle = curl_easy_init(); only at the startup of my application and
curl_easy_cleanup(curl_handle); only in the end.
But after a few minutes the application crashes. I suppose, because something went wrong in a request and curl_easy_init and curl_easy_cleanup were not called for the next request.
What can I do?
Thanks,
Michael
Here is my Class:
CCommunicator::CCommunicator()
{
repeat_requests = 4;
agent_name = "Mozilla/5.0 (Windows; U; Windows NT 5.1; de; rv:1.8.1.7) Gecko/20070914 Firefox/2.0.0.7";
// Initiierung der Curl-Session
curl_handle = curl_easy_init();
if (curl_handle)
{
cookie_engine_started = false;
// User-Agent bestimmen
curl_easy_setopt(curl_handle, CURLOPT_USERAGENT, agent_name);
// Timeout
curl_easy_setopt(curl_handle, CURLOPT_CONNECTTIMEOUT, (int) 5);
curl_easy_setopt(curl_handle, CURLOPT_TIMEOUT, (int) 5);
}
}
CCommunicator::~CCommunicator()
{
// Aufräumen
curl_easy_cleanup(curl_handle);
}
//
// Curl: Reallocation of the memory
//
void *myrealloc(void *ptr, size_t size)
{
if(ptr) return realloc(ptr, size);
else return malloc(size);
}
//
// Curl: Callback zum Speichern der Daten
//
size_t WriteMemoryCallback(void *ptr, size_t size, size_t nmemb, void *data)
{
size_t realsize = size * nmemb;
struct CCommunicator_MemoryStruct *mem = (struct CCommunicator_MemoryStruct *)data;
mem->memory = (char *)myrealloc(mem->memory, mem->size + realsize + 1);
if (mem->memory)
{
memcpy(&(mem->memory[mem->size]), ptr, realsize);
mem->size += realsize;
mem->memory[mem->size] = 0;
}
return realsize;
}
//
// Curl: Send-Request
//
bool CCommunicator::execute(CString url, CString &quellcode, CString POST)
{
quellcode = "";
struct CCommunicator_MemoryStruct chunk;
chunk.memory = NULL;
chunk.size = 0;
bool rueckgabe = true;
if (curl_handle)
{
// Optionen der Session
//
// Einstellungen für https
if(url.Find("https")!=-1)
{
curl_easy_setopt(curl_handle, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_easy_setopt(curl_handle, CURLOPT_SSL_VERIFYHOST, 2);
}
// URL, die abgefragt werden soll
curl_easy_setopt(curl_handle, CURLOPT_URL, url);
// Klasse soll alle Daten an WriteMemoryCallback senden
curl_easy_setopt(curl_handle, CURLOPT_WRITEFUNCTION, WriteMemoryCallback);
// Über gabe des "chunk" structs an die Callback-Funktion
curl_easy_setopt(curl_handle, CURLOPT_WRITEDATA, (void *)&chunk);
// Cookies
if(cookie_engine_started)
curl_easy_setopt(curl_handle, CURLOPT_COOKIELIST, "ALL"); // Alte Cookies löschen
if(url.Find("betfair")!=-1 && url.Find("LoadMarketDataAction")!=-1)
{
// Cookie-Engine starten
if(!cookie_engine_started)
{
curl_easy_setopt(curl_handle, CURLOPT_COOKIEFILE, "");
curl_easy_setopt(curl_handle, CURLOPT_COOKIELIST, "ALL");
cookie_engine_started = true;
}
char nline[256];
// Cookies setzen
//
_snprintf(nline, 256,
"Set-Cookie: betexPtk=betexLocale=en~betexRegion=GBR~betexCurrency=EUR~betexTimeZone=Europe/Berlin;"
"expires=Sun, 17-Jan-2038 19:14:07 GMT; path=/; domain=.betfair.com");
curl_easy_setopt(curl_handle, CURLOPT_COOKIELIST, nline);
//
////
}
// POST - Daten
if(POST!="")
curl_easy_setopt(curl_handle, CURLOPT_POSTFIELDS, POST);
//
////
//
// Anfrage ausführen
//
result = curl_easy_perform(curl_handle);
if (result != CURLE_OK) {
fprintf(stderr, "Curl perform failed: %s\n", curl_easy_strerror(result));
quellcode.Format("Anfrage wurde nicht beantwortet.\r\nBitte überprüfen Sie die Internetverbindung!\r\n\r\nMeldung: %s",curl_easy_strerror(result));
rueckgabe = false;
}
//
// TMP: Ausgabe
//
if(quellcode=="" && rueckgabe)
quellcode = chunk.memory;
} // Schließt: if(curl_handle)
else
{
quellcode = "curl_handle konnte nicht initiiert werden!\r\nBitte kontaktieren Sie den Support.";
rueckgabe = false;
}
// 1. Ausnahmefehler
if(quellcode.Find("The requested URL could not be retrieved")!=-1)
rueckgabe = false;
// 2. Ausnahmefehler
if(quellcode.Find("Service Unavailable")!=-1)
rueckgabe = false;
//
// Aufräumen
//
if(chunk.memory)
free(chunk.memory);
return rueckgabe;
}
bool CCommunicator::communicate(CString url, CString &quellcode, CString POST)
{
Global::cs_communicator.Lock();
bool res = execute(url,quellcode,POST);
Global::cs_communicator.Unlock();
return res;
}
--
Psssst! Schon vom neuen GMX MultiMessenger gehört?
Der kanns mit allen: http://www.gmx.net/de/go/multimessenger
_______________________________________________
cURLpp mailing list
cURLpp_at_rrette.com
http://www.rrette.com/mailman/listinfo/curlpp
Received on 2007-10-25