libevent的地基:-event_base;
使用libevent函数之前需要分配一个或多个event_base结构体,每个event_base结构体持有一个事件集合,可以检测以确定是哪个事件激活的,event_base相当于epoll红黑树的树根节点。
常用的API函数: 1 struct event_base*event_base_new(void);//event.h的337行
函数说明:获得event_base结构体;
返回值:成功返回指向event_base结构体的指针
失败返回NULL.
/**
* Create and return a new event_base to use with the rest of Libevent.
*
* @return a new event_base on success, or NULL on failure.
*
* @see event_base_free(), event_base_new_with_config()
*/
2 void event_base_free(struct event_base*);//event.h的561行
函数说明:释放event_base结构体指针
/**
Deallocate all memory associated with an event_base, and free the base.Note that this function will not close any fds or free any memory passed
to event_new as the argument to callback.@param eb an event_base to be freed
*/
int event_reinit(struct event_base *base);//event.h的349行
函数说明:如果有子进程,且子进程也要使用base,则子进程需要对event_base重新初始化
函数参数:由event_base_new返回的执行event_base结构的指针
/**
Reinitialize the event base after a forkSome event mechanisms do not survive across fork. The event base needs
to be reinitialized with the event_reinit() function.@param base the event base that needs to be re-initialized
@return 0 if successful, or -1 if some events could not be re-added.
@see event_base_new()
*/
const char **event_get_supported_methods(void);
函数说明:获得当前系统支持的方法有哪些
/**
Gets all event notification mechanisms supported by Libevent.This functions returns the event mechanism in order preferred by
Libevent. Note that this list will include all backends that
Libevent has compiled-in support for, and will not necessarily check
your OS to see whether it has the required resources.@return an array with pointers to the names of support methods.
The end of the array is indicated by a NULL pointer. If an
error is encountered NULL is returned.
*/
const char *event_base_get_method(const struct event_base *);
函数说明:获得当前base节点使用的多路io方法
函数参数:event_base结构的base指针
返回值:获得当前base节点使用的多路io方法的指针
/**
Get the kernel event notification mechanism used by Libevent.@param eb the event_base structure returned by event_base_new()
@return a string identifying the kernel event mechanism (kqueue, epoll, etc.)
*/
使用:
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<event2/event.h>
int main()
{
int i=0;
//获取支持哪些版本
const char **p=event_get_supported_methods();
while(p[i]!=NULL)
{
printf("%s \t",p[i++]);
}
printf("\n");
//创建一个地基
struct event_base *base=event_base_new();
if(base==NULL)
{
printf("new error");
return -1;
}
//获取当前节点使用的方法
const char*t=event_base_get_method(base);
printf("%s\n",t);
//释放地基
event_base_free(base);
return 0;
}
结果:
libevent在打好地基后,,需要等待事件的产生,也就是事件被激活,所以程序不能退出,对于epoll来说,我们需要使用while(1)来让程序不退出,在libevent中提供了API接口,类似while(1)的功能;
int event_base_loop(struct event_base *, int);//event.h 660行
函数说明:进入循环等待事件
/**@}*/
/**
Wait for events to become active, and run their callbacks.This is a more flexible version of event_base_dispatch().
By default, this loop will run the event base until either there are no more
pending or active events, or until something calls event_base_loopbreak() or
event_base_loopexit(). You can override this behavior with the 'flags'
argument.@param eb the event_base structure returned by event_base_new() or
event_base_new_with_config()
@param flags any combination of EVLOOP_ONCE(只触发一次,如果事件没有触发,则阻塞等待) | EVLOOP_NONBLOCK(非阻塞方式检测事件是否被触发,不管事件触发于否,都会立刻返回)
@return 0 if successful, -1 if an error occurred, or 1 if we exited because
no events were pending or active.
@see event_base_loopexit(), event_base_dispatch(), EVLOOP_ONCE,
EVLOOP_NONBLOCK
*/
int event_base_dispatch(struct event_base *);//event.h 364
函数说明:进入循环等待事件
调用该函数,相当于没有标志位的event_base_loop。程序会一直运行,直到没有需要检查的事件,或者被结束循环的API终止。
结束循环的API:
int event_base_loopexit(struct event_base *, const struct timeval *);
/**
Exit the event loop after the specified timeThe next event_base_loop() iteration after the given timer expires will
complete normally (handling all queued events) then exit without
blocking for events again.Subsequent invocations of event_base_loop() will proceed normally.
@param eb the event_base structure returned by event_init()
@param tv the amount of time after which the loop should terminate,
or NULL to exit after running all currently active events.struct timeval
{
long tv_sec;
long tv_usec;
}
@return 0 if successful, or -1 if an error occurred
@see event_base_loopbreak()
*/
int event_base_loopbreak(struct event_base *);
/**
Abort(中断) the active event_base_loop() immediately.event_base_loop() will abort the loop after the next event is completed;
event_base_loopbreak() is typically invoked from this event's callback.
This behavior is analogous to the "break;" statement.Subsequent invocations of event_loop() will proceed normally.
@param eb the event_base structure returned by event_init()
@return 0 if successful, or -1 if an error occurred
@see event_base_loopexit()
*/文章来源:https://www.toymoban.com/news/detail-840081.html
这两个函数的区别是如果正在执行激活事件的回调函数,那么会在事件回调函数执行结束后终止循环(如果时间tv为非NULL,那么将等待tv设置的时间后立即结束循环),而 event_base_loopbreak立刻终止循环文章来源地址https://www.toymoban.com/news/detail-840081.html
到了这里,关于libevent常用的API函数的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!