redis 事件处理
来源:互联网 发布:淘宝网九块九包邮女装 编辑:程序博客网 时间:2024/05/16 07:16
事件处理其实很简单,下面这个函数
403 //主函数404 void aeMain(aeEventLoop *eventLoop) {405 eventLoop->stop = 0;406 while (!eventLoop->stop) {407 if (eventLoop->beforesleep != NULL)408 eventLoop->beforesleep(eventLoop);409 aeProcessEvents(eventLoop, AE_ALL_EVENTS);410 }411 }
就是不断的循环,调用 aeProcessEvents.
aeProcessEvents:
这个函数就是处理事件的,包括了时间事件和文件事件。
int aeProcessEvents(aeEventLoop *eventLoop, int flags){ int processed = 0, numevents; /* Nothing to do? return ASAP */ if (!(flags & AE_TIME_EVENTS) && !(flags & AE_FILE_EVENTS)) return 0; /* Note that we want call select() even if there are no * file events to process as long as we want to process time * events, in order to sleep until the next time event is ready * to fire. */ //即使没有文件事件,只要有时间事件(并且 AE_DONT_WAIT), 也计算等待时间 if (eventLoop->maxfd != -1 || ((flags & AE_TIME_EVENTS) && !(flags & AE_DONT_WAIT))) { int j; aeTimeEvent *shortest = NULL; struct timeval tv, *tvp; //查找和现在最近的事件 if (flags & AE_TIME_EVENTS && !(flags & AE_DONT_WAIT)) shortest = aeSearchNearestTimer(eventLoop); if (shortest) { //最早触发事件的时间 long now_sec, now_ms; /* Calculate the time missing for the nearest * timer to fire. */ aeGetTime(&now_sec, &now_ms); tvp = &tv; tvp->tv_sec = shortest->when_sec - now_sec; //计算时间差值,微妙不够,从秒这里拿1 if (shortest->when_ms < now_ms) { tvp->tv_usec = ((shortest->when_ms+1000) - now_ms)*1000; tvp->tv_sec --; } else { tvp->tv_usec = (shortest->when_ms - now_ms)*1000; } //最早触发时间已经过了,那就不需要等了 if (tvp->tv_sec < 0) tvp->tv_sec = 0; if (tvp->tv_usec < 0) tvp->tv_usec = 0; } else { /* If we have to check for events but need to return * ASAP because of AE_DONT_WAIT we need to se the timeout * to zero */ if (flags & AE_DONT_WAIT) { tv.tv_sec = tv.tv_usec = 0; tvp = &tv; } else { /* Otherwise we can block */ tvp = NULL; /* wait forever */ } } //从上面计算的到在这里阻塞的时间 numevents = aeApiPoll(eventLoop, tvp); for (j = 0; j < numevents; j++) { aeFileEvent *fe = &eventLoop->events[eventLoop->fired[j].fd]; int mask = eventLoop->fired[j].mask; int fd = eventLoop->fired[j].fd; int rfired = 0; /* note the fe->mask & mask & ... code: maybe an already processed * event removed an element that fired and we still didn't * processed, so we check if the event is still valid. */ if (fe->mask & mask & AE_READABLE) { rfired = 1; fe->rfileProc(eventLoop,fd,fe->clientData,mask); } if (fe->mask & mask & AE_WRITABLE) { if (!rfired || fe->wfileProc != fe->rfileProc) fe->wfileProc(eventLoop,fd,fe->clientData,mask); } processed++; } } /* Check time events */ if (flags & AE_TIME_EVENTS) processed += processTimeEvents(eventLoop); return processed; /* return the number of processed file/time events */}
我们可以把主程序当作是一个开车的父亲,时间事件是接女儿放学,文件事件是去车站接客人赚钱。
那么AE_DONT_WAIT可以理解为不能在车站停靠。
1 首先决定是否需要计算在车站等待时间:
if (eventLoop->maxfd != -1 ||
((flags & AE_TIME_EVENTS) && !(flags & AE_DONT_WAIT)))
需要满足下面任何一个条件:
一 有客人会出现
二 今天要去接女儿 并且 车站允许停靠车子
2 计算女儿放学时间:
如果今天上学,
那么就得到放学时间,和当前时间比较,如果还没有到,那么再等等,到了就不要等了
如果不上学,那么就只要处理乘客,如果车站不让听,马上走,如果给停,一定等待乘客出现
3 把乘客送过去(如果有)
4 送女儿回家(如果上学)
- redis 事件处理
- Redis 事件处理
- Redis的事件处理机制
- 深入redis内部--事件处理机制
- redis对过期事件的处理
- redis学习笔记->事件处理流程
- Redis事件处理及源码剖析
- redis网络事件的特殊处理——stale event
- Redis源码分析笔记5-事件处理组件AE
- Redis文件事件处理用到的基本数据结构视图
- redis3.0.7源码阅读(十二)redis事件处理
- redis 事件
- Redis 事件
- redis事件
- redis 事件
- Redis事件
- 【redis】事件
- Redis源码剖析和注释(十九)--- Redis 事件处理实现
- 利用NPOI开源的读写Excel、WORD等微软OLE2组件读写execl,控制样式或单元格
- 在MFC对话框中,如何让控件Static和Radio和CheckBox背景透明?
- uva 11111 - Generalized Matrioshkas
- 使用gdbserver远程调试
- 飞思卡尔推出四款脚位相容的64位处理器
- redis 事件处理
- 从来没有一种工作叫钱多、事少、离家近(转)
- mastudio 使用webpage来获取输出网页
- ibatis的一些优点
- 第三章——分组密码
- GDB远程调试
- Android JNI 头文件的生成
- ORACLE数据库NLS_CHARACTERSET和NLS_NCHAR_CHARACTERSET区别
- 为UITableViewCell增加按钮及处理按钮事件的方法