上一篇讲完了initServer的大体流程,其中aeCreateEventLoop(),这个函数

没有详细说明,我们在这一篇里讲述Ae.h和Ae.c, 这里面的api阐述了如何创建

eventLoop和添加文件读写事件等等。

ae.h中的解释

  1. //文件读写事件回调函数
  2. typedef void aeFileProc(struct aeEventLoop *eventLoop, int fd, void *clientData, int mask);
  3.  
  4. //定时器回调函数
  5. typedef int aeTimeProc(struct aeEventLoop *eventLoop, long long id, void *clientData);
  6. //事件结束回调函数,析构一些资源
  7. typedef void aeEventFinalizerProc(struct aeEventLoop *eventLoop, void *clientData);
  8. //不是很清楚,应该是进程结束前做的回调函数
  9. typedef void aeBeforeSleepProc(struct aeEventLoop *eventLoop);
  10.  
  11. //文件事件回调函数
  12. typedef struct aeFileEvent {
  13. int mask; /* one of AE_(READABLE|WRITABLE) */ //文件事件类型 读/写
  14. aeFileProc *rfileProc;
  15. aeFileProc *wfileProc;
  16. void *clientData;
  17. } aeFileEvent;
  18.  
  19. /* A fired event */
  20. typedef struct aeFiredEvent {
  21. int fd; ////已出现的事件的文件号对应的事件描述在aeEventLoop.events[]中的下标
  22. int mask; //文件事件类型 AE_WRITABLE||AE_READABLE
  23. } aeFiredEvent;
  24.  
  25. typedef struct aeTimeEvent {
  26. long long id; /* time event identifier. */ //由aeEventLoop.timeEventNextId进行管理
  27. long when_sec; /* seconds */
  28. long when_ms; /* milliseconds */
  29. aeTimeProc *timeProc;
  30. aeEventFinalizerProc *finalizerProc;
  31. void *clientData;
  32. struct aeTimeEvent *next;
  33. } aeTimeEvent;
  34.  
  35. /* State of an event based program */
  36. typedef struct aeEventLoop {
  37. int maxfd; //监听的最大文件号
  38. int setsize; //跟踪的文件描述符最大数量
  39. long long timeEventNextId; //定时器事件的ID编号管理(分配ID号所用)
  40. time_t lastTime; /* Used to detect system clock skew */
  41. aeFileEvent *events; //注册的文件事件,这些是需要进程关注的文件
  42. aeFiredEvent *fired; //poll结果,待处理的文件事件的文件号和事件类型
  43. aeTimeEvent *timeEventHead; //定时器时间链表
  44. int stop; //时间轮询是否结束?
  45. void *apidata; //polling API 特殊的数据
  46. aeBeforeSleepProc *beforesleep; //休眠前的程序
  47. } aeEventLoop;
  48.  
  49. /* Prototypes */
  50. //创建eventLoop结构
  51. aeEventLoop *aeCreateEventLoop(int setsize);
  52. //删除eventloop
  53. void aeDeleteEventLoop(aeEventLoop *eventLoop);
  54. //事件派发停止
  55. void aeStop(aeEventLoop *eventLoop);
  56. //添加文件读写事件
  57. int aeCreateFileEvent(aeEventLoop *eventLoop, int fd, int mask,
  58. aeFileProc *proc, void *clientData);
  59. //删除文件读写事件
  60. void aeDeleteFileEvent(aeEventLoop *eventLoop, int fd, int mask);
  61. //获取文件事件对应类型(读或写)
  62. int aeGetFileEvents(aeEventLoop *eventLoop, int fd);
  63. //创建定时器事件
  64. long long aeCreateTimeEvent(aeEventLoop *eventLoop,
  65. long long milliseconds,aeTimeProc *proc, void *clientData,
  66. aeEventFinalizerProc *finalizerProc);
  67. //删除定时器事件
  68. int aeDeleteTimeEvent(aeEventLoop *eventLoop, long long id);
  69. //派发事件
  70. int aeProcessEvents(aeEventLoop *eventLoop, int flags);
  71. //等待millionseconds直到文件描述符可读或者可写
  72. int aeWait(int fd, int mask, long long milliseconds);
  73. //ae事件轮询主函数
  74. void aeMain(aeEventLoop *eventLoop);
  75. //获取当前网络模型
  76. char *aeGetApiName(void);
  77. //进程休眠前回调函数
  78. void aeSetBeforeSleepProc(aeEventLoop *eventLoop,
  79. aeBeforeSleepProc *beforesleep);
  80. //获取eventloop所有的事件个数
  81. int aeGetSetSize(aeEventLoop *eventLoop);
  82. //重新设置eventloop事件个数
  83. int aeResizeSetSize(aeEventLoop *eventLoop, int setsize);

ae.cpp中,一个函数一个函数解析

  1. //定义了几个宏,根据不同的宏加载
  2. //不同的网络模型
  3. #ifdef HAVE_EVPORT
  4. #include "ae_evport.c"
  5. #else
  6. #ifdef HAVE_EPOLL
  7. #include "ae_epoll.c"
  8. #else
  9. #ifdef HAVE_KQUEUE
  10. #include "ae_kqueue.c"
  11. #else
  12. #include "ae_select.c"
  13. #endif
  14. #endif
  15. #endif

aeCreateEventLoop,主要负责eventloop结构的创建和初始化,以及模型的初始化

  1. aeEventLoop *aeCreateEventLoop(int setsize) {
  2. aeEventLoop *eventLoop;
  3. int i;
  4. //创建eventloop
  5. if ((eventLoop = zmalloc(sizeof(*eventLoop))) == NULL) goto err;
  6. //为进程要注册的文件开辟空间
  7. eventLoop->events = zmalloc(sizeof(aeFileEvent)*setsize);
  8. //为激活的要处理的文件开辟空间
  9. eventLoop->fired = zmalloc(sizeof(aeFiredEvent)*setsize);
  10. //开辟失败报错
  11. if (eventLoop->events == NULL || eventLoop->fired == NULL) goto err;
  12. //设置监听事件总数
  13. eventLoop->setsize = setsize;
  14. //更新为当前时间
  15. eventLoop->lastTime = time(NULL);
  16. eventLoop->timeEventHead = NULL;
  17. eventLoop->timeEventNextId = ;
  18. eventLoop->stop = ;
  19. eventLoop->maxfd = -;
  20. eventLoop->beforesleep = NULL;
  21. //将不同模式的api注册到eventloop里
  22. if (aeApiCreate(eventLoop) == -) goto err;
  23. /* Events with mask == AE_NONE are not set. So let's initialize the
  24. * vector with it. */
  25. for (i = ; i < setsize; i++)
  26. //将所有文件事件类型初始为空
  27. eventLoop->events[i].mask = AE_NONE;
  28. return eventLoop;
  29.  
  30. err:
  31. if (eventLoop) {
  32. zfree(eventLoop->events);
  33. zfree(eventLoop->fired);
  34. zfree(eventLoop);
  35. }
  36. return NULL;
  37. }

//事件队列大小和重置

  1. //获取eventloop事件队列大小
  2. int aeGetSetSize(aeEventLoop *eventLoop) {
  3. return eventLoop->setsize;
  4. }
  5.  
  6. //重新设置大小
  7. int aeResizeSetSize(aeEventLoop *eventLoop, int setsize) {
  8. int i;
  9.  
  10. if (setsize == eventLoop->setsize) return AE_OK;
  11. if (eventLoop->maxfd >= setsize) return AE_ERR;
  12. //不同的网络模型调用不同的resize
  13. if (aeApiResize(eventLoop,setsize) == -) return AE_ERR;
  14. //重新开辟空间
  15. eventLoop->events = zrealloc(eventLoop->events,sizeof(aeFileEvent)*setsize);
  16. eventLoop->fired = zrealloc(eventLoop->fired,sizeof(aeFiredEvent)*setsize);
  17. eventLoop->setsize = setsize;
  18.  
  19. /* Make sure that if we created new slots, they are initialized with
  20. * an AE_NONE mask. */
  21. //重新初始化事件类型
  22. for (i = eventLoop->maxfd+; i < setsize; i++)
  23. eventLoop->events[i].mask = AE_NONE;
  24. return AE_OK;
  25. }

删除eventloop和stop事件轮询

  1. //删除eventloop结构
  2. void aeDeleteEventLoop(aeEventLoop *eventLoop) {
  3. aeApiFree(eventLoop);
  4. zfree(eventLoop->events);
  5. zfree(eventLoop->fired);
  6. zfree(eventLoop);
  7. }
  8.  
  9. //设置eventloop停止标记
  10. void aeStop(aeEventLoop *eventLoop) {
  11. eventLoop->stop = ;
  12. }

创建监听事件

  1. //创建监听事件
  2. int aeCreateFileEvent(aeEventLoop *eventLoop, int fd, int mask,
  3. aeFileProc *proc, void *clientData)
  4. {
  5. //判断fd大于eventloop设置的事件队列大小
  6. if (fd >= eventLoop->setsize) {
  7. errno = ERANGE;
  8. return AE_ERR;
  9. }
  10.  
  11. //取出对应的aeFileEvent事件
  12. aeFileEvent *fe = &eventLoop->events[fd];
  13.  
  14. //添加读写事件到不同的模型
  15. if (aeApiAddEvent(eventLoop, fd, mask) == -)
  16. return AE_ERR;
  17. //文件类型按位或
  18. fe->mask |= mask;
  19. //根据最终的类型设置读写回调函数
  20. if (mask & AE_READABLE) fe->rfileProc = proc;
  21. if (mask & AE_WRITABLE) fe->wfileProc = proc;
  22. //fe中读写操作的clientdata
  23. fe->clientData = clientData;
  24. //如果fd大于当前最大的eventLoop maxfdfd
  25. if (fd > eventLoop->maxfd)
  26. eventLoop->maxfd = fd;
  27. return AE_OK;
  28. }

删除监听事件

  1. void aeDeleteFileEvent(aeEventLoop *eventLoop, int fd, int mask)
  2. {
  3. if (fd >= eventLoop->setsize) return;
  4. aeFileEvent *fe = &eventLoop->events[fd];
  5. if (fe->mask == AE_NONE) return;
  6. //网络模型里删除对应的事件
  7. aeApiDelEvent(eventLoop, fd, mask);
  8. //清除对应的类型标记
  9. fe->mask = fe->mask & (~mask);
  10. //如果删除的fd是maxfd,并且对应的事件为空,那么更新maxfd
  11. if (fd == eventLoop->maxfd && fe->mask == AE_NONE) {
  12. /* Update the max fd */
  13. int j;
  14.  
  15. for (j = eventLoop->maxfd-; j >= ; j--)
  16. if (eventLoop->events[j].mask != AE_NONE) break;
  17. eventLoop->maxfd = j;
  18. }
  19. }
  1. //获取文件类型
  2. int aeGetFileEvents(aeEventLoop *eventLoop, int fd) {
  3. if (fd >= eventLoop->setsize) return ;
  4. aeFileEvent *fe = &eventLoop->events[fd];
  5. //返回对应的类型标记
  6. return fe->mask;
  7. }

事件派发函数

  1. //派发事件的函数
  2. int aeProcessEvents(aeEventLoop *eventLoop, int flags)
  3. {
  4. int processed = , numevents;
  5.  
  6. /* Nothing to do? return ASAP */
  7. if (!(flags & AE_TIME_EVENTS) && !(flags & AE_FILE_EVENTS)) return ;
  8.  
  9. //为了休眠,直到有时间事件触发,即便是没有文件事件处理,我们也会
  10. //调用对应的事件时间
  11. //这部分不是很清楚,知道大体意思是设置时间,
  12. //为了aeApiPoll设置等待的时间
  13. if (eventLoop->maxfd != - ||
  14. ((flags & AE_TIME_EVENTS) && !(flags & AE_DONT_WAIT))) {
  15. int j;
  16. aeTimeEvent *shortest = NULL;
  17. struct timeval tv, *tvp;
  18.  
  19. if (flags & AE_TIME_EVENTS && !(flags & AE_DONT_WAIT))
  20. shortest = aeSearchNearestTimer(eventLoop);
  21. if (shortest) {
  22. long now_sec, now_ms;
  23.  
  24. /* Calculate the time missing for the nearest
  25. * timer to fire. */
  26. aeGetTime(&now_sec, &now_ms);
  27. tvp = &tv;
  28. tvp->tv_sec = shortest->when_sec - now_sec;
  29. if (shortest->when_ms < now_ms) {
  30. tvp->tv_usec = ((shortest->when_ms+) - now_ms)*;
  31. tvp->tv_sec --;
  32. } else {
  33. tvp->tv_usec = (shortest->when_ms - now_ms)*;
  34. }
  35. if (tvp->tv_sec < ) tvp->tv_sec = ;
  36. if (tvp->tv_usec < ) tvp->tv_usec = ;
  37. } else {
  38. /* If we have to check for events but need to return
  39. * ASAP because of AE_DONT_WAIT we need to set the timeout
  40. * to zero */
  41. if (flags & AE_DONT_WAIT) {
  42. tv.tv_sec = tv.tv_usec = ;
  43. tvp = &tv;
  44. } else {
  45. /* Otherwise we can block */
  46. tvp = NULL; /* wait forever */
  47. }
  48. }
  49. //调用不同的网络模型poll事件
  50. numevents = aeApiPoll(eventLoop, tvp);
  51. for (j = ; j < numevents; j++) {
  52. //轮询处理就绪事件
  53. aeFileEvent *fe = &eventLoop->events[eventLoop->fired[j].fd];
  54. int mask = eventLoop->fired[j].mask;
  55. int fd = eventLoop->fired[j].fd;
  56. int rfired = ;
  57. //可读就绪事件
  58. if (fe->mask & mask & AE_READABLE) {
  59. rfired = ;
  60. fe->rfileProc(eventLoop,fd,fe->clientData,mask);
  61. }
  62. //可写就绪事件
  63. if (fe->mask & mask & AE_WRITABLE) {
  64. if (!rfired || fe->wfileProc != fe->rfileProc)
  65. fe->wfileProc(eventLoop,fd,fe->clientData,mask);
  66. }
  67. processed++;
  68. }
  69. }
  70. /* Check time events */
  71. //处理所有定时器事件
  72. if (flags & AE_TIME_EVENTS)
  73. processed += processTimeEvents(eventLoop);
  74.  
  75. return processed; /* return the number of processed file/time events */
  76. }
  1. /等待millionseconds,直到有可读或者可写事件触发
  2. int aeWait(int fd, int mask, long long milliseconds) {
  3. struct pollfd pfd;
  4. int retmask = , retval;
  5.  
  6. memset(&pfd, , sizeof(pfd));
  7. pfd.fd = fd;
  8. if (mask & AE_READABLE) pfd.events |= POLLIN;
  9. if (mask & AE_WRITABLE) pfd.events |= POLLOUT;
  10.  
  11. if ((retval = poll(&pfd, , milliseconds))== ) {
  12. if (pfd.revents & POLLIN) retmask |= AE_READABLE;
  13. if (pfd.revents & POLLOUT) retmask |= AE_WRITABLE;
  14. if (pfd.revents & POLLERR) retmask |= AE_WRITABLE;
  15. if (pfd.revents & POLLHUP) retmask |= AE_WRITABLE;
  16. return retmask;
  17. } else {
  18. return retval;
  19. }
  20. }
  1. //ae主函数
  2. void aeMain(aeEventLoop *eventLoop) {
  3. //stop初始为0
  4. eventLoop->stop = ;
  5. while (!eventLoop->stop) {
  6. //调用beforesleep函数
  7. if (eventLoop->beforesleep != NULL)
  8. eventLoop->beforesleep(eventLoop);
  9. //派发所有的事件
  10. aeProcessEvents(eventLoop, AE_ALL_EVENTS);
  11. }
  12. }
  13.  
  14. //获取api名字
  15. char *aeGetApiName(void) {
  16. return aeApiName();
  17. }
  18.  
  19. //sleep之前的回调函数
  20. void aeSetBeforeSleepProc(aeEventLoop *eventLoop, aeBeforeSleepProc *beforesleep) {
  21. eventLoop->beforesleep = beforesleep;
  22. }

这就是ae文件里大体的几个api,其他的没理解的还在研究。

我的微信公众号:

对于redis框架的理解(三)的更多相关文章

  1. 对于redis框架的理解(四)

    上一篇讲述了eventloop的结构和创建,添加文件事件删除文件事件,派发等等. 而eventloop主要就是调用不同网络模型完成事件监听和派发的. 这一篇主要讲述epoll网络模型,redis是如何 ...

  2. 对于redis框架的理解(二)

    之前梳理过redis main函数主体流程 大体是 initServerConfig() -> loadServerConfig() -> daemonize() -> initSe ...

  3. redis 单线程的理解

    单线程模型 Redis客户端对服务端的每次调用都经历了发送命令,执行命令,返回结果三个过程.其中执行命令阶段,由于Redis是单线程来处理命令的,所有每一条到达服务端的命令不会立刻执行,所有的命令都会 ...

  4. iOS10通知框架UserNotification理解与应用

    iOS10通知框架UserNotification理解与应用 一.引言 关于通知,无论与远程Push还是本地通知,以往的iOS系统暴漏给开发者的接口都是十分有限的,开发者只能对标题和内容进行简单的定义 ...

  5. redis之(二十一)redis之深入理解Spring Redis的使用

    关于spring redis框架的使用,网上的例子很多很多.但是在自己最近一段时间的使用中,发现这些教程都是入门教程,包括很多的使用方法,与spring redis丰富的api大相径庭,真是浪费了这么 ...

  6. Nginx Http框架的理解

    Nginx Http框架的理解 HTTP框架是Nginx基础框架的一部分,Nginx的其它底层框架如master-worker进程模型.event模块.mail 模块等. HTTP框架代码主要有2个模 ...

  7. Redis 小白指南(三)- 事务、过期、消息通知、管道和优化内存空间

    Redis 小白指南(三)- 事务.过期.消息通知.管道和优化内存空间 简介 <Redis 小白指南(一)- 简介.安装.GUI 和 C# 驱动介绍> 讲的是 Redis 的介绍,以及如何 ...

  8. Hadoop框架基础(三)

    ** Hadoop框架基础(三) 上一节我们使用eclipse运行展示了hdfs系统中的某个文件数据,这一节我们简析一下离线计算框架MapReduce,以及通过eclipse来编写关于MapReduc ...

  9. C#使用Thrift作为RPC框架入门(三)之三层架构

    前言 这是我们讲解Thrift框架的第三篇文章,前两篇我们讲了Thrift作为RPC框架的基本用法以及架构的设计.为了我们更好的使用和理解Thrift框架,接下来,我们将来学习一下Thrift框架提供 ...

随机推荐

  1. 解决登录linux输入密码问题

    1.使用密钥 ssh-keyssh -i .ssh/*.key root@<ip_addr> 2.使用sshpass 安装 rpm 包:yum install sshpass 配置文件: ...

  2. Selenium WebDriver 下 plugin container for firefox has stopped working

    用selenium 的webdriver 和 firefox 浏览器做自动化测试,经常会出现 plugin container for firefox has stopped working 如下图所 ...

  3. git实验

    四.实例应用 应用1.现有项目移植到git代管 进入目标项目,进行git初始化: 初始化:git init 修改config:git config -- local user.name '名称'  和 ...

  4. ORM(object relational Maping)

    ORM即对象关系映射,是一种为了解决面向对象与关系数据库存在的互不匹配的现象的技术. 简单的说,ORM是通过使用描述对象和数据库之间映射的元数据,将java程序中的对象自动持久化到关系数据库中.本质上 ...

  5. unrecognized selector send to instancd 快速定位

    1.在Debug菜单中Breakpoints->Create Symbolic Breakpoint; 2.在Symbolic中填写方法签名: -[NSObject(NSObject) does ...

  6. Rsyslog的三种传输协议简要介绍

    rsyslog的三种传输协议 rsyslog 可以理解为多线程增强版的syslog. rsyslog提供了三种远程传输协议,分别是: 1. UDP 传输协议 基于传统UDP协议进行远程日志传输,也是传 ...

  7. 第5章 首次登录与在线求助man page

    首次登录系统 centos默认图像界面为GNOME. Linux默认情况下会提供6个Terminal来让用户登录,切换方式为ctrl+alt+[F1-F6],系统将这六个操作界面命名为tty1-tty ...

  8. 【第八周】【新蜂】新NABCD

    由小组成员宫成荣撰写 一.小组项目申请时提交的NABCD: 痛点:普通的俄罗斯方块是不现实距离下一级有多远的,我们的游戏能显示距离下一等级游戏有多远.方便玩家体验. nabc: n:能满足大多数玩家的 ...

  9. 【第三周】【】cppunit!

    coding.net地址:https://coding.net/u/Boxer_ ssh:git@git.coding.net:Boxer_/homework.git https://coding.n ...

  10. Python入门:认识变量和字符串

    几个月前,我开始学习个人形象管理,从发型.妆容.服饰到仪表仪态,都开始做全新改造,在塑造个人风格时,最基础的是先了解自己属于哪种风格,然后找到参考对象去模仿,可以是自己欣赏的人.明星或模特等,直至最后 ...