-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTimerManager.h
47 lines (39 loc) · 1.03 KB
/
TimerManager.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
47
#pragma once
#include <queue>
#include <vector>
#include <sys/time.h>
#include <unordered_map>
#include <memory>
typedef long long LL;
class Channel;
typedef std::shared_ptr<Channel> SP_Channel;
class TimerNode{
public:
TimerNode(SP_Channel Channel, int timeout);
~TimerNode();
LL getExpiredtime();
void update(int timeout);
bool isValib();
bool isDeleted();
SP_Channel getChannel();
private:
SP_Channel channel;
LL expiredtime;
};
typedef std::shared_ptr<TimerNode> SP_TimerNode;
struct TimerCmp{
bool operator()(SP_TimerNode& a, SP_TimerNode& b){
return a->getExpiredtime()>b->getExpiredtime();
}
};
class TimerManager{
public:
void addTimer(SP_Channel channel, int timeout);
void handleExpiredEvent();
private:
std::priority_queue<SP_TimerNode,
std::vector<SP_TimerNode>,
TimerCmp > timerheap;
std::unordered_map<int,SP_TimerNode> timermap;
};
typedef std::shared_ptr<TimerManager> SP_TimerManager;