cURL / Mailing Lists / curl-library / Single Mail

curl-library

Re: red:call back funcitons

From: Joe N. <jnardone_at_gmail.com>
Date: Thu, 03 Jul 2008 06:55:17 -0400

> use it for object specific.I dont want to have them static. as they are
> not thread safe.Is it not possible. I need this kind of scenario to make
> it some thing like thread safe so that parallel
>
> connections, dont have a chance of breaking it. please advice on this.
>

You can do this no problem. You set up a static callback function
(CURLOPT_READFUNCTION/CURLOPT_WRITEFUNCTION), and as the
CURLOPT_READDATA (or CURLOPT_WRITEDATA) you pass in a reference to your
specific class instance. This pointer gets passed into your callback,
which should take this data (the last parameter--it's just a void *
after all), cast it back to your class, and then call a class method to
handle the incoming data.

Pseudocode (not tested, typed freehand into this email):

static size_t WriteCallbackWrapper( void *ptr, size_t size, size_t
nmemb, void *stream)
{
    MyClass *c = (MyClass)stream;
    return c->ProcessData(ptr, size, nmemb); /* ProcessData(void *ptr,
size_t size, size_t nmemb) */
}

...
MyClass::ProcessData(void *ptr, size_t size, size_t nmemb)
{
    .....
}
...

MyClass::CallsCurl()
{
...
    curl_easy_setopt(curlhandle, CURLOPT_WRITEFUNCTION,
WriteCallbackWrapper);
    curl_easy_setopt(curlhandle, CURLOPT_WRITEDATA, this); // Pass in a
reference to our class instance
...
}

j
Received on 2008-07-03