kindy / libev

libev sync from http://cvs.schmorp.de/libev/
157 stars 97 forks source link

libev have some bug? #3

Open jarwin123 opened 6 years ago

jarwin123 commented 6 years ago

i have write a code:

#include <stdio.h>
#include <unistd.h>
#include<arpa/inet.h>
#include<netinet/in.h>
#include<sys/types.h>
#include<sys/socket.h>

ev_io stdin_watcher;
ev_timer timeout_watcher;
static void timeout_cb (EV_P_ ev_timer *w, int revents) ;

static void udp_cb (EV_P_ ev_io *w, int revents)
{
        char buf[101] = {0};
        read(w->fd,buf,100);
        printf("read data---->%s\n",buf);
        ev_io_stop (EV_A_ w);
        ev_break (EV_A_ EVBREAK_ALL);
}

static int flag = 0;
struct ev_loop *loop;

static void timeout_cb (EV_P_ ev_timer *w, int revents)
{
        if(flag == 0){
                sleep(30);
                flag = 1;
        }
        ev_timer_stop (loop, w);
        ev_timer_set (w, 1, 0.0);
        ev_timer_start (loop, w);
        printf("2-----ev_now=%d,ev_time=%d\n",ev_now(EV_A),ev_time());
        printf("end\n");
}

int main (void)
{
        loop = EV_DEFAULT;
        int sock = socket(AF_INET,SOCK_DGRAM,0);
        if(sock<0){
                perror("socket");
                exit(1);
        }

        struct sockaddr_in local;
        local.sin_family = AF_INET;
        local.sin_port = htons(7777);
        local.sin_addr.s_addr = inet_addr("0.0.0.0");
        if(bind(sock,(struct sockaddr*)&local,sizeof(local))<0){
                perror("bind");
                exit(0);
        }

        ev_io_init (&stdin_watcher, udp_cb,sock, EV_READ);
        ev_io_start (loop, &stdin_watcher);

        ev_timer_init (&timeout_watcher, timeout_cb, 1,0.0);
        ev_timer_start(loop, &timeout_watcher);
        ev_run (loop, 0);

        return 0;
}

compile : gcc -o test 1.c -lev

test it and the result is : before the timeout happy and udp sock recived data in this period. this timeout_cb always trigger twice and quit. i cannot decide this is bug? Why it triggers twice instead of once?

kindy commented 6 years ago

@jarwin123 sorry, but I'm not maintainer, please ask question at: