测试libev

#include <stdio.h>
#include <stdlib.h>
#include <syscall.h>
#include <pthread.h>
#include <errno.h>
#include <string.h>
#include <ev.h>




extern int Createfd();
extern int UdpRecver(int fd);


struct ev_loop *loop2;
static int ioCnt=0;


int CreateTask();


static void async_callback(struct ev_loop *loop, ev_async *w, int revents)
{
        ioCnt--;
    if (revents & EV_ASYNC) {
        printf("Call async_callback %d,data %d, pri=%d, ioCnt=%d\n",
            syscall(SYS_gettid), *(int *)w->data, w->priority, ioCnt);
    }


    sleep(2);
}


static void idle_callback(struct ev_loop *loop, ev_idle *w, int revents)
{
        ioCnt--;


        printf("Call async_callback %d,data %d, pri=%d, ioCnt=%d\n",
            syscall(SYS_gettid), *(int *)w->data, w->priority, ioCnt);




    sleep(2);
}


static void stdin_cb (EV_P_ ev_io *w, int revents)
{
    printf("stdin_cb\n");


    sleep(2);


    ev_io_stop (EV_A_ w);
    ev_break (EV_A_ EVBREAK_ALL);


}
void Trans(EV_P_ int a)
{
    ioCnt++;
    int *i;
    i = (int *)malloc(sizeof(int));
    *i = a;
#if 0
    ev_async async_watcher;
    ev_async_init(&async_watcher, async_callback);
    async_watcher.data = i;
    ev_set_priority(&async_watcher, a);
    //以下两个一起用
    //ev_async_start(EV_A, &async_watcher);
    //ev_async_send(EV_A, &async_watcher);
    ev_feed_event(EV_A, &async_watcher,EV_ASYNC);
#endif
    ev_idle idle_watcher;
    ev_idle_init(&idle_watcher, idle_callback);
    idle_watcher.data = i;
    ev_set_priority(&idle_watcher, a);
    ev_feed_event(EV_A, &idle_watcher,EV_IDLE);
    //ev_invoke(EV_A, &idle_watcher,EV_IDLE);
    //ev_idle_start (EV_A_ &idle_watcher);


}
static void io_cb (EV_P_ ev_io *w, int revents)
{


    int i;


    printf("Call io_cb %d,fd %d, ioCnt=%d\n",
        syscall(SYS_gettid), w->fd, ioCnt);
    i = UdpRecver(w->fd);
    Trans(EV_A_ i-3);
    //ev_io_stop(loop, w);


}
static void timeout_cb3 (EV_P_ ev_timer *w, int revents)
{
    printf("timeout_cb main3\n");
}
static void timeout_cb (EV_P_ ev_timer *w, int revents)
{
    printf("timeout_cb\n");
    //ev_break (EV_A_ EVBREAK_ONE);
    //ev_timer_stop(EV_A_ &timeout_watcher2);


#if 0
    ev_timer timeout_watcher3;
    ev_timer_init (&timeout_watcher3, timeout_cb3, 2.0, 2.0);
    ev_set_priority(&timeout_watcher3, -2);
    ev_invoke(EV_A_ &timeout_watcher3,EV_TIMER);
#endif
#if 0
    int *i;
    i = (int *)malloc(sizeof(int));
    *i = 110;
    ev_async async_watcher;
    ev_async_init(&async_watcher, async_callback);
    async_watcher.data = i;
    ev_set_priority(&async_watcher, 0);
    //以下两个一起用
    //ev_async_start(loop2, &async_watcher);
    //ev_async_send(loop2, &async_watcher);


    //ev_invoke(loop2,&async_watcher,EV_ASYNC);


    ev_feed_event(loop,&async_watcher,EV_ASYNC);
#endif


}


static void timeout_cb2 (EV_P_ ev_timer *w, int revents)
{
    printf("timeout_cb main2\n");
}
int main(void)
{
    printf("main start %d\n", syscall(SYS_gettid));




    unsigned int flags = 0;
    flags = ev_supported_backends();
    printf("ev_supported_backends(%d)\n", flags);
    flags = ev_recommended_backends();
    printf("ev_recommended_backends(%d)\n", flags);


    //struct ev_loop *loop = EV_DEFAULT;
    struct ev_loop *loop = ev_loop_new(EVFLAG_AUTO);


    unsigned int backend = ev_backend(loop);
    printf("loop backend %d\n", backend);


    ev_io stdin_watcher;
    ev_io_init (&stdin_watcher, stdin_cb,  0, EV_READ);
    ev_io_start (loop, &stdin_watcher);


    ev_timer timeout_watcher;
    ev_timer timeout_watcher2;
    ev_timer_init (&timeout_watcher, timeout_cb, 1.0, 0.0);
    ev_timer_init (&timeout_watcher2, timeout_cb2, 1.0, 0.0);


    ev_timer_start (loop, &timeout_watcher);
    ev_timer_start (loop, &timeout_watcher2);






    ev_set_priority(&timeout_watcher, 1);
    ev_set_priority(&timeout_watcher2,-1);


#if 0
    ev_timer timeout_watcher3;
    ev_timer_init (&timeout_watcher3, timeout_cb3, 2.0, 2.0);
    ev_set_priority(&timeout_watcher3, -2);
    ev_timer_start (loop, &timeout_watcher3);
#endif


    int ifd = Createfd();
    ev_io io_watcher;
    ev_io_init (&io_watcher, io_cb,  ifd, EV_READ);
    ev_set_priority(&io_watcher, 0);
    ev_io_start (loop, &io_watcher);
    printf("wait on fd(%d)\n",ifd);


    //loop2 = ev_loop_new(EVFLAG_AUTO);


    //CreateTask();


    ev_run (loop, 0);


    printf("main stop\n");
    return 0;
}


/*****************************************************************************************/
static void
task2_timeout_cb (EV_P_ ev_timer *w, int revents)
{
    printf("timeout_cb 222\n");
}


static  void *Task2(void *arg)
{
    printf("Task2 start %d\n", syscall(SYS_gettid));
    ev_timer task2_timeout_watcher;


    ev_timer_init (&task2_timeout_watcher, task2_timeout_cb, 1, 2);
    ev_timer_start (loop2, &task2_timeout_watcher);


    ev_run (loop2, 0);


    printf("Task2 stop\n");
    return NULL;
}
int CreateTask()
{
    pthread_t TaskID;


    errno = 0;
    if (0 != pthread_create(&TaskID, NULL, (void *)Task2, NULL)) {
        printf("pthread_create err(%d:%s)\n", errno, strerror(errno));
        return -1;
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值