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

模拟服务器

开发平台:

C/C++

  1. #include "KCore.h"
  2. #include "KNewProtocolProcess.h"
  3. #include "KPlayer.h"
  4. #include <malloc.h>
  5. #include "IClient.h"
  6. #include "KPlayerSet.h"
  7. using OnlineGameLib::Win32::CBuffer;
  8. KNewProtocolProcess g_NewProtocolProcess;
  9. KNewProtocolProcess::KNewProtocolProcess()
  10. : m_theAllocator( 1024, 500 )
  11. {
  12. m_ProtocolTable[MAKEWORD(pf_gamemaster, gm_c2s_execute)] = P_ProcessGMExecute;
  13. m_ProtocolTable[MAKEWORD(pf_gamemaster, gm_c2s_findplayer)] = P_ProcessGMFindPlayer;
  14. m_ProtocolTable[MAKEWORD(pf_playercommunity, playercomm_channelchat)] = P_ProcessPlayerCommExtend;
  15. m_ProtocolTable[MAKEWORD(pf_playercommunity, playercomm_someonechat)] = P_ProcessPlayerCommExtend;
  16. m_ProtocolTable[MAKEWORD(pf_playercommunity, playercomm_s2c_notifychannelid)] = P_ProcessPlayerCommExtend;
  17. m_ProtocolTable[MAKEWORD(pf_chat, chat_someonechat)] = P_ProcessChatExtend;
  18. m_ProtocolTable[MAKEWORD(pf_chat, chat_channelchat)] = P_ProcessChatExtend;
  19. }
  20. KNewProtocolProcess::~KNewProtocolProcess()
  21. {
  22. }
  23. void KNewProtocolProcess::ProcessNetMsg(IClient* pTransfer,
  24. DWORD dwFromIP, DWORD dwFromRelayID,
  25. int nPlayerIndex, BYTE* pData, int nDataSize)
  26. {
  27. FUN_PROCESS_NEW_PROTOCOL func = m_ProtocolTable[(*((WORD*)pData))];
  28. if (func)
  29. (*func)(pTransfer, dwFromIP, dwFromRelayID, nPlayerIndex, pData, nDataSize);
  30. }
  31. void KNewProtocolProcess::SendNetMsgToTransfer(IClient* pClient)
  32. {
  33. if (pClient)
  34. {
  35. DATAList::iterator it;
  36. for (it = m_theTransList.begin(); it != m_theTransList.end(); ++it)
  37. {
  38. CBuffer *pBuffer = *it;
  39. if (pBuffer)
  40. {
  41. pClient->SendPackToServer( 
  42. ( const void * )( pBuffer->GetBuffer() ), 
  43. pBuffer->GetUsed() );
  44. SAFE_RELEASE( pBuffer );
  45. }
  46. }
  47. m_theTransList.clear();
  48. }
  49. }
  50. void KNewProtocolProcess::SendNetMsgToChat(IClient* pClient)
  51. {
  52. if (pClient)
  53. {
  54. DATAList::iterator it;
  55. for (it = m_theChatList.begin(); it != m_theChatList.end(); ++it)
  56. {
  57. CBuffer *pBuffer = *it;
  58. if (pBuffer)
  59. {
  60. pClient->SendPackToServer( 
  61. ( const void * )( pBuffer->GetBuffer() ), 
  62. pBuffer->GetUsed() );
  63. SAFE_RELEASE( pBuffer );
  64. }
  65. }
  66. m_theChatList.clear();
  67. }
  68. }
  69. void KNewProtocolProcess::SendNetMsgToTong(IClient* pClient)
  70. {
  71. if (pClient)
  72. {
  73. DATAList::iterator it;
  74. for (it = m_theTongList.begin(); it != m_theTongList.end(); ++it)
  75. {
  76. CBuffer *pBuffer = *it;
  77. if (pBuffer)
  78. {
  79. pClient->SendPackToServer( 
  80. ( const void * )( pBuffer->GetBuffer() ), 
  81. pBuffer->GetUsed() );
  82. SAFE_RELEASE( pBuffer );
  83. }
  84. }
  85. m_theTongList.clear();
  86. }
  87. }
  88. void KNewProtocolProcess::PushMsgInTransfer(const void * const pData, const size_t datalength)
  89. {
  90. if (pData && datalength > 0)
  91. {
  92. CBuffer *pBuffer = m_theAllocator.Allocate();
  93. ASSERT(pBuffer);
  94. pBuffer->AddData((BYTE*) pData, datalength);
  95. m_theTransList.push_back(pBuffer);
  96. }
  97. }
  98. void KNewProtocolProcess::PushMsgInChat(const void * const pData, const size_t datalength)
  99. {
  100. if (pData && datalength > 0)
  101. {
  102. CBuffer *pBuffer = m_theAllocator.Allocate();
  103. ASSERT(pBuffer);
  104. pBuffer->AddData((BYTE*) pData, datalength);
  105. m_theChatList.push_back(pBuffer);
  106. }
  107. }
  108. void KNewProtocolProcess::PushMsgInTong(const void * const pData, const size_t datalength)
  109. {
  110. if (pData && datalength > 0)
  111. {
  112. CBuffer *pBuffer = m_theAllocator.Allocate();
  113. ASSERT(pBuffer);
  114. pBuffer->AddData((BYTE*) pData, datalength);
  115. m_theTongList.push_back(pBuffer);
  116. }
  117. }
  118. void KNewProtocolProcess::BroadcastGlobal(const void * const pData, const size_t datalength)
  119. {
  120. if (pData && datalength > 0)
  121. {
  122. CBuffer *pBuffer = m_theAllocator.Allocate();
  123. ASSERT(pBuffer);
  124. RELAY_DATA data;
  125. data.ProtocolFamily = pf_relay;
  126. data.ProtocolID = relay_c2c_data;
  127. data.nToIP = INADDR_BROADCAST;
  128. data.nToRelayID = 0;
  129. data.nFromIP = 0;
  130. data.nFromRelayID = 0;
  131. data.routeDateLength = 1 + datalength;
  132. pBuffer->AddData((BYTE*) &data, sizeof(RELAY_DATA));
  133. char cp = s2s_broadcast;
  134. pBuffer->AddData((BYTE*) &cp, sizeof(char));
  135. pBuffer->AddData((BYTE*) pData, datalength);
  136. m_theTransList.push_back(pBuffer);
  137. }
  138. }
  139. void KNewProtocolProcess::BroadcastLocalServer(const void * const pData, const size_t datalength)
  140. {
  141. if (pData && datalength > 0)
  142. {
  143. int nTargetIdx;
  144. nTargetIdx = PlayerSet.GetFirstPlayer();
  145. while (nTargetIdx)
  146. {
  147. g_pServer->PackDataToClient(Player[nTargetIdx].m_nNetConnectIdx, pData, datalength);
  148. nTargetIdx = PlayerSet.GetNextPlayer();
  149. }
  150. }
  151. }
  152. void KNewProtocolProcess::P_ProcessPlayerCommExtend(IClient* pTransfer,
  153.    DWORD dwFromIP, DWORD dwFromRelayID,
  154.    int nPlayerIndex, BYTE* pData, int nDataSize)
  155. {
  156. size_t pckgsize = sizeof(tagExtendProtoHeader) + nDataSize;
  157. #ifdef WIN32
  158. tagExtendProtoHeader* pExHeader = (tagExtendProtoHeader*)_alloca(pckgsize);
  159. #else
  160. tagExtendProtoHeader* pExHeader = (tagExtendProtoHeader*)(new char[pckgsize]);
  161. #endif
  162. pExHeader->ProtocolType = s2c_extend;
  163. pExHeader->wLength = pckgsize - 1;
  164. memcpy(pExHeader + 1, pData, nDataSize);
  165. g_pServer->PackDataToClient(Player[nPlayerIndex].m_nNetConnectIdx, pExHeader, pckgsize);
  166. #ifndef WIN32
  167. delete ((char*)pExHeader);
  168. #endif
  169. }
  170. void KNewProtocolProcess::P_ProcessChatExtend(IClient* pTransfer,
  171.    DWORD dwFromIP, DWORD dwFromRelayID,
  172.    int nPlayerIndex, BYTE* pData, int nDataSize)
  173. {
  174. void* pChatPckg = pData + 1;
  175. size_t chatsize = nDataSize - 1;
  176. size_t pckgsize = sizeof(tagExtendProtoHeader) + chatsize;
  177. #ifdef WIN32
  178. tagExtendProtoHeader* pExHeader = (tagExtendProtoHeader*)_alloca(pckgsize);
  179. #else
  180. tagExtendProtoHeader* pExHeader = (tagExtendProtoHeader*)(new char[pckgsize]);
  181. #endif
  182. pExHeader->ProtocolType = s2c_extendchat;
  183. pExHeader->wLength = pckgsize - 1;
  184. memcpy(pExHeader + 1, pChatPckg, chatsize);
  185. g_pServer->PackDataToClient(Player[nPlayerIndex].m_nNetConnectIdx, pExHeader, pckgsize);
  186. #ifndef WIN32
  187. delete ((char*)pExHeader);
  188. #endif
  189. }