1、服务器内容
a、不同机器上的分为
DBProxy //数据库
Guild //公会数据
Master //主服务器 Agent //副本服务器
4种服务器,代码提供了很清晰的每个服务器的HANDLER FUNC TABLE(HASH)。
class PacketHandler : public Singleton<PacketHandler>
{
typedef VOID (*fnHandler)( CScence * pScence, GamePackHeader * pMsg, WORD wSize );
//typedef VOID (*fnHandler_CG)( Player * pPlayer, GamePackHeader * pMsg, WORD wSize );
public:
PacketHandler();
~PacketHandler();
BOOL RegisterHandler_DG();
//BOOL RegisterHandler_CG();
BOOL RegisterHandler_GM();
BOOL RegisterHandler_AG();
BOOL RegisterHandler_Actor();
VOID ParsePacket_DG( CScence * pScence, GamePackHeader * pMsg, WORD wSize );
//VOID ParsePacket_CG( Player * pPlayer, GamePackHeader * pMsg, WORD wSize );
VOID ParsePacket_GM( CScence * pScence, GamePackHeader * pMsg, WORD wSize );
VOID ParsePacket_AG( CScence * pScence, GamePackHeader * pMsg, WORD wSize );
VOID ParsePacket_Actor( CScence * pScence, GamePackHeader * pMsg, WORD wSize );
private:
BOOL AddHandler_DG( BYTE category, BYTE protocol, fnHandler fnHandler );
//BOOL AddHandler_CG( BYTE category, BYTE protocol, fnHandler_CG fnHandler );
BOOL AddHandler_GM( BYTE category, BYTE protocol, fnHandler fnHandler );
BOOL AddHandler_AG( BYTE category, BYTE protocol, fnHandler fnHandler );
BOOL m_FunctionMap_Acotr( BYTE category, BYTE protocol, fnHandler fnHandler );
struct FUNC_DG : public BASE_FUNC
{
fnHandler m_fnHandler;
};
struct FUNC_GM : public BASE_FUNC
{
fnHandler m_fnHandler;
};
struct FUNC_AG : public BASE_FUNC
{
fnHandler m_fnHandler;
};
struct FUNC_ACTOR : public BASE_FUNC
{
fnHandler m_fnHandler;
};
FunctionMap m_FunctionMap_DG;
FunctionMap m_FunctionMap_CG;
FunctionMap m_FunctionMap_GM;
FunctionMap m_FunctionMap_AG;
FunctionMap m_FunctionMap_Actor;
};
CPP。
#include "PacketHandler.h"
PacketHandler::PacketHandler()
{
}
PacketHandler::~PacketHandler()
{
}
BOOL PacketHandler::RegisterHandler_DG()
{
//#define HANDLER_DG( c, p ) if( !AddHandler_DG( c, p, Handler_DG_CHARINFO::On##p ) ) return FALSE
return TRUE;
}
BOOL PacketHandler::RegisterHandler_Actor()
{
#define HANDLER_GZ( c, p ) if( !AddHandler_Actor( c, p, Handler_GZ_GUILD::On##p ) ) return FALSE
return TRUE;
}
BOOL PacketHandler::RegisterHandler_GM()
{
//if( !AddHandler_GM( GM_CONNECTION, GM_CONNECTION_SERVER_INFO_CMD, Handler_GM::OnGM_CONNECTION_SERVER_INFO_CMD ) )
// return FALSE;
//if( !AddHandler_GM( GM_OPERATION, GM_RELOAD_DATA_CMD, Handler_GM::OnGM_RELOAD_DATA_CMD ) )
// return FALSE;
//if( !AddHandler_GM( SERVERCOMMON, SERVERCOMMON_SERVERSHUTDOWN_REQ, Handler_GM::OnSERVERCOMMON_SERVERSHUTDOWN_REQ ) )
// return FALSE;
return TRUE;
}
BOOL PacketHandler::RegisterHandler_AG()
{
// CG_CHARINFO
//if( !AddHandler_AG( CG_CHARINFO, CG_CHARINFO_SELECT_INFO_SYN, Handler_CG_CHARINFO::OnCG_CHARINFO_SELECT_INFO_SYN))
// return FALSE;
return TRUE;
}
VOID PacketHandler::ParsePacket_Actor( CScence * pScence, GamePackHeader * pMsg, WORD wSize )
{
if( 0xff == pMsg->m_byCategory )
{
}
FUNC_GZ * pFuncInfo = (FUNC_GZ *)m_FunctionMap_GZ.Find( MAKEWORD( pMsg->m_byCategory,pMsg->m_byProtocol ) );
if( NULL == pFuncInfo )
{
//SUNLOG( eCRITICAL_LOG, "[PacketHandler::ParsePacket_GZ] PacketType Error GZ!!");
return ;
}
pFuncInfo->m_fnHandler( pScence, pMsg, wSize );
}
VOID PacketHandler::ParsePacket_DG( CScence * pScence, GamePackHeader * pMsg, WORD wSize )
{
if( 0xff == pMsg->m_byCategory )
{
}
FUNC_DG * pFuncInfo = (FUNC_DG *)m_FunctionMap_DG.Find( MAKEWORD( pMsg->wType,pMsg->m_byProtocol ) );
if( NULL == pFuncInfo )
{
//SUNLOG( eCRITICAL_LOG, "[PacketHandler::ParsePacket_DG] PacketType Error DG!!");
return ;
}
pFuncInfo->m_fnHandler( pScence, pMsg, wSize );
}
VOID PacketHandler::ParsePacket_GM( CScence * pScence, GamePackHeader * pMsg, WORD wSize )
{
if( 0xff == pMsg->m_byCategory )
{
}
FUNC_GM * pFuncInfo = (FUNC_GM *)m_FunctionMap_GM.Find( MAKEWORD( pMsg->m_byCategory,pMsg->m_byProtocol ) );
if( NULL == pFuncInfo )
{
//SUNLOG( eCRITICAL_LOG, "[PacketHandler::ParsePacket_GM] PacketType Error!! GM");
return ;
}
pFuncInfo->m_fnHandler( pScence, pMsg, wSize );
}
VOID PacketHandler::ParsePacket_AG( CScence * pScence, GamePackHeader * pMsg, WORD wSize )
{
if( 0xff == pMsg->m_byCategory )
{
}
FUNC_AG * pFuncInfo = (FUNC_AG *)m_FunctionMap_AG.Find( MAKEWORD( pMsg->m_byCategory,pMsg->m_byProtocol ) );
if( NULL == pFuncInfo )
{
//SUNLOG( eCRITICAL_LOG, "[PacketHandler::ParsePacket_AG] PacketType Error!! AG Category[%d] Protocol[%d] ", pMsg->m_byCategory,pMsg->m_byProtocol);
return ;
}
pFuncInfo->m_fnHandler( pScence, pMsg, wSize );
}
BOOL PacketHandler::AddHandler_Acotr( BYTE category, BYTE protocol, fnHandler fnHandler)
{
FUNC_ACTOR * pFuncInfo = new FUNC_ACTOR;
pFuncInfo->m_dwFunctionKey = MAKEWORD( category, protocol );
pFuncInfo->m_fnHandler = fnHandler;
return m_FunctionMap_Actor.Add( pFuncInfo );
}
BOOL PacketHandler::AddHandler_DG( BYTE category, BYTE protocol, fnHandler fnHandler)
{
FUNC_DG * pFuncInfo = new FUNC_DG;
pFuncInfo->m_dwFunctionKey = MAKEWORD( category, protocol );
pFuncInfo->m_fnHandler = fnHandler;
return m_FunctionMap_DG.Add( pFuncInfo );
}
BOOL PacketHandler::AddHandler_GM( BYTE category, BYTE protocol, fnHandler fnHandler)
{
FUNC_GM * pFuncInfo = new FUNC_GM;
pFuncInfo->m_dwFunctionKey = MAKEWORD( category, protocol );
pFuncInfo->m_fnHandler = fnHandler;
return m_FunctionMap_GM.Add( pFuncInfo );
}
BOOL PacketHandler::AddHandler_AG( BYTE category, BYTE protocol, fnHandler fnHandler)
{
FUNC_AG * pFuncInfo = new FUNC_AG;
pFuncInfo->m_dwFunctionKey = MAKEWORD( category, protocol );
pFuncInfo->m_fnHandler = fnHandler;
return m_FunctionMap_AG.Add( pFuncInfo );
}
值得注意的是此类是singleton,这样只能实例化一次,带来的好处就是没有多个实例造成的代码泛滥
b、代码实现