Reator中的定时器
来源:互联网 发布:淘宝收藏店铺失败 编辑:程序博客网 时间:2024/04/28 20:34
在封装好了的MmReator中
在头文件中:先是有一个定时器,再接着是定时任务(只是列出相关的代码)
/** * 定时器 */ struct Timer { Timer(unsigned int i, long long t) : timerId(i), absTime(t) {} unsigned int timerId; long long absTime; bool operator <(const Timer& t) const { return absTime > t.absTime; } void reset(){ absTime = 0; timerId = 0; } }; /** * 定时任务 */ struct Task { Task(EventHandler* h, int f) : handler(h), fd(f) {} EventHandler*handler; int fd;}; unsigned int _timerId;vector<Timer> _timerHeap;map<unsigned int, Task> _tasks;timeval_timeVal;
1)unsigned int Reactor::setTimer(EventHandler*handler,int timeout);//设置定时处理
2) void Reactor::cancelTimer(unsigned int timerId);//取消定时处理
3) void Reactor::fireTimeout();//处理超时
4)int Reactor::nextTimeout();//获取下一超时时间
unsigned int Reactor::setTimer(EventHandler* handler, int timeout) { int fd = handler->getSocket(); if (fd < 0) { return 0; } else if (fd >= static_cast<int>(_ctxs.size())) { _ctxs.insert(_ctxs.end(), fd + 1 - _ctxs.size(), EMPTY_CTX); } _ctxs[fd].handler = handler; // ++_timerId; if(0 == _timerId) { ++_timerId; } //加入任务映射表 Task t(handler, fd); _tasks.insert(make_pair(_timerId, t)); //加入计时器堆 Timer timer(_timerId, now() + timeout); _timerHeap.push_back(timer); push_heap(_timerHeap.begin(), _timerHeap.end()); return _timerId;}
void Reactor::cancelTimer(unsigned int timerId) { map<unsigned int, Task>::iterator iter = _tasks.find(timerId); if (iter != _tasks.end()) { int fd = iter->second.fd; if (fd >= 0 && fd < static_cast<int>(_ctxs.size()) && _ctxs[fd].events == 0) { _ctxs[fd].handler = 0; } _tasks.erase(iter); }}
void Reactor::fireTimeout() { while (!_timerHeap.empty()) { //检查是否超时 if (_timerHeap.front().absTime > now()) { break; } //删除堆顶 Timer timer = _timerHeap.front(); pop_heap(_timerHeap.begin(), _timerHeap.end()); _timerHeap.pop_back(); //检查计时器是否已经取消 map<unsigned int, Task>::iterator iter = _tasks.find(timer.timerId); if (iter == _tasks.end()) { continue; } //删除任务 Task task = iter->second; _tasks.erase(iter); //检查是否有效句柄 if (task.fd < 0 || task.fd >= static_cast<int>(_ctxs.size())) { continue; } //检查是否已经失效 EventHandler* handler = _ctxs[task.fd].handler; if (task.handler != handler || handler == 0) { continue; } if (_ctxs[task.fd].events == 0) { _ctxs[task.fd].handler = 0; } //处理超时 if (!handler->handleTimeout(timer.timerId)) { fireError(0, handler); } }}
int Reactor::nextTimeout() { if (_timerHeap.empty()) { return -1; } else { return max(0, int(_timerHeap.front().absTime - now())); }}
0 0
- Reator中的定时器
- Reator模式 论文阅读整理
- JavaScript中的定时器
- spring中的定时器
- jAVA 中的定时器
- JavaScript中的定时器
- VC中的定时器
- Java中的定时器.
- js 中的定时器 例子
- java中的定时器
- Linux中的定时器
- java中的定时器Timer
- 网络游戏中的定时器设计
- linux应用程序中的定时器
- dll工程中的定时器
- javascript中的定时器
- MFC中的定时器
- MFC中的定时器用法
- 博客声明!
- LeetCode 263. Ugly Number
- cdh配置kerberos
- svn
- python中mxnet相关函数记录
- Reator中的定时器
- mongodb聚合中$group的联合主键写法
- The linux command line--part one(一) Learing Shell
- Jmeter统计返回值数量
- 【移动端html5】 android video播放进度精确控制
- CentOS的sz,rz的安装以及使用
- Service和Thread有什么关系
- Linux 权限管理控制 + 用户管理
- Linux crontab使用详解(转载)