1 Star 0 Fork 1

taozier/roughserver

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
Timer.cpp 1.55 KB
一键复制 编辑 原始数据 按行查看 历史
taozier 提交于 2020-06-07 04:59 . update
#include "Timer.h"
#include "HTTPData.h"
Timer::Timer(std::shared_ptr<HTTPData> requestData, int timeout)
: deleted_(false), httpdata_(requestData)
{
struct timeval now;
gettimeofday(&now, NULL);
expiredTime_ = timeConvert(now) + timeout;
}
Timer::~Timer()
{
if (httpdata_)
httpdata_->closeConn(); //超时后,析构时关闭连接
}
/*
Timer::Timer(Timer &timer)
: httpdata_(timer.httpdata_), expiredTime_(0)
{
}
void Timer::update(int timeout)
{
struct timeval now;
gettimeofday(&now, NULL);
expiredTime_ = timeConvert(now) + timeout;
}
*/
bool Timer::isUnexpired()
{
struct timeval now;
gettimeofday(&now, NULL);
size_t temp = timeConvert(now);
if (temp < expiredTime_)
return true;
else
{
this->setDeleted();
return false;
}
}
void Timer::resetHTTPSP()
{
httpdata_.reset(); //shared_ptr重置
this->setDeleted();
}
TimerManager::TimerManager() {}
TimerManager::~TimerManager() {}
void TimerManager::addTimer(std::shared_ptr<HTTPData> httpdata_, int timeout)
{
TimerSP newTimer(new Timer(httpdata_, timeout));
timerQueue.push(newTimer);
httpdata_->setTimer(newTimer);
}
void TimerManager::handleExpiredEvent()
{
while (!timerQueue.empty())
{
TimerSP topTimer = timerQueue.top();
if (topTimer->isDeleted())
timerQueue.pop();
else if (topTimer->isUnexpired() == false)
timerQueue.pop(); //pop后,pop出来的Timer的shared_ptr引用减为0,该Timer析构
else
break;
}
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
C++
1
https://gitee.com/taozier/roughserver.git
git@gitee.com:taozier/roughserver.git
taozier
roughserver
roughserver
master

搜索帮助