kdbprocessthread.cpp
Upload User: dzyhzl
Upload Date: 2019-04-29
Package Size: 56270k
Code Size: 4k
Development Platform:

C/C++

  1. #include "stdafx.h"
  2. #include <objbase.h>
  3. #include <initguid.h>
  4. #include "KRoleInfomation.h"
  5. #include "KRoleDBServer.h"
  6. #include "KException.h"
  7. #include "S3DBINTERFACE.H"
  8. #include "KDBProcessThread.h"
  9. #include "KRoleDBHeader.h"
  10. #include "....HeadersKProtocolDef.h"
  11. #include "..MultiServerCommontstring.h"
  12. using OnlineGameLib::Win32::_tstring;
  13. #pragma warning(disable:4786)
  14. #include <crtdbg.h>
  15. #include <list>
  16. #include "stdio.h"
  17. using namespace std;
  18. static const int g_snMaxPlayerCount = 5000;
  19. static const int g_snPrecision = 10;
  20. static const int g_snMaxBuffer = 15;
  21. static const int g_snBufferSize = 1024;
  22. IServer *g_pNetServer = NULL;
  23. static HMODULE hDllModule;
  24. DWORD g_dwPerSaveTime = 100;//每次存档的时间
  25. DWORD g_dwMaxUnModifyTime;
  26. list<TCmd*> g_MainThreadCmdList;
  27. list<TGetRoleInfoFromDB*> g_DBLoadThreadCmdList;
  28. list<TCmd*> g_NetServiceThreadCmdList;
  29. extern BOOL InitNetEngine();
  30. extern RoleDBServer::KNetServer g_NetServer;
  31. unsigned char g_LoadDBThreadMark = MARK1;
  32. unsigned char g_SaveDBThreadMark = MARK1;
  33. unsigned char g_NetServerMark = MARK1;;
  34. unsigned char g_MainLoopMark = MARK1;
  35. int  g_nMistakeCount = 0;
  36. size_t g_dwSendLen = 0;
  37. size_t g_dwRecvLen = 0;
  38. OnlineGameLib::Win32::CCriticalSection g_RoleInfoMutex;
  39. OnlineGameLib::Win32::CCriticalSection g_RoleInfoSetMutex;
  40. OnlineGameLib::Win32::CCriticalSection g_MainMsgListMutex;
  41. OnlineGameLib::Win32::CCriticalSection g_NetMsgListMutex;
  42. OnlineGameLib::Win32::CCriticalSection g_GetRoleInfoMutex;
  43. TThreadData g_ThreadData[4];
  44. char * GetRoleNameFromRoleBuffer(char * pRoleBuffer)
  45. {
  46. if (pRoleBuffer == NULL)
  47. return NULL;
  48. try
  49. {
  50. TRoleData *pRoleData = (TRoleData*) pRoleBuffer;
  51. return pRoleData->BaseInfo.szName;
  52. }
  53. catch(...)
  54. {
  55. g_nMistakeCount ++;
  56. throw KException("GetRoleNameFromRoleBuffer", "内存有问题");
  57. }
  58. }
  59. int InitServer()
  60. {
  61. int nReturn;
  62. S3DBI_InitDBInterface();
  63. nReturn = InitNetEngine();
  64. return nReturn;
  65. }
  66. DWORD ReleaseServer()
  67. {
  68. for(int i = 0; i < 4; i ++)
  69. TerminateThread(g_ThreadData[i].hThread,0);
  70. g_pNetServer->CloseService();
  71. g_pNetServer->Cleanup();
  72. g_pNetServer->Release();
  73. ::FreeLibrary( hDllModule );
  74. S3DBI_ReleaseDBInterface();
  75. return 1;
  76. }
  77. typedef HRESULT ( __stdcall * pfnCreateServerInterface )(
  78.  REFIID riid,
  79.  void **ppv
  80.  );
  81. void __stdcall ServerEventNotify(
  82.  LPVOID lpParam,
  83.  const unsigned long &ulnID,
  84.  const unsigned long &ulnEventType )
  85. {
  86. switch( ulnEventType )
  87. {
  88. case enumClientConnectCreate:
  89. g_NetServer.RegisterClient(ulnID);
  90. break;
  91. case enumClientConnectClose:
  92. g_NetServer.DestoryClient(ulnID);
  93. break;
  94. }
  95. }
  96. BOOL InitNetEngine()
  97. {
  98. hDllModule = ::LoadLibrary( "heaven.dll" );
  99. if ( hDllModule )
  100. {
  101. pfnCreateServerInterface pFactroyFun = ( pfnCreateServerInterface )GetProcAddress( hDllModule, "CreateInterface" );
  102. IServerFactory *pServerFactory = NULL;
  103. if ( SUCCEEDED( pFactroyFun( IID_IServerFactory, reinterpret_cast< void ** >( &pServerFactory ) ) ) )
  104. {
  105. pServerFactory->SetEnvironment( g_snMaxPlayerCount, g_snPrecision, g_snMaxBuffer, g_snBufferSize  );
  106. pServerFactory->CreateServerInterface( IID_IIOCPServer, reinterpret_cast< void ** >( &g_pNetServer ) );
  107. pServerFactory->Release();
  108. }
  109. }
  110. else
  111. return FALSE;
  112. g_pNetServer->Startup();
  113. g_pNetServer->RegisterMsgFilter( reinterpret_cast< void * >( g_pNetServer ), ServerEventNotify );
  114. g_pNetServer->OpenService( 0, 5001 );
  115. return 1;
  116. }