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

模拟服务器

开发平台:

C/C++

  1. // DealPackage.cpp: implementation of the CDealPackage class.
  2. //
  3. //////////////////////////////////////////////////////////////////////
  4. #include "stdafx.h"
  5. #include "S3Relay.h"
  6. #include "DealPackage.h"
  7. //////////////////////////////////////////////////////////////////////
  8. // Construction/Destruction
  9. //////////////////////////////////////////////////////////////////////
  10. CDealPackage::CDealPackage()
  11. {
  12. memset(m_Protocol2Proc, sizeof(m_Protocol2Proc), 0);
  13. }
  14. CDealPackage::~CDealPackage()
  15. {
  16. }
  17. BOOL CDealPackage::Initialize()
  18. {
  19. memset(m_Protocol2Proc, sizeof(m_Protocol2Proc), 0);
  20. m_Protocol2Proc[c2s_registerfamily] = Proc_c2sRegisterFamily;
  21. m_Protocol2Proc[c2s_entergame] = Proc_c2sEnterGame;
  22. m_Protocol2Proc[c2s_leavegame] = Proc_c2sLeaveGame;
  23. m_Protocol2Proc[c2s_relay] = Proc_c2sRelay;
  24. return TRUE;
  25. }
  26. BOOL CDealPackage::Uninitialize()
  27. {
  28. return TRUE;
  29. }
  30. BOOL CDealPackage::DealPackage(const CNetConnect* pConn, const void* pData, unsigned len)
  31. {
  32. assert(pConn != NULL && pData != NULL && len >= sizeof(BYTE));
  33. BYTE Protocol = *(BYTE*)pData;
  34. in_addr ia;
  35. ia.s_addr = pConn->GetIP();
  36. gTrace("%s -- recv size: %d", inet_ntoa(ia), len);
  37. if (!m_Protocol2Proc[Protocol])
  38. return FALSE;
  39. return (this->*m_Protocol2Proc[Protocol])(pConn, pData, len);
  40. }
  41. ///////////////////////////
  42. //deal proc ...
  43. BOOL CDealPackage::Proc_c2sRegisterFamily(const CNetConnect* pConn, const void* pData, unsigned len)
  44. {
  45. if (pData == NULL || len < sizeof(tagRegisterFamily))
  46. return FALSE;
  47. tagRegisterFamily* pRegFami = (tagRegisterFamily*)pData;
  48. assert(pRegFami->cProtocol == c2s_registerfamily);
  49. if (pRegFami->bRegister)
  50. {
  51. g_pConnTrack->RegConnect(pConn->GetID(), pRegFami->nFamily, pRegFami->RelayMethod);
  52. }
  53. else
  54. {
  55. g_pConnTrack->UnregConnect(pConn->GetID(), pRegFami->nFamily, pRegFami->RelayMethod);
  56. if (pRegFami->nFamily == 1)
  57. g_pPlayerTrack->ClearOnSvr(pConn->GetID());
  58. }
  59. return TRUE;
  60. }
  61. BOOL CDealPackage::Proc_c2sEnterGame(const CNetConnect* pConn, const void* pData, unsigned len)
  62. {
  63. if (pData == NULL || len < sizeof(tagEnterGame2))
  64. return FALSE;
  65. tagEnterGame2* pEnterGame2 = (tagEnterGame2*)pData;
  66. assert(pEnterGame2->cProtocol == c2s_entergame);
  67. if (!g_pConnTrack->IsConnOnMethod(pConn->GetID(), 1)) //forbid
  68. return FALSE;
  69. g_pPlayerTrack->Login(pEnterGame2->szAccount, pEnterGame2->szRoleName, pEnterGame2->dwNameID, pConn->GetID(), pEnterGame2->nPlayerIdx);
  70. return TRUE;
  71. }
  72. BOOL CDealPackage::Proc_c2sLeaveGame(const CNetConnect* pConn, const void* pData, unsigned len)
  73. {
  74. if (pData == NULL || len < sizeof(tagLeaveGame))
  75. return FALSE;
  76. tagLeaveGame* pLeaveGame = (tagLeaveGame*)pData;
  77. assert(pLeaveGame->cProtocol == c2s_leavegame);
  78. g_pPlayerTrack->Logout(pLeaveGame->szAccountName);
  79. return TRUE;
  80. }
  81. BOOL CDealPackage::Proc_c2sRelay(const CNetConnect* pConn, const void* pData, unsigned len)
  82. {
  83. if (pData == NULL || len < sizeof(RELAY_COMMAND))
  84. return FALSE;
  85. RELAY_COMMAND* pRelayCmd = (RELAY_COMMAND*)pData;
  86. assert(pRelayCmd->ProtocolType == c2s_relay);
  87. RELAY_COMMAND* pRelayCmdDup = (RELAY_COMMAND*)g_pMemory->Alloc(len);
  88. assert(pRelayCmdDup != NULL);
  89. memcpy(pRelayCmdDup, pData, len);
  90. pRelayCmdDup->ProtocolType = s2c_relay;
  91. pRelayCmdDup->nIP = pConn->GetIP();
  92. pRelayCmdDup->port = pConn->GetPort();
  93. in_addr ia;
  94. ia.s_addr = pConn->GetIP();
  95. gTrace("%s -- recv size: %d", inet_ntoa(ia), len);
  96. if (g_pNetRelay->RelayPackage(pConn->GetID(), pRelayCmd->nIP, pRelayCmd->port, pRelayCmdDup, len))
  97. return TRUE;
  98. g_pMemory->Free(pRelayCmdDup);
  99. return FALSE;
  100. }