定时器C++版

/********************************************************************
*  文件名:   Server.h
*  文件描述: 网络游戏模拟
*  创建人:   陈泽丹 ,2012年11月16日     
*  版本号:   1.0
*  修改记录:
********************************************************************/
#pragma once


template< class _Name, class _I, class _N >
class Proxy_Factory
{
public:
	static void open()
	{  
		if( NULL == m_p_proxy )
			m_p_proxy = new _N;
	}
	template< class _Par >
	static void open( _Par par)
	{  
		if( NULL == m_p_proxy )
			m_p_proxy = new _N( par );
	}
	template< class _Par1, class _Par2 >
	static void open( _Par1 par1, _Par2 _par2 )
	{  
		if( NULL == m_p_proxy )
			m_p_proxy = new _N( par1, _par2 );
	}
	static void close()
	{ 
		if( NULL != m_p_proxy )
		{
			delete m_p_proxy;
			m_p_proxy = NULL;
		}
	}
	static _I* get_proxy(){ return m_p_proxy; }

private:
	static _I* m_p_proxy;
};
template< class _Name, class _I, class _N >
_I* Proxy_Factory< _Name, _I, _N >::m_p_proxy = NULL;


 

/********************************************************************
*  文件名:   ITime_Axis.h
*  文件描述: 参数绑定器
*  创建人:   陈泽丹 ,2012年11月17日     
*  版本号:   1.0
*  备注:     
********************************************************************/
#pragma once
#include <map>



using namespace std;

#define  G_TIME_AXIS_MODULE_NAME	1

// 定时器回调接口
struct ITimerSink
{
	virtual ~ITimerSink(){}
	// 定时器回调
	virtual void on_time(long _user_sign) = 0;
};

//时间辅接口
struct ITime_Axis
{
	virtual ~ITime_Axis(){}
	//增加计时器
	virtual void set_time_tgr( ITimerSink* _p_time_tgr, long _user_sign, long _delay_time, long _interval_time ) = 0;

	//减少计时器
	virtual void kill_time_tgr( ITimerSink* _p_time_tgr, long _user_sign ) = 0;

	//模糊计时
	virtual void on_vague_time() = 0;

	//精确计时
	virtual void on_exact_time() = 0;
};


//时间轴
struct Time_Grid;
struct Delete_Sign;
class Time_Axis: public ITime_Axis
{
public:
	Time_Axis();
	virtual ~Time_Axis();

	//增加计时器
	void set_time_tgr( ITimerSink* _p_time_tgr, long _user_sign, long _delay_time, long _interval_time );

	//减少计时器
	void kill_time_tgr( ITimerSink* _p_time_tgr, long _user_sign );

	//模糊计时
	void on_vague_time();

	//精确计时
	void on_exact_time();

private:
	typedef map< pair<ITimerSink*, long>, Time_Grid* >		MAP_TYPE;
	MAP_TYPE												time_grids;
	MAP_TYPE::iterator										m_nonius;
	Delete_Sign*											mp_delete_sign;
};


template< class _ITime_Axis >
class TimerSink: public ITimerSink
{
public:
	TimerSink( _ITime_Axis* _p_itime_axis ):mp_itime_axis(_p_itime_axis){}
	virtual ~TimerSink(){}
	//定时器绑定
	virtual void get_timer_sink( long _user_sign, long _delay_time, long _interval_time )
	{
		mp_itime_axis->set_time_tgr(this, _user_sign, _delay_time, _interval_time); 
	}
	//定时器解除
	virtual void release_timer_sink( long _user_sign )
	{
		mp_itime_axis->kill_time_tgr(this, _user_sign); 
	}

private:
	_ITime_Axis*	mp_itime_axis;
};


template< class _ITime_Axis, class _Fun >
class Fun_Time_Tgr: public TimerSink< _ITime_Axis >
{
public:
	Fun_Time_Tgr( _ITime_Axis* _p_itime_axis, const _Fun& _fun, long _delay_time, long _interval_time )
		:TimerSink<_ITime_Axis>(_p_itime_axis), m_fun(_fun)
	{
		get_timer_sink(1, _delay_time, _interval_time)
	}
	virtual ~Fun_Time_Tgr()
	{
		release_timer_sink(1);
	}
	// 定时器回调
	virtual void on_time( long _user_sign )
	{
		m_fun();
	}

private:
	_Fun			m_fun;
};


template< class _ITime_Axis, class _PtrObj, class _MemFn >
class MemFun_Time_Tgr: public TimerSink< _ITime_Axis >
{
public:
	MemFun_Time_Tgr( _ITime_Axis* _p_itime_axis, const _PtrObj& _p_obj, _MemFn _p_menfn, long _delay_time, long _interval_time )
		:TimerSink<_ITime_Axis>(_p_itime_axis),  m_p_obj(_p_obj), m_p_memfn(_p_menfn)
	{
		get_timer_sink(1, _delay_time, _interval_time);
	}
	virtual ~MemFun_Time_Tgr()
	{
		release_timer_sink(1);
	}
	// 定时器回调
	virtual void on_time( long _user_sign )
	{
		((*m_p_obj).*m_p_memfn)();
	}

private:
	_PtrObj			m_p_obj;
	_MemFn			m_p_memfn;
};




template< class _ITime_Axis, class _Fun >
Fun_Time_Tgr<_ITime_Axis, _Fun>* new_fun_time_tgr_impl( _ITime_Axis* _p_itime_axis, const _Fun& _fun, long _delay_time, long _interval_time)
{
	Fun_Time_Tgr<_ITime_Axis, _Fun>* p_tgr = new Fun_Time_Tgr<_ITime_Axis, _Fun>(_p_itime_axis, _fun, _delay_time, _interval_time);
	if( NULL == p_tgr){ /*Log语句*/ }
	return p_tgr;
}

template< class _ITime_Axis, class _PtrObj, class _MemFn  >
 MemFun_Time_Tgr<_ITime_Axis, _PtrObj, _MemFn>* new_memfn_time_tgr_impl( _ITime_Axis* _p_itime_axis, const _PtrObj& _p_obj, _MemFn _p_menfn, long _delay_time, long _interval_time)
{
	MemFun_Time_Tgr<_ITime_Axis, _PtrObj, _MemFn>* p_tgr = new MemFun_Time_Tgr<_ITime_Axis, _PtrObj, _MemFn>(_p_itime_axis, _p_obj, _p_menfn, _delay_time, _interval_time);
	if( NULL == p_tgr){ /*Log语句*/ }
	return p_tgr;
}


 

#include "stdafx.h"
#include "Time_Axis.h"
#include <map>

using namespace std;

struct Time_Grid
{
	Time_Grid(ITimerSink* _p_time_tgr, long _user_sign, long _delay_time, long _interval_time)
		:p_time_tgr(_p_time_tgr), user_sign(_user_sign), delay_time(_delay_time), interval_time(_interval_time), left_time(_interval_time){}
	ITimerSink* p_time_tgr;
	long		user_sign;
	long		delay_time;
	long		interval_time;
	long		left_time;
};

class Delete_Sign
{
public:
	inline void in_for(){ operator_state = 1; }
	inline void out_for(){ operator_state = 0; }
	inline void delete_in_for(){ operator_state = 2;}
	inline bool is_for(){ return (operator_state >= 1)? true : false; }
	inline bool no_deletion(){ return (2 == operator_state)? false : true; }
private:
	byte operator_state;
};


//时间轴接口
Time_Axis::Time_Axis(){ mp_delete_sign = new Delete_Sign(); mp_delete_sign->out_for(); }
Time_Axis::~Time_Axis(){ delete mp_delete_sign; mp_delete_sign = NULL; }

//增加计时器
void Time_Axis::set_time_tgr( ITimerSink* _p_time_tgr, long _user_sign, long _delay_time, long _interval_time )
{
	kill_time_tgr( _p_time_tgr, _user_sign );
	time_grids[ make_pair(_p_time_tgr, _user_sign) ] = new Time_Grid(_p_time_tgr, _user_sign, _delay_time, _interval_time);
}

//减少计时器
void Time_Axis::kill_time_tgr( ITimerSink* _p_time_tgr, long _user_sign )
{
	map< pair<ITimerSink*,long>, Time_Grid* >::iterator it = time_grids.find( make_pair(_p_time_tgr, _user_sign) );
	if( time_grids.end() != it )
	{
		if( mp_delete_sign->is_for() && it == m_nonius )
		{
			delete it->second;
			m_nonius = time_grids.erase(it);
			mp_delete_sign->delete_in_for();
		}
		else 
		{
			delete it->second;
			time_grids.erase(it);
		}
	}
}

//模糊计时
void Time_Axis::on_vague_time()
{
	const long tm_grid_unit = 20;
	static DWORD tm_start = GetTickCount();
	DWORD tm_cur = GetTickCount();
	long tm_dt = tm_cur - tm_start;
	if( tm_dt >= tm_grid_unit )
	{
		tm_start = tm_cur;
		for( m_nonius = time_grids.begin(); time_grids.end() != m_nonius; )
		{
			mp_delete_sign->in_for();
			MAP_TYPE::iterator it = m_nonius;
			Time_Grid* p_time_grid = it->second;
			if( p_time_grid->delay_time >= 0)
			{
				p_time_grid->delay_time = p_time_grid->delay_time - tm_dt;
				if( p_time_grid->delay_time<= 0)
				{
					p_time_grid->delay_time = p_time_grid->delay_time - tm_dt;
					p_time_grid->p_time_tgr->on_time( p_time_grid->user_sign );
				}
			}
			else
			{
				p_time_grid->left_time = p_time_grid->left_time - tm_dt;
				if( p_time_grid->left_time <= 0 )
				{
					p_time_grid->left_time = p_time_grid->interval_time;
					p_time_grid->p_time_tgr->on_time( p_time_grid->user_sign );
				}
			}
			if( mp_delete_sign->no_deletion() )
				m_nonius++;
			mp_delete_sign->out_for();
		}
	}
}

//精确计时
void Time_Axis::on_exact_time()
{
	const long tm_grid_unit = 20;
	static unsigned long tm_start = GetTickCount();
	unsigned long tm_cur = GetTickCount();
	long tm_dt = tm_cur - tm_start;
	if( tm_dt >= tm_grid_unit )
	{
		tm_start = tm_cur;
		for( m_nonius = time_grids.begin(); time_grids.end() != m_nonius;)
		{
			mp_delete_sign->in_for();
			MAP_TYPE::iterator it = m_nonius;
			Time_Grid* p_time_grid = it->second;

			if( p_time_grid->delay_time >= 0)
			{
				p_time_grid->delay_time = p_time_grid->delay_time - tm_grid_unit;
				if( p_time_grid->delay_time<= 0)
				{
					p_time_grid->delay_time = p_time_grid->delay_time - tm_grid_unit;
					p_time_grid->p_time_tgr->on_time( p_time_grid->user_sign );
				}
			}
			else
			{
				p_time_grid->left_time = p_time_grid->left_time - tm_grid_unit;
				if( p_time_grid->left_time <= 0 )
				{
					p_time_grid->left_time = p_time_grid->interval_time;
					p_time_grid->p_time_tgr->on_time( p_time_grid->user_sign );
				}
			}
			if( mp_delete_sign->no_deletion() )
				m_nonius++;
			mp_delete_sign->out_for();
		}
	}
}


 

#pragma once
#include "..//Game_Module//Time_Axis.h"


class App_SetSize
{
public:
	App_SetSize(long _delay_time, long _interval_time);
	virtual ~App_SetSize();
	// 定时器回调
	virtual void update_setsize();

private:
	ITimerSink* mp_timer;
};


 

#include "stdafx.h"
#include "App_SetSize.h"
#include "..//Game_Module//Map_Par_Binder.h"
#include "..//Game_Module//Time_Axis.h"
#include "..//Server_KillerDlg.h"
#include <psapi.h>   
#pragma comment(lib,"psapi.lib") 
#include "App_Center.h"


App_SetSize::App_SetSize(long _delay_time, long _interval_time)
{ 
	mp_timer = new_memfn_time_tgr_impl(App_Server::get_time_axis(), this, &App_SetSize::update_setsize, _delay_time, _interval_time);
}

App_SetSize::~App_SetSize()
{ 
	RETURN_IF_NULL(mp_timer);
	delete mp_timer;
	mp_timer = NULL;
}

void App_SetSize::update_setsize()
{
	CServer_KillerDlg* p_dlg = App_Server::get_main_dlg();
	RETURN_IF_NULL(p_dlg);

	static long  time_count = 1;
	static DWORD sum_WorkingSetSize = 0;
	static DWORD sum_PeakWorkingSetSize = 0;
	static DWORD old_WorkingSetSize = 0;
	static DWORD old_PeakWorkingSetSize = 0;

	TCHAR num[64];
	CString text = _T("");
	_ltow(time_count, num, 10);
	text += num;
	text += '\n';

	PROCESS_MEMORY_COUNTERS pmc;
	HANDLE handle = GetCurrentProcess();  
	GetProcessMemoryInfo(handle,&pmc,sizeof(pmc));  
	DWORD cur_WorkingSetSize = pmc.WorkingSetSize/1024;
	_ltow(cur_WorkingSetSize, num, 10);
	text += "内存使用: ";
	text += num;
	text += "K\n";

	DWORD cur_PeakWorkingSetSize = pmc.PeakWorkingSetSize/1024;
	_ltow(cur_PeakWorkingSetSize, num, 10);
	text += "峰值内存: ";
	text += num;
	text += "K\n";

	if( old_WorkingSetSize != cur_WorkingSetSize || old_PeakWorkingSetSize != cur_PeakWorkingSetSize) 
	{
		++time_count;
		long dt_WorkingSetSize = cur_WorkingSetSize - old_WorkingSetSize;
		_ltow(dt_WorkingSetSize, num, 10);
		if( dt_WorkingSetSize >= 0)
			text += "+ 内存使用较上一次增长量: ";
		else
			text += "- 内存使用较上一次增长量: ";
		text += num;
		text += "K\n";

		long dt_PeakWorkingSetSize = cur_PeakWorkingSetSize - old_PeakWorkingSetSize;
		_ltow(dt_PeakWorkingSetSize, num, 10);
		if( dt_PeakWorkingSetSize >= 0)
			text += "+ 峰值内存较上一次增长量: ";
		else
			text += "- 峰值内存较上一次增长量: ";
		text += num;
		text += "K\n";

		sum_WorkingSetSize = sum_WorkingSetSize + dt_WorkingSetSize;
		_ltow(sum_WorkingSetSize, num, 10);
		text += "内存使用总增长: ";
		text += num;
		text += "K\n";

		sum_PeakWorkingSetSize = sum_PeakWorkingSetSize + dt_PeakWorkingSetSize;
		_ltow(sum_PeakWorkingSetSize, num, 10);
		text += "峰值内存总增长: ";
		text += num;
		text += "K\n";

		old_WorkingSetSize = cur_WorkingSetSize;
		old_PeakWorkingSetSize = cur_PeakWorkingSetSize;

		CString dlg_text = _T("");
		p_dlg->m_main_dlg_richedit_setsize.GetWindowText(dlg_text); 
		dlg_text += text;
		dlg_text += "\n";
		p_dlg->m_main_dlg_richedit_setsize.SetWindowText(dlg_text);
	}

	//测试直接干掉的情况是否满足
	delete mp_timer;
	mp_timer = NULL;
}


 

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值