forked from marklion/zinx
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathZinxTimer.cpp
More file actions
201 lines (161 loc) · 4.24 KB
/
ZinxTimer.cpp
File metadata and controls
201 lines (161 loc) · 4.24 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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
#include "ZinxTimer.h"
#include<sys/timerfd.h>
//创建定时器文件描述符
//output_hello* pout_hello = new output_hello();
//定义全局唯一管理类单例
TimeOutMng TimeOutMng::single;
bool ZinxTimerChannel::Init()
{
bool bRet = false;
//创建文件描述符
int iFd = timerfd_create(CLOCK_MONOTONIC, 0);
if (0 <= iFd)
{
//设置定时周期
struct itimerspec period
{
//表示第一次触发为3s 0ns 之后间隔 3s 0ns触发
{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中的内容复制到buff中
_input.assign(buff, sizeof(buff));
/*std::cout << buff << std::endl;*/
}
//uint64_t count = 0;
//read(m_TimerFd, &count, sizeof(count)); // 直接解析为 uint64_t
//std::cout << count << std::endl;
return bRet;
}
bool ZinxTimerChannel::WriteFd(std::string& _output)
{
return false;
}
void ZinxTimerChannel::Fini()
{
close(m_TimerFd);
m_TimerFd = -1;
//这里给fd赋非法值的原因是: 系统在释放掉timerFd后 会将原来的文件描述符分配给其他文件
//如果不赋非法值 可能后面会不小心操作timerFd导致其他文件数据被误操作 导致数据流混乱 所以这里赋非法值 为了防止误操作
}
int ZinxTimerChannel::GetFd()
{
return m_TimerFd;
}
std::string ZinxTimerChannel::GetChannelInfo()
{
return "TimerFd";
}
//返回超时事件处理的对象
AZinxHandler* ZinxTimerChannel::GetInputNextStage(BytesMsg& _oInput)
{
return &TimeOutMng::GetInstance();
}
//IZinxMsg* output_hello::InternelHandle(IZinxMsg& _oInput)
//{
// auto pchannel = ZinxKernel::Zinx_GetChannel_ByInfo("stdout");
// std::string output = "hello world";
// ZinxKernel::Zinx_SendOut(output, *pchannel);
//
// return nullptr;
//}
//
//
//AZinxHandler* output_hello::GetNextHandler(IZinxMsg& _oNextMsg)
//{
// return nullptr;
//}
TimeOutMng::TimeOutMng()
{
//create 10 teeth
for (int i = 0; i < 10; i++)
{
list<TimeOutProc*> temp;
m_timer_wheel.push_back(temp);
}
}
IZinxMsg* TimeOutMng::InternelHandle(IZinxMsg& _oInput)
{
//移动刻度
cur_index++;
cur_index %= 10;
list<TimeOutProc*> 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);
/*(*itr)->proc();*/
TimeOutProc* ptmp = *itr;
itr = m_timer_wheel[cur_index].erase(itr);
AddTask(ptmp);
}
else
{
++itr;
}
}
for (auto task : m_cache)
{
task->proc();
}
/* for (auto task : m_task_list)
{
task->iCount--;
if (task->iCount <= 0)
{
task->proc();
task->iCount = task->GetTimeSec();
}
}*/
return nullptr;
}
AZinxHandler* TimeOutMng::GetNextHandler(IZinxMsg& _oNextMsg)
{
return nullptr;
}
void TimeOutMng::AddTask(TimeOutProc* _ptask)
{
//计算当前任务需要放到哪个齿上
int index = (_ptask->GetTimeSec() + cur_index) % 10;
//把任务存到齿上
m_timer_wheel[index].push_back(_ptask);
//计算时间齿对应任务此任务的圈数
_ptask->iCount = _ptask->GetTimeSec() / 10;
/*m_task_list.push_back(_ptask);
_ptask->iCount = _ptask->GetTimeSec();*/
}
void TimeOutMng::DelTask(TimeOutProc* _ptask)
{
/*遍历所有时间齿 删掉任务*/
for (auto &tooth : m_timer_wheel)
{
for (auto task : tooth)
{
if (task == _ptask)
{
tooth.remove(task);
return;
}
}
}
/* m_task_list.remove(_ptask);*/
}