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
原创粉丝点击