把"没有同步机制的类型"转成"中心服和代理服有同步机制的类型"

/******************************************************************** 
 file name : ServerType.h 
 author  :   Clark/陈泽丹 
 created :   2011-12-20
 网游网络通讯经常采用中心服模型:
	读: 用户消息->代理服数据->用户端
	写: 用户消息->中心服数据->更新代理服数据->用户端
 以下类用于把"没有同步机制的类型"转成"中心服和代理服有同步机制的类型"
 1, 采用模板继承是为了能使用源类型的内部功能(没有用组合的原因)
 2, 留三个虚函数是为了客户端可以添加相应的指令和其解析功能。
 使用时,只对外留下Send接口,用户不需解决Send到那和那里接收的问题
 对源类型要求有拷贝构造函数。
*********************************************************************/ 
#pragma once

int GetLogicServerID()
{
	return 105;
}


template<class T>
class ServerType: protected T
{
public:
	ServerType(const int _CUR_SERVER_ID, const T& _other): CUR_SERVER_ID(_CUR_SERVER_ID), T(_other){}
	void Send(const int _CmdID, char* _buf)
	{
		if( CUR_SERVER_ID == GetLogicServerID()){ OnLogicRecv(_CmdID, _buf); }
		else{ SendToLogic(_CmdID, _buf); }
	}
	const int CUR_SERVER_ID;


protected:
	void SendToProxy(const int _CmdID, char* _buf){ API_SendTo_ProxyServer(_CmdID, _buf); }
	virtual void SendToLogic(const int _CmdID, char* _buf){ API_SendTo_LogicServer(_CmdID, _buf); }
private:
	void Recv(const int _CmdID, char* _buf)
	{
		if( CUR_SERVER_ID == GetLogicServerID()){ OnLogicRecv(_CmdID, _buf); }
		else{ OnProxyRecv(_CmdID, _buf); }
	}
	friend void API_SendTo_LogicServer(const int _CmdID, char* _buf);
	friend void API_SendTo_ProxyServer(const int _CmdID, char* _buf);
	virtual void OnLogicRecv(const int _CmdID, char* _buf) = 0;
	virtual void OnProxyRecv(const int _CmdID, char* _buf) = 0;
};


//test

 

 

#include <iostream>
#include "ServerType.h"

using namespace std;



class SourceType
{
public:
	SourceType(int _i1, int _i2):m_val1(_i1),m_val2(_i2){}
	SourceType(const SourceType& _other){ m_val1 = _other.m_val1; m_val2 = _other.m_val2; }
	void Show(){ cout<<this->m_val1<<" "<<this->m_val2<<endl; }
	int m_val1;
	int m_val2;
};

class ZcFb:public ServerType<SourceType>
{
public:
	ZcFb(const int _CUR_SERVER_ID, const SourceType& _data): ServerType<SourceType>(_CUR_SERVER_ID, _data){}
	void OnLogicRecv(const int _CmdID, char* _buf)
	{
		this->m_val1 = _CmdID;
		this->m_val2 = *((int*)_buf);
		cout<<"逻辑服内容"<<endl;
		this->Show();
		SendToProxy(_CmdID, (char*)this);
	}
	void OnProxyRecv(const int _CmdID, char* _buf)
	{
		ZcFb* pZcFb = (ZcFb*)_buf;
		this->m_val1 = pZcFb->m_val1;
		this->m_val2 = pZcFb->m_val2;
		cout<<"代理服内容"<<endl;
		this->Show();
	}
};



ServerType<SourceType>* pServerLogic = NULL;
ServerType<SourceType>* pServerProxy1 = NULL;
ServerType<SourceType>* pServerProxy2 = NULL;

void API_SendTo_LogicServer(const int _CmdID, char* _buf)
{
	pServerLogic->Recv(_CmdID, _buf);
}

void API_SendTo_ProxyServer(const int _CmdID, char* _buf)
{
	pServerProxy1->Recv(_CmdID, _buf);
	pServerProxy2->Recv(_CmdID, _buf);
}

void InitLogic()
{
	SourceType objLogic(1,1);
	pServerLogic = new ZcFb(105, objLogic);
}

void InitProxy()
{
	SourceType objProxy(2,2);
	pServerProxy1 = new ZcFb(99, objProxy);
	pServerProxy2 = new ZcFb(100, objProxy);
}

void Over()
{
	if( NULL != pServerLogic) delete pServerLogic;
	if( NULL != pServerProxy1) delete pServerProxy1;
	if( NULL != pServerProxy2) delete pServerProxy2;
}

void main()
{
	
	InitLogic();
	InitProxy();

	int it = 1;
	pServerLogic->Send(1,(char*)&it);
	it = 2;
	pServerProxy1->Send(2,(char*)&it);
	it = 3;
	pServerProxy2->Send(3,(char*)&it);


	system("pause");


}


你可能感兴趣的:(把"没有同步机制的类型"转成"中心服和代理服有同步机制的类型")