c++ 简单模拟实现 游戏定时开启任务

// game_task.cpp : 定义控制台应用程序的入口点。
//

#include "stdafx.h"
#include <Windows.h>
#include <iostream>
#include <sys/timeb.h>
#include <time.h>
#include <process.h>
#include <map>
#include <assert.h>
using namespace std;

// time_test.cpp : 定义控制台应用程序的入口点。
//
/*
author:郑金玮
time:2014/07/19
desc:impl task manager
*/
//


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

	HANDLE getHandle() const;
	bool wait();
	bool wait(DWORD timeoutMillsecs);
	void start();
	DWORD terminate(DWORD exitcode=0);

private:

	virtual int run() = 0;
	static unsigned int __stdcall threadFunction(void *threadParam);
	void resetHandle();
private:
	CThread(const CThread&);
	CThread& operator = (const CThread&);
private:
	HANDLE m_handle;
};

CThread::CThread():m_handle(NULL)
{
}

CThread::~CThread()
{
	resetHandle();
}

HANDLE CThread::getHandle() const
{
	return m_handle;
}
void CThread::start()
{
	if (m_handle == NULL)
	{
		unsigned int _threadID = 0;

		m_handle = (HANDLE)::_beginthreadex(0,
			0,
			threadFunction,
			(void*)this,
			0,
			&_threadID
			);
		assert(m_handle!=NULL);
	}
}

bool CThread::wait()
{
	return wait(INFINITE);
}
bool CThread::wait(DWORD timeoutMillsecs)
{
	bool _isOK = false;

	if (NULL == m_handle)
	{
		return true;
	}

	DWORD _result = ::WaitForSingleObject(m_handle,timeoutMillsecs);

	if (_result == WAIT_TIMEOUT)
	{
		_isOK = false;
	}
	if (_result == WAIT_OBJECT_0)
	{
		_isOK = true;
	}
	resetHandle();

	return _isOK;
}

DWORD CThread::terminate(DWORD exitcode)
{
	if (m_handle != NULL && ::TerminateThread(m_handle,exitcode))
	{
		m_handle = NULL;
		return ERROR_SUCCESS;
	}
	else
	{
		return ::GetLastError();
	}
	return 0;
}

unsigned int __stdcall CThread::threadFunction(void *threadParam)
{
	CThread *pThread = (CThread*)threadParam;
	int _ret = pThread->run();

	pThread->resetHandle();
	return _ret;
}
void CThread::resetHandle()
{
	if (m_handle)
	{
		::CloseHandle(m_handle);
		m_handle =NULL;
	}
}


//


class CRealDateTime
{
public:
	CRealDateTime():m_dif(0),m_dif2(0)
	{}
	~CRealDateTime(){}
private:
	struct tm *m_t,m_t2;
	time_t m_tb;
	 __int64 m_saveedTime;
public:
	void calcTime() //计算当前时间到指定时间之间的时间差(秒级)
	{
		this->update();

		m_saveedTime = m_dif2-m_dif;
	}
	 __int64 getCalcTime()
	{
		return m_saveedTime;
	}
	 void updateCalcTime(__int64 newTime)
	 {
		 m_saveedTime = newTime;
	 }
	void setTime(int year,int month,int day,int hour,int minute,int sec) // set specify time
	{
		m_t2.tm_year = year - 1900;  //tm结构记录年为实际-1900
		m_t2.tm_mon  = month - 1;
		m_t2.tm_mday = day;
		m_t2.tm_hour = hour;
		m_t2.tm_min  = minute;
		m_t2.tm_sec  = sec;
		m_dif2=mktime(&m_t2);
	}
	void setTime(int hour,int minute,int sec)
	{
		::time(&m_tb);  
		struct tm *_t;
		_t= ::localtime(&m_tb);
		m_t2.tm_year = _t->tm_year;
		m_t2.tm_mon  = _t->tm_mon ;
		m_t2.tm_mday = _t->tm_mday;
		m_t2.tm_hour = hour;
		m_t2.tm_min  = minute;
		m_t2.tm_sec  = sec;
		m_dif2=mktime(&m_t2);
	}
	void update() //get current time
	{
		::time(&m_tb);  
		m_t= ::localtime(&m_tb);
		m_dif=mktime(m_t);
	}
	 __int64 gettime()
	{
		return m_dif;
	}
	 void updateBegain(__int64 newBegain)
	 {
		 m_dif = newBegain;
	 }
	 __int64 get_now2Begain(__int64 t)
	 {
		 return t-m_dif;
	 }
private:
	 __int64 m_dif,m_dif2;
};


class CTimerManager
{
public:
	 __int64 getDiffTime(CRealDateTime *last,CRealDateTime *now)
	{
		__int64 __diff = now->gettime() - last->gettime();
		return __diff;
	}
public:
	static CTimerManager* instance()
	{
		static CTimerManager __inst;
		return &__inst;
	}
};

#define TIMERMANAGER() CTimerManager::instance()



class CAction
{
public:
	CAction(){}
	virtual ~CAction(){}
	virtual	void process() = 0;
};

class CActionLogin : public CAction
{
public:
	CActionLogin(){}
	~CActionLogin(){}
public:
	void process(){
		cout<<"login  process...."<<endl;
	}
};

class CActionGivePrize :public CAction
{
public:
	CActionGivePrize(){}
	~CActionGivePrize(){}
public:
	void process(){
		cout<<"givePrize  process...."<<endl;
	}
};

//


class CTask 
{
public:
	
	void init(unsigned int nTaskID,CAction& action,CRealDateTime& objTime)
	{
		m_pAction = &action;
		m_nTaskID = nTaskID;
		m_TrrigleTime = &objTime;
		m_bState = true;
	}
	bool getState(){ return m_bState ;}
	void setState(bool state)
	{
		m_bState = state;
	}
	unsigned int getID(){ return m_nTaskID;}
	bool checkTime(__int64 _nowEclps,__int64 _now)
	{
		 __int64 _calctime = this->m_TrrigleTime->getCalcTime();
		if (_calctime > 0) 
		{
			_calctime -=_nowEclps;
			if(_calctime<=0)
			{
				_calctime=0;
			}
			 this->m_TrrigleTime->updateBegain(_now);
			 this->m_TrrigleTime->updateCalcTime(_calctime);
			return false;
		}
		setState(false);//修改任务状态
		return true;
	}
	void processevent()
	{
		m_pAction->process();
	}
	CRealDateTime* getTimer(){
		return m_TrrigleTime;
	}
private:
	
	CAction* m_pAction;
	unsigned int m_nTaskID;
	CRealDateTime *m_TrrigleTime;
	bool m_bState;
};

//
class CTaskManager :public CThread
{
	CTaskManager(){
		start();
	}
	~CTaskManager(){
		terminate(0);
	}
public:
	static CTaskManager* instance()
	{
		static CTaskManager __instance;
		return &__instance;
	}
public:
	bool add(CTask &task)
	{
		unsigned int _nTaskID = task.getID();
		m_typeMapTask::iterator _itor = m_mapTask.find(_nTaskID);
		m_typeMapTask::iterator _end = m_mapTask.end();

		if (_itor != _end)
		{
			return false;
		}
		m_mapTask.insert(make_pair<unsigned int,CTask*>(_nTaskID,&task));
		return true;
	}
	bool close(unsigned int nTaskID)
	{
		m_typeMapTask::iterator _itor = m_mapTask.find(nTaskID);
		m_typeMapTask::iterator _end = m_mapTask.end();

		if (_itor != _end)
		{
			m_mapTask.erase(_itor);
			return true;
		}
		return false;
	}
	bool close(CTask &task)
	{
		unsigned int _nTaskID = task.getID();
		m_typeMapTask::iterator _itor = m_mapTask.find(_nTaskID);
		m_typeMapTask::iterator _end = m_mapTask.end();

		if (_itor != _end)
		{
			m_mapTask.erase(_itor);
			return true;
		}
		return false;
	}
	bool triggle(CTask &task)//定时器触发
	{
		//判断逻辑
		CRealDateTime _now;
		_now.update(); //保存当前时间
		 __int64 _nowDif = _now.gettime(); //获取当前时间的时间戳
		__int64  _Eclpse= task.getTimer()->get_now2Begain(_nowDif);//获取当前时间到任务开始加入时的时间差
		 if( (task.getState()) && (task.checkTime(_Eclpse,_nowDif)))
		 {
			 task.processevent();
			 return true;
		 }
		return false;
	}
	bool triggle(unsigned int nTaskID) //主动触发
	{
		m_typeMapTask::iterator _itor = m_mapTask.find(nTaskID);
		m_typeMapTask::iterator _end = m_mapTask.end();

		if (_itor != _end)
		{
			return triggle(*(_itor->second));
		}
		return false;
	}
	int run()//定时器在这里处理
	{
		while(true)
		{
			m_typeMapTask::iterator _begain = m_mapTask.begin();
			m_typeMapTask::iterator _end      = m_mapTask.end();

			m_typeMapTask::iterator _itor;
			for (_itor = _begain;_itor != _end ;_itor++)
			{
				triggle(*(_itor->second));
			}
		}
		return 0;
	}
private:
	typedef map<unsigned int,CTask*> m_typeMapTask;
	m_typeMapTask m_mapTask;
};

#define  TASKMANAGER() CTaskManager::instance()


#define SAFE_RELEASE(point) \
{ \
	delete point; \
	point = 0; \
}

int _tmain(int argc, _TCHAR* argv[])
{

	CRealDateTime timer1;
	timer1.setTime(16,44,20);
	timer1.calcTime();

	CAction *paction = new CActionLogin();
	CTask task;

	task.init(1,*paction,timer1);
	TASKMANAGER()->add(task);
	

	//
	CRealDateTime timer2;
	timer2.setTime(2014,7,19,16,44,30);
	timer2.calcTime();

	CAction *paction2 = new CActionGivePrize();
	CTask task2;

	task2.init(2,*paction2,timer2);
	TASKMANAGER()->add(task2);

	while (true);

	SAFE_RELEASE(paction);
	SAFE_RELEASE(paction2);
	return 0;
}


 

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值