curl-library
How to stop a lengthy operation
Date: Tue, 6 Jan 2004 09:35:52 -0800
Hi,
I am using curl to connect to an online quote server in a C++ (VC++ 6)
Windows application. Once the http connection is established, the server
keeps sending data back. The connection code runs in its own thread and I
use a callback to receive the data - responseCallback which is a static
member of CSession.
What is the "clean" way of disconnecting from the server during this lengthy
operation, if for example I need to restart the thread (in case some
parameters have changed such as the list of symbols for which I need quotes)
or I need to exit the application.
Here is my code. The way I disconnect is I test the _run member variable,
which if false indicates that the thread must be exited and the connection
closed, and in this case I set the return from the callback to 0, which
triggers a curl error in curl_easy_perform, but this doesn't seem too clean
to me. Moreover, after I disconnect once using this method and then I
restart the thread and create a new connection (without exiting the
process), I don't receive any more data, even if all the curl calls return
CURLE_OK. If I restart the application though, then I can, again, open a
connection (and only one) and get data. What could be the reason for this?
typedef std::basic_string< TCHAR > t_string;
size_t CSession::responseCallback(void *ptr, size_t size, size_t nmemb, void
*data)
{
CSession* pSession = static_cast< CSession* >( data );
if( pSession -> _run )
{
int realsize = size * nmemb;
// the processing has to be done one character at a time
for( int n = 0; n < size * nmemb; n++ )
pSession -> processData( ((char*)ptr)[ n ] );
return realsize;
}
else
// return a different value than realsize to signal
// that we want to terminate (it will trigger an error and we'll exit)
return 0;
}
void CSession::execute( const ExecuteContext& context ) throw
(CurlErrorException)
{
CURL *curl;
CURLcode res;
curl = curl_easy_init();
if (!curl)
throw CurlErrorException( _T( "Curl initialization error" ), -1 );
// make request constructs the quote request for the server
const t_string request( makeRequest( context ) );
if( ( res = curl_easy_setopt(curl, CURLOPT_URL, request.c_str() ) ) !=
CURLE_OK )
throw CurlErrorException( _T( "Curl error in CURLOPT_URL: " ), res );
if( ( res = curl_easy_setopt(curl, CURLOPT_CAINFO, pCACertFile) ) !=
CURLE_OK )
throw CurlErrorException( _T( "Curl error in CURLOPT_CAINFO: " ), res );
if( ( res = curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION,
responseCallback ) ) != CURLE_OK )
throw CurlErrorException( _T( "Curl error in CURLOPT_WRITEFUNCTION: " ),
res );
if( ( res = curl_easy_setopt(curl, CURLOPT_FILE, (void *)this) ) !=
CURLE_OK )
throw CurlErrorException( _T( "Curl error in CURLOPT_FILE: " ), res );
res = curl_easy_perform( curl );
if( res != CURLE_WRITE_ERROR && res != CURLE_OK )
throw CurlErrorException( _T( "Curl error in perform: " ), res );
curl_easy_cleanup(curl);
}
Thanks,
Adrian
-------------------------------------------------------
This SF.net email is sponsored by: IBM Linux Tutorials.
Become an expert in LINUX or just sharpen your skills. Sign up for IBM's
Free Linux Tutorials. Learn everything from the bash shell to sys admin.
Click now! http://ads.osdn.com/?ad_id=1278&alloc_id=3371&op=click
Received on 2004-01-06