定义事件
struct Event{
int type;
void* data;
struct event ev;
};
//回调函数处理
static void event_cb(evutil_socket_t fd, short event, void *arg)
{
Event* myEvent = (Event*)arg;
if (!myEvent)
{
return ;
}
switch(myEvent->type)
{
case 0:
std::cout<<"event 1 occur"<<std::endl;
if (myEvent->data)
{
delete[] myEvent->data;
}
break;
default:
break;
}
delete myEvent;
return;
}
主函数处理如下:
struct event_base *base;
WORD wVersionRequested;
WSADATA wsaData;
wVersionRequested = MAKEWORD(2, 2);
(void)WSAStartup(wVersionRequested, &wsaData);
/* Initalize the event library */
base = event_base_new();
Event* myEvent = new Event();
myEvent->type = 0;
myEvent->data = new char[10];
event_assign(&(myEvent->ev), base, -1, EV_PERSIST, event_cb, myEvent);
event_add(&myEvent->ev,NULL);
//触发自定义事件
event_active(&myEvent->ev,MY_EVENT_TRAN,0);
event_base_dispatch(base);