Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
201 changes: 201 additions & 0 deletions ZinxTimer.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,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�� �Ὣԭ�����ļ�����������������ļ�
//��������Ƿ�ֵ ���ܺ���᲻С�IJ���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)//˵���������ڵ�ǰʱ��̶ȴ���Ҫ����
{
//����������ij�ʱ�ڵ�
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);*/
}

69 changes: 69 additions & 0 deletions ZinxTimer.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
#pragma once
#include<zinx.h>
#include<vector>
#include<list>
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<list<TimeOutProc*>> m_timer_wheel;
int cur_index = 0;
static TimeOutMng single;
//���캯��
TimeOutMng();
static TimeOutMng& GetInstance()
{
return single;
}


list<TimeOutProc*> m_task_list;

// ͨ�� AZinxHandler �̳�
IZinxMsg* InternelHandle(IZinxMsg& _oInput) override;

AZinxHandler* GetNextHandler(IZinxMsg& _oNextMsg) override;

void AddTask(TimeOutProc* _ptask);

void DelTask(TimeOutProc* _ptask);


};
7 changes: 7 additions & 0 deletions hello.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#include <iostream>
using namespace std;
int main() {
cout << "Hello, World!" << endl;
return 0;
}