-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTimerManager.cpp
73 lines (62 loc) · 1.6 KB
/
TimerManager.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
#include "TimerManager.h"
#include "Channel.h"
#include "MemoryPool.h"
typedef struct timeval ST;
TimerNode::TimerNode(SP_Channel Channel, int timeout)
: channel(Channel)
{
ST now;
gettimeofday(&now, NULL);
expiredtime = (LL)now.tv_sec*1000 + now.tv_usec/1000 + timeout;
}
TimerNode::~TimerNode(){
}
LL TimerNode::getExpiredtime(){
return expiredtime;
}
void TimerNode::update(int timeout){
ST now;
gettimeofday(&now, NULL);
expiredtime=(LL)now.tv_sec*1000+now.tv_usec/1000+timeout;
}
bool TimerNode::isValib(){// set timeout time
ST now;
gettimeofday(&now,NULL);
LL temp=(LL)now.tv_sec*1000+now.tv_usec/1000;
if(temp<expiredtime)
return true;
return false;
}
bool TimerNode::isDeleted(){
return channel->isDeleted();
}
SP_Channel TimerNode::getChannel(){
return channel;
}
void TimerManager::addTimer(SP_Channel channel, int timeout){
SP_TimerNode timernode(newElement<TimerNode>(channel,timeout),
deleteElement<TimerNode>);
int cfd = channel->getFd();
if(channel->isFirst()){// detect if it's the first time to come
timerheap.push(timernode);
channel->setnotFirst();
}
timermap[cfd] = std::move(timernode);
}
void TimerManager::handleExpiredEvent(){
while(!timerheap.empty()){
SP_TimerNode now=timerheap.top();
if(now->isDeleted()||!now->isValib()){
timerheap.pop();
SP_TimerNode timernode=timermap[now->getChannel()->getFd()];
if(now==timernode){
timermap.erase(now->getChannel()->getFd());
now->getChannel()->handleClose();
}
else
timerheap.push(timernode);
}
else
break;
}
}