-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTimer.h
46 lines (38 loc) · 1.07 KB
/
Timer.h
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
#pragma once
#include "base/MutexLock.h"
#include "base/noncopyable.h"
#include <memory>
#include <queue>
#include <deque>
class HttpData;
class TimerNode {
std::shared_ptr<HttpData> SPHttpData_;
public:
TimerNode(std::shared_ptr<HttpData> requestData, int timeout);
~TimerNode();
TimerNode(TimerNode &tn);
void update(int timeout);
bool isValid();
void clearReq();
void setDeleted() { deleted_ = true; }
bool isDeleted() const { return deleted_; }
size_t getExpTime() const { return expiredTime_; }
private:
bool deleted_;
size_t expiredTime_;
};
struct TimerCmp {
bool operator()(std::shared_ptr<TimerNode>& a, std::shared_ptr<TimerNode>& b) const {
return a->getExpTime() > b->getExpTime();
}
};
class TimerManager {
public:
TimerManager();
~TimerManager();
void addTimer(std::shared_ptr<HttpData> SPHttpData, int timeout);
void handleExpiredEvent();
private:
typedef std::shared_ptr<TimerNode> SPTimerNode;
std::priority_queue<SPTimerNode, std::deque<SPTimerNode>,TimerCmp> timerNodeQueue_;
};