diff --git a/ZinxTimer.cpp b/ZinxTimer.cpp new file mode 100644 index 0000000..342083e --- /dev/null +++ b/ZinxTimer.cpp @@ -0,0 +1,201 @@ +#include "ZinxTimer.h" +#include + +//创建定时器文件描述符 + + +//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 temp; + m_timer_wheel.push_back(temp); + } +} + +IZinxMsg* TimeOutMng::InternelHandle(IZinxMsg& _oInput) +{ + //移动刻度 + cur_index++; + cur_index %= 10; + + list 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);*/ +} + diff --git a/ZinxTimer.h b/ZinxTimer.h new file mode 100644 index 0000000..2832b3c --- /dev/null +++ b/ZinxTimer.h @@ -0,0 +1,69 @@ +#pragma once +#include +#include +#include +using namespace std; +class ZinxTimerChannel : + public Ichannel +{ +public: + int m_TimerFd; + + // 通过 Ichannel 继承 + bool Init() override; + bool ReadFd(string& _input) override; + bool WriteFd(string& _output) override; + void Fini() override; + int GetFd() override; + string GetChannelInfo() override; + AZinxHandler* GetInputNextStage(BytesMsg& _oInput) override; +}; + +//class output_hello :public AZinxHandler +//{ +// // 通过 AZinxHandler 继承 +// IZinxMsg* InternelHandle(IZinxMsg& _oInput) override; +// AZinxHandler* GetNextHandler(IZinxMsg& _oNextMsg) override; +//}; + + +//定义事件处理类 +class TimeOutProc +{ +public: + virtual void proc() = 0; + virtual int GetTimeSec() = 0; + //所剩圈数 + int iCount = -1; +}; + + +//定义时间事件管理类 +class TimeOutMng :public AZinxHandler +{ +public: + //定义时间轮 + vector> m_timer_wheel; + int cur_index = 0; + static TimeOutMng single; + //构造函数 + TimeOutMng(); + static TimeOutMng& GetInstance() + { + return single; + } + + + list m_task_list; + + // 通过 AZinxHandler 继承 + IZinxMsg* InternelHandle(IZinxMsg& _oInput) override; + + AZinxHandler* GetNextHandler(IZinxMsg& _oNextMsg) override; + + void AddTask(TimeOutProc* _ptask); + + void DelTask(TimeOutProc* _ptask); + + +}; \ No newline at end of file diff --git a/hello.cpp b/hello.cpp new file mode 100644 index 0000000..5e6e86f --- /dev/null +++ b/hello.cpp @@ -0,0 +1,7 @@ +#include +using namespace std; +int main() { + cout << "Hello, World!" << endl; + return 0; +} +