-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathTimer.cpp
76 lines (66 loc) · 1.94 KB
/
Timer.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
/*************************************************************************
> File Name: Timer.cpp
> Author: zhangfeng
> Mail: [email protected]
> Created Time: Wed 02 Oct 2019 07:34:15 PM CST
> Target:
************************************************************************/
#include "Timer.h"
#include <sys/time.h>
#include <unistd.h>
#include <queue>
TimerNode::TimerNode(std::shared_ptr<HttpData> requestData, int timeout)
: deleted_(false),
SPHttpData(requestData)
{
struct timeval now;
gettimeofday(&now, nullptr);
expiredTime_ = (((now.tv_sec % 10000)*1000) + (now.tv_usec / 1000)) + timeout;
}
TimerNode::TimerNode(TimerNode &timerNode)
: SPHttpData(timerNode.SPHttpData)
{}
TimerNode::~TimerNode() {
if(SPHttpData) {
SPHttpData->handleClose();
}
}
void TimerNode::update(int timeout) {
struct timeval now;
gettimeofday(&now, nullptr);
expiredTime_ = (((now.tv_sec % 10000) * 1000) + (now.tv_usec / 1000)) + timeout;
}
bool TimerNode::isValid() {
struct timeval now;
gettimeofday(&now, nullptr);
size_t diff = (((now.tv_sec % 10000) * 1000) + (now.tv_usec / 1000));
if(diff < expiredTime_) {
return true;
} else {
this->setDeleted();
return false;
}
}
void TimerNode::clearReq() {
SPHttpData.reset();
this->setDeleted();
}
TimerManager::TimerManager() {}
TimerManager::~TimerManager() {}
void TimerManager::addTimer(std::shared_ptr<HttpData> SPHttpData, int timeout) {
SPTimerNode new_node(new TimerNode(SPHttpData, timeout));
timerNodeQueue.push(new_node);
SPHttpData->linkTimer(new_node);
}
void TimerManager::handleExpiredEvent() {
while(!timerNodeQueue.empty()) {
SPTimerNode ptimer_now = timerNodeQueue.top();
if(ptimer_now->isDeleted()) {
timerNodeQueue.pop();
} else if(ptimer_now->isValid() == false) {
timerNodeQueue.pop();
} else {
break;
}
}
}