libevent常用的API函数

这篇具有很好参考价值的文章主要介绍了libevent常用的API函数。希望对大家有所帮助。如果存在错误或未考虑完全的地方,请大家不吝赐教,您也可以点击"举报违法"按钮提交疑问。

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 fork

  Some 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常用的API函数,服务器,运维,linux,网络,算法

 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 time

  The 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()
 */

这两个函数的区别是如果正在执行激活事件的回调函数,那么会在事件回调函数执行结束后终止循环(如果时间tv为非NULL,那么将等待tv设置的时间后立即结束循环),而  event_base_loopbreak立刻终止循环文章来源地址https://www.toymoban.com/news/detail-840081.html

到了这里,关于libevent常用的API函数的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处: 如若内容造成侵权/违法违规/事实不符,请点击违法举报进行投诉反馈,一经查实,立即删除!

领支付宝红包 赞助服务器费用

相关文章

  • 服务器搭建(TCP套接字)-libevent版(服务端)

         Libevent 是一个开源的 事件驱动库 ,用于开发高性能、并发的网络应用程序。它提供了跨平台的事件处理和网络编程功能,具有高性能、可扩展性和可移植性。下面详细讲解 Libevent 的主要组成部分和使用方法。 事件基础结构(event_base)是 Libevent 的核心组件,用于

    2024年02月07日
    浏览(82)
  • VMware vCenter服务器常用的巡检命令、运维命令和PowerShell脚本

    一、前言 最近整理一些VMware vCenter和Esxi常用的巡检命令和运维命令如下: 二、巡检命令 三、运维命令 运维常用命令: 四、Powershell脚本 以上就是vCenter和ESXi常用的运维与监控命令,可以帮助vSphere管理员管理和监控环境。

    2024年02月11日
    浏览(40)
  • TCP服务器的演变过程:C++使用libevent库开发服务器程序

    手把手教你从0开始编写TCP服务器程序,体验开局一块砖,大厦全靠垒。 为了避免篇幅过长使读者感到乏味,对【TCP服务器的开发】进行分阶段实现,一步步进行优化升级。 在上一章节介绍了如何使用epoll构建reactor网络模型开发高效的服务器,有了上一节的基础,本节将介绍

    2024年01月23日
    浏览(39)
  • 运维 | 查看 Linux 服务器 IP 地址

    大多数在操作 Linux 系统时,我们经常需要知道服务器的 IP 比便于后续的一系列操作,这时候有快速查看主机 IP 的命令行操作,能够有效的帮助我们 本章节主要记录一些常用查看服务器 IP 的命令,希望对大家有所帮助。 查看 Linux 服务器的 IP 地址的命令大体上有以下几种。

    2024年04月27日
    浏览(52)
  • libevent实践11:主动模式的FTP服务器

    代码仅供参考,如用于正式项目,根据实际添加并发处理代码。代码使用的libevent库和使用的CMakeLists.txt文件参考本系列博客前面的篇章。  在本次测试中实现的命令: CWD:切换到指定的目录 USER:登录用户 CDUP:回到上一层目录 PORT:主动传输方式 LIST:获取目录列表 RETR:请

    2024年02月16日
    浏览(30)
  • 【运维】Linux 跨服务器复制文件文件夹

    如果是云服务 建议用内网ip scp是secure copy的简写,用于在Linux下进行远程拷贝文件的命令,和它类似的命令有cp,不过cp只是在本机进行拷贝不能跨服务器,而且scp传输是加密的。可能会稍微影响一下速度。当你服务器硬盘变为只读 read only system时,用scp可以帮你把文件移出来

    2024年02月08日
    浏览(46)
  • 10 款常用的 API Mock 服务器工具

    你是否曾因为后端接口还没开发完成而苦恼,而你作为前端开发人员却迫不及待地想要开始写代码?API Mock 服务器就是你的救星!它们可以快速搭建一个虚拟的后端环境,使你可以立即开始测试和开发。这篇文章将介绍 10 款 API Mock 服务器工具,并以轻松幽默的语气向你展示

    2024年02月08日
    浏览(35)
  • 【Linux 服务器运维】定时任务 crontab 详解 | 文末送书

    本文思维导图概述的主要内容: 1.1 什么是 crontab Crontab 是一个在 Unix 和 Linux 操作系统上 用于定时执行任务 的工具。它允许用户创建和管理计划任务,以便在特定的时间间隔或时间点自动运行命令或脚本。Crontab 是 cron table 的缩写, cron 指的是 Unix 系统中的一个后台进程,它

    2024年02月08日
    浏览(62)
  • 【Linux运维】shell脚本检查服务器内存和CPU利用率

    在管理服务器时候写了一个 shell脚本,在服务上实现每天凌晨3点查系统的指定文件夹下的容量大小,如果超过10G就要删除3天前的内容,还要时刻查询内存和cpu利用率,如果超过80%就要提示用户出现过载 将以上代码保存为一个.sh文件,然后通过crontab在每天凌晨3点运行即可:

    2024年02月09日
    浏览(48)
  • Linux服务器常见运维性能测试(1)综合跑分unixbench、superbench

    最近需要测试一批服务器的相关硬件性能,以及在常规环境下的硬件运行稳定情况,需要持续拷机测试稳定性。所以找了一些测试用例。本次测试包括在服务器的高低温下性能记录及压力测试,高低电压下性能记录及压力测试,常规环境下CPU满载稳定运行的功率记录。 这个系

    2024年02月04日
    浏览(55)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

请作者喝杯咖啡吧~博客赞助

支付宝扫一扫领取红包,优惠每天领

二维码1

领取红包

二维码2

领红包