2012-08-16 2 views
3

que je fais avec succès un http appel POST en utilisant le code suivant:libcURL WriteCallback (Async?) - C++

std::string curlString; 
CURL* pCurl = curl_easy_init(); 

if(!pCurl) 
    return NULL; 

string outgoingUrl = Url; 
string postFields = fields; 

curl_easy_setopt(pCurl, CURLOPT_TIMEOUT, 0); 

curl_easy_setopt(pCurl, CURLOPT_URL, outgoingUrl.c_str()); 
curl_easy_setopt(pCurl, CURLOPT_POST, 1); 

curl_easy_setopt(pCurl, CURLOPT_POSTFIELDS, postFields.c_str()); 
curl_easy_setopt(pCurl, CURLOPT_POSTFIELDSIZE, (long)postFields.size()); 

curl_easy_setopt(pCurl, CURLOPT_WRITEFUNCTION, CurlWriteCallback); 
curl_easy_setopt(pCurl, CURLOPT_WRITEDATA, &curlString); 

curl_easy_perform(pCurl); 

curl_easy_cleanup(pCurl); 

Le rappel d'écriture a le prototype suivant:

size_t CurlWriteCallback(char* a_ptr, size_t a_size, size_t a_nmemb, void* a_userp); 

est-il un moyen faire cela de manière asynchrone? Actuellement, il attend que le rappel se termine avant que curl_easy_perform ne renvoie. Cette méthode de blocage ne fonctionnera pas pour un serveur avec plusieurs utilisateurs.

+0

Êtes-vous venu pour résoudre ce problème? Pouvez-vous ajouter votre solution à la question? –

Répondre

4

De la documentation libcurl easy:

Quand tout est configuré, vous libCurl pour effectuer le transfert à l'aide curl_easy_perform (3). Il fera alors l'opération entière et ne retournera pas jusqu'à ce que ce soit fait (avec ou sans succès).

Des libcurl multi interface docs, l'une des caractéristiques, par opposition à l'interface « facile »:

  1. Activer plusieurs transferts simultanés dans le même fil sans qu'il soit compliqué pour l'application.

Il semble que vous souhaitiez utiliser l'approche "multi".

+2

Un exemple d'utilisation de l'interface multi en C++ peut être trouvé à http://www.godpatterns.com/2011/09/asynchronous-non-blocking-curl-multi.html –