cURL / Mailing Lists / curl-library / Single Mail

curl-library

Persistant connection with a memory problem

From: Michael Hecker <Hecker.Michael_at_gmx.de>
Date: Sun, 25 May 2008 09:55:50 +0200

Hello,
I think I followed your example (persistant.c) and wrote a class, which
connects to a server and holds the connection.

 My main problem is that private bytes (virtual memory) and working set
( physical memory) are continously getting bigger and bigger.
What can I do about that?

Additionally, I'm getting something like 100.000 'page faults' per
seconds. What can I do about that?

I'm working on Windows XP with VC++.

Thanks,
Michael

The main parts:
Communicator::Communicator(CWnd* pParent /*=NULL*/)
{
    ParentDlg = pParent;

    //
    // Für cURL
    //
        curl_global_init(CURL_GLOBAL_ALL);
        curl_handle = curl_easy_init();

        // Rahmeninformationen der Kommunikation
// curl_easy_setopt(curl_handle, CURLOPT_VERBOSE, 1);
// curl_easy_setopt(curl_handle, CURLOPT_DEBUGFUNCTION,
Communicator::my_trace);
// curl_easy_setopt(curl_handle, CURLOPT_DEBUGDATA, this);

        // Content encoding
            curl_easy_setopt(curl_handle, CURLOPT_ENCODING, "gzip,deflate");

        // Weiterleitungen folgen
            curl_easy_setopt(curl_handle, CURLOPT_FOLLOWLOCATION, 1);

        // SSL | Host verifizieren
            curl_easy_setopt(curl_handle, CURLOPT_SSL_VERIFYPEER, 1);

        // SSL | Pfad der pem-Datei
            curl_easy_setopt(curl_handle, CURLOPT_CAINFO,
"C:\\Project\\libcurl\\VisualStudio\\MyApplication\\Release\\cacert.pem");

        // Cookie-Engine starten
            curl_easy_setopt(curl_handle, CURLOPT_COOKIEFILE, "");

        // Alte Cookies löschen
            curl_easy_setopt(curl_handle, CURLOPT_COOKIELIST, "ALL");

        // Individuelle Header Informationen
            slist=NULL;
            slist = curl_slist_append(slist, "Accept:
text/xml,application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5");
            slist = curl_slist_append(slist, "Accept-Language:
de-de,de;q=0.8,en-us;q=0.5,en;q=0.3");
            slist = curl_slist_append(slist, "Accept-Charset:
ISO-8859-1,utf-8;q=0.7,*;q=0.7");
            slist = curl_slist_append(slist, "Keep-Alive: 300");
            slist = curl_slist_append(slist, "Connection: keep-alive");

            curl_easy_setopt(curl_handle, CURLOPT_HTTPHEADER, slist);
}

bool Communicator::communicate(CString url, CString POST_data, CString
&quellcode, CString location)
{
    // Variablen
    bool rueckgabewert=true;
    chunk_mem chunk1;

    // Wenn POST, dann hier Daten an cURL übergeben
    if(POST_data.GetLength()>0)
        curl_easy_setopt(curl_handle, CURLOPT_POSTFIELDS, POST_data);

    // Klasse soll alle Daten an WriteMemoryCallback senden
    curl_easy_setopt(curl_handle, CURLOPT_WRITEFUNCTION,
myWriteMemoryCallback);

    // Über gabe des "chunk" structs an die Callback-Funktion
    curl_easy_setopt(curl_handle, CURLOPT_WRITEDATA, (void *)&chunk1);
   
    // Abzufragende URL
    curl_easy_setopt(curl_handle, CURLOPT_URL, url);
       
    // Abfrage ausführen
    CURLcode result = curl_easy_perform(curl_handle);
   
    //
    // Response Code ermitteln
    //
        long responseCode;
        CURLcode r = curl_easy_getinfo(curl_handle,
CURLINFO_RESPONSE_CODE, &responseCode);
    //
    // Quellcode für die Rückgabe speichern
    //
        if( result==CURLE_OK && responseCode==200 )
            quellcode=chunk1.memory;
        else
        {
            rueckgabewert=false;
            quellcode.Format("COMMUNICATION ERROR:\r\nMeldung:
%s",curl_easy_strerror(result));
        }

        if(rueckgabewert)
        {
            if( quellcode.Find("Service Unavailable")
!=-1 ||
                quellcode.Find("The requested URL could not be
retrieved") !=-1 )
            {
                quellcode = "COMMUNICATION ERROR:\r\nMeldung:
Bekannter Fehler!";
                rueckgabewert = false;
            }
        }
    //
    ////

    // Wenn POST dann hier wieder auf GET wechseln
    if(POST_data.GetLength()>0)
    curl_easy_setopt(curl_handle, CURLOPT_HTTPGET, 1);

    // bwin_chunk aufräumen
    if(chunk1.memory)
    free(chunk1.memory);

    return rueckgabewert;
}

Communicator::~Communicator()
{
    curl_easy_setopt(curl_handle, CURLOPT_HTTPHEADER, NULL);
    curl_slist_free_all(slist);

    curl_easy_cleanup(curl_handle);
}
Received on 2008-05-25