高性能,分布式,轻量级缓存组件memcached的源码剖析---第二篇,libevent
2007-10-05 21:32
363 查看
step0: 概述
关于libevent, 先摘来网上某哥们一段描述:
libevent是一个跨平台的事件驱动库,他目前支持Linux, *BSD, Mac OS X, Solaris 和 Windows。如果你将要开发的应用程序需要支持以上所列出的平台中的两个以上,那么强烈建议你采用这个库,即使你的应用程序只需要支持一个平台,选择libevent也是有好处的,因为它可以根据编译/运行环境切换底层的事件驱动机制,这既能充分发挥系统的性能,又增加了软件的可移植性。
它封装并且隔离了事件驱动的底层机制,除了一般的文件描述符读写操作外,它还提供有读写超时、定时器和信号回调,另外,它还允许为事件设定不同的优先级,当前版本的libevent还提供dns和http协议的异步封装,这一切都让这个库尤其适合于事件驱动应用程序的开发。
step1: 使用方法介绍
OK,理论到位了,接下来简单介绍下libevent中几个核心函数:
event_set(&c->event, sfd, event_flags, event_handler, (void *)c)
把sfd这个文件描述符放入c->event,并且告知当事件event_flags发生时回调event_handler,并以c为回调参数。
event_add(&ev, NULL)
把ev注册到事件队列里面,第二个参数指定的是超时值,设定成NULL表示忽略这项设定。
event_dispatch()
表示进入事件循环,当队列里任何一个文件描述符发生事件的时候就会执行回调函数。
step2: 演示例子
下面举个小例子,就timer server吧,比较SB易懂咯,赶紧贴上源码:
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
#include <netinet/in.h>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
#include <sys/socket.h>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
#include <sys/types.h>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
#include <stdio.h>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
#include <time.h>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
#include <event.h>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)
void connection_time(int fd, short event, struct event *arg)...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
char buf[32];
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
struct tm t;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
time_t now;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
time(&now);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
localtime_r(&now, &t);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
asctime_r(&t, buf);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
write(fd, buf, strlen(buf));
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
shutdown(fd, SHUT_RDWR);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
free(arg);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)
void connection_accept(int fd, short event, void *arg)...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
/**//* for debugging */
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
fprintf(stderr, "%s(): fd = %d, event = %d. ", __func__, fd, event);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
/**//* 接受新的连接. */
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
struct sockaddr_in s_in;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
socklen_t len = sizeof(s_in);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
int ns = accept(fd, (struct sockaddr *) &s_in, &len);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
if (ns < 0) ...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
perror("accept");
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
return;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
struct event *ev = malloc(sizeof(struct event));
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
event_set(ev, ns, EV_WRITE, (void *) connection_time, ev);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
event_add(ev, NULL);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)
int main(void)...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
/**//* 创建socket. */
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
int s = socket(PF_INET, SOCK_STREAM, 0);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
if (s < 0) ...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
perror("socket");
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
exit(1);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
/**//* 绑定bind() */
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
struct sockaddr_in s_in;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
bzero(&s_in, sizeof(s_in));
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
s_in.sin_family = AF_INET;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
s_in.sin_port = htons(7000);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
s_in.sin_addr.s_addr = INADDR_ANY;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
if (bind(s, (struct sockaddr *) &s_in, sizeof(s_in)) < 0) ...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
perror("bind");
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
exit(1);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
/**//* 监听listen() */
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
if (listen(s, 5) < 0) ...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
perror("listen");
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
exit(1);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
/**//* 初始化libevent. */
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
event_init();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
/**//* 创建事件 */
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
struct event ev;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
event_set(&ev, s, EV_READ | EV_PERSIST, connection_accept, &ev);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
/**//* 添加事件 */
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
event_add(&ev, NULL);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
event_dispatch();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
return 0;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
怕有新手不懂,补充两句了:
其实我一直认为,任何单一的编程模型本身,必能找出一个贴切的隐喻或现实参照物,只要洞悉本质,一切皆是那么容易(当然,我这里并不否认复合模型的复杂性)。
libevent是基于事件的,看它名字都知道,我大致说说上面小例子的章法:
拿个龌龊例子来隐喻下,假设有个买春店,这个店好比server socket,各种服务(如按摩,做爱)代表事件(连接,可读),小姐们代表一个个对等客户连接,客房代表空闲线程,嫖客好比过来的客户端连接,嫖客有欲望了,找上买春店,绑定到具体的对等连接(小姐),激发了连接事件并执行对应的回调(比如按摩,做爱),此回调被安排在某个空闲的客房(比如线程池中某个空闲连接)里进行。如果嫖客与小妹是初次见面,那么留下联系方式,日后嫖客有需求,来店后可直接去找老相好(这就是为何在connection_accept回调函数中还要注册一次,因为第一次事件注册是没有目标性的,也即来个嫖客就随便找个小妹,而在回调中对连接事件的注册已经是有目标性的注册了,下次嫖客再来,就找上次的老相好,这样子做是有必要性的,对于嫖客,妹子而言可能旧情未了,需要重温, 而对于网络通信,可能前面的通信并没有把数据传递完,所以需要把对等端连接关联起来,记录上次数据处理的状态信息,以备后续处理)。
关于libevent, 先摘来网上某哥们一段描述:
libevent是一个跨平台的事件驱动库,他目前支持Linux, *BSD, Mac OS X, Solaris 和 Windows。如果你将要开发的应用程序需要支持以上所列出的平台中的两个以上,那么强烈建议你采用这个库,即使你的应用程序只需要支持一个平台,选择libevent也是有好处的,因为它可以根据编译/运行环境切换底层的事件驱动机制,这既能充分发挥系统的性能,又增加了软件的可移植性。
它封装并且隔离了事件驱动的底层机制,除了一般的文件描述符读写操作外,它还提供有读写超时、定时器和信号回调,另外,它还允许为事件设定不同的优先级,当前版本的libevent还提供dns和http协议的异步封装,这一切都让这个库尤其适合于事件驱动应用程序的开发。
step1: 使用方法介绍
OK,理论到位了,接下来简单介绍下libevent中几个核心函数:
event_set(&c->event, sfd, event_flags, event_handler, (void *)c)
把sfd这个文件描述符放入c->event,并且告知当事件event_flags发生时回调event_handler,并以c为回调参数。
event_add(&ev, NULL)
把ev注册到事件队列里面,第二个参数指定的是超时值,设定成NULL表示忽略这项设定。
event_dispatch()
表示进入事件循环,当队列里任何一个文件描述符发生事件的时候就会执行回调函数。
step2: 演示例子
下面举个小例子,就timer server吧,比较SB易懂咯,赶紧贴上源码:
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
#include <netinet/in.h>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
#include <sys/socket.h>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
#include <sys/types.h>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
#include <stdio.h>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
#include <time.h>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
#include <event.h>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)
void connection_time(int fd, short event, struct event *arg)...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
char buf[32];
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
struct tm t;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
time_t now;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
time(&now);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
localtime_r(&now, &t);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
asctime_r(&t, buf);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
write(fd, buf, strlen(buf));
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
shutdown(fd, SHUT_RDWR);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
free(arg);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)
void connection_accept(int fd, short event, void *arg)...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
/**//* for debugging */
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
fprintf(stderr, "%s(): fd = %d, event = %d. ", __func__, fd, event);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
/**//* 接受新的连接. */
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
struct sockaddr_in s_in;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
socklen_t len = sizeof(s_in);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
int ns = accept(fd, (struct sockaddr *) &s_in, &len);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
if (ns < 0) ...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
perror("accept");
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
return;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
struct event *ev = malloc(sizeof(struct event));
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
event_set(ev, ns, EV_WRITE, (void *) connection_time, ev);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
event_add(ev, NULL);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)
int main(void)...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
/**//* 创建socket. */
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
int s = socket(PF_INET, SOCK_STREAM, 0);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
if (s < 0) ...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
perror("socket");
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
exit(1);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
/**//* 绑定bind() */
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
struct sockaddr_in s_in;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
bzero(&s_in, sizeof(s_in));
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
s_in.sin_family = AF_INET;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
s_in.sin_port = htons(7000);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
s_in.sin_addr.s_addr = INADDR_ANY;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
if (bind(s, (struct sockaddr *) &s_in, sizeof(s_in)) < 0) ...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
perror("bind");
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
exit(1);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
/**//* 监听listen() */
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
if (listen(s, 5) < 0) ...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
perror("listen");
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
exit(1);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
/**//* 初始化libevent. */
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
event_init();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
/**//* 创建事件 */
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
struct event ev;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
event_set(&ev, s, EV_READ | EV_PERSIST, connection_accept, &ev);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
/**//* 添加事件 */
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
event_add(&ev, NULL);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
event_dispatch();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
return 0;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
怕有新手不懂,补充两句了:
其实我一直认为,任何单一的编程模型本身,必能找出一个贴切的隐喻或现实参照物,只要洞悉本质,一切皆是那么容易(当然,我这里并不否认复合模型的复杂性)。
libevent是基于事件的,看它名字都知道,我大致说说上面小例子的章法:
拿个龌龊例子来隐喻下,假设有个买春店,这个店好比server socket,各种服务(如按摩,做爱)代表事件(连接,可读),小姐们代表一个个对等客户连接,客房代表空闲线程,嫖客好比过来的客户端连接,嫖客有欲望了,找上买春店,绑定到具体的对等连接(小姐),激发了连接事件并执行对应的回调(比如按摩,做爱),此回调被安排在某个空闲的客房(比如线程池中某个空闲连接)里进行。如果嫖客与小妹是初次见面,那么留下联系方式,日后嫖客有需求,来店后可直接去找老相好(这就是为何在connection_accept回调函数中还要注册一次,因为第一次事件注册是没有目标性的,也即来个嫖客就随便找个小妹,而在回调中对连接事件的注册已经是有目标性的注册了,下次嫖客再来,就找上次的老相好,这样子做是有必要性的,对于嫖客,妹子而言可能旧情未了,需要重温, 而对于网络通信,可能前面的通信并没有把数据传递完,所以需要把对等端连接关联起来,记录上次数据处理的状态信息,以备后续处理)。
相关文章推荐
- 高性能的分布式内存缓存服务器系统——memcached核心原理详细剖析
- 高性能的分布式内存对象缓存系统Memcached
- memcached高性能的分布式的内存对象缓存系统
- Memcached:高性能分布式对象缓存系统
- 剖析Memcached分布式内存对象缓存系统的工作原理
- Memcached 高性能分布式对象缓存系统
- 高性能、分布式内存对象缓存系统memcached初识
- Memcached 是一个高性能的分布式内存对象缓存系统
- 高性能的分布式内存对象缓存系统Memcached
- 第二篇:Python高性能Web框架tornado源码剖析之待请求阶段 推荐
- 高性能分布式对象缓存系统MemCached
- PHP高性能分布式内存对象缓存系统扩展Memcached的安装及使用
- 高性能的分布式内存对象缓存系统-Memcached简介
- 在LAMP架构中安装Memcached高性能内存对象缓存应用(内含所有源码包)
- Memcached:高性能分布式对象缓存系统 推荐
- [转]memcache_engine + memcachedb = 高性能分布式内存数据库
- .NET分布式缓存之Memcached执行速度检测
- libevent源码深度剖析八
- libevent源码深度剖析四