#include <iostream>
#include <WinSock2.h>
#include <event2/event.h>
#include <event2/event_struct.h>
using namespace std;
//#include <sys/time.h>
// 定时事件回调函数
// struct event ev; //事件
struct timeval tv; //定时器
struct event_base *base; //定义一个event_base
struct event *ev;
/*事件处理函数,cb=callback*/
void time_cb(int fd, short _event, void *argc)
{
cout << "timer wakeup" << endl;
event_add(ev, &tv);/*重新添加定时器*/
}
int main(int argc,char **argv)
{
WSADATA WSAData; /*初始化WINDOWS socket库*/
WSAStartup(0x101, &WSAData);
base = event_base_new(); //初始化一个event_base
tv.tv_sec = 1; //间隔
tv.tv_usec = 0;
ev = event_new(base, -1, EV_TIMEOUT , time_cb, base);
// event_assign(&ev, base, -1, 0,time_cb, base);
event_add(ev, &tv);//注册事件 相当于调用Reactor::register_handler()函数注册事件
event_base_dispatch(base);//进入消息循环和消息分发
}
网友评论