Sometimes however, it hangs indefinitely. This happens when you go out of reach of your router. Because the libcurl is in the same thread as the game, the game effectively freezes. I would move libcurl to another thread, but that thread would still end up hanging whenever you go out of range, so wouldn't update again.
This is the code i'm using:
Code: Select all
struct curl_httppost *formpost=NULL;
struct curl_httppost *lastptr=NULL;
curl_slist* responseHeaders = NULL ;
curl = curl_easy_init();
if(curl)
{
//add items to the form
curl_formadd(&formpost, &lastptr,CURLFORM_COPYNAME, "Name",CURLFORM_COPYCONTENTS, myServerName,CURLFORM_CONTENTSLENGTH, strlen(myServerName), CURLFORM_END);
curl_formadd(&formpost, &lastptr,CURLFORM_COPYNAME, "Location",CURLFORM_COPYCONTENTS, myServerLocation,CURLFORM_CONTENTSLENGTH, strlen(myServerLocation), CURLFORM_END);
curl_formadd(&formpost, &lastptr,CURLFORM_COPYNAME, "NumberPlayers",CURLFORM_COPYCONTENTS, myServerNumberPlayersString,CURLFORM_CONTENTSLENGTH, strlen(myServerNumberPlayersString), CURLFORM_END);
curl_formadd(&formpost, &lastptr,CURLFORM_COPYNAME, "MaxPlayers",CURLFORM_COPYCONTENTS,myServerMaxPlayersString,CURLFORM_CONTENTSLENGTH, strlen(myServerMaxPlayersString), CURLFORM_END);
//set url
curl_easy_setopt(curl, CURLOPT_URL, "http://www.mywebspace.com/script.php");
curl_easy_setopt(curl, CURLOPT_TIMEOUT, 2);
curl_easy_setopt( curl , CURLOPT_HTTPPOST , formpost);
//tell curl to actually get the page etc etc
res = curl_easy_perform(curl);
// always cleanup
curl_easy_cleanup(curl);
curl_formfree(formpost);
//curl_slist_free_all (responseHeaders);
}
Code: Select all
curl_easy_setopt(curl, CURLOPT_CONNECTTIMEOUT, 2);
curl_easy_setopt(curl, CURLOPT_FTP_RESPONSE_TIMEOUT, 2);
curl_easy_setopt(curl, CURLOPT_TCP_NODELAY, 1);
curl_easy_setopt(curl, CURLOPT_NOSIGNAL, 1);
curl_easy_setopt(curl, CURLOPT_LOW_SPEED_TIME, 2);
curl_easy_setopt(curl, CURLOPT_LOW_SPEED_LIMIT, 2);
curl_easy_setopt(curl, CURLOPT_FRESH_CONNECT, 1);
curl_easy_setopt(curl, CURLOPT_FORBID_REUSE, 1);