Suspected as much.

Thank you for a quick and very exhaustive reply!
:-)

Regards
 /Joachim

On 05/18/2011 11:35 AM, Pierre-Yves Kerembellec wrote:
>> We use libev in Linux (2.6.35 on big/small endian ARM) for a simple
>> network client application where we want to timeout connect() attempts.
>>
>>      http://www.unixguide.net/network/socketfaq/3.5.shtml
>>
>> For this purpose we use the following construct in a libev callback.
>>
>>        timeout = NULL_TIMER;
>>        timeout.it_value.tv_sec = 10;
>>      setitimer(ITIMER_REAL, &timeout, NULL);
>>      if (connect (...) == -1) { ... }
>>        timeout = NULL_TIMER;
>>        setitimer (ITIMER_REAL, &timeout, NULL);
>>
>> Is this OK for use with libev, or do you have any other recommendation?
>> Basically we're a bit concerned if it conflicts with the internal timer
>> implementation, or event loop, in libev.
> 
> This is probably not the right thing to do with libev (as you are supposed
> to be in a blocking loop managed by ev_loop()). Instead, you should create
> a non-blocking client TCP socket, an ev_write and an ev_timer, and add both
> to the loop. Something like this (kinda pseudo-code):
> 
> --------8<----------8<----------8<----------8<----------8<----------8<-------
> void connect_handler(struct ev_loop *loop, struct ev_io *watcher, int events)
> {
>    // socket is connected: kill the associated timer and proceed
> }
> 
> void timeout(struct ev_loop *loop, struct ev_timer *watcher, int events)
> {
>    // connection timeout occurred: close the associated socket and bail
> }
> 
> ev_loop   *loop;
> ev_io     write_watcher;
> ev_timer  timeout_watcher;
> int       client;
> 
> client = socket(...);
> set_non_blocking(client);
> ev_io_init(&write_watcher, connect_handler, client, EV_WRITE);
> ev_timer_init(&timeout_watcher, timeout_handler, 5.0, 0);
> ev_timer_start(loop, &timeout_watcher);
> connect(client, ...);
> ev_loop(loop, 0);
> --------8<----------8<----------8<----------8<----------8<----------8<-------
> 
> Of course, this code is sub-optimal if you intend to open a large number of
> connections from the same loop. Also, you need to tie the 2 watchers (write
> and timeout) together via some sort of allocated structure and offsetof magic
> (see libev examples for more info).
> 
> Regards,
> Pierre-Yves
> 


_______________________________________________
libev mailing list
[email protected]
http://lists.schmorp.de/cgi-bin/mailman/listinfo/libev

Reply via email to