Skip to content

Commit

Permalink
update TsTimer interface.
Browse files Browse the repository at this point in the history
  • Loading branch information
freeeyes committed May 22, 2019
1 parent 3dbc495 commit 3b19854
Show file tree
Hide file tree
Showing 5 changed files with 98 additions and 57 deletions.
7 changes: 1 addition & 6 deletions purenessscopeserver/Common/ITSTimer.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,7 @@ class ITSTimerManager
virtual ~ITSTimerManager() {};

//添加定时器
virtual bool Add_Timer(int nTimerID,
int nFrequency,
ts_timer::CTime_Value* pttBegin,
ts_timer::Timeout_Callback fn_Timeout_Callback,
void* pArgContext,
ts_timer::Timeout_Error_Callback fn_Timeout_Error_Callback) = 0;
virtual bool Add_Timer(ts_timer::ITimeNode* pTimeNode, void* pArgContext) = 0;

//删除定时器
virtual bool Del_Timer(int nTimerID) = 0;
Expand Down
58 changes: 37 additions & 21 deletions purenessscopeserver/Common/TimerInfo.cpp
Original file line number Diff line number Diff line change
@@ -1,45 +1,58 @@
#include "TimerInfo.h"

ts_timer::ITimerInfo::ITimerInfo() : m_nTimerID(-1), m_nFrequency(-1)
ts_timer::ITimerInfo::ITimerInfo() : m_pTimeNode(NULL), m_pArgContext(NULL)
{
//初始化时间
m_ttBeginTime.Set_time(0, 0);
m_ttLastRunTime.Set_time(0, 0);
m_fn_Timeout_Error = NULL;
m_fn_Timeout_Callback = NULL;
m_ttLastRunTime.Set_time(0, 0);;
}

ts_timer::ITimerInfo::~ITimerInfo()
{

}

void ts_timer::ITimerInfo::Set_Timer_Param(int nTimerID, int nFrequency, CTime_Value ttBegin, Timeout_Callback fn_Timeout_Callback, void* pArgContext, Timeout_Error_Callback fn_Timeout_Error_Callback)
void ts_timer::ITimerInfo::Set_Timer_Param(ITimeNode* pTimeNode, void* pArgContext)
{
m_nTimerID = nTimerID;
m_nFrequency = nFrequency;
m_ttBeginTime = ttBegin;
m_fn_Timeout_Callback = fn_Timeout_Callback;
m_pTimeNode = pTimeNode;
m_pArgContext = pArgContext;
m_fn_Timeout_Error = fn_Timeout_Error_Callback;
}

int ts_timer::ITimerInfo::Get_Timer_ID()
{
return m_nTimerID;
if(NULL != m_pTimeNode)
{
return m_pTimeNode->GetTimerID();
}
else
{
return -1;
}
}

int ts_timer::ITimerInfo::Get_Timer_Frequency()
{
return m_nFrequency;
if (NULL != m_pTimeNode)
{
return m_pTimeNode->GetFrequency();
}
else
{
return -1;
}
}

int ts_timer::ITimerInfo::Get_Next_Timer(CTime_Value ttNow, bool blState)
{
CTime_Value ttInterval;

int nSeconds = m_nFrequency / 1000;
int nUseconds = (m_nFrequency % 1000) * 1000;
if (NULL == m_pTimeNode)
{
return -1;
}

int nSeconds = m_pTimeNode->GetFrequency() / 1000;
int nUseconds = (m_pTimeNode->GetFrequency() % 1000) * 1000;

if (m_ttLastRunTime.IsZero() == true && m_ttNextTime.IsZero() == true)
{
Expand Down Expand Up @@ -92,23 +105,26 @@ ts_timer::EM_Timer_State ts_timer::ITimerInfo::Do_Timer_Event(ts_timer::CTime_Va

//执行回调函数
EM_Timer_State emState = TIMER_STATE_OK;
m_fn_Timeout_Callback(Get_Timer_ID(), obj_Now, m_pArgContext, emState);

if (NULL != m_pTimeNode)
{
m_pTimeNode->Run(obj_Now, m_pArgContext, emState);

//设置下次运行时间
int nSeconds = m_nFrequency / 1000;
int nUseconds = (m_nFrequency % 1000) * 1000;
//设置下次运行时间
int nSeconds = m_pTimeNode->GetFrequency() / 1000;
int nUseconds = (m_pTimeNode->GetFrequency() % 1000) * 1000;

m_ttNextTime = m_ttNextTime + CTime_Value(nSeconds, nUseconds);
m_ttNextTime = m_ttNextTime + CTime_Value(nSeconds, nUseconds);
}

return emState;
}

void ts_timer::ITimerInfo::Do_Error_Events(int nLastRunTimerID, int nTimeoutTime, std::vector<CTime_Value>& vecTimoutList)
{
if (NULL != m_fn_Timeout_Error)
if (NULL != m_pTimeNode)
{
m_fn_Timeout_Error(nLastRunTimerID, nTimeoutTime, Get_Timer_ID(), vecTimoutList, m_pArgContext);
m_pTimeNode->Error(nLastRunTimerID, nTimeoutTime, vecTimoutList, m_pArgContext);
}
}

Expand Down
67 changes: 56 additions & 11 deletions purenessscopeserver/Common/TimerInfo.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,17 +24,14 @@

namespace ts_timer
{
#define MAX_TIMER_LIST_COUNT 10

enum EM_Timer_State
{
TIMER_STATE_OK = 0, //定时器正常运行
TIMER_STATE_DEL, //定时器删除
};

#define MAX_TIMER_LIST_COUNT 10

typedef void(*Timeout_Callback)(int, CTime_Value&, void*, EM_Timer_State&);
typedef void(*Timeout_Error_Callback)(int, int, int, std::vector<CTime_Value>, void*);

enum EM_Event_Type
{
TIMER_STOP = 0, //停止线程
Expand All @@ -54,14 +51,65 @@ namespace ts_timer
typedef pthread_t TIMER_THREAD_ID;
#endif

//定时器节点基础类,用于集成实现
class ITimeNode
{
public:
ITimeNode() : m_nTimerID(0), m_nFrequency(0), m_ttBegin(GetTimeofDay()) {};
virtual ~ITimeNode() {};

void SetTimerID(int nTimerID)
{
m_nTimerID = nTimerID;
}

int GetTimerID()
{
return m_nTimerID;
}

void SetFrequency(int nFrequency)
{
m_nFrequency = nFrequency;
}

int GetFrequency()
{
return m_nFrequency;
}

void SetBeginTime(CTime_Value ttBegin)
{
m_ttBegin = ttBegin;
}

CTime_Value GetBeginTime()
{
return m_ttBegin;
}

//定时器执行函数
virtual void Run(CTime_Value& tvNow, void* pArg, EM_Timer_State& emState) = 0;

//定时器执行超时函数
//nLastRunTimerID 执行时间被占用的定时器ID
//nTimeoutTime 被占据的定时器时间长度
//vecTimoutList 被占用的定时器时段
virtual void Error(int nLastRunTimerID, int nTimeoutTime, std::vector<CTime_Value>& vecTimoutList, void* pArg) = 0;

private:
int m_nTimerID; //定时器ID
int m_nFrequency; //当前定时器时间间隔(ms)
CTime_Value m_ttBegin; //当前定时器需要的开始时间
};

class ITimerInfo
{
public:
ITimerInfo();
virtual ~ITimerInfo();

void Set_Timer_Param(int nTimerID, int nFrequency, CTime_Value ttBegin, Timeout_Callback fn_Timeout_Callback, void* pArgContext, Timeout_Error_Callback fn_Timeout_Error_Callback);
void Set_Timer_Param(ITimeNode* pTimeNode, void* pArgContext);

int Get_Timer_ID();

Expand All @@ -76,15 +124,12 @@ namespace ts_timer
void Do_Error_Events(int nLastRunTimerID, int nTimeoutTime, std::vector<CTime_Value>& vecTimoutList);

private:
int m_nTimerID; //当前唯一的Timer标识
int m_nFrequency; //当前的操作频度(单位是毫秒)
CTime_Value m_ttBeginTime; //开始定时器的时间
CTime_Value m_ttLastRunTime; //上一次成功运行定时器的时间
CTime_Value m_ttNextTime; //下一次运行的时间

Timeout_Callback m_fn_Timeout_Callback; //回调函数
Timeout_Error_Callback m_fn_Timeout_Error; //回调定时器执行超时函数
void* m_pArgContext; //回调函数上下文
ITimeNode* m_pTimeNode; //定时器ID
void* m_pArgContext; //回调函数上下文
};

//定时事件列表
Expand Down
16 changes: 3 additions & 13 deletions purenessscopeserver/Common/TimerThread.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -227,26 +227,16 @@ void ts_timer::CTimerThread::Run()
* pArgContext 任务到期执行的参数
* fn_Timeout_Error_Callback 定时器没按时执行的报错事件,可以为空
*/
bool ts_timer::CTimerThread::Add_Timer(int nTimerID, int nFrequency, CTime_Value* pttBegin, Timeout_Callback fn_Timeout_Callback, void* pArgContext, Timeout_Error_Callback fn_Timeout_Error_Callback)
bool ts_timer::CTimerThread::Add_Timer(ITimeNode* pTimeNode, void* pArgContext)
{
if (NULL == fn_Timeout_Callback)
if (NULL == pTimeNode)
{
return false;
}

ITimerInfo* pTimerInfo = new ITimerInfo();

if (NULL != pttBegin)
{
//有开始时间
pTimerInfo->Set_Timer_Param(nTimerID, nFrequency, *pttBegin, fn_Timeout_Callback, pArgContext, fn_Timeout_Error_Callback);
}
else
{
//从现在开始
CTime_Value ttBegin = GetTimeofDay();
pTimerInfo->Set_Timer_Param(nTimerID, nFrequency, ttBegin, fn_Timeout_Callback, pArgContext, fn_Timeout_Error_Callback);
}
pTimerInfo->Set_Timer_Param(pTimeNode, pArgContext);

bool blRet = m_TimerInfoList.Add_Timer(pTimerInfo);

Expand Down
7 changes: 1 addition & 6 deletions purenessscopeserver/Common/TimerThread.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,7 @@ namespace ts_timer
void Run();

//添加定时器
bool Add_Timer(int nTimerID,
int nFrequency,
CTime_Value* pttBegin,
Timeout_Callback fn_Timeout_Callback,
void* pArgContext,
Timeout_Error_Callback fn_Timeout_Error_Callback);
bool Add_Timer(ITimeNode* pTimeNode, void* pArgContext);

//删除定时器
bool Del_Timer(int nTimerID);
Expand Down

0 comments on commit 3b19854

Please sign in to comment.