-
Notifications
You must be signed in to change notification settings - Fork 5
/
Timer.h
executable file
·48 lines (42 loc) · 1.32 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
47
48
#ifndef KHAKI_TIMER_H
#define KHAKI_TIMER_H
#include <time.h>
#include <sys/time.h>
#include <sys/timerfd.h>
#include <stdio.h>
#include <vector>
#include <memory>
#include <map>
#include "Log.h"
namespace khaki {
typedef std::function<void()> TimerCallback;
class Timer {
public:
Timer(const TimerCallback& cb, unsigned int timeout, unsigned int iv) :
timeout_(timeout), cb_(cb), iv_(iv) {
}
~Timer(){}
unsigned int GetIv() { return iv_; }
void AddTimeOut() { timeout_ += iv_; }
unsigned int GetTimeoutTime() { return timeout_; }
void TimeOut() { if (cb_) cb_(); else log4cppError(logger, "bad function"); }
private:
unsigned int timeId;
unsigned int timeout_;
unsigned int iv_;
TimerCallback cb_;
};
class TimerManager {
public:
TimerManager();
~TimerManager();
void AddTimer(const TimerCallback& cb, unsigned int timeout/*second*/, unsigned int iv/*second*/);
void RemoveTimer(unsigned int timerId);
void Run(unsigned int timeout);
private:
unsigned long long ddwTimerId;
std::multimap<unsigned int/*timeout*/, unsigned int/*timerId*/> timerLists;
std::map<unsigned int/*timerId*/, Timer> timerIdLists;
};
}
#endif