概述
定义事件
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);
最后
以上就是标致犀牛为你收集整理的libevent 自定义事件处理的全部内容,希望文章能够帮你解决libevent 自定义事件处理所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
发表评论 取消回复