Regd ev_timer

Varun Chandramohan cvaruneng04 at gmail.com
Tue Apr 2 05:48:05 CEST 2013


Hi Folks,

Iam trying to understand the timer behaviour using a simple program. Let me
first post the pseudo code.

/*global */

#define DEFAULT_CONNECT_TIMER 7.0

struct ps_hb {

    ev_io io;

    ev_timer timer;

    int sd;

    struct sockaddr_in serveraddr;

}ps_hb;

int init_ps(struct ps_hb *ps_hb, struct ev_loop *loop) {

    ev_io_init(&ps_hb->io, hb_conn_cb, ps_hb->sd, EV_WRITE);

    ev_timer_init(&ps_hb->timer, hb_timeout_handler, DEFAULT_CONNECT_TIMER,
0);

    ev_io_start(loop, &ps_hb->io);

    ev_timer_start(loop, &ps_hb->timer);

    return 0;

}

static void hb_conn_cb(struct ev_loop *loop, struct ev_io *w, int events) {



    struct ps_hb *ps_hb = (struct ps_hb *)w;

    printf("HB Timer\n");

    if (EV_WRITE & events) {

        ev_io_stop(loop, &ps_hb->io);

        ev_timer_stop(loop, &ps_hb->timer);

        close(ps_hb->sd);

        sleep(10);



        if ((ps_hb->sd = socket(AF_INET, SOCK_STREAM, 0)) == -1) {

               return;

        }



        if (set_socket_nonblock(ps_hb->sd)) {

            return;

        }

        ev_io_set(&ps_hb->io, ps_hb->sd, EV_WRITE);

        ev_timer_set(&ps_hb->timer, DEFAULT_CONNECT_TIMER + ev_now (loop) -
ev_time (), 0);

        ev_io_start(loop, &ps_hb->io);

        ev_timer_start(loop, &ps_hb->timer);



       connect();

    return;



}

static void hb_timeout_handler(struct ev_loop *loop, struct ev_timer
*w, intevents) {



    struct ps_hb *ps_hb = container_of(w, struct ps_hb, timer);

    printf("Checking HB timeout\n");

    if (events & EV_TIMEOUT) {

    ev_io_stop(loop, &ps_hb->io);

    ev_timer_stop(loop, &ps_hb->timer);

    close(ps_hb->sd);

    sleep(10);

    if ((ps_hb->sd = socket(AF_INET, SOCK_STREAM, 0)) == -1) {

        bps_log(LOG_ERR, "Socket cannot be created to connect to policy
server (HB mode)", errno);

        return;

    }

    if (set_socket_nonblock(ps_hb->sd)) {

        bps_log(LOG_ERR, "Could not set the socket to non-block (HB mode)",
-1);

        return;

    }

    ev_io_set(&ps_hb->io, ps_hb->sd, EV_WRITE);

    ev_timer_set(&ps_hb->timer, DEFAULT_CONNECT_TIMER + ev_now (loop) -
ev_time (), 0);

    ev_io_start(loop, &ps_hb->io);

    ev_timer_start(loop, &ps_hb->timer);



    connect();

    return;

}

main() {

struct ev_loop *loop = ev_default_loop(0);

sd = socket();

set_non_block(sd);

init_ps(&ps_hb, loop);

connect(sd);

ev_loop(loop, 0);

}


As you can see its a pretty simple program and my intention is to do a
connect to say "google.com" periodically every 10 sec (originally 2 mins)
and see if its connects or not. Since by default connect timeout is too
long for me I want set sd to non-block and set a timeout for
DEFAULT_CONNECT_TIMER. That is if that timer exipres and we have not
connected then I will not connect to google.com failed. If the timer does
not expire and connect happens before that then I simply reset the timer
and do this forever.

However, the above code does not work because of using sleep(10) which is
more than DEFAULT_CONNECT_TIMER. If I increase DEFAULT_CONNECT_TIMER to
11.0 then everything works as planned. But I do not want connect to have
such big timeout. Is there a way I can make the timer stop and not record
events when Iam sleeping? Iam not quite sure why this must happen even
after I did  ev_timer_stop(loop, &ps_hb->timer). Right now my output is
something like

HB Timer

Checking HB timeout

Checking HB timeout

Checking HB timeout

Checking HB timeout

This goes on...I wonder why Iam not able to see pending HB Timer connect
events? I mean shouldnt that also get triggered?

Regards,

Varun
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.schmorp.de/pipermail/libev/attachments/20130402/62b36f6b/attachment.html>


More information about the libev mailing list