Quantcast
Channel: CSDN博客推荐文章
Viewing all articles
Browse latest Browse all 35570

AMPS:定时器模块源码解读

$
0
0

  Timer模块的实现比较复杂,需要结合前面的文章《AMPS:定时器管理》才能有所理解,下面看看AMPS中的定时器实现,有些细节方面我也没有想清楚。

 AMPS_Timer.h

#ifndef __HEADER_AMPS_TIMER_H__
#define __HEADER_AMPS_TIMER_H__

#ifdef __cplusplus
    extern "C" {
#endif

#include "AMPS_Defines.h"
#include "AMPS_Core.h"
#include "AMPS_SystemAPI.h"
#include "AMPS_TimerTicker.h"

typedef struct _AMPSTimer           					t_AMPSTimer;
typedef struct _AMPSTimerNodeData   					t_AMPSTimerNodeData;
typedef struct _AMPSTimerSlot       					t_AMPSTimerSlot;
typedef struct _AMPSTimerMessage    					t_AMPSTimerMessage;

#define AMPS_TIMER_CURRENT_SLOT_MILLISECONDS		1
#define AMPS_TIMER_CURRENT_SLOT_SECONDS				2
#define AMPS_TIMER_CURRENT_SLOT_MINUTES				3
#define AMPS_TIMER_CURRENT_SLOT_HOURS				4
#define AMPS_TIMER_CURRENT_SLOT_DAYS				5

#define AMPS_TIMER_STATUS_ENABLE					1
#define AMPS_TIMER_STATUS_DISABLE					2

#define AMPS_TIMER_SERVER_MSG_PORT					5000

//#define AMPS_TIMER_NUM_OF_MILLI_SECOND_SLOTS      50
//#define AMPS_TIMER_MIN_MILLI_SECOND_SLOT_TIME     20    //20 Milli Second

#define AMPS_TIMER_NUM_OF_SECOND_SLOTS             	60
#define AMPS_TIMER_MIN_SECOND_SLOT_TIME            	1000 //1 Second

#define AMPS_TIMER_NUM_OF_MINUTE_SLOTS             	60
#define AMPS_TIMER_MIN_MINUTE_SLOT_TIME            	(AMPS_TIMER_MIN_SECOND_SLOT_TIME * AMPS_TIMER_NUM_OF_SECOND_SLOTS) //1 Minute

#define AMPS_TIMER_NUM_OF_HOUR_SLOTS             	24
#define AMPS_TIMER_MIN_HOUR_SLOT_TIME            	(AMPS_TIMER_MIN_MINUTE_SLOT_TIME * AMPS_TIMER_NUM_OF_MINUTE_SLOTS) //1 Hour

#define AMPS_TIMER_NUM_OF_DAY_SLOTS             	7
#define AMPS_TIMER_MIN_DAY_SLOT_TIME            	(AMPS_TIMER_MIN_HOUR_SLOT_TIME * AMPS_TIMER_NUM_OF_HOUR_SLOTS) //1 Day

//#define AMPS_TIMER_MIN_SLOT_TIME             		AMPS_TIMER_MIN_MILLI_SECOND_SLOT_TIME  //Milli Seconds

//#define AMPS_TIMER_SLOT_TIMEOUT_SEC            	0
//#define AMPS_TIMER_SLOT_TIMEOUT_MSEC           	AMPS_TIMER_MIN_MILLI_SECOND_SLOT_TIME

//#define AMPS_TIMER_MIN_TIMER_VALUE          		AMPS_TIMER_MIN_MILLI_SECOND_SLOT_TIME   //Milli Seconds
//#define AMPS_TIMER_MAX_TIMER_VALUE          		(1000 * 60 * 60 * 24 * 7)   			//Milli Seconds it is 1 month now

struct _AMPSTimerSlot
{
	t_AMPSDList*		poDList;
};

/*定时器模块结构*/
struct _AMPSTimer
{
	//int					nTimerConnectionListenPort;

	//int					nSlotTimeOutInMilliSeconds;
	//int					nSlotTimeOutInSeconds;
	int					nMinSlotTime;   /*最小的结点个数*/
	int					nMinTimerValue; /*最小时间间隔*/
	int					nMaxTimerValue; /*最大时间间隔*/

    /*毫秒链表*/
    //t_AMPSTimerSlot   	poTimerSlotMilliSeconds[AMPS_TIMER_NUM_OF_MILLI_SECOND_SLOTS];
	int					nNoOfMilliSecondsSlots;  /*毫秒链表中的结点总个数*/
	int					nMinMilliSecondsSlotTime;/*毫秒链表中的最小时间间隔*/
	t_AMPSTimerSlot*   	poTimerSlotMilliSeconds; /*毫秒链表*/
    unsigned int    	unIndexForMilliSecondsSlot;/*当前链表中结点个数*/

    /*秒数组*/
    t_AMPSTimerSlot   	poTimerSlotSeconds[AMPS_TIMER_NUM_OF_SECOND_SLOTS];
    unsigned int    	unIndexForSecondsSlot;

    /*分数组*/ 
    t_AMPSTimerSlot   	poTimerSlotMinutes[AMPS_TIMER_NUM_OF_MINUTE_SLOTS];
    unsigned int    	unIndexForMinutesSlot;

    /*小时数组*/
    t_AMPSTimerSlot   	poTimerSlotHours[AMPS_TIMER_NUM_OF_HOUR_SLOTS];
    unsigned int    	unIndexForHoursSlot;

    /*天数组*/
    t_AMPSTimerSlot   	poTimerSlotDays[AMPS_TIMER_NUM_OF_DAY_SLOTS];
    unsigned int    	unIndexForDaysSlot;

    /*定时器线程句柄*/
	t_AMPSThreads     	oTimerThread;

    /*ticker时间*/
	t_AMPSTimerTicker*	poAMPSTimerTicker;

};

/*定时器结点结构*/
struct _AMPSTimerNodeData
{
    AMPS_TimeOutNotifyCallBack  pfAMPS_TimerCallBack;
    void*           pvData;
    char            chStatus;
    void*           pvAMPSContext;

	void*			pvSListNodePtr;

	unsigned int	unCurrentTimerSlot;

	unsigned int 	unDaysSlotNo;
	unsigned int 	unHoursSlotNo;
	unsigned int 	unMinutesSlotNo;
	unsigned int 	unSecondsSlotNo;
	unsigned int 	unMilliSecondsSlotNo;
};

struct _AMPSTimerMessage
{
    unsigned int	unTickCount;
};

int Timer_Init(t_AMPSContext* r_poAMPSContext, e_AMPSTimerResolution r_oAMPSTimerResolution, int r_nTimerConnectionListenPort);
void Timer_Cleanup(t_AMPSContext* r_poAMPSContext);
int Timer_TickerThread(void* r_pvAMPSContext);
int Timer_CreateTimerConnection(t_AMPSContext* r_poAMPSContext, void* r_pvAMPSTimer, int r_nTimerConnectionListenPort);

int Timer_ProcessTimerEvent(void* r_pvAMPSContext);
int Timer_RecvDataCallback(AMPS_HANDLE r_hAMPS_HANDLE, AMPS_APP_HANDLE r_hAMPS_APP_HANDLE, AMPS_NET_HANDLE r_hAMPS_NET_HANDLE, unsigned char* r_puchBuff, int r_nBuffLength, int r_nPort, char* r_chIPAddress);
void Timer_ProcessTimerSlot(t_AMPSContext* r_poAMPSContext, t_AMPSTimer* r_poAMPSTimer);

void* Timer_StartTimer(void* r_pvAMPSContext, unsigned int r_unTimerValue, AMPS_TimeOutNotifyCallBack r_pfAMPS_TimerCallBack, void* r_pvData);
int Timer_DeleteTimer(void* r_pvAMPSContext, void* r_pvLinkNode);

int Timer_FreeNodeData(void** r_ppvAMPSTimerNodeData);

void Timer_ProcessDaysTimer(t_AMPSContext* r_poAMPSContext, t_AMPSTimer* r_poAMPSTimer, t_AMPSDList* r_poAMPSDListForDaysTimer);
void Timer_ProcessHoursTimer(t_AMPSContext* r_poAMPSContext, t_AMPSTimer* r_poAMPSTimer, t_AMPSDList* r_poAMPSDListForHoursTimer);
void Timer_ProcessMinutesTimer(t_AMPSContext* r_poAMPSContext, t_AMPSTimer* r_poAMPSTimer, t_AMPSDList* r_poAMPSDListForMinutesTimer);
void Timer_ProcessSecondsTimer(t_AMPSContext* r_poAMPSContext, t_AMPSTimer* r_poAMPSTimer, t_AMPSDList* r_poAMPSDListForSecondsTimer);
int Timer_ProcessMilliSecondsNodeData(void** r_ppvAMPSTimerNodeData);

#ifdef __cplusplus
   }
#endif

#endif /* __HEADER_AMPS_TIMER_H__ */

AMPS_Timer.c

#include "AMPS_TCP.h"
#include "AMPS_LinkList.h"
#include "AMPS_Timer.h"
#include "AMPS_MemMgt.h"
#include "AMPS_Trace.h"

/*****************************************************************
函数名称: Timer_Init
功能描述: Timer模块初始化
入参:
        t_AMPSContext* r_poAMPSContext AMPS应用上下文
        e_AMPSTimerResolution r_oAMPSTimerResolution 定时器时长
        int r_nTimerConnectionListenPort

出参:
     --
返回值:
    int

*****************************************************************/
int Timer_Init(t_AMPSContext* r_poAMPSContext, e_AMPSTimerResolution r_oAMPSTimerResolution, int r_nTimerConnectionListenPort)
{
    t_AMPSTimer* poAMPSTimer = NULL;
    unsigned int unCurrentTime = 0;
    int nCounter = 0;

    /*分配Timer解句柄*/
    r_poAMPSContext->pvTimerContext = AMPS_InternalMalloc(sizeof(t_AMPSTimer));
    if (NULL == r_poAMPSContext->pvTimerContext)
    {
        TRACE( TIMER_TRACE_ID(r_poAMPSContext), AMPS_TRACE_LEVEL_ERROR, "AMPS_InternalMalloc failed for r_poAMPSContext->pvTimerContext.\n");
        return AMPS_ERROR_FAILURE;
    }
    poAMPSTimer = r_poAMPSContext->pvTimerContext;

    /*根据不同的超时时间进行Timer内容初始化*/
	if(AMPS_TIMER_RESOLUTION_20MS == r_oAMPSTimerResolution)
	{
		poAMPSTimer->nMinMilliSecondsSlotTime = 20;
		poAMPSTimer->nNoOfMilliSecondsSlots = 1000/20;
	}
	else
	if(AMPS_TIMER_RESOLUTION_25MS == r_oAMPSTimerResolution)
	{
		poAMPSTimer->nMinMilliSecondsSlotTime = 25;
		poAMPSTimer->nNoOfMilliSecondsSlots = 1000/25;
	}
	else
	if(AMPS_TIMER_RESOLUTION_50MS == r_oAMPSTimerResolution)
	{
		poAMPSTimer->nMinMilliSecondsSlotTime = 50;
		poAMPSTimer->nNoOfMilliSecondsSlots = 1000/50;
	}
	else
	if(AMPS_TIMER_RESOLUTION_100MS == r_oAMPSTimerResolution)
	{
		poAMPSTimer->nMinMilliSecondsSlotTime = 100;
		poAMPSTimer->nNoOfMilliSecondsSlots = 1000/100;
	}

    /*分配Timer中ticker指针内存*/
	poAMPSTimer->poAMPSTimerTicker = AMPS_InternalMalloc(sizeof(t_AMPSTimerTicker));
	if (NULL == poAMPSTimer->poAMPSTimerTicker)
	{
		TRACE(TIMER_TRACE_ID(r_poAMPSContext), AMPS_TRACE_LEVEL_ERROR, "AMPS_InternalMalloc failed for poAMPSTimerTicker.\n");
		return AMPS_ERROR_FAILURE;
	}

    /*初始化Ticker成员*/
	poAMPSTimer->poAMPSTimerTicker->nTimerIsRunning = AMPS_TRUE;
	poAMPSTimer->poAMPSTimerTicker->nTimerTraceID = TIMER_TRACE_ID(r_poAMPSContext);
	poAMPSTimer->poAMPSTimerTicker->nTimerConnectionListenPort = r_nTimerConnectionListenPort;
	poAMPSTimer->poAMPSTimerTicker->nSlotTimeOutInMilliSeconds = poAMPSTimer->nMinMilliSecondsSlotTime;
	poAMPSTimer->poAMPSTimerTicker->nSlotTimeOutInSeconds = 0;


	//poAMPSTimer->nSlotTimeOutInMilliSeconds = poAMPSTimer->nMinMilliSecondsSlotTime;
	//poAMPSTimer->nSlotTimeOutInSeconds = 0;
	poAMPSTimer->nMinSlotTime = poAMPSTimer->nMinMilliSecondsSlotTime;
	poAMPSTimer->nMinTimerValue = poAMPSTimer->nMinMilliSecondsSlotTime;
    /*最大的定时器时长,一周*/
	poAMPSTimer->nMaxTimerValue = AMPS_TIMER_NUM_OF_DAY_SLOTS * AMPS_TIMER_MIN_DAY_SLOT_TIME;
	//poAMPSTimer->nTimerConnectionListenPort = r_nTimerConnectionListenPort;

	poAMPSTimer->poTimerSlotMilliSeconds = AMPS_InternalMalloc(sizeof(t_AMPSTimerSlot) * poAMPSTimer->nNoOfMilliSecondsSlots);
	if (NULL == poAMPSTimer->poTimerSlotMilliSeconds)
	{
		TRACE( TIMER_TRACE_ID(r_poAMPSContext), AMPS_TRACE_LEVEL_ERROR, "AMPS_InternalMalloc failed for poAMPSTimer->poTimerSlotMilliSeconds.\n");
		return AMPS_ERROR_FAILURE;
	}

	//poAMPSTimer->poNetworkMessage = NULL;
	/*获取当前时长,单位:毫秒*/
    unCurrentTime = SAPI_GetCurrentTimeInMilliSec(r_poAMPSContext) + poAMPSTimer->nMinSlotTime; // add min slot time for the first slot

    /*不同时长的定时器数组初始化*/
	//For Milli-Seconds Slots
	poAMPSTimer->unIndexForMilliSecondsSlot = 0;
    for(nCounter = 0; nCounter < poAMPSTimer->nNoOfMilliSecondsSlots; nCounter++)
    {
        if(NULL == DList_Init(&poAMPSTimer->poTimerSlotMilliSeconds[nCounter].poDList))
        {
            TRACE( TIMER_TRACE_ID(r_poAMPSContext), AMPS_TRACE_LEVEL_ERROR, "DList_Init failed.\n");
            return AMPS_ERROR_FAILURE;
        }
    }
	//For Seconds Slots
	poAMPSTimer->unIndexForSecondsSlot = 0;
    for(nCounter = 0; nCounter < AMPS_TIMER_NUM_OF_SECOND_SLOTS; nCounter++)
    {
        if(NULL == DList_Init(&poAMPSTimer->poTimerSlotSeconds[nCounter].poDList))
        {
            TRACE( TIMER_TRACE_ID(r_poAMPSContext), AMPS_TRACE_LEVEL_ERROR, "DList_Init failed.\n");
            return AMPS_ERROR_FAILURE;
        }
    }
	//For Minutes Slots
	poAMPSTimer->unIndexForMinutesSlot = 0;
    for(nCounter = 0; nCounter < AMPS_TIMER_NUM_OF_MINUTE_SLOTS; nCounter++)
    {
        if(NULL == DList_Init(&poAMPSTimer->poTimerSlotMinutes[nCounter].poDList))
        {
            TRACE( TIMER_TRACE_ID(r_poAMPSContext), AMPS_TRACE_LEVEL_ERROR, "DList_Init failed.\n");
            return AMPS_ERROR_FAILURE;
        }
    }
	//For Hours Slots
	poAMPSTimer->unIndexForHoursSlot = 0;
    for(nCounter = 0; nCounter < AMPS_TIMER_NUM_OF_HOUR_SLOTS; nCounter++)
    {
        if(NULL == DList_Init(&poAMPSTimer->poTimerSlotHours[nCounter].poDList))
        {
            TRACE( TIMER_TRACE_ID(r_poAMPSContext), AMPS_TRACE_LEVEL_ERROR, "DList_Init failed.\n");
            return AMPS_ERROR_FAILURE;
        }
    }
	//For Days Slots
	poAMPSTimer->unIndexForDaysSlot = 0;
    for(nCounter = 0; nCounter < AMPS_TIMER_NUM_OF_DAY_SLOTS; nCounter++)
    {
        if(NULL == DList_Init(&poAMPSTimer->poTimerSlotDays[nCounter].poDList))
        {
            TRACE( TIMER_TRACE_ID(r_poAMPSContext), AMPS_TRACE_LEVEL_ERROR, "DList_Init failed.\n");
            return AMPS_ERROR_FAILURE;
        }
    }

    /*建立定时器服务器,用于接收事件通知,用来接收Ticker通知?*/
    if(AMPS_ERROR_FAILURE == Timer_CreateTimerConnection(r_poAMPSContext, poAMPSTimer, r_nTimerConnectionListenPort))
    {
        TRACE( TIMER_TRACE_ID(r_poAMPSContext), AMPS_TRACE_LEVEL_ERROR, "Timer_CreateTimerConnection failed.\n");
        return AMPS_ERROR_FAILURE;
    }

    return AMPS_SUCCESS;
}

/*****************************************************************
函数名称: Timer_Cleanup
功能描述: Timer模块销毁
入参:
        t_AMPSContext* r_poAMPSContext AMPS应用上下文

出参:
     --
返回值:
    void

*****************************************************************/
void Timer_Cleanup(t_AMPSContext* r_poAMPSContext)
{
    t_AMPSTimer* poAMPSTimer = (t_AMPSTimer*)r_poAMPSContext->pvTimerContext;
    t_AMPSThreads     oTimerThread = poAMPSTimer->oTimerThread;
    int nCounter = 0;

    TRACE( TIMER_TRACE_ID(r_poAMPSContext), AMPS_TRACE_LEVEL_INFO, "Entering.\n");

    /*设置定时器工作模式为FALSE*/
    poAMPSTimer->poAMPSTimerTicker->nTimerIsRunning = AMPS_FALSE;

    /*线程挂起,等待线程函数处理完成*/
    if (AMPS_ERROR_FAILURE == SAPI_WaitForThread(r_poAMPSContext, &oTimerThread))
    {
        TRACE( TIMER_TRACE_ID(r_poAMPSContext), AMPS_TRACE_LEVEL_ERROR, "SAPI_WaitForThread failed.\n");
    }

    for (nCounter = 0; nCounter < poAMPSTimer->nNoOfMilliSecondsSlots; nCounter++)
    {
        DList_Free(&poAMPSTimer->poTimerSlotMilliSeconds[nCounter].poDList, NULL);
    }
    for (nCounter = 0; nCounter < AMPS_TIMER_NUM_OF_SECOND_SLOTS; nCounter++)
    {
        DList_Free(&poAMPSTimer->poTimerSlotSeconds[nCounter].poDList, NULL);
    }
    for (nCounter = 0; nCounter < AMPS_TIMER_NUM_OF_MINUTE_SLOTS; nCounter++)
    {
        DList_Free(&poAMPSTimer->poTimerSlotMinutes[nCounter].poDList, NULL);
    }
    for (nCounter = 0; nCounter < AMPS_TIMER_NUM_OF_HOUR_SLOTS; nCounter++)
    {
        DList_Free(&poAMPSTimer->poTimerSlotHours[nCounter].poDList, NULL);
    }
    for (nCounter = 0; nCounter < AMPS_TIMER_NUM_OF_DAY_SLOTS; nCounter++)
    {
        DList_Free(&poAMPSTimer->poTimerSlotDays[nCounter].poDList, NULL);
    }

    /*释放定时器句柄*/
    AMPS_InternalFree(r_poAMPSContext->pvTimerContext);   

    TRACE( TIMER_TRACE_ID(r_poAMPSContext), AMPS_TRACE_LEVEL_INFO, "Leaving.\n");
}

/*****************************************************************
函数名称: Timer_FreeNodeData
功能描述: 释放定时器结点
入参:
        void** r_ppvAMPSTimerNodeData 定时器结点数据

出参:
     --
返回值:
    int

*****************************************************************/
int Timer_FreeNodeData(void** r_ppvAMPSTimerNodeData)
{
    t_AMPSTimerNodeData* poAMPSTimerNodeData = *r_ppvAMPSTimerNodeData;
 
	if(NULL != poAMPSTimerNodeData)
	{
		AMPS_InternalFree(poAMPSTimerNodeData);
	}

	*r_ppvAMPSTimerNodeData = NULL;
	return AMPS_SUCCESS;
}

/*****************************************************************
函数名称: Timer_FreeNodeData
功能描述: Timer连接事件回调
入参:
        void* r_pvAMPSContext
        void* r_pvAMPSTimer
        void* r_pvAMPSNetworkMsg
        int r_nPort
        char* r_chIPAddress

出参:
     --
返回值:
    int

*****************************************************************/
int Timer_ConnectEvtCallback(void* r_pvAMPSContext, void* r_pvAMPSTimer, void* r_pvAMPSNetworkMsg, int r_nPort, char* r_chIPAddress)
{
	t_AMPSContext* poAMPSContext = r_pvAMPSContext;
	t_AMPSNetworkMsg* poAMPSNetworkMsg = r_pvAMPSNetworkMsg;

	TRACE( TIMER_TRACE_ID(r_pvAMPSContext), AMPS_TRACE_LEVEL_INFO, "Entering.\n");

	if(NULL == poAMPSNetworkMsg)
	{
		TRACE( TIMER_TRACE_ID(r_pvAMPSContext), AMPS_TRACE_LEVEL_ERROR, "error in connection.\n");
	}
	else
	{
		TRACE( TIMER_TRACE_ID(r_pvAMPSContext), AMPS_TRACE_LEVEL_DEBUG, "Connected successfully.\n");
		poAMPSContext->poAMPSNetworkTimerMsgForConnectedSocket = poAMPSNetworkMsg;
		TRACE( TIMER_TRACE_ID(r_pvAMPSContext), AMPS_TRACE_LEVEL_DEBUG, "Handle %d.\n", (int)poAMPSNetworkMsg->nHandle);
	}
	TRACE( TIMER_TRACE_ID(r_pvAMPSContext), AMPS_TRACE_LEVEL_INFO, "Leaving.\n");
	return AMPS_SUCCESS;
}

/*****************************************************************
函数名称: Timer_CreateTimerConnection
功能描述: Timer创建连接
入参:
        t_AMPSContext* r_poAMPSContext
        void* r_pvAMPSTimer
        int r_nTimerConnectionListenPort


出参:
     --
返回值:
    int

*****************************************************************/
int Timer_CreateTimerConnection(t_AMPSContext* r_poAMPSContext, void* r_pvAMPSTimer, int r_nTimerConnectionListenPort)
{
	t_AMPSTimer* poAMPSTimer = r_pvAMPSTimer;
	void* pvAMPSNetworkMsg = NULL;

	TRACE( TIMER_TRACE_ID(r_poAMPSContext), AMPS_TRACE_LEVEL_INFO, "Entering.\n");

    /*在环回地址上创建一个socket并绑定指定的port,做为一个非阻塞的时间服务器*/
	//Listener socket to accept Timer connection
	TRACE( TIMER_TRACE_ID(r_poAMPSContext), AMPS_TRACE_LEVEL_DEBUG, "Create listener socket to accept Timer connection.\n");
	if(AMPS_SUCCESS != TCP_InternalCreateNonBlockingListener(r_poAMPSContext, AMPS_MEMORY_SCHEME_STATIC, NULL, NULL, NULL, AMPS_CONNECTION_TYPE_TCP, r_nTimerConnectionListenPort, AMPS_LOOP_BACK_IP_ADDRESS, 0, sizeof(t_AMPSTimerMessage), Timer_ConnectEvtCallback, poAMPSTimer, Timer_RecvDataCallback, &pvAMPSNetworkMsg, NULL))
	{
		TRACE( TIMER_TRACE_ID(r_poAMPSContext), AMPS_TRACE_LEVEL_ERROR, "AMPS_InternalCreateNonBlockingListener failed for timer.\n");
		return AMPS_ERROR_FAILURE;
	}

    /*设置定时器线程优先级为最高*/
	if(AMPS_SUCCESS != SAPI_SetThreadSchedFIFOWithPriority(r_poAMPSContext, &poAMPSTimer->oTimerThread, AMPS_PRIORITY_TYPE_MANUAL, 10))
	{
		TRACE(TIMER_TRACE_ID(r_poAMPSContext), AMPS_TRACE_LEVEL_ERROR, "SAPI_SetThreadSchedRRWithPriority failed.\n");
		return AMPS_ERROR_FAILURE;
	}

    /*创建线程*/
	if(AMPS_SUCCESS != SAPI_ThreadCreate(r_poAMPSContext, &poAMPSTimer->oTimerThread, Timer_TickerThread, poAMPSTimer->poAMPSTimerTicker))
	{
		TRACE( TIMER_TRACE_ID(r_poAMPSContext), AMPS_TRACE_LEVEL_ERROR, "SAPI_ThreadCreate failed.\n");
		return AMPS_ERROR_FAILURE;
	}

	TRACE( TIMER_TRACE_ID(r_poAMPSContext), AMPS_TRACE_LEVEL_INFO, "Leaving.\n");
    return AMPS_SUCCESS;
}

/*****************************************************************
函数名称: Timer_StartTimer
功能描述: 启动定时器
入参:
        void* r_pvAMPSContext
        unsigned int r_unTimerValue 定时器时长
        AMPS_TimeOutNotifyCallBack r_pfAMPS_TimerCallBack 超时处理函数
        void* r_pvData

出参:
     --
返回值:
    int

*****************************************************************/
void* Timer_StartTimer(void* r_pvAMPSContext, unsigned int r_unTimerValue, AMPS_TimeOutNotifyCallBack r_pfAMPS_TimerCallBack, void* r_pvData)
{
	t_AMPSContext* poAMPSContext = r_pvAMPSContext;
    t_AMPSTimer* poAMPSTimer = poAMPSContext->pvTimerContext;
    t_AMPSTimerNodeData* poAMPSTimerNodeData = NULL;
	t_AMPSDList* poDListForTimerSlot = NULL;

    if ((unsigned int)poAMPSTimer->nMinTimerValue > r_unTimerValue || (unsigned int)poAMPSTimer->nMaxTimerValue <= r_unTimerValue)
    {
        TRACE( TIMER_TRACE_ID(r_pvAMPSContext), AMPS_TRACE_LEVEL_ERROR, "Invalid Timer Value = %ul.\n", r_unTimerValue);
        return NULL;
    }

    /*分配定时器结点*/
    poAMPSTimerNodeData = (t_AMPSTimerNodeData*)AMPS_InternalMalloc(sizeof(t_AMPSTimerNodeData));
    if (NULL == poAMPSTimerNodeData)
    {
        TRACE( TIMER_TRACE_ID(r_pvAMPSContext), AMPS_TRACE_LEVEL_ERROR, "AMPS_InternalMalloc is failed for Timer Node.\n");
        return NULL;
    }

    /*超时处理函数*/ 
    poAMPSTimerNodeData->pfAMPS_TimerCallBack = r_pfAMPS_TimerCallBack;
    poAMPSTimerNodeData->pvData = r_pvData;
    poAMPSTimerNodeData->chStatus = AMPS_TIMER_STATUS_ENABLE;
    poAMPSTimerNodeData->pvAMPSContext = r_pvAMPSContext;

	TRACE( TIMER_TRACE_ID(r_pvAMPSContext), AMPS_TRACE_LEVEL_DEBUG, "Timer value is %d.\n", r_unTimerValue);

    /*时长大于1天*/
	if(AMPS_TIMER_MIN_DAY_SLOT_TIME <= r_unTimerValue)
	{
        /*定时器结点中从天到毫秒各链表结点赋值,用来表示当前时长*/
		poAMPSTimerNodeData->unCurrentTimerSlot = AMPS_TIMER_CURRENT_SLOT_DAYS;
		poAMPSTimerNodeData->unDaysSlotNo = ((poAMPSTimer->unIndexForDaysSlot + (r_unTimerValue/AMPS_TIMER_MIN_DAY_SLOT_TIME)) % AMPS_TIMER_NUM_OF_DAY_SLOTS);
		poAMPSTimerNodeData->unHoursSlotNo = ((poAMPSTimer->unIndexForHoursSlot + (r_unTimerValue/AMPS_TIMER_MIN_HOUR_SLOT_TIME)) % AMPS_TIMER_NUM_OF_HOUR_SLOTS);
		TRACE( TIMER_TRACE_ID(r_pvAMPSContext), AMPS_TRACE_LEVEL_DEBUG, "Timer is in Hour Slot # %d.\n", poAMPSTimerNodeData->unHoursSlotNo);
        if(poAMPSTimerNodeData->unHoursSlotNo  < poAMPSTimer->unIndexForHoursSlot)
        {
			poAMPSTimerNodeData->unDaysSlotNo= ((poAMPSTimerNodeData->unDaysSlotNo+ 1) % AMPS_TIMER_NUM_OF_DAY_SLOTS);
        }
		poAMPSTimerNodeData->unMinutesSlotNo = ((poAMPSTimer->unIndexForMinutesSlot + (r_unTimerValue/AMPS_TIMER_MIN_MINUTE_SLOT_TIME)) % AMPS_TIMER_NUM_OF_MINUTE_SLOTS);
		TRACE( TIMER_TRACE_ID(r_pvAMPSContext), AMPS_TRACE_LEVEL_DEBUG, "With Minute Slot # %d.\n", poAMPSTimerNodeData->unMinutesSlotNo);
        if(poAMPSTimerNodeData->unMinutesSlotNo < poAMPSTimer->unIndexForMinutesSlot)
        {
			poAMPSTimerNodeData->unHoursSlotNo = ((poAMPSTimerNodeData->unHoursSlotNo + 1) % AMPS_TIMER_NUM_OF_HOUR_SLOTS);
        }
		poAMPSTimerNodeData->unSecondsSlotNo = ((poAMPSTimer->unIndexForSecondsSlot + (r_unTimerValue/AMPS_TIMER_MIN_SECOND_SLOT_TIME)) % AMPS_TIMER_NUM_OF_SECOND_SLOTS);

	    if(poAMPSTimerNodeData->unSecondsSlotNo < poAMPSTimer->unIndexForSecondsSlot)
        {
			poAMPSTimerNodeData->unMinutesSlotNo = ((poAMPSTimerNodeData->unMinutesSlotNo + 1) % AMPS_TIMER_NUM_OF_MINUTE_SLOTS);
        }
		poAMPSTimerNodeData->unMilliSecondsSlotNo = ((poAMPSTimer->unIndexForMilliSecondsSlot + (r_unTimerValue/poAMPSTimer->nMinMilliSecondsSlotTime)) % poAMPSTimer->nNoOfMilliSecondsSlots);
		if(poAMPSTimerNodeData->unMilliSecondsSlotNo < poAMPSTimer->unIndexForMilliSecondsSlot )
		{
			poAMPSTimerNodeData->unSecondsSlotNo = ((poAMPSTimerNodeData->unSecondsSlotNo+ 1) % AMPS_TIMER_NUM_OF_SECOND_SLOTS); 
		}
		poDListForTimerSlot = poAMPSTimer->poTimerSlotDays[poAMPSTimerNodeData->unDaysSlotNo].poDList;
	}
	else
	if(AMPS_TIMER_MIN_HOUR_SLOT_TIME <= r_unTimerValue)
	{
		poAMPSTimerNodeData->unCurrentTimerSlot = AMPS_TIMER_CURRENT_SLOT_HOURS;
		poAMPSTimerNodeData->unHoursSlotNo = ((poAMPSTimer->unIndexForHoursSlot + (r_unTimerValue/AMPS_TIMER_MIN_HOUR_SLOT_TIME)) % AMPS_TIMER_NUM_OF_HOUR_SLOTS);
		TRACE( TIMER_TRACE_ID(r_pvAMPSContext), AMPS_TRACE_LEVEL_DEBUG, "Timer is in Hour Slot # %d.\n", poAMPSTimerNodeData->unHoursSlotNo);
		poAMPSTimerNodeData->unMinutesSlotNo = ((poAMPSTimer->unIndexForMinutesSlot + (r_unTimerValue/AMPS_TIMER_MIN_MINUTE_SLOT_TIME)) % AMPS_TIMER_NUM_OF_MINUTE_SLOTS);
		TRACE( TIMER_TRACE_ID(r_pvAMPSContext), AMPS_TRACE_LEVEL_DEBUG, "With Minute Slot # %d.\n", poAMPSTimerNodeData->unMinutesSlotNo);
		
        if(poAMPSTimerNodeData->unMinutesSlotNo < poAMPSTimer->unIndexForMinutesSlot)
        {
			poAMPSTimerNodeData->unHoursSlotNo = ((poAMPSTimerNodeData->unHoursSlotNo + 1) % AMPS_TIMER_NUM_OF_HOUR_SLOTS);
        }
		poAMPSTimerNodeData->unSecondsSlotNo = ((poAMPSTimer->unIndexForSecondsSlot + (r_unTimerValue/AMPS_TIMER_MIN_SECOND_SLOT_TIME)) % AMPS_TIMER_NUM_OF_SECOND_SLOTS);

	    if(poAMPSTimerNodeData->unSecondsSlotNo < poAMPSTimer->unIndexForSecondsSlot)
        {
            poAMPSTimerNodeData->unMinutesSlotNo = ((poAMPSTimerNodeData->unMinutesSlotNo + 1) % AMPS_TIMER_NUM_OF_MINUTE_SLOTS);
        }
		poAMPSTimerNodeData->unMilliSecondsSlotNo = ((poAMPSTimer->unIndexForMilliSecondsSlot + (r_unTimerValue/poAMPSTimer->nMinMilliSecondsSlotTime)) % poAMPSTimer->nNoOfMilliSecondsSlots);
		
		if(poAMPSTimerNodeData->unMilliSecondsSlotNo < poAMPSTimer->unIndexForMilliSecondsSlot )
		{
			poAMPSTimerNodeData->unSecondsSlotNo = ((poAMPSTimerNodeData->unSecondsSlotNo+ 1) % AMPS_TIMER_NUM_OF_SECOND_SLOTS); 
		}
		
		poDListForTimerSlot = poAMPSTimer->poTimerSlotHours[poAMPSTimerNodeData->unHoursSlotNo].poDList;
	}
	else
	if(AMPS_TIMER_MIN_MINUTE_SLOT_TIME <= r_unTimerValue)
	{
		poAMPSTimerNodeData->unCurrentTimerSlot = AMPS_TIMER_CURRENT_SLOT_MINUTES;
		poAMPSTimerNodeData->unMinutesSlotNo = ((poAMPSTimer->unIndexForMinutesSlot + (r_unTimerValue/AMPS_TIMER_MIN_MINUTE_SLOT_TIME)) % AMPS_TIMER_NUM_OF_MINUTE_SLOTS);
		TRACE( TIMER_TRACE_ID(r_pvAMPSContext), AMPS_TRACE_LEVEL_DEBUG, "Timer is in Minute Slot # %d, where current Slot is %d.\n", poAMPSTimerNodeData->unMinutesSlotNo, poAMPSTimer->unIndexForMinutesSlot);
		poAMPSTimerNodeData->unSecondsSlotNo = ((poAMPSTimer->unIndexForSecondsSlot + (r_unTimerValue/AMPS_TIMER_MIN_SECOND_SLOT_TIME)) % AMPS_TIMER_NUM_OF_SECOND_SLOTS);
		TRACE( TIMER_TRACE_ID(r_pvAMPSContext), AMPS_TRACE_LEVEL_DEBUG, "With Second Slot # %d, where current Slot is %d.\n", poAMPSTimerNodeData->unSecondsSlotNo, poAMPSTimer->unIndexForSecondsSlot);
		if(poAMPSTimerNodeData->unSecondsSlotNo < poAMPSTimer->unIndexForSecondsSlot)
		{
            poAMPSTimerNodeData->unMinutesSlotNo = ((poAMPSTimerNodeData->unMinutesSlotNo + 1) % AMPS_TIMER_NUM_OF_MINUTE_SLOTS);
		}
		poAMPSTimerNodeData->unMilliSecondsSlotNo = ((poAMPSTimer->unIndexForMilliSecondsSlot + (r_unTimerValue/poAMPSTimer->nMinMilliSecondsSlotTime)) % poAMPSTimer->nNoOfMilliSecondsSlots);
		if(poAMPSTimerNodeData->unMilliSecondsSlotNo < poAMPSTimer->unIndexForMilliSecondsSlot )
		{
			poAMPSTimerNodeData->unSecondsSlotNo = ((poAMPSTimerNodeData->unSecondsSlotNo+ 1) % AMPS_TIMER_NUM_OF_SECOND_SLOTS); 
		}
		TRACE( TIMER_TRACE_ID(r_pvAMPSContext), AMPS_TRACE_LEVEL_DEBUG, "With MilliSecond Slot # %d, where current Slot is %d.\n", poAMPSTimerNodeData->unMilliSecondsSlotNo, poAMPSTimer->unIndexForMilliSecondsSlot);
		poDListForTimerSlot = poAMPSTimer->poTimerSlotMinutes[poAMPSTimerNodeData->unMinutesSlotNo].poDList;
	}
	else
	if(AMPS_TIMER_MIN_SECOND_SLOT_TIME <= r_unTimerValue)
	{
		poAMPSTimerNodeData->unCurrentTimerSlot = AMPS_TIMER_CURRENT_SLOT_SECONDS;
        poAMPSTimerNodeData->unSecondsSlotNo = ((poAMPSTimer->unIndexForSecondsSlot + (r_unTimerValue/AMPS_TIMER_MIN_SECOND_SLOT_TIME)) % AMPS_TIMER_NUM_OF_SECOND_SLOTS);
		TRACE( TIMER_TRACE_ID(r_pvAMPSContext), AMPS_TRACE_LEVEL_DEBUG, "Timer is in Second Slot # %d, where current Slot is %d.\n", poAMPSTimerNodeData->unSecondsSlotNo, poAMPSTimer->unIndexForSecondsSlot);
		poAMPSTimerNodeData->unMilliSecondsSlotNo = ((poAMPSTimer->unIndexForMilliSecondsSlot + (r_unTimerValue/poAMPSTimer->nMinMilliSecondsSlotTime)) % poAMPSTimer->nNoOfMilliSecondsSlots);
		TRACE( TIMER_TRACE_ID(r_pvAMPSContext), AMPS_TRACE_LEVEL_DEBUG, "With MilliSecond Slot # %d, where current Slot is %d.\n", poAMPSTimerNodeData->unMilliSecondsSlotNo, poAMPSTimer->unIndexForMilliSecondsSlot);
		if(poAMPSTimerNodeData->unMilliSecondsSlotNo < poAMPSTimer->unIndexForMilliSecondsSlot )
		{
			poAMPSTimerNodeData->unSecondsSlotNo = ((poAMPSTimerNodeData->unSecondsSlotNo+ 1) % AMPS_TIMER_NUM_OF_SECOND_SLOTS); 
		}
		
		poDListForTimerSlot = poAMPSTimer->poTimerSlotSeconds[poAMPSTimerNodeData->unSecondsSlotNo].poDList;
	}
	else
	{
		poAMPSTimerNodeData->unCurrentTimerSlot = AMPS_TIMER_CURRENT_SLOT_MILLISECONDS;
		poAMPSTimerNodeData->unMilliSecondsSlotNo = ((poAMPSTimer->unIndexForMilliSecondsSlot + (r_unTimerValue/poAMPSTimer->nMinMilliSecondsSlotTime)) % poAMPSTimer->nNoOfMilliSecondsSlots);
		TRACE( TIMER_TRACE_ID(r_pvAMPSContext), AMPS_TRACE_LEVEL_DEBUG, "Timer is in MilliSecond Slot # %d, where current Slot is %d.\n", poAMPSTimerNodeData->unMilliSecondsSlotNo, poAMPSTimer->unIndexForMilliSecondsSlot);
		poDListForTimerSlot = poAMPSTimer->poTimerSlotMilliSeconds[poAMPSTimerNodeData->unMilliSecondsSlotNo].poDList;
	}

    /*增加结点*/
	poAMPSTimerNodeData->pvSListNodePtr = DList_Prepend(poDListForTimerSlot, poAMPSTimerNodeData);
    if (NULL == poAMPSTimerNodeData->pvSListNodePtr)
    {
        TRACE( TIMER_TRACE_ID(r_pvAMPSContext), AMPS_TRACE_LEVEL_ERROR, "SList_Prepend is failed.\n");
        AMPS_InternalFree(poAMPSTimerNodeData);
    }
    return poAMPSTimerNodeData->pvSListNodePtr;
}

/*****************************************************************
函数名称: Timer_DeleteTimer
功能描述: 删除定时器
入参:
        void* r_pvAMPSContext
        void* r_pvLinkNode

出参:
     --
返回值:
    int

*****************************************************************/
int Timer_DeleteTimer(void* r_pvAMPSContext, void* r_pvLinkNode)
{
	t_AMPSContext* poAMPSContext = r_pvAMPSContext;
    t_AMPSTimer* poAMPSTimer = poAMPSContext->pvTimerContext;
    t_AMPSSList* poLinkNode = (t_AMPSSList*)r_pvLinkNode;
    t_AMPSTimerNodeData* poAMPSTimerNodeData = (t_AMPSTimerNodeData*)poLinkNode->pvData;

    /*根据不同时长从不同的定时器链表中删除指定的定时器结点*/
	if(AMPS_TIMER_CURRENT_SLOT_DAYS == poAMPSTimerNodeData->unCurrentTimerSlot)
	{
		if(AMPS_SUCCESS != DList_Remove(&poAMPSTimer->poTimerSlotDays[poAMPSTimerNodeData->unDaysSlotNo].poDList, r_pvLinkNode, Timer_FreeNodeData))
		{
			TRACE( TIMER_TRACE_ID(r_pvAMPSContext), AMPS_TRACE_LEVEL_ERROR, "DList_Remove failed.\n");
			return AMPS_ERROR_FAILURE;
		}
	}
	else
	if(AMPS_TIMER_CURRENT_SLOT_HOURS == poAMPSTimerNodeData->unCurrentTimerSlot)
	{
		if(AMPS_SUCCESS != DList_Remove(&poAMPSTimer->poTimerSlotHours[poAMPSTimerNodeData->unHoursSlotNo].poDList, r_pvLinkNode, Timer_FreeNodeData))
		{
			TRACE( TIMER_TRACE_ID(r_pvAMPSContext), AMPS_TRACE_LEVEL_ERROR, "DList_Remove failed.\n");
			return AMPS_ERROR_FAILURE;
		}
	}
	else
	if(AMPS_TIMER_CURRENT_SLOT_MINUTES == poAMPSTimerNodeData->unCurrentTimerSlot)
	{
		if(AMPS_SUCCESS != DList_Remove(&poAMPSTimer->poTimerSlotMinutes[poAMPSTimerNodeData->unMinutesSlotNo].poDList, r_pvLinkNode, Timer_FreeNodeData))
		{
			TRACE( TIMER_TRACE_ID(r_pvAMPSContext), AMPS_TRACE_LEVEL_ERROR, "DList_Remove failed.\n");
			return AMPS_ERROR_FAILURE;
		}
	}
	else
	if(AMPS_TIMER_CURRENT_SLOT_SECONDS == poAMPSTimerNodeData->unCurrentTimerSlot)
	{
		if(AMPS_SUCCESS != DList_Remove(&poAMPSTimer->poTimerSlotSeconds[poAMPSTimerNodeData->unSecondsSlotNo].poDList, r_pvLinkNode, Timer_FreeNodeData))
		{
			TRACE( TIMER_TRACE_ID(r_pvAMPSContext), AMPS_TRACE_LEVEL_ERROR, "DList_Remove failed.\n");
			return AMPS_ERROR_FAILURE;
		}
	}
	else
	if(AMPS_TIMER_CURRENT_SLOT_MILLISECONDS == poAMPSTimerNodeData->unCurrentTimerSlot)
	{
		//Timer is in MilliSeconds Slot, so wait for deletion
		poAMPSTimerNodeData->chStatus = AMPS_TIMER_STATUS_DISABLE;
	}

	return AMPS_SUCCESS;
}

/*
int Timer_TickerThread(void* r_poAMPSContext)
{
    t_AMPSContext* poAMPSContext = r_poAMPSContext;
    t_AMPSTimer* poAMPSTimer = poAMPSContext->pvTimerContext;
	t_AMPSTimerMessage oAMPSTimerMessage = {0};
	int nReturnValue = AMPS_ERROR_FAILURE;

	TRACE( TIMER_TRACE_ID(r_poAMPSContext), AMPS_TRACE_LEVEL_INFO, "Entering.\n");

	//Connected socket to send Timer ticks
	TRACE( TIMER_TRACE_ID(r_poAMPSContext), AMPS_TRACE_LEVEL_DEBUG, "Create connected socket to send Timer ticks.\n");
	if(AMPS_SUCCESS != AMPS_InternalCreateBlockingConn(r_poAMPSContext, AMPS_LOOP_BACK_IP_ADDRESS, poAMPSTimer->nTimerConnectionListenPort, AMPS_LOOP_BACK_IP_ADDRESS, NULL, (void**)&poAMPSTimer->poNetworkMessage, NULL))
	{
		TRACE( TIMER_TRACE_ID(r_poAMPSContext), AMPS_TRACE_LEVEL_ERROR, "AMPS_InternalCreateBlockingConn failed.\n");
		return AMPS_ERROR_FAILURE;
	}

	TRACE( TIMER_TRACE_ID(r_poAMPSContext), AMPS_TRACE_LEVEL_INFO, "Handle %d.\n", (int)poAMPSTimer->poNetworkMessage->nHandle);

    while (poAMPSTimer->nTimerIsRunning == AMPS_TRUE)
    {
		oAMPSTimerMessage.unTickCount += 1;
        if(AMPS_TIMER_EVENT_TIMEOUT == SAPI_GetNextEvent(r_poAMPSContext, poAMPSTimer->poNetworkMessage->nHandle, poAMPSTimer->nSlotTimeOutInSeconds, poAMPSTimer->nSlotTimeOutInMilliSeconds))
        {
			TRACE(TIMER_TRACE_ID(r_poAMPSContext), AMPS_TRACE_LEVEL_DEBUG, "Send Msg to Main Thread.\n");
			nReturnValue = SAPI_SocketWriteBlocking(poAMPSContext, poAMPSTimer->poNetworkMessage, &oAMPSTimerMessage, sizeof(t_AMPSTimerMessage), NULL);
			if(AMPS_SUCCESS != nReturnValue)
			{
				TRACE( TIMER_TRACE_ID(r_poAMPSContext), AMPS_TRACE_LEVEL_ERROR, "SAPI_SocketWriteBlocking failed.\n");
				continue;
			}
			else
			if(AMPS_ERROR_SOCKET_CLOSED == nReturnValue)
			{
				TRACE( TIMER_TRACE_ID(r_poAMPSContext), AMPS_TRACE_LEVEL_ERROR, "Connection is closed from remote side\n");
				printf("Connection is closed from remote side ....  Connect again .....\n");
				if(AMPS_SUCCESS != AMPS_InternalCreateBlockingConn(r_poAMPSContext, AMPS_LOOP_BACK_IP_ADDRESS, poAMPSTimer->nTimerConnectionListenPort, AMPS_LOOP_BACK_IP_ADDRESS, NULL, (void**)&poAMPSTimer->poNetworkMessage, NULL))
				{
					TRACE( TIMER_TRACE_ID(r_poAMPSContext), AMPS_TRACE_LEVEL_ERROR, "AMPS_InternalCreateBlockingConn failed.\n");
					return AMPS_ERROR_FAILURE;
				}
			}
			else
			{
				oAMPSTimerMessage.unTickCount = 0;
			}
        }
    }

    TRACE( TIMER_TRACE_ID(r_poAMPSContext), AMPS_TRACE_LEVEL_INFO, "Leaving.\n");
    return AMPS_SUCCESS;
}
*/

/*****************************************************************
函数名称: Timer_ProcessTimerEvent
功能描述: 处理定时器事件
入参:
        void* r_pvAMPSContext

出参:
     --
返回值:
    int

*****************************************************************/
int Timer_ProcessTimerEvent(void* r_pvAMPSContext)
{
	t_AMPSContext* poAMPSContext = r_pvAMPSContext;
	t_AMPSNetworkMsg* poAMPSNetworkMsg = poAMPSContext->poAMPSNetworkTimerMsgForConnectedSocket;

	TRACE( TIMER_TRACE_ID(r_pvAMPSContext), AMPS_TRACE_LEVEL_INFO, "Entering.\n");

	if((NULL != poAMPSNetworkMsg) && (AMPS_FALSE == poAMPSNetworkMsg->bLastOperationPending))
	{
		poAMPSNetworkMsg->pfAMPS_SNMReadEvtHandler(poAMPSContext, NULL, poAMPSNetworkMsg);
	}

	TRACE( TIMER_TRACE_ID(r_pvAMPSContext), AMPS_TRACE_LEVEL_INFO, "Leaving.\n");
	return AMPS_SUCCESS;
}

/*****************************************************************
函数名称: Timer_ProcessTimerEvent
功能描述: 定时器服务器接收数据后处理函数
入参:
        AMPS_HANDLE r_hAMPS_HANDLE
        AMPS_APP_HANDLE r_hAMPS_APP_HANDLE
        AMPS_NET_HANDLE r_hAMPS_NET_HANDLE
        unsigned char* r_puchBuff
        int r_nBuffLength
        int r_nPort
        char* r_chIPAddress

出参:
     --
返回值:
    int

*****************************************************************/
int Timer_RecvDataCallback(AMPS_HANDLE r_hAMPS_HANDLE, AMPS_APP_HANDLE r_hAMPS_APP_HANDLE, AMPS_NET_HANDLE r_hAMPS_NET_HANDLE, unsigned char* r_puchBuff, int r_nBuffLength, int r_nPort, char* r_chIPAddress)
{
	t_AMPSTimerMessage* poAMPSTimerMessage = (t_AMPSTimerMessage*)r_puchBuff;

	TRACE( TIMER_TRACE_ID(r_hAMPS_HANDLE), AMPS_TRACE_LEVEL_INFO, "Entering.\n");

	if(sizeof(t_AMPSTimerMessage) != r_nBuffLength)
	{
		TRACE(TIMER_TRACE_ID(r_hAMPS_HANDLE), AMPS_TRACE_LEVEL_WARNING, "Invalid AMPSTimerMessage size %d.\n", r_nBuffLength);
	}

	TRACE( TIMER_TRACE_ID(r_hAMPS_HANDLE), AMPS_TRACE_LEVEL_DEBUG_2, "Timer event is received of size %d and uchCount %d.\n", r_nBuffLength, poAMPSTimerMessage->unTickCount);
	for( ; 0 < poAMPSTimerMessage->unTickCount; poAMPSTimerMessage->unTickCount--)
	{
		Timer_ProcessTimerSlot(r_hAMPS_HANDLE, r_hAMPS_APP_HANDLE);
	}

	TRACE( TIMER_TRACE_ID(r_hAMPS_HANDLE), AMPS_TRACE_LEVEL_INFO, "Leaving.\n");
	return AMPS_SUCCESS;
}

/*****************************************************************
函数名称: Timer_ProcessTimerEvent
功能描述: 定时器服务器接收数据后处理函数
入参:
        t_AMPSContext* r_poAMPSContext
        t_AMPSTimer* r_poAMPSTimer


出参:
     --
返回值:
    int

*****************************************************************/
void Timer_ProcessTimerSlot(t_AMPSContext* r_poAMPSContext, t_AMPSTimer* r_poAMPSTimer)
{
    TRACE( TIMER_TRACE_ID(r_poAMPSContext), AMPS_TRACE_LEVEL_INFO, "Entering.\n");

	//process all the slots
	TRACE( TIMER_TRACE_ID(r_poAMPSContext), AMPS_TRACE_LEVEL_DEBUG_2, "Process all the slots.\n");

    /*处理并释放毫秒定时器链表中的所有结点*/
    DList_FreeNodes(&r_poAMPSTimer->poTimerSlotMilliSeconds[r_poAMPSTimer->unIndexForMilliSecondsSlot].poDList, Timer_ProcessMilliSecondsNodeData);

    // set all the counters
	TRACE( TIMER_TRACE_ID(r_poAMPSContext), AMPS_TRACE_LEVEL_DEBUG_2, "Set all the counters.\n");
	r_poAMPSTimer->unIndexForMilliSecondsSlot = (r_poAMPSTimer->unIndexForMilliSecondsSlot + 1) % r_poAMPSTimer->nNoOfMilliSecondsSlots;

    /*毫秒定时器链表结点已使用完*/
	if(r_poAMPSTimer->nNoOfMilliSecondsSlots == (r_poAMPSTimer->unIndexForMilliSecondsSlot + 1))
	{
		r_poAMPSTimer->unIndexForSecondsSlot = (r_poAMPSTimer->unIndexForSecondsSlot + 1) % AMPS_TIMER_NUM_OF_SECOND_SLOTS;
		TRACE( TIMER_TRACE_ID(r_poAMPSContext), AMPS_TRACE_LEVEL_DEBUG_2, "Current Index for Seconds Slot %d.\n", r_poAMPSTimer->unIndexForSecondsSlot);
		Timer_ProcessSecondsTimer(r_poAMPSContext, r_poAMPSTimer, r_poAMPSTimer->poTimerSlotSeconds[r_poAMPSTimer->unIndexForSecondsSlot].poDList);

		if(AMPS_TIMER_NUM_OF_SECOND_SLOTS == (r_poAMPSTimer->unIndexForSecondsSlot + 1))
		{
			r_poAMPSTimer->unIndexForMinutesSlot = (r_poAMPSTimer->unIndexForMinutesSlot + 1) % AMPS_TIMER_NUM_OF_MINUTE_SLOTS;
			TRACE( TIMER_TRACE_ID(r_poAMPSContext), AMPS_TRACE_LEVEL_DEBUG_2, "Current Index for Minutes Slot %d.\n", r_poAMPSTimer->unIndexForMinutesSlot);
			Timer_ProcessMinutesTimer(r_poAMPSContext, r_poAMPSTimer, r_poAMPSTimer->poTimerSlotMinutes[r_poAMPSTimer->unIndexForMinutesSlot].poDList);

			if(AMPS_TIMER_NUM_OF_MINUTE_SLOTS == (r_poAMPSTimer->unIndexForMinutesSlot + 1))
			{
				r_poAMPSTimer->unIndexForHoursSlot = (r_poAMPSTimer->unIndexForHoursSlot + 1) % AMPS_TIMER_NUM_OF_HOUR_SLOTS;
				TRACE( TIMER_TRACE_ID(r_poAMPSContext), AMPS_TRACE_LEVEL_DEBUG_2, "Current Index for Hours Slot %d.\n", r_poAMPSTimer->unIndexForHoursSlot);
				Timer_ProcessHoursTimer(r_poAMPSContext, r_poAMPSTimer, r_poAMPSTimer->poTimerSlotHours[r_poAMPSTimer->unIndexForHoursSlot].poDList);

				if(AMPS_TIMER_NUM_OF_HOUR_SLOTS == (r_poAMPSTimer->unIndexForHoursSlot + 1))
				{
					r_poAMPSTimer->unIndexForDaysSlot = (r_poAMPSTimer->unIndexForDaysSlot + 1) % AMPS_TIMER_NUM_OF_DAY_SLOTS;
					TRACE( TIMER_TRACE_ID(r_poAMPSContext), AMPS_TRACE_LEVEL_DEBUG_2, "Current Index for Days Slot %d.\n", r_poAMPSTimer->unIndexForDaysSlot);
					Timer_ProcessDaysTimer(r_poAMPSContext, r_poAMPSTimer, r_poAMPSTimer->poTimerSlotDays[r_poAMPSTimer->unIndexForDaysSlot].poDList);
				}
			}
		}
	}

	TRACE( TIMER_TRACE_ID(r_poAMPSContext), AMPS_TRACE_LEVEL_INFO, "Leaving.\n");
}

/*****************************************************************
函数名称: Timer_ProcessDaysTimer
功能描述: 处理天定时器
入参:
        t_AMPSContext* r_poAMPSContext
        t_AMPSTimer* r_poAMPSTimer
        t_AMPSDList* r_poAMPSDListForDaysTimer


出参:
     --
返回值:
    void

*****************************************************************/
void Timer_ProcessDaysTimer(t_AMPSContext* r_poAMPSContext, t_AMPSTimer* r_poAMPSTimer, t_AMPSDList* r_poAMPSDListForDaysTimer)
{
	void* pvAMPSSListCurrentNode = r_poAMPSDListForDaysTimer->poAMPSSListHead;
	t_AMPSTimerNodeData* poAMPSTimerNodeData = NULL;

	TRACE( TIMER_TRACE_ID(r_poAMPSContext), AMPS_TRACE_LEVEL_INFO, "Entering.\n");

	while(pvAMPSSListCurrentNode)
	{
		poAMPSTimerNodeData = SList_GetNodeData(pvAMPSSListCurrentNode);
		TRACE( TIMER_TRACE_ID(r_poAMPSContext), AMPS_TRACE_LEVEL_DEBUG_2, "DList_RemoveWithOutFree called.\n");
		if(AMPS_SUCCESS != DList_RemoveWithOutFree(r_poAMPSDListForDaysTimer, pvAMPSSListCurrentNode))
		{
			TRACE( TIMER_TRACE_ID(r_poAMPSContext), AMPS_TRACE_LEVEL_ERROR, "DList_RemoveWithOutFree failed.\n");
		}
		poAMPSTimerNodeData->unDaysSlotNo = 0;
		poAMPSTimerNodeData->unCurrentTimerSlot = AMPS_TIMER_CURRENT_SLOT_HOURS;
		TRACE( TIMER_TRACE_ID(r_poAMPSContext), AMPS_TRACE_LEVEL_DEBUG_2, "DList_PrependGivenNode called.\n");
		DList_PrependGivenNode(r_poAMPSTimer->poTimerSlotHours[poAMPSTimerNodeData->unHoursSlotNo].poDList, poAMPSTimerNodeData, pvAMPSSListCurrentNode);
		pvAMPSSListCurrentNode = r_poAMPSDListForDaysTimer->poAMPSSListHead;
	}

	TRACE( TIMER_TRACE_ID(r_poAMPSContext), AMPS_TRACE_LEVEL_INFO, "Leaving.\n");
}

/*****************************************************************
函数名称: Timer_ProcessDaysTimer
功能描述: 处理小时定时器
入参:
        t_AMPSContext* r_poAMPSContext
        t_AMPSTimer* r_poAMPSTimer
        t_AMPSDList* r_poAMPSDListForDaysTimer


出参:
     --
返回值:
    void

*****************************************************************/
void Timer_ProcessHoursTimer(t_AMPSContext* r_poAMPSContext, t_AMPSTimer* r_poAMPSTimer, t_AMPSDList* r_poAMPSDListForHoursTimer)
{
	void* pvAMPSSListCurrentNode = r_poAMPSDListForHoursTimer->poAMPSSListHead;
	t_AMPSTimerNodeData* poAMPSTimerNodeData = NULL;

	TRACE( TIMER_TRACE_ID(r_poAMPSContext), AMPS_TRACE_LEVEL_INFO, "Entering.\n");

	while(pvAMPSSListCurrentNode)
	{
		poAMPSTimerNodeData = SList_GetNodeData(pvAMPSSListCurrentNode);
		TRACE( TIMER_TRACE_ID(r_poAMPSContext), AMPS_TRACE_LEVEL_DEBUG_2, "DList_RemoveWithOutFree called.\n");
		if(AMPS_SUCCESS != DList_RemoveWithOutFree(r_poAMPSDListForHoursTimer, pvAMPSSListCurrentNode))
		{
			TRACE( TIMER_TRACE_ID(r_poAMPSContext), AMPS_TRACE_LEVEL_ERROR, "DList_RemoveWithOutFree failed.\n");
		}
		poAMPSTimerNodeData->unHoursSlotNo = 0;
		poAMPSTimerNodeData->unCurrentTimerSlot = AMPS_TIMER_CURRENT_SLOT_MINUTES;
		TRACE( TIMER_TRACE_ID(r_poAMPSContext), AMPS_TRACE_LEVEL_DEBUG_2, "DList_PrependGivenNode called.\n");
		DList_PrependGivenNode(r_poAMPSTimer->poTimerSlotMinutes[poAMPSTimerNodeData->unMinutesSlotNo].poDList, poAMPSTimerNodeData, pvAMPSSListCurrentNode);
		pvAMPSSListCurrentNode = r_poAMPSDListForHoursTimer->poAMPSSListHead;
	}

	TRACE( TIMER_TRACE_ID(r_poAMPSContext), AMPS_TRACE_LEVEL_INFO, "Leaving.\n");
}

/*****************************************************************
函数名称: Timer_ProcessDaysTimer
功能描述: 处理分定时器
入参:
        t_AMPSContext* r_poAMPSContext
        t_AMPSTimer* r_poAMPSTimer
        t_AMPSDList* r_poAMPSDListForDaysTimer


出参:
     --
返回值:
    void

*****************************************************************/
void Timer_ProcessMinutesTimer(t_AMPSContext* r_poAMPSContext, t_AMPSTimer* r_poAMPSTimer, t_AMPSDList* r_poAMPSDListForMinutesTimer)
{
	void* pvAMPSSListCurrentNode = r_poAMPSDListForMinutesTimer->poAMPSSListHead;
	t_AMPSTimerNodeData* poAMPSTimerNodeData = NULL;

	TRACE( TIMER_TRACE_ID(r_poAMPSContext), AMPS_TRACE_LEVEL_INFO, "Entering.\n");

	while(pvAMPSSListCurrentNode)
	{
		poAMPSTimerNodeData = SList_GetNodeData(pvAMPSSListCurrentNode);
		TRACE( TIMER_TRACE_ID(r_poAMPSContext), AMPS_TRACE_LEVEL_DEBUG_2, "DList_RemoveWithOutFree called.\n");
		if(AMPS_SUCCESS != DList_RemoveWithOutFree(r_poAMPSDListForMinutesTimer, pvAMPSSListCurrentNode))
		{
			TRACE( TIMER_TRACE_ID(r_poAMPSContext), AMPS_TRACE_LEVEL_ERROR, "DList_RemoveWithOutFree failed.\n");
		}
		poAMPSTimerNodeData->unMinutesSlotNo = 0;
		poAMPSTimerNodeData->unCurrentTimerSlot = AMPS_TIMER_CURRENT_SLOT_SECONDS;
		TRACE( TIMER_TRACE_ID(r_poAMPSContext), AMPS_TRACE_LEVEL_DEBUG_2, "DList_PrependGivenNode called.\n");
		DList_PrependGivenNode(r_poAMPSTimer->poTimerSlotSeconds[poAMPSTimerNodeData->unSecondsSlotNo].poDList, poAMPSTimerNodeData, pvAMPSSListCurrentNode);
		pvAMPSSListCurrentNode = r_poAMPSDListForMinutesTimer->poAMPSSListHead;
	}

	TRACE( TIMER_TRACE_ID(r_poAMPSContext), AMPS_TRACE_LEVEL_INFO, "Leaving.\n");
}

/*****************************************************************
函数名称: Timer_ProcessDaysTimer
功能描述: 处理秒定时器
入参:
        t_AMPSContext* r_poAMPSContext
        t_AMPSTimer* r_poAMPSTimer
        t_AMPSDList* r_poAMPSDListForDaysTimer


出参:
     --
返回值:
    void

*****************************************************************/
void Timer_ProcessSecondsTimer(t_AMPSContext* r_poAMPSContext, t_AMPSTimer* r_poAMPSTimer, t_AMPSDList* r_poAMPSDListForSecondsTimer)
{
	void* pvAMPSSListCurrentNode = r_poAMPSDListForSecondsTimer->poAMPSSListHead;
	t_AMPSTimerNodeData* poAMPSTimerNodeData = NULL;

	TRACE( TIMER_TRACE_ID(r_poAMPSContext), AMPS_TRACE_LEVEL_INFO, "Entering.\n");

	while(pvAMPSSListCurrentNode)
	{
		poAMPSTimerNodeData = SList_GetNodeData(pvAMPSSListCurrentNode);
		TRACE( TIMER_TRACE_ID(r_poAMPSContext), AMPS_TRACE_LEVEL_DEBUG_2, "DList_RemoveWithOutFree called.\n");
		if(AMPS_SUCCESS != DList_RemoveWithOutFree(r_poAMPSDListForSecondsTimer, pvAMPSSListCurrentNode))
		{
			TRACE( TIMER_TRACE_ID(r_poAMPSContext), AMPS_TRACE_LEVEL_ERROR, "DList_RemoveWithOutFree failed.\n");
		}
		poAMPSTimerNodeData->unSecondsSlotNo = 0;
		poAMPSTimerNodeData->unCurrentTimerSlot = AMPS_TIMER_CURRENT_SLOT_MILLISECONDS;
		TRACE( TIMER_TRACE_ID(r_poAMPSContext), AMPS_TRACE_LEVEL_DEBUG_2, "DList_PrependGivenNode called.\n");
		DList_PrependGivenNode(r_poAMPSTimer->poTimerSlotMilliSeconds[poAMPSTimerNodeData->unMilliSecondsSlotNo].poDList, poAMPSTimerNodeData, poAMPSTimerNodeData->pvSListNodePtr);
		pvAMPSSListCurrentNode = r_poAMPSDListForSecondsTimer->poAMPSSListHead;
	}

	TRACE( TIMER_TRACE_ID(r_poAMPSContext), AMPS_TRACE_LEVEL_INFO, "Leaving.\n");
}

/*****************************************************************
函数名称: Timer_ProcessDaysTimer
功能描述: 处理毫秒定时器
入参:
        t_AMPSContext* r_poAMPSContext
        t_AMPSTimer* r_poAMPSTimer
        t_AMPSDList* r_poAMPSDListForDaysTimer


出参:
     --
返回值:
    void

*****************************************************************/

int Timer_ProcessMilliSecondsNodeData(void** r_ppvAMPSTimerNodeData)
{
    t_AMPSTimerNodeData* poAMPSTimerNodeData = *r_ppvAMPSTimerNodeData;
    t_AMPSContext* poAMPSContext = poAMPSTimerNodeData->pvAMPSContext;

    if((NULL != poAMPSTimerNodeData->pfAMPS_TimerCallBack) && (AMPS_TIMER_STATUS_ENABLE == poAMPSTimerNodeData->chStatus))
    {
		TRACE( TIMER_TRACE_ID(poAMPSContext), AMPS_TRACE_LEVEL_DEBUG, "poTimerNode->m_pfTimer_TimeoutFunction is called.\n");
		//printf("poTimerNode->m_pfTimer_TimeoutFunction is called.\n");
        /*执行定时器超时函数*/
        if (AMPS_SUCCESS != poAMPSTimerNodeData->pfAMPS_TimerCallBack(poAMPSContext, poAMPSTimerNodeData->pvData))
        {
            TRACE( TIMER_TRACE_ID(poAMPSContext), AMPS_TRACE_LEVEL_ERROR, "Timer CallBack is failed.\n");
        }
		//printf("Leaving.\n");
    } 
    else
    {
        TRACE( TIMER_TRACE_ID(poAMPSContext), AMPS_TRACE_LEVEL_WARNING, "Either Timer is disabled or callback funtion is NULL.\n");
    }

    AMPS_InternalFree(*r_ppvAMPSTimerNodeData);
    *r_ppvAMPSTimerNodeData = NULL;
    return AMPS_SUCCESS;
}


作者:dyx1024 发表于2013-1-27 22:54:27 原文链接
阅读:36 评论:0 查看评论

Viewing all articles
Browse latest Browse all 35570

Trending Articles