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

模拟服务器

开发平台:

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. #pragma warning(disable : 4786)  // identifier was truncated to '255' characters 
  13.                                  // in the debug information
  14. #include "CriticalSection.h"
  15. #include "tstring.h"
  16. #include "Buffer.h"
  17. #include "IClient.h"
  18. #include "RainbowInterface.h"
  19. #include "IServer.h"
  20. #include "HeavenInterface.h"
  21. #include <vector>
  22. #include <map>
  23. using namespace std;
  24. /*
  25.  * class CGamePlayer
  26.  */
  27. class CGamePlayer : public IPlayer
  28. {
  29. public:
  30. CGamePlayer( IClient *pAccSvrClient, 
  31. IServer *pPlayerServer, 
  32. IClient *pDBRoleClient, 
  33. UINT nIdentityID = ( UINT )( -1 ) );
  34. virtual ~CGamePlayer();
  35. /*
  36.  * Task option
  37.  */
  38. enum
  39. {
  40. /*
  41.  * Task command
  42.  */
  43. enumLogin = 0,
  44. enumLogOut,
  45. enumExchangeRegin,
  46. /*
  47.  * Task status
  48.  */
  49. enumToNextTask,
  50. enumCompleted,
  51. enumRepeat,
  52. enumError,
  53. enumNone
  54. };
  55. enum enumCDataQueueOwner
  56. {
  57. enumOwnerAccSvr = 0,
  58. enumOwnerRoleSvr,
  59. enumOwnerPlayer,
  60. enumOwnerTotal
  61. };
  62. virtual bool DispatchTask( UINT nTaskID );
  63. virtual bool IsWorking();
  64. virtual int  Run();
  65. virtual bool AppendData( UINT nOwner, const void *pData, size_t dataLength );
  66. virtual bool Create( const char * const pName, const char * const pPassword );
  67. virtual bool Destroy( const char * const /*pPassword*/ );
  68. static bool SetupGlobalAllocator( size_t bufferSize, size_t maxFreeBuffers );
  69. private:
  70. static OnlineGameLib::Win32::CBuffer::Allocator m_theGlobalAllocator;
  71. static LONG m_slnIdentityCounts;
  72. LONG m_lnIdentityID;
  73. /*
  74.  * Task system
  75.  */
  76. class CTask
  77. {
  78. public:
  79. explicit CTask( CGamePlayer *pReceiver, UINT nTaskID );
  80. ~CTask();
  81. typedef UINT ( CGamePlayer::* Action )();
  82. size_t AddCmd( Action pFun );
  83. UINT Execute();
  84. void Reset() { m_indexCmd = 0; }
  85. protected:
  86. CTask();
  87. OnlineGameLib::Win32::CCriticalSection m_csTask;
  88. private:
  89. CGamePlayer *m_pReceiver;
  90. typedef vector< ICommand * > stdVector;
  91. stdVector m_stdCommand;
  92. size_t m_indexCmd;
  93. UINT m_nTaskProgID;
  94. };
  95. CTask m_theLoginTask;
  96. CTask m_theLogoutTask;
  97. CTask m_theExchangeReginTask;
  98. void SetCurrentTask( UINT nID ) { m_nCurrentTaskID = nID; /*::InterlockedExchange( &m_nCurrentTaskID, nID );*/ }
  99. UINT GetCurrentTask() { return m_nCurrentTaskID; /*::InterlockedExchange( &m_nCurrentTaskID, m_nCurrentTaskID );*/ }
  100. UINT m_nCurrentTaskID;
  101. void InitTaskProcessor();
  102. /*
  103.  * Information
  104.  */
  105. OnlineGameLib::Win32::_tstring m_sAccountName;
  106. OnlineGameLib::Win32::_tstring m_sPassword;
  107. DWORD m_dwAccountID;
  108. static const int s_nRoleListCount;
  109. IClient *m_pAccSvrClient;
  110. IServer *m_pPlayerServer;
  111. IClient *m_pDBRoleClient;
  112. /*
  113.  * class CDataQueue
  114.  */
  115. class CDataQueue
  116. {
  117. public:
  118. /*
  119.  * Struct
  120.  */
  121. typedef struct tagData
  122. {
  123. WPARAM wParam;
  124. LPARAM lParam;
  125. }DQ, NEAR *PDQ, FAR *LPDQ;
  126. explicit CDataQueue( size_t bufferSize = sizeof( DQ ), size_t maxFreeBuffers = 3 );
  127. ~CDataQueue();
  128. /*
  129.  * Function
  130.  */
  131. bool Set( LONG lnID, WPARAM wParam, LPARAM lParam );
  132. bool Peek( LONG lnID, LPDQ lpDQ );
  133. bool Get( LONG lnID, LPDQ lpDQ );
  134. void Clear( LONG lnID );
  135. void Empty();
  136. private:
  137. /*
  138.  * Data
  139.  */
  140. typedef map< LONG, OnlineGameLib::Win32::CBuffer * > stdDataMap;
  141. stdDataMap m_theData;
  142. OnlineGameLib::Win32::CBuffer::Allocator m_theDQAllocator;
  143. OnlineGameLib::Win32::CCriticalSection m_csQueue;
  144. };
  145. CDataQueue m_theDataQueue[enumOwnerTotal];
  146. OnlineGameLib::Win32::CPackager m_thePackReceiver;
  147. protected:
  148. /*
  149.  * Helper function
  150.  */
  151. bool TaskCompleted();
  152. bool SmallPackProcess( UINT nOwner, const void *pData, size_t dataLength );
  153. bool LargePackProcess( UINT nOwner, const void *pData, size_t dataLength );
  154. bool DispatchTaskForAccount( const void *pData, size_t dataLength );
  155. bool DispatchTaskForDBRole( const void *pData, size_t dataLength );
  156. bool DispatchTaskForPlayer( const void *pData, size_t dataLength );
  157. /*
  158.  * Task function
  159.  */
  160. UINT QueryAccPwd();
  161. UINT VerifyAccount();
  162. UINT QueryRoleList();
  163. UINT ProcessRoleList();
  164. UINT SelectRole();
  165. UINT ProcessRoleInfo();
  166. /*
  167.  * Help function for task
  168.  */
  169. bool _VerifyAccount_ToPlayer( UINT nQueryResult );
  170. bool _QueryRoleInfo_ToDBRole( const char *pRoleName );
  171. bool _SyncRoleInfo_ToGameServer( const void *pData, size_t dataLength );
  172. };
  173. #endif // __INCLUDE_GAMEPLAYER_H__