Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

libev have some bug? #3

Open
jarwin123 opened this issue Apr 17, 2018 · 1 comment
Open

libev have some bug? #3

jarwin123 opened this issue Apr 17, 2018 · 1 comment

Comments

@jarwin123
Copy link

jarwin123 commented Apr 17, 2018

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
Copy link
Owner

kindy commented Apr 21, 2018

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants