diff --git a/purenessscopeserver/Common/ITSTimer.h b/purenessscopeserver/Common/ITSTimer.h index 513ac4eb9..18aa1651a 100644 --- a/purenessscopeserver/Common/ITSTimer.h +++ b/purenessscopeserver/Common/ITSTimer.h @@ -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; diff --git a/purenessscopeserver/Common/TimerInfo.cpp b/purenessscopeserver/Common/TimerInfo.cpp index cc3c942e8..355edcbe8 100644 --- a/purenessscopeserver/Common/TimerInfo.cpp +++ b/purenessscopeserver/Common/TimerInfo.cpp @@ -1,12 +1,10 @@ #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() @@ -14,32 +12,47 @@ 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) { @@ -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& 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); } } diff --git a/purenessscopeserver/Common/TimerInfo.h b/purenessscopeserver/Common/TimerInfo.h index dc231463b..ba5b980da 100644 --- a/purenessscopeserver/Common/TimerInfo.h +++ b/purenessscopeserver/Common/TimerInfo.h @@ -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, void*); - enum EM_Event_Type { TIMER_STOP = 0, //停止线程 @@ -54,6 +51,57 @@ 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& vecTimoutList, void* pArg) = 0; + + private: + int m_nTimerID; //定时器ID + int m_nFrequency; //当前定时器时间间隔(ms) + CTime_Value m_ttBegin; //当前定时器需要的开始时间 + }; class ITimerInfo { @@ -61,7 +109,7 @@ namespace ts_timer 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(); @@ -76,15 +124,12 @@ namespace ts_timer void Do_Error_Events(int nLastRunTimerID, int nTimeoutTime, std::vector& 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; //回调函数上下文 }; //定时事件列表 diff --git a/purenessscopeserver/Common/TimerThread.cpp b/purenessscopeserver/Common/TimerThread.cpp index e4ab501f6..db9eddde3 100644 --- a/purenessscopeserver/Common/TimerThread.cpp +++ b/purenessscopeserver/Common/TimerThread.cpp @@ -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); diff --git a/purenessscopeserver/Common/TimerThread.h b/purenessscopeserver/Common/TimerThread.h index 5840f14a8..6293a9d51 100644 --- a/purenessscopeserver/Common/TimerThread.h +++ b/purenessscopeserver/Common/TimerThread.h @@ -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);