-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtimer.cpp
More file actions
100 lines (93 loc) · 1.82 KB
/
Copy pathtimer.cpp
File metadata and controls
100 lines (93 loc) · 1.82 KB
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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
#include <memory>
#include <unistd.h>
#include <iostream>
#include <unordered_map>
#include <sys/time.h>
#include <string>
#include <deque>
#include <queue>
#include <sys/epoll.h>
#include "mutexLock.hpp"
#include "timer.h"
#include "requestData.h"
#include "epoll.h"
#include "timer.h"
using namespace std;
TimerNode::TimerNode(shared_ptr<RequestData> _request_data, int timeout):
deleted(false),
request_data(_request_data)
{
struct timeval now;
gettimeofday(&now, NULL);
expired_time = ((now.tv_sec * 1000) + (now.tv_usec / 1000)) + timeout;
}
TimerNode::~TimerNode()
{
if(request_data)
{
Epoll::epoll_del(request_data->getFd());
}
}
void TimerNode::update(int timeout)
{
struct timeval now;
gettimeofday(&now, NULL);
expired_time = ((now.tv_sec * 1000) + (now.tv_usec / 1000)) +timeout;
}
bool TimerNode::isValid()
{
struct timeval now;
gettimeofday(&now, NULL);
size_t tmp = ((now.tv_sec * 1000) + (now.tv_usec / 1000));
if(tmp < expired_time)
{
return true;
}
else
{
this->setDeleted();
return false;
}
}
void TimerNode::clearReq()
{
request_data.reset();
this->setDeleted();
}
void TimerNode::setDeleted()
{
deleted = true;
}
bool TimerNode::isDeleted() const
{
return deleted;
}
size_t TimerNode::getExpireTime() const
{
return expired_time;
}
void TimerManager::addTimer(shared_ptr<RequestData> request_data, int timeout)
{
shared_ptr<TimerNode> new_node(new TimerNode(request_data, timeout));
{
MutexLockGuard locker(this->lock);
timerNodeQueue.push(new_node);
}
request_data->linkTimer(new_node);
}
void TimerManager::handle_expired_events()
{
MutexLockGuard locker(lock);
while(!timerNodeQueue.empty())
{
shared_ptr<TimerNode> ptimer_now = timerNodeQueue.top();
if( (ptimer_now->isDeleted()) || (ptimer_now->isValid() == false))
{
timerNodeQueue.pop();
}
else
{
break;
}
}
}