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

模拟服务器

开发平台:

C/C++

  1. // -------------------------------------------------------------------------
  2. // 文件名 : GatewayClient.cpp
  3. // 创建者 : 万里
  4. // 创建时间 : 2003-6-6 20:42:39
  5. // 功能描述 :
  6. //
  7. // -------------------------------------------------------------------------
  8. #include "stdafx.h"
  9. #include "Global.h"
  10. #include "GatewayClient.h"
  11. #include "GatewayCenter.h"
  12. #include "S3Relay.h"
  13. #include "../../sword3paysys/s3accserver/AccountLoginDef.h"
  14. CGatewayClient::CGatewayClient(CGatewayCenter* pGatewayCenter, BOOL bAutoFree)
  15. : CNetClient(pGatewayCenter, bAutoFree), m_lastPckgID(0)
  16. {
  17. }
  18. CGatewayClient::~CGatewayClient()
  19. {
  20. assert(m_mapAskWayMaps.empty());
  21. }
  22. void CGatewayClient::RecvPackage(const void* pData, size_t size)
  23. {
  24. assert(size > 0 && pData != NULL);
  25. BYTE protocol = *(BYTE*)pData;
  26. if (protocol == s2c_notifysvrip)
  27. {
  28. Proc0_NotifySvrIP(pData, size);
  29. }
  30. }
  31. void CGatewayClient::OnServerEventCreate()
  32. {
  33. gOnOneClientCreate(oneclient_gateway);
  34. rTRACE("gateway client create: %s", _ip2a(GetSvrIP()));
  35. }
  36. void CGatewayClient::OnServerEventClose()
  37. {
  38. rTRACE("gateway client close: %s", _ip2a(GetSvrIP()));
  39. {{
  40. AUTOLOCKWRITE(m_lockAskWayMap);
  41. BYTE buffer[max_packagesize];
  42. //clearup
  43. for (ID2ASKWAYMAPMAP::iterator it = m_mapAskWayMaps.begin(); it != m_mapAskWayMaps.end(); it++)
  44. {
  45. ASKWAYMAP& rAskWayMap = (*it).second;
  46. size_t sizePckg = sizeof(RELAY_ASKWAY_DATA) + rAskWayMap.pAskWayMap->wMethodDataLength + rAskWayMap.pAskWayMap->routeDateLength;
  47. size_t sizeLoseData = sizeof(RELAY_DATA) + sizePckg;
  48. assert(sizeLoseData <= max_packagesize);
  49. if (sizeLoseData <= max_packagesize)
  50. {
  51. CNetConnectDup conndup = g_HostServer.FindHostConnectByIP(rAskWayMap.fromIP);
  52. if (conndup.IsValid())
  53. {
  54. size_t sizeLoseData = sizeof(RELAY_DATA) + sizePckg;
  55. RELAY_DATA* pLoseDataDup = (RELAY_DATA*)buffer;
  56. gFillLosewayPckg(0, 0, 
  57. rAskWayMap.pAskWayMap, sizePckg, pLoseDataDup, sizeLoseData);
  58. conndup.SendPackage(pLoseDataDup, sizeLoseData);
  59. }
  60. }
  61. free(rAskWayMap.pAskWayMap);
  62. }
  63. m_mapAskWayMaps.clear();
  64. }}
  65. gOnOneClientClose(oneclient_gateway);
  66. if (!g_RootClient.IsReady())
  67. return;
  68. size_t pckgsize = sizeof(RELAY_ASKWAY_DATA) + sizeof(EXTEND_HEADER) + sizeof(KServerInfo);
  69. RELAY_ASKWAY_DATA* pAskWayData = (RELAY_ASKWAY_DATA*)_alloca(pckgsize);
  70. pAskWayData->ProtocolFamily = pf_relay;
  71. pAskWayData->ProtocolID = relay_c2c_askwaydata;
  72. pAskWayData->nFromIP = 0;
  73. pAskWayData->nFromRelayID = 0;
  74. pAskWayData->seekRelayCount = 0;
  75. pAskWayData->seekMethod = rm_gm;
  76. pAskWayData->wMethodDataLength = 0;
  77. pAskWayData->routeDateLength = sizeof(EXTEND_HEADER) + sizeof(KServerInfo);
  78. EXTEND_HEADER* pHeader = (EXTEND_HEADER*)(pAskWayData + 1);
  79. pHeader->ProtocolFamily = pf_normal;
  80. pHeader->ProtocolID = s2c_gatewayinfo;
  81. KServerInfo* pSvrInfo = (KServerInfo*)(pHeader + 1);
  82. pSvrInfo->Size = sizeof(KServerInfo);
  83. pSvrInfo->Type = ServerInfo;
  84. pSvrInfo->Operate = 0;
  85. pSvrInfo->Version = ACCOUNT_CURRENT_VERSION;
  86. pSvrInfo->nValue = 0;
  87. pSvrInfo->Account[0] = 0;
  88. pSvrInfo->nServerType = server_Logout;
  89. pSvrInfo->nValue = GetSvrIP();
  90. g_RootClient.SendPackage(pAskWayData, pckgsize);
  91. }
  92. void CGatewayClient::Proc0_NotifySvrIP(const void* pData, size_t size)
  93. {
  94. tagNotifySvrIp* pN = (tagNotifySvrIp*)pData;
  95. if (pN->cIPType != INTRANER_IP) //except !
  96. return;
  97. AUTOLOCKWRITE(m_lockAskWayMap);
  98. ID2ASKWAYMAPMAP::iterator itMap = m_mapAskWayMaps.find(pN->pckgID);
  99. if (itMap == m_mapAskWayMaps.end()) //miss
  100. return;
  101. ASKWAYMAP& rAskWayMap = (*itMap).second;
  102. assert(rAskWayMap.pAskWayMap);
  103. DWORD mapID = *(DWORD*)(rAskWayMap.pAskWayMap + 1);
  104. assert(pN->dwMapID == mapID);
  105. if (pN->dwMapID != mapID) //except !
  106. return;
  107. BOOL passed = FALSE;
  108. size_t sizePckg = sizeof(RELAY_ASKWAY_DATA) + rAskWayMap.pAskWayMap->wMethodDataLength + rAskWayMap.pAskWayMap->routeDateLength;
  109. if (pN->dwSvrIP != 0)
  110. {
  111. CNetConnectDup conndup = g_HostServer.FindHostConnectByIP(pN->dwSvrIP);
  112. if (conndup.IsValid())
  113. {//relay
  114. conndup.SendPackage(rAskWayMap.pAskWayMap, sizePckg);
  115. passed = TRUE;
  116. }
  117. }
  118. if (!passed)
  119. {//lose
  120. CNetConnectDup conndup2 = g_HostServer.FindHostConnectByIP(rAskWayMap.fromIP);
  121. if (conndup2.IsValid())
  122. {
  123. size_t sizeLoseData = sizeof(RELAY_DATA) + sizePckg;
  124. RELAY_DATA* pLoseDataDup = (RELAY_DATA*)_alloca(sizeLoseData);
  125. gFillLosewayPckg(0, 0, 
  126. rAskWayMap.pAskWayMap, sizePckg, pLoseDataDup, sizeLoseData);
  127. conndup2.SendPackage(pLoseDataDup, sizeLoseData);
  128. }
  129. }
  130. free(rAskWayMap.pAskWayMap);
  131. m_mapAskWayMaps.erase(itMap);
  132. }
  133. BOOL CGatewayClient::PassAskWayMap(DWORD fromIP, const RELAY_ASKWAY_DATA* pAskWayMap)
  134. {
  135. assert(pAskWayMap && pAskWayMap->seekMethod == rm_map_id);
  136. size_t size = sizeof(RELAY_ASKWAY_DATA) + pAskWayMap->wMethodDataLength + pAskWayMap->routeDateLength;
  137. AUTOLOCKWRITE(m_lockAskWayMap);
  138. if (IsReady())
  139. {
  140. if (m_mapAskWayMaps.size() < MAX_PACKAGECOUNT)
  141. {
  142. for ( ; ; )
  143. {
  144. std::pair<ID2ASKWAYMAPMAP::iterator, bool> insret = m_mapAskWayMaps.insert(ID2ASKWAYMAPMAP::value_type(++m_lastPckgID, ASKWAYMAP()));
  145. if (insret.second)
  146. {//insert, not existed
  147. ASKWAYMAP& rAskWayMap = (*insret.first).second;
  148. rAskWayMap.tick = ::GetTickCount();
  149. rAskWayMap.fromIP = fromIP;
  150. rAskWayMap.pAskWayMap = (RELAY_ASKWAY_DATA*)malloc(size);
  151. memcpy(rAskWayMap.pAskWayMap, pAskWayMap, size);
  152. break;
  153. }
  154. }
  155. DWORD mapID = *(DWORD*)(pAskWayMap + 1);
  156. tagRequestSvrIp req;
  157. req.cProtocol = c2s_requestsvrip;
  158. req.pckgID = m_lastPckgID;
  159. req.cIPType = INTRANER_IP;
  160. req.dwMapID = mapID;
  161. SendPackage(&req, sizeof(req));
  162. return TRUE;
  163. }
  164. }
  165. {{
  166. //lose way
  167. CNetConnectDup conndup = g_HostServer.FindHostConnectByIP(fromIP);
  168. if (conndup.IsValid())
  169. {
  170. size_t sizeLoseData = sizeof(RELAY_DATA) + size;
  171. RELAY_DATA* pLoseDataDup = (RELAY_DATA*)_alloca(sizeLoseData);
  172. gFillLosewayPckg(0, 0, 
  173. pAskWayMap, size, pLoseDataDup, sizeLoseData);
  174. conndup.SendPackage(pLoseDataDup, sizeLoseData);
  175. return TRUE;
  176. }
  177. }}
  178. return FALSE;
  179. }