Subject: Re: Integrating with libev

Re: Integrating with libev

From: Brian McFarland <mcfarlandjb_at_gmail.com>
Date: Wed, 25 Apr 2012 11:30:09 -0400

>Yes, it seems that state callback is invoked also on socket creation.
>However, you cannot distinguish really if it's created or deleted there
>since in both cases readable and writable are 0. If you need to
>allocate stuff on socket creation, you need to use the sock_create_cb.
>(Checked version 1.7.5.)
I did some more digging around in the 1.7.5 code. As far as I can tell,
the socket state callback is always invoked through the macro
SOCK_STATE_CALLBACK. A simple find & grep shows that it is only called
with 0/0 when closing the socket. If you look at open_[tcp|udp]_socket()
in ares_process.c, you'll see that SOCK_STATE_CALLBACK(channel, s, 1, 0) is
called immediately after the sock_create_cb, but only if sock_create_cb
return ARES_SUCCESS.

So sock_create_cb, according to the current implementation, is definitely
the appropriate place to allocate any per-connection memory since you can
return ARES_ENOMEM if malloc fails. As far as I can tell, that's the only
way you can indicate to your gethostbyname callbacks that a malloc error
occurred.

Brian
Received on 2012-04-25