cURL / Mailing Lists / curl-library / Single Mail

curl-library

Re: Asynchronous communication using libcurl

From: Dan Fandrich <dan_at_coneharvesters.com>
Date: Tue, 2 Jul 2013 00:31:09 +0200

On Tue, Jul 02, 2013 at 02:51:45AM +0530, Kowsik Tulabandula wrote:
> I am new to HTTP, HTTPS and libcurl.
>
> My requirement is as below:
>
> 1. A client program should handle communication with more than 3000 servers
> using XML/HTTPS.

Only 3000? I've forgotten how to count that low :-) Seriously, people have done
far more than that with libcurl without much issue.

> 2. Communication involves client program sending requests and waiting for
> response to the request. And also, the server can send asynchronously
> events to client program. I am looking for a asynchronous approach
> considering the high scale numbers.
>
> I saw a few sample programs, and identified three approaches.
>
> Approach 1: (Using curl_easy_handle)
>
>        One using curl easy handle and do synchronous communication with server.
> This will require one dedicated thread to send request and wait for
> curl_easy_perform to complete i.e, until response is received. Also, using
> curl_easy_perform, client program cant subscribe for events from servers. I
> wrote a sample program which is able to send xml requests over https and
> receive response. Since easy handle only provides synchronous communication, i
> think it is better not to use this approach.  

Right, the thread overhead is going to be very large. But, it is a valid
approach.

> Approach 2: (Using curl_easy_recv and curl_easy_send)

This isn't a real approach. There are hardly any reasons to ever use these two
functions, and this definitely isn't one of them.

> Approach 3: (Using curl_multi_handle)
>
> Other approach i haven't tried is using multi handle to achieve asynchronous
> implementation. Is there any sample example i can refer to ?

Lots and lots. See docs/examples/. Look especially at the "hiper" stuff
which was created for this kind of case.

> Asynchronous Event Notification:
>
> Also my client program should be listening to events sent by servers, i want to
> know if the following works or not?
>
> open a curl connection using Approach 2 or Approach 3 and send a request to
> server. On server, apache receives the connection request and will get hold of
> the socket used for https connection and will keep writing the events on this
> socket while the client program will get socket from handle and will be doing
> poll/select to listen on event notifications followed by receive.
>
> Is there any better approach for subscribing to event notifications using
> libcurl ?
>
> Also, if you know of any better approaches please guide me. Thanks in Advance.

There are example programs using libev and libevent which should give some
clues as to how to accomplish this. The man pages will fill you in on the
details of some of the functions you see in there, and of course you can ask
here with detailed questions.

>>> Dan
-------------------------------------------------------------------
List admin: http://cool.haxx.se/list/listinfo/curl-library
Etiquette: http://curl.haxx.se/mail/etiquette.html
Received on 2013-07-02