cURL / Mailing Lists / curl-library / Single Mail

curl-library

Re: is curl_multi_socket_all() deprecated?

From: Daniel Stenberg <daniel_at_haxx.se>
Date: Thu, 23 Oct 2008 11:29:14 +0200 (CEST)

On Thu, 23 Oct 2008, Ragnar Lonn wrote:

> I may be intellectually challenged, or something, but it is still a bit
> unclear to me what actually happens initially. How can I be sure that I will
> receive a call to the timeout callback before I have to use the timeout
> value in e.g. my epoll_wait() call? Isn't it better to call
> curl_multi_timeout() to retrieve the timeout value every time just before I
> wait on the sockets?

No.

The reason for this is that the best way for you to work with the
curl_multi_socket() API is to use an event-based underlying technology that
tells you when there's something going on in what of the sockets you
"superwise". I'd recommend libevent or libev for this on *nix systems. This
way, you'd then get called by the socket-event layer when there's socket
action or an expired timeout, and by libcurl when there's a timeout timer to
change or a socket setup change.

> Also, Josef Drexler wrote that adding an easy handle to a multi handle will
> cause "the callbacks to be called" (timeout callback?). I thought that CURL
> would only actually do stuff, call callbacks etc. when you invoked one of
> its perform-functions?

The timeout callback can be called from multiple functions just to make sure
that you have the right timeout time set. When you add new handles, libcurl
will make sure that it expires very soon so that you'll call libcurl soon and
it'll then notice the new handle(s) and act on them.

> From what you tell me it seems what I should do is:
>
> 1) create a multi handle
> 2) set a timeout callback

And of course 2b) set the socket action callback CURLMOPT_SOCKETFUNCTION.

> 3) add easy handles, which will cause my timeout callback to get called (will
> it?), letting me know what timeout to use
> 4) immediately call curl_multi_socket_action() with sockfd argument set to
> CURL_SOCKET_TIMEOUT (because I don't yet know about any sockets, and curl has
> not performed any work yet, which should mean there are no active sockets, so
> waiting for nothing seems rather pointless, or...?)

Right, when you haven't added any handles there is no sockets to wait for. But
the moment you call curl_multi_socket_action() libcurl will start working and
soon you'll get the socket callback called with info about what socket(s) to
wait on.

> 5) enter main loop, properly wait on sockets, using the timeout value, and
> call curl_multi_socket_action() when things happen (or when I get a timeout)

Exactly.

> The above would basically mean that the encouraged way to get things started
> is to trigger a timeout - call curl_multi_socket_action() with sockfd set to
> CURL_SOCKET_TIMEOUT.

The timeout is triggered automatically by libcurl when you call
curl_multi_add_handle() so if you just have the callbacks setup you're ready
to go.

> This is something I never understood when I read the docs.

If you have any suggestions on how the docs could be rephrased to make this
somewhat less blurry, I'll be happy to work that into the docs for the future!

> Or am I completely out in the blue here? If I get the hang of this I could
> perhaps write a code example for the next person who has trouble with this -
> an updated hiperfifo.c maybe.

That would be great. I believe hiperfifo.c is a bit outdated...

-- 
  / daniel.haxx.se
Received on 2008-10-23