cURL / Mailing Lists / curl-library / Single Mail

curl-library

Re: passing state in write callbacks

From: Kamil Dudka <kdudka_at_redhat.com>
Date: Thu, 15 Sep 2011 23:28:19 +0200

On Thursday 15 September 2011 23:07:25 Alex Loukissas wrote:
> I'm experiencing a difficulty using write callbacks and I'd like to
> ask how/whether is possible to do what I have to do. I outline the
> scenario briefly below:
>
> * The callback I want to use is a public member function of class A.
> The reasoning behind this is that I'd like to update some state in
> this class (e.g. a member variable that holds bytes read so far), each
> time the callback is called.

If the function is non-static, you cannot call it directly from libcurl.
There is simply no 'this' pointer to be passed to the method. I suggest to
create a static to non-static wrapper -- set the pointer to the instance as
the user pointer in libcurl. Then you can static_cast it in the static
wrapper to a pointer to the class and call the method using the pointer.

> * The code that performs the curl read operations is in member
> function of another class (class B), say B::readData( ).

Basically the same problem as above.

> * The way I'm implementing the above now (and is not working) is to
> boost::bind the callback to the instance of A, pass the
> boost::function object that bind returns to B::readData( ), and there
> do the appropriate reinterpret_cast-ing to produce a C-style function
> pointer that libcurl expects.

If you typed-cast a pointer to member to pointer to a static function, you
would most likely cause a crash. Code pointer is still code pointer, but
the calling convention differ. If you call a member function, you need the
value of 'this' on the stack.

> My question is whether libcurl supports/will support a different kind
> of signature for the callback function, one with an extra (void *)
> argument, where we can pass in some state (and thus make it a truer
> sort-of-speak callback). Right now, there's no way to pass any state
> to the callback from what I understand. If there were such an extra
> argument, we could pass a pointer to an A object to the callback and
> use it appropriately there.

There is such a way. See the options CURLOPT_WRITEDATA and CURLOPT_READDATA.

Kamil
-------------------------------------------------------------------
List admin: http://cool.haxx.se/list/listinfo/curl-library
Etiquette: http://curl.haxx.se/mail/etiquette.html
Received on 2011-09-15