GamePlayer.h
上传用户:dzyhzl
上传日期:2019-04-29
资源大小:56270k
文件大小:6k
源码类别:

模拟服务器

开发平台:

C/C++

  1. /********************************************************************
  2. created: 2003/04/08
  3. file base: GamePlayer
  4. file ext: h
  5. author: liupeng
  6. purpose:
  7. *********************************************************************/
  8. #ifndef __INCLUDE_GAMEPLAYER_H__
  9. #define __INCLUDE_GAMEPLAYER_H__
  10. #include "IPlayer.h"
  11. #include "ICommand.h"
  12. #include "CriticalSection.h"
  13. #include "tstring.h"
  14. #include "Buffer.h"
  15. #include "IClient.h"
  16. #include "RainbowInterface.h"
  17. #include "IServer.h"
  18. #include "HeavenInterface.h"
  19. #include "IGServer.h"
  20. #include "GameServer.h"
  21. #include "PlayerCreator.h"
  22. #include <vector>
  23. #include <map>
  24. #include <list>
  25. using namespace std;
  26. /*
  27.  * class CGamePlayer
  28.  */
  29. class CGamePlayer : public IPlayer
  30. {
  31. public:
  32. CGamePlayer( UINT nIdentityID = ( UINT )( -1 ) );
  33. virtual ~CGamePlayer();
  34. /*
  35.  * Task option
  36.  */
  37. enum
  38. {
  39. /*
  40.  * Task command
  41.  */
  42. enumLogin = 0,
  43. enumSelAddDelRole,
  44. enumLoginCreateRole,
  45. enumLoginSelectRole,
  46. enumLoginDeleteRole,
  47. enumSafeClose,
  48. /*
  49.  * Task status
  50.  */
  51. enumToGotoTask,
  52. enumToNextTask,
  53. enumCompleted,
  54. enumRepeat,
  55. enumError,
  56. enumNone
  57. };
  58. enum enumCDataQueueOwner
  59. {
  60. enumOwnerAccSvr = 0,
  61. enumOwnerRoleSvr,
  62. enumOwnerPlayer,
  63. enumOwnerTotal
  64. };
  65. virtual bool DispatchTask( UINT nTaskID );
  66. virtual bool IsWorking();
  67. virtual bool Run();
  68. virtual bool AppendData( UINT nOwner, const void *pData, size_t dataLength );
  69. static bool SetupGlobalAllocator( size_t bufferSize, size_t maxFreeBuffers );
  70. virtual bool Attach( const char *pRoleName );
  71. virtual bool IsActive() { return m_bActiveStatus; };
  72. virtual bool Active();
  73. virtual bool Inactive();
  74. static bool Add( const char *pRoleName, IPlayer *pPlayer );
  75. static bool Del( const char *pRoleName );
  76. static IPlayer *Get( const char *pRoleName );
  77. static void ATTACH_NETWORK( IClient *pAccSvrClient, 
  78. IServer *pPlayerServer, 
  79. IClient *pDBRoleClient );
  80. static void DETACH_NETWORK();
  81. static LONG GetCapability() { return ::InterlockedExchange( &m_slnIdentityCounts, m_slnIdentityCounts ); }
  82. static LONG GetWorking() { return ::InterlockedExchange( &m_lnWorkingCounts, m_lnWorkingCounts ); }
  83. private:
  84. static OnlineGameLib::Win32::CBuffer::Allocator m_theGlobalAllocator;
  85. static LONG m_slnIdentityCounts;
  86. LONG m_lnIdentityID;
  87. static LONG m_lnWorkingCounts;
  88. int m_nExtPoint;
  89. /*
  90.  * Task system
  91.  */
  92. class CTask
  93. {
  94. public:
  95. explicit CTask( CGamePlayer *pReceiver, UINT nTaskID );
  96. ~CTask();
  97. typedef UINT ( CGamePlayer::* Action )();
  98. size_t AddCmd( Action pFun );
  99. UINT Execute();
  100. void Reset() { m_indexCmd = 0; }
  101. protected:
  102. CTask();
  103. //OnlineGameLib::Win32::CCriticalSection m_csTask;
  104. private:
  105. CGamePlayer *m_pReceiver;
  106. typedef vector< ICommand * > stdVector;
  107. stdVector m_stdCommand;
  108. size_t m_indexCmd;
  109. UINT m_nTaskProgID;
  110. };
  111. CTask m_theLoginTask;
  112. CTask m_theSelAddDelTask;
  113. CTask m_theLoginCreateRoleTask;
  114. CTask m_theLoginSelectRoleTask;
  115. CTask m_theLoginDeleteRoleTask;
  116. CTask m_theSafeCloseTask;
  117. void SetCurrentTask( LONG nID ) { /* m_nCurrentTaskID = nID; */ ::InterlockedExchange( &m_nCurrentTaskID, nID ); }
  118. UINT GetCurrentTask() { return /* m_nCurrentTaskID; */ ::InterlockedExchange( &m_nCurrentTaskID, m_nCurrentTaskID ); }
  119. LONG m_nCurrentTaskID;
  120. void InitTaskProcessor();
  121. /*
  122.  * Information
  123.  */
  124. OnlineGameLib::Win32::_tstring m_sAccountName;
  125. OnlineGameLib::Win32::_tstring m_sPassword;
  126. OnlineGameLib::Win32::_tstring m_sRoleName;
  127.     bool    m_bUseSuperPassword;
  128.     OnlineGameLib::Win32::_tstring m_sSuperPassword;
  129.     OnlineGameLib::Win32::_tstring m_sDelRoleName;
  130. size_t m_nAttachServerID;
  131. bool m_bActiveStatus;
  132. DWORD m_dwTaskBeginTimer;
  133. DWORD m_dwTaskTotalTimer;
  134. bool m_bAutoUnlockAccount;
  135. static OnlineGameLib::Win32::CCriticalSection CGamePlayer::m_csMapSP;
  136. typedef map< OnlineGameLib::Win32::_tstring, IPlayer * > stdMapSP;
  137. static stdMapSP m_sthePlayerTable;
  138. static const int s_nRoleListCount;
  139. static const int s_nLoginTimeoutTimer;
  140. static const int s_nProcessTimeoutTimer;
  141. // static IClient *m_pAccSvrClient;
  142. static IServer *m_pPlayerServer;
  143. static IClient *m_pDBRoleClient;
  144. /*
  145.  * class CDataQueue
  146.  */
  147. class CDataQueue
  148. {
  149. public:
  150. explicit CDataQueue( size_t bufferSize = 1024 * 64, size_t maxFreeBuffers = 1 );
  151. ~CDataQueue();
  152. /*
  153.  * Function
  154.  */
  155. bool AddData( LONG lnID, const BYTE *pData, size_t datalength );
  156. OnlineGameLib::Win32::CBuffer *Attach( LONG lnID );
  157. void Detach( LONG lnID );
  158. void Clear( LONG lnID );
  159. void Empty();
  160. private:
  161. /*
  162.  * Data
  163.  */
  164. typedef map< LONG, OnlineGameLib::Win32::CBuffer * > stdDataMap;
  165. stdDataMap m_theData;
  166. OnlineGameLib::Win32::CBuffer::Allocator m_theDQAllocator;
  167. OnlineGameLib::Win32::CCriticalSection m_csQueue;
  168. };
  169. CDataQueue m_theDataQueue[enumOwnerTotal];
  170. OnlineGameLib::Win32::CPackager m_thePackager;
  171. static CPlayerCreator m_thePlayerCreator;
  172. protected:
  173. /*
  174.  * Helper function
  175.  */
  176. bool TaskCompleted();
  177. bool SmallPackProcess( UINT nOwner, const void *pData, size_t dataLength );
  178. bool LargePackProcess( UINT nOwner, const void *pData, size_t dataLength );
  179. bool DispatchTaskForAccount( const void *pData, size_t dataLength );
  180. bool DispatchTaskForDBRole( const void *pData, size_t dataLength );
  181. bool DispatchTaskForPlayer( const void *pData, size_t dataLength );
  182. /*
  183.  * Task function
  184.  */
  185. // {{
  186. /*
  187.  * Login
  188.  */
  189. UINT WaitForAccPwd();
  190. UINT QueryAccPwd();
  191. UINT VerifyAccount();
  192. UINT QueryRoleList();
  193. UINT ProcessRoleList();
  194. UINT SelAddDelRole();
  195. UINT DelRole_WaitForVerify();
  196. UINT WaitForCreateResult();
  197. UINT WaitForDeleteResult();
  198. UINT ProcessRoleInfo();
  199. UINT WaitForGameSvrPermit();
  200. /*
  201.  * Logout
  202.  */
  203. // }}
  204. UINT SafeClose();
  205. /*
  206.  * Help function for task
  207.  */
  208. void _ClearTaskQueue();
  209. bool _VerifyAccount_ToPlayer( UINT nQueryResult , unsigned long nLeftTime);
  210. bool _QueryRoleInfo_ToDBRole( const char *pRoleName );
  211. bool _SyncRoleInfo_ToGameServer( const void *pData, size_t dataLength );
  212. bool _CreateNewPlayer_ToDBRole( const char *pRoleName,
  213. int nRoleSex /* male or female */, 
  214. int nRoleClass, 
  215. unsigned short nMapID );
  216. UINT _DeleteRole_ToDBRole( const void *pData, size_t dataLength );
  217. bool _UnlockAccount();
  218. };
  219. #endif // __INCLUDE_GAMEPLAYER_H__