-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathZinxTimer.cpp
More file actions
171 lines (153 loc) · 3.32 KB
/
ZinxTimer.cpp
File metadata and controls
171 lines (153 loc) · 3.32 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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
#include "ZinxTimer.h"
#include <sys/timerfd.h>
using namespace std;
ZinxTimerChannel::ZinxTimerChannel()
{
}
ZinxTimerChannel::~ZinxTimerChannel()
{
}
/*创建定时器文件描述符*/
bool ZinxTimerChannel::Init()
{
bool bRet = false;
/*创建文件描述符*/
int iFd = timerfd_create(CLOCK_MONOTONIC, 0);
if (0 <= iFd)
{
/*设置定时周期*/
struct itimerspec period = {
{1,0}, {1,0}
};
if (0 == timerfd_settime(iFd, 0, &period, NULL))
{
bRet = true;
m_TimerFd = iFd;
}
}
return bRet;
}
/*读取超时次数*/
bool ZinxTimerChannel::ReadFd(std::string & _input)
{
bool bRet = false;
char buff[8] = { 0 };
if (sizeof(buff) == read(m_TimerFd, buff, sizeof(buff)))
{
bRet = true;
_input.assign(buff, sizeof(buff));
}
return bRet;
}
bool ZinxTimerChannel::WriteFd(std::string & _output)
{
return false;
}
/*关闭文件描述符*/
void ZinxTimerChannel::Fini()
{
close(m_TimerFd);
m_TimerFd = -1;
}
/*返回当前的定时器文件描述符*/
int ZinxTimerChannel::GetFd()
{
return m_TimerFd;
}
std::string ZinxTimerChannel::GetChannelInfo()
{
return "TimerFd";
}
class output_hello :public AZinxHandler {
// 通过 AZinxHandler 继承
virtual IZinxMsg * InternelHandle(IZinxMsg & _oInput) override
{
auto pchannel = ZinxKernel::Zinx_GetChannel_ByInfo("stdout");
std::string output = "hello world";
ZinxKernel::Zinx_SendOut(output, *pchannel);
return nullptr;
}
virtual AZinxHandler * GetNextHandler(IZinxMsg & _oNextMsg) override
{
return nullptr;
}
} *pout_hello = new output_hello();
/*返回处理超时事件的对象*/
AZinxHandler * ZinxTimerChannel::GetInputNextStage(BytesMsg & _oInput)
{
return &TimerOutMng::GetInstance();
}
TimerOutMng TimerOutMng::single;
TimerOutMng::TimerOutMng()
{
/*创建10个齿*/
for (int i = 0; i < 10; i++)
{
list<TimerOutProc *> tmp;
m_timer_wheel.push_back(tmp);
}
}
IZinxMsg * TimerOutMng::InternelHandle(IZinxMsg & _oInput)
{
unsigned long iTimeoutCount = 0;
GET_REF2DATA(BytesMsg, obytes, _oInput);
obytes.szData.copy((char *)&iTimeoutCount, sizeof(iTimeoutCount), 0);
while (iTimeoutCount-- > 0)
{
/*移动刻度*/
cur_index++;
cur_index %= 10;
list<TimerOutProc *> m_cache;
/*遍历当前刻度所有节点,指向处理函数或圈数-1,*/
for (auto itr = m_timer_wheel[cur_index].begin(); itr != m_timer_wheel[cur_index].end(); )
{
if ((*itr)->iCount <= 0)
{
/*缓存待处理的超时节点*/
m_cache.push_back(*itr);
auto ptmp = *itr;
itr = m_timer_wheel[cur_index].erase(itr);
AddTask(ptmp);
}
else
{
(*itr)->iCount--;
++itr;
}
}
/*统一待处理超时任务*/
for (auto task : m_cache)
{
task->Proc();
}
}
return nullptr;
}
AZinxHandler * TimerOutMng::GetNextHandler(IZinxMsg & _oNextMsg)
{
return nullptr;
}
void TimerOutMng::AddTask(TimerOutProc * _ptask)
{
/*计算当前任务需要放到哪个齿上*/
int index = (_ptask->GetTimeSec() + cur_index) % 10;
/*把任务存到该齿上*/
m_timer_wheel[index].push_back(_ptask);
/*计算所需圈数*/
_ptask->iCount = _ptask->GetTimeSec() / 10;
}
void TimerOutMng::DelTask(TimerOutProc * _ptask)
{
/*遍历时间轮所有齿,删掉任务*/
for (list<TimerOutProc *> &chi : m_timer_wheel)
{
for (auto task : chi)
{
if (task == _ptask)
{
chi.remove(_ptask);
return;
}
}
}
}