-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTimer.h
More file actions
53 lines (33 loc) · 907 Bytes
/
Timer.h
File metadata and controls
53 lines (33 loc) · 907 Bytes
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
#ifndef HTTPSERVER_TIMER_H
#define HTTPSERVER_TIMER_H
#include <stdint.h>
#include <vector>
#include <map>
#include <functional>
#include <memory>
const int kTimerSlot = 60 * 1000;
typedef std::function<void()> TimerCallback;
struct TimerNode {
int fd;
int64_t expired;
TimerCallback callback;
};
class TimerHeap {
public:
typedef std::shared_ptr<TimerHeap> ptr;
TimerHeap()=default;
~TimerHeap();
void addTimer(int connFd, int64_t interval, TimerCallback callback);
void adjustTimer(int fd);
void delTimer(int fd);
void tick();
int64_t getNextTick();
private:
void del_(int index);
int siftUp_(int index); // 上浮
int siftDown_(int index, int len); // 下沉
void swapNode_(int lhs, int rhs); // 交换节点
std::map<int, int> fd2Idx_;
std::vector<TimerNode*> timers_;
};
#endif // HTTPSERVER_TIMEQUEUE_H