Hanging in libcurl

Discuss the development of new homebrew software, tools and libraries.

Moderators: cheriff, TyRaNiD

Post Reply
AnonymousTipster
Posts: 197
Joined: Fri Jul 01, 2005 2:50 am

Hanging in libcurl

Post by AnonymousTipster »

I'm currently using libcurl to do some form submitting to a .php script periodically. This is done every minute in the game to keep the serverlist up to date. Usually this works fine, and there's a slight pause (0.5seconds) when uploading the data.
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);
            }
I've also tried adding the following curl_opts to no avail:

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);
I'm assuming that libcurl uses blocking sockets, might this require a source change in the library?
Post Reply