再写服务器_打死不修改_第二版
/********************************************************************* 文件名: CLK_LIB.h* 文件描述: Switch_Case对象* 创建人: 陈泽丹 ,2012年8月27日 * 版本号: 1.0* 修改记录:********************************************************************/#pragma once#include <iostream>#include <tchar.h>#include <windows.h>#include <map>#include <algorithm>using namespace std;#ifdef _DEBUG#define TIME_GUARD( _x ) Time_Guard( _x )#else#define TIME_GUARD( _x ) NULL#endif // _DEBUG//时间守卫 - 用于测试经历时间class Time_Guard{public:Time_Guard(const char* _p_caption){size_t len = strlen(_p_caption);if( len < MAX_PATH ) {sprintf(m_caption,"%s", _p_caption);}else{sprintf(m_caption, "%s", "Error: caption is over stack! ");cout<<"Error: "<<_p_caption<<"is over stack!"<<endl;}m_time = GetTickCount();}~Time_Guard(){ULONG tm = GetTickCount() - m_time;cout<<m_caption<<": "<<tm<<" (ms)"<<endl;}private:char m_caption[256];ULONG m_time;};//Switch_Case对象template< template< int v > class _PAR_TYPE, int _start, int _end> class CLK_Switch{public:template <class _PtrObj>static bool switch_case(const _PtrObj& _p, const int _msg_type_ID, void* buf){if( _msg_type_ID < _start || _msg_type_ID > _end || _start > _end) return false; const int POS = (_start + _end)/2; if( POS == _msg_type_ID){_PAR_TYPE< POS >* p_msg = ( _PAR_TYPE< POS >* ) buf;_p->dispatch( p_msg );return true;}else if( _msg_type_ID < POS ) {return CLK_Switch<_PAR_TYPE, _start, POS>::switch_case(_p, _msg_type_ID, buf);}else{return CLK_Switch<_PAR_TYPE, POS+1, _end>::switch_case(_p, _msg_type_ID, buf);}}};template< class _Local_Type> class CLK_Base_Case{public:CLK_Base_Case(_Local_Type* _p_local):m_p_local(_p_local){}virtual ~CLK_Base_Case(){ m_p_local = NULL; }virtual void operator()(const int _msg_type_ID, void* buf) = 0;protected:_Local_Type* m_p_local;};
/********************************************************************* 文件名: Common.h* 文件描述: 网络游戏模拟* 创建人: 陈泽丹 ,2012年9月6日 * 版本号: 1.0* 修改记录: 风格:编译期类型有大写, 执行期类型用小写********************************************************************/#pragma oncenamespace Common_Lib{//事件struct EVENT_ID{enum{//心跳事件HEARTBEAT_EVENT = 0,//移动事件MOVE_EVENT,};};//消息struct Game_Msg{ virtual int get_event_ID() = 0; };template<int _VAL> struct IEvt_To_Msg: public Game_Msg{ enum {EVT_ID = _VAL}; virtual int get_event_ID(){ return EVT_ID; }; };template<int _VAL> struct Evt_To_Msg{};//心跳消息template<> struct Evt_To_Msg<EVENT_ID::HEARTBEAT_EVENT>: public IEvt_To_Msg<EVENT_ID::HEARTBEAT_EVENT>{};//移动消息template<> struct Evt_To_Msg<EVENT_ID::MOVE_EVENT>: public IEvt_To_Msg<EVENT_ID::MOVE_EVENT>{Evt_To_Msg<EVENT_ID::MOVE_EVENT>(long _user_id, int _to_x, int _to_y):m_user_id(_user_id), m_to_x(_to_x),m_to_y(_to_y){}long m_user_id;int m_to_x;int m_to_y;};}
/********************************************************************* 文件名: Server.h* 文件描述: 网络游戏模拟* 创建人: 陈泽丹 ,2012年9月6日 * 版本号: 1.0* 修改记录:********************************************************************/#pragma once#include <iostream>#include <vector>#include "CLK_LIB.h"//--风格:编译期类型有大写, 执行期类型用小写using namespace std;//-------------------------------------------------------------namespace Server_Lib{// 事件否决者订阅者接口template< class _TMsg >struct IMsg_Vote_Listener{ virtual bool on_vote(_TMsg *_p_msg) = 0; };// 行为执行者订阅者接口template< class _TMsg >struct IMsg_Action_Listener{ virtual void on_action(_TMsg *_p_msg) = 0; };// 事件响应者订阅者接口template< class _TMsg >struct IMsg_Response_Listener{ virtual void on_response(_TMsg *_p_msg) = 0; };// 事件服务器 -- 事件驱动,消息处理template< class _TMsg >class IMsg_Server{public:typedef IMsg_Vote_Listener< _TMsg >MSG_VOTE_LISTENER;typedef IMsg_Action_Listener< _TMsg >MSG_ACTION_LISTENER;typedef IMsg_Response_Listener< _TMsg >MSG_RESPONSE_LISTENER;virtual bool add_listener(MSG_VOTE_LISTENER* _p_listener)= 0;virtual void remove_listener(MSG_VOTE_LISTENER* _p_listener)= 0;virtual bool add_listener(MSG_ACTION_LISTENER* _p_listener)= 0;virtual void remove_listener(MSG_ACTION_LISTENER* _p_listener)= 0;virtual bool add_listener(MSG_RESPONSE_LISTENER* _p_listener)= 0;virtual void remove_listener(MSG_RESPONSE_LISTENER* _p_listener)= 0;virtual void dispatch_msg(_TMsg *_p_msg) const= 0;};template< class _TMsg >class Msg_Server: public IMsg_Server<_TMsg>{public:typedef vector< MSG_VOTE_LISTENER* >V_MSG_VOTE_LISTENE;typedef vector< MSG_ACTION_LISTENER* >V_MSG_ACTION_LISTENER;typedef vector< MSG_RESPONSE_LISTENER* >V_MSG_RESPONSE_LISTENER;//---------------------------------------------------------------------virtual bool add_listener(MSG_VOTE_LISTENER* _p_listener){ return Msg_Manager<MSG_VOTE_LISTENER>(this).add_listener(_p_listener); }virtual void remove_listener(MSG_VOTE_LISTENER* _p_listener){ return Msg_Manager<MSG_VOTE_LISTENER>(this).remove_listener(_p_listener); }virtual bool add_listener(MSG_ACTION_LISTENER* _p_listener){ return Msg_Manager<MSG_ACTION_LISTENER>(this).add_listener(_p_listener); }virtual void remove_listener(MSG_ACTION_LISTENER* _p_listener){ return Msg_Manager<MSG_ACTION_LISTENER>(this).remove_listener(_p_listener); }virtual bool add_listener(MSG_RESPONSE_LISTENER* _p_listener){ return Msg_Manager<MSG_RESPONSE_LISTENER>(this).add_listener(_p_listener); }virtual void remove_listener(MSG_RESPONSE_LISTENER* _p_listener){ return Msg_Manager<MSG_RESPONSE_LISTENER>(this).remove_listener(_p_listener); }void dispatch_msg(_TMsg *_p_msg)const{//否决处理for( V_MSG_VOTE_LISTENE::const_iterator it = m_vote_msg.begin(); m_vote_msg.end() != it; ++it){if( (*it)->on_vote(_p_msg) )return;}//执行处理for( V_MSG_ACTION_LISTENER::const_iterator it = m_action_msg.begin(); m_action_msg.end() != it; ++it){(*it)->on_action(_p_msg);}//响应处理for( V_MSG_RESPONSE_LISTENER::const_iterator it = m_response_msg.begin(); m_response_msg.end() != it; ++it){(*it)->on_response(_p_msg);}}private://----------------------- 管理器 ----------------------template< class _TMSG_LISTENER> class Msg_Manager{public:typedef vector< _TMSG_LISTENER* >V_TMSG_LISTENER;typedef Msg_Server< _TMsg >LOCAL_TYPE;Msg_Manager( LOCAL_TYPE *_p_local):mp_local(_p_local){}bool add_listener(_TMSG_LISTENER* _p_listener) const{V_TMSG_LISTENER* p_msgs = Get_Listeners<_TMSG_LISTENER>()(mp_local);V_TMSG_LISTENER::const_iterator it = find(p_msgs->begin(), p_msgs->end(), _p_listener);if( p_msgs->end() == it){p_msgs->push_back(_p_listener);return true;}return false;}void remove_listener(_TMSG_LISTENER* _p_listener) const {V_TMSG_LISTENER* p_msgs = Get_Listeners<_TMSG_LISTENER>()(mp_local);V_TMSG_LISTENER::const_iterator it = find(p_msgs->begin(), p_msgs->end(), _p_listener);if( p_msgs->end() != it){p_msgs->erase(it);}}private:template< class _T> struct Get_Listeners;template<> struct Get_Listeners<MSG_VOTE_LISTENER>{ vector< MSG_VOTE_LISTENER* >* operator()(LOCAL_TYPE *_p_local){ return &(_p_local->m_vote_msg); }};template<> struct Get_Listeners<MSG_ACTION_LISTENER>{ vector< MSG_ACTION_LISTENER* >* operator()(LOCAL_TYPE *_p_local){ return &(_p_local->m_action_msg); }};template<> struct Get_Listeners<MSG_RESPONSE_LISTENER>{ vector< MSG_RESPONSE_LISTENER* >* operator()(LOCAL_TYPE *_p_local){ return &(_p_local->m_response_msg); }};LOCAL_TYPE *mp_local;};V_MSG_VOTE_LISTENEm_vote_msg;V_MSG_ACTION_LISTENERm_action_msg;V_MSG_RESPONSE_LISTENERm_response_msg;template<class _TMSG_LISTENER>friend class Get_Listeners;};}
/********************************************************************* 文件名: Server_Module.h* 文件描述: 网络游戏模拟* 创建人: 陈泽丹 ,2012年9月6日 * 版本号: 1.0* 修改记录:********************************************************************/#pragma once#include <iostream>#include "Common_Lib.h"#include "Server_Lib.h"//--风格:编译期类型有大写, 执行期类型用小写using namespace std;using namespace Common_Lib;namespace Server_Module{using namespace Server_Lib;typedef Game_Msg Server_Msg;//服务器class Global_Module{public:typedef IMsg_Server< Server_Msg >G_Msg_Server;static G_Msg_Server* get_msg_server(){ return mp_event_server; }private:static G_Msg_Server* mp_event_server;};//否决服务厂商class Some_Manufacturer_Vote: public IMsg_Vote_Listener< Server_Msg > {public:Some_Manufacturer_Vote();virtual ~Some_Manufacturer_Vote();bool on_vote(Server_Msg *_p_msg);};//执行服务厂商class Some_Manufacturer_Action: public IMsg_Action_Listener< Server_Msg > {public:Some_Manufacturer_Action();virtual ~Some_Manufacturer_Action();void on_action(Server_Msg *_p_msg);};//心跳服务厂商class Heartbeat_Manufacturer_Action: public IMsg_Action_Listener< Server_Msg > {public:Heartbeat_Manufacturer_Action();virtual ~Heartbeat_Manufacturer_Action();void on_action(Server_Msg *_p_msg);};}extern Server_Module::Some_Manufacturer_Vote some_manufacturer_vote;extern Server_Module::Some_Manufacturer_Action some_manufacturer_action;extern Server_Module::Heartbeat_Manufacturer_Action heartbeat_manufacturer_action;
/********************************************************************* 文件名: Server_Module.cpp* 文件描述: 网络游戏模拟* 创建人: 陈泽丹 ,2012年9月6日 * 版本号: 1.0* 修改记录:********************************************************************/#include "Server_Module.h"using namespace Server_Module;//否决服务厂商Some_Manufacturer_Vote::Some_Manufacturer_Vote(){ Global_Module::get_msg_server()->add_listener( (Global_Module::G_Msg_Server::MSG_VOTE_LISTENER*)this ); }Some_Manufacturer_Vote::~Some_Manufacturer_Vote(){ Global_Module::get_msg_server()->remove_listener((Global_Module::G_Msg_Server::MSG_VOTE_LISTENER*)this ); }bool Some_Manufacturer_Vote::on_vote(Server_Msg *_p_msg) { if( EVENT_ID::MOVE_EVENT == _p_msg->get_event_ID() ){Evt_To_Msg<EVENT_ID::MOVE_EVENT>* p_msg = (Evt_To_Msg<EVENT_ID::MOVE_EVENT>*) _p_msg;int r = rand()%10;if( r <= 4 ){cout<<p_msg->m_user_id<<" 号玩家运气相当不好, 被冰雹打中, 移动失败!"<<endl;return true;}else{cout<<p_msg->m_user_id<<" 号玩家成功躲过冰雹, 移动成功!"<<endl;return false;}}return false; }//执行服务厂商Some_Manufacturer_Action::Some_Manufacturer_Action(){ Global_Module::get_msg_server()->add_listener( (Global_Module::G_Msg_Server::MSG_ACTION_LISTENER*)this ); }Some_Manufacturer_Action::~Some_Manufacturer_Action(){ Global_Module::get_msg_server()->remove_listener((Global_Module::G_Msg_Server::MSG_ACTION_LISTENER*)this ); }void Some_Manufacturer_Action::on_action(Server_Msg *_p_msg) { if( EVENT_ID::MOVE_EVENT == _p_msg->get_event_ID() ){Evt_To_Msg<EVENT_ID::MOVE_EVENT>* p_msg = (Evt_To_Msg<EVENT_ID::MOVE_EVENT>*) _p_msg;cout<<p_msg->m_user_id<<" 号玩家移动到 ("<<p_msg->m_to_x<<", "<<p_msg->m_to_y<<")"<<endl;}}//执行服务厂商Heartbeat_Manufacturer_Action::Heartbeat_Manufacturer_Action(){ Global_Module::get_msg_server()->add_listener( (Global_Module::G_Msg_Server::MSG_ACTION_LISTENER*)this ); }Heartbeat_Manufacturer_Action::~Heartbeat_Manufacturer_Action(){ Global_Module::get_msg_server()->remove_listener((Global_Module::G_Msg_Server::MSG_ACTION_LISTENER*)this ); }void Heartbeat_Manufacturer_Action::on_action(Server_Msg *_p_msg) { if( EVENT_ID::HEARTBEAT_EVENT == _p_msg->get_event_ID() ){cout<<"刷新所有玩家和NPC"<<endl;}}Global_Module::G_Msg_Server* Global_Module::mp_event_server = new Msg_Server<Server_Msg>();Some_Manufacturer_Vote some_manufacturer_vote;Some_Manufacturer_Action some_manufacturer_action;Heartbeat_Manufacturer_Action heartbeat_manufacturer_action;
/********************************************************************* 文件名: Client_Module.h* 文件描述: 网络游戏模拟* 创建人: 陈泽丹 ,2012年9月6日 * 版本号: 1.0* 修改记录: 风格:编译期类型有大写, 执行期类型用小写********************************************************************/#pragma once#include <iostream>#include "Common_Lib.h"#include "Internet_Environment.h"using namespace Common_Lib;namespace Client_Module{class User{public:void create_move_event();};class System_User{public:void create_heartbeat_event();};}extern Client_Module::User user;extern Client_Module::System_User system_user;
/********************************************************************* 文件名: Client_Module.cpp* 文件描述: 网络游戏模拟* 创建人: 陈泽丹 ,2012年9月6日 * 版本号: 1.0* 修改记录:********************************************************************/#include "Client_Module.h"#include "Internet_Environment.h"using namespace Client_Module;void User::create_move_event(){Evt_To_Msg<EVENT_ID::MOVE_EVENT> move_event(7, 200, 300);NET->sent_to_server(&move_event);}void System_User::create_heartbeat_event(){Evt_To_Msg<EVENT_ID::HEARTBEAT_EVENT> heartbeat_event;NET->sent_to_server(&heartbeat_event);}User user;System_User system_user;
/********************************************************************* 文件名: Internet_Environment.h* 文件描述: 网络游戏模拟* 创建人: 陈泽丹 ,2012年9月6日 * 版本号: 1.0* 修改记录: 风格:编译期类型有大写, 执行期类型用小写********************************************************************/#pragma once#include "Common_Lib.h"using namespace Common_Lib;#define NET gp_net_environmentclass Internet_Environment {public:virtual void sent_to_server(Game_Msg *_p_msg) = 0;virtual void sent_to_client(Game_Msg *_p_msg) = 0;};extern Internet_Environment *gp_net_environment;
#include "Internet_Environment.h"#include "Common_Lib.h"#include "Server_Module.h"#include "Client_Module.h"using namespace Server_Module;using namespace Client_Module;class Realy_Net_Environment: public Internet_Environment{public:virtual void sent_to_server(Game_Msg *_p_msg){Server_Module::Global_Module::get_msg_server()->dispatch_msg(_p_msg);}virtual void sent_to_client(Game_Msg *_p_msg){NULL;}};Internet_Environment *gp_net_environment = new Realy_Net_Environment();
#include <iostream>#include <time.h>#include <vector>#include "CLK_Lib.h"#include "Client_Module.h"#include "Server_Lib.h"//using namespace Client_Module;void Environment(){TIME_GUARD("Server");for( int i=0; i<20; i++){cout<<i+1<<": ";int r = rand()%2;if( 0 == r )user.create_move_event();elsesystem_user.create_heartbeat_event();}}using namespace Server_Lib;void main(){srand(time(0));Environment();Msg_Server< int > msg_server;system("pause");}