-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtimer.cpp
More file actions
128 lines (113 loc) · 2.52 KB
/
Copy pathtimer.cpp
File metadata and controls
128 lines (113 loc) · 2.52 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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
#include "timer.h"
#include "epoll.h"
#include <unordered_map>
#include <string>
#include <sys/time.h>
#include <unistd.h>
#include <deque>
#include <queue>
#include <iostream>
using namespace std;
TimerNode::TimerNode(SP_ReqData _request_data, int timeout):
deleted(false),
request_data(_request_data)
{
//cout << "TimerNode()" << endl;
struct timeval now;
gettimeofday(&now, NULL);
// 以毫秒计
expired_time = ((now.tv_sec * 1000) + (now.tv_usec / 1000)) + timeout;
}
TimerNode::~TimerNode()
{
//cout << "~TimerNode()" << endl;
if (request_data)
{
Epoll::epoll_del(request_data->getFd());
}
//request_data.reset();
// if (request_data)
// {
// cout << "request_data=" << request_data << endl;
// delete request_data;
// request_data = NULL;
// }
}
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 temp = ((now.tv_sec * 1000) + (now.tv_usec / 1000));
if (temp < 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::getExpTime() const
{
return expired_time;
}
TimerManager::TimerManager()
{
}
TimerManager::~TimerManager()
{
}
void TimerManager::addTimer(SP_ReqData request_data, int timeout)
{
SP_TimerNode new_node(new TimerNode(request_data, timeout));
{
MutexLockGuard locker(lock);
TimerNodeQueue.push(new_node);
}
request_data->linkTimer(new_node);
}
void TimerManager::addTimer(SP_TimerNode timer_node)
{
}
void TimerManager::handle_expired_event()
{
MutexLockGuard locker(lock);
while (!TimerNodeQueue.empty())
{
SP_TimerNode ptimer_now = TimerNodeQueue.top();
if (ptimer_now->isDeleted())
{
TimerNodeQueue.pop();
//delete ptimer_now;
}
else if (ptimer_now->isvalid() == false)
{
TimerNodeQueue.pop();
//delete ptimer_now;
}
else
{
break;
}
}
}