libevent 学习:官方案例 time-test

路径:libevent-2.1.8-stable/sample

文件 time-test.c

这个案例实现的是一个简单的定时任务,根据是否有 “-p” 选项决定是触发一次还是不断周期性触发(注释写到非UNIX平台无法运行……)

#include <sys/types.h>

#include <event2/event-config.h>

#include <sys/stat.h>
#ifndef _WIN32
#include <sys/queue.h>
#include <unistd.h>
#endif
#include <time.h>
#ifdef EVENT__HAVE_SYS_TIME_H
#include <sys/time.h>
#endif
#include <fcntl.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <errno.h>

#include <event2/event.h>
#include <event2/event_struct.h>
#include <event2/util.h>

#ifdef _WIN32
#include <winsock2.h>
#endif

struct timeval lasttime;

int event_is_persistent;

static void
timeout_cb(evutil_socket_t fd, short event, void *arg)
{
	struct timeval newtime, difference;
	struct event *timeout = arg;
	double elapsed;

	evutil_gettimeofday(&newtime, NULL);
	evutil_timersub(&newtime, &lasttime, &difference);
	elapsed = difference.tv_sec +
	    (difference.tv_usec / 1.0e6);

	printf("timeout_cb called at %d: %.3f seconds elapsed.\n",
	    (int)newtime.tv_sec, elapsed);
	lasttime = newtime;

	//if (! event_is_persistent) {
	//	struct timeval tv;
	//	evutil_timerclear(&tv);
	//	tv.tv_sec = 2;
	//	event_add(timeout, &tv);
	//}
}

int
main(int argc, char **argv)
{
	struct event timeout;
	struct timeval tv;
	struct event_base *base;
	int flags;

#ifdef _WIN32
	WORD wVersionRequested;
	WSADATA wsaData;

	wVersionRequested = MAKEWORD(2, 2);

	(void)WSAStartup(wVersionRequested, &wsaData);
#endif

	if (argc == 2 && !strcmp(argv[1], "-p")) {
		event_is_persistent = 1;
		flags = EV_PERSIST;
	} else {
		event_is_persistent = 0;
		flags = 0;
	}

	/* Initalize the event library */
	base = event_base_new();

	/* Initalize one event */
	event_assign(&timeout, base, -1, flags, timeout_cb, (void*) &timeout);

	evutil_timerclear(&tv);
	tv.tv_sec = 2;
	event_add(&timeout, &tv);

	evutil_gettimeofday(&lasttime, NULL);

	event_base_dispatch(base);

	return (0);
}

编译:

gcc -I/usr/local/include -o timetest time-test.c -L/usr/local/lib -levent

周期性运行:

./timetest -p

输出:

timeout_cb called at 1547066714: 2.002 seconds elapsed.
timeout_cb called at 1547066716: 1.998 seconds elapsed.
timeout_cb called at 1547066718: 1.998 seconds elapsed.
timeout_cb called at 1547066720: 2.002 seconds elapsed.
……

单次运行:

./timetest

输出一次:

timeout_cb called at 1547066765: 2.002 seconds elapsed.

如果给以下代码取消注释:

	//if (! event_is_persistent) {
	//	struct timeval tv;
	//	evutil_timerclear(&tv);
	//	tv.tv_sec = 2;
	//	event_add(timeout, &tv);
	//}

那么即使单次运行,结果也是周期性输出,因为每次都重新添加了一次定时事件~

结构体:

  • evutil_socket_t:定义(#define evutil_socket_t   int),保存socket()或accept()返回的套接字
  • event_base:保存事件(分发)循环的信息和状态
  • event:单个事件

函数:

  • evutil_gettimeofday:获取当前时间
  • evutil_timersub:对时间类型timeval 做减法的宏定义
  • event_base_new:新建一个event_base
  • event_assign:创建一个event
  • evutil_timerclear:对一个timeval 清零
  • event_add:添加事件到事件循环
  • event_base_dispatch:开启事件循环

 

参考:

libevent Documentation

 

 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值