pkthandlers.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404
  1. /*
  2. ** Command & Conquer Renegade(tm)
  3. ** Copyright 2025 Electronic Arts Inc.
  4. **
  5. ** This program is free software: you can redistribute it and/or modify
  6. ** it under the terms of the GNU General Public License as published by
  7. ** the Free Software Foundation, either version 3 of the License, or
  8. ** (at your option) any later version.
  9. **
  10. ** This program is distributed in the hope that it will be useful,
  11. ** but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. ** GNU General Public License for more details.
  14. **
  15. ** You should have received a copy of the GNU General Public License
  16. ** along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. */
  18. /***********************************************************************************************
  19. *** Confidential - Westwood Studios ***
  20. ***********************************************************************************************
  21. * *
  22. * Project Name : Commando *
  23. * *
  24. * $Archive:: /Commando/Code/Commando/pkthandlers.cpp $*
  25. * *
  26. * $Author:: Steve_t $*
  27. * *
  28. * $Modtime:: 1/18/02 11:33a $*
  29. * *
  30. * $Revision:: 53 $*
  31. * *
  32. *---------------------------------------------------------------------------------------------*
  33. * Functions: *
  34. * - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
  35. #include "cnetwork.h"
  36. #include "debug.h"
  37. #include "networkobjectmgr.h"
  38. #include "networkobjectfactory.h"
  39. #include "networkobjectfactorymgr.h"
  40. #include "playermanager.h"
  41. #include "apppacketstats.h"
  42. #include "specialbuilds.h"
  43. extern char * Addr_As_String(sockaddr_in *addr);
  44. extern bool g_is_loading;
  45. //-----------------------------------------------------------------------------
  46. NetworkObjectClass *
  47. Create_Network_Object (cPacket &packet, int class_id, int network_obj_id)
  48. {
  49. WWASSERT(network_obj_id > 0);
  50. //
  51. // Lookup the factory for this type of network object
  52. //
  53. NetworkObjectFactoryClass *factory = NetworkObjectFactoryMgrClass::Find_Factory (class_id);
  54. WWASSERT (factory != NULL);
  55. //
  56. // Create the new object
  57. //
  58. NetworkObjectClass *new_object = factory->Create (packet);
  59. WWASSERT (new_object != NULL);
  60. new_object->Set_Network_ID(network_obj_id);//TSS2001b
  61. return new_object;
  62. }
  63. //-----------------------------------------------------------------------------
  64. void cNetwork::Server_Packet_Handler(cPacket & packet, int rhost_id)
  65. {
  66. #ifndef BETACLIENT
  67. WWASSERT(I_Am_Server());
  68. WWASSERT(
  69. rhost_id >= PServerConnection->Get_Min_RHost() &&
  70. rhost_id <= PServerConnection->Get_Max_RHost());
  71. WWASSERT(Receiver != NULL);
  72. /*TSS082801
  73. // TSS - hack
  74. if (g_is_loading) {
  75. Debug_Say(("Server flushing packet during loading\n"));
  76. packet.Flush();
  77. return;
  78. }
  79. */
  80. //
  81. // TSS112401 XXX
  82. //
  83. if (PServerConnection->Get_Remote_Host(rhost_id) == NULL) {
  84. //
  85. // We were getting this crash:
  86. // pkthandlers.cpp (95) - received BIT_CREATION for existing object of type
  87. // APPPACKETTYPE_CSDAMAGEEVENT following messages "Insufficient bandwidth to send all guaranteed packets".
  88. //
  89. // The only scenario I can envisage is that the server destroys a client, but
  90. // some damage packets are on the wire. Then a new client joins, gets the same client_id,
  91. // and sends some damage packets with the same id's.
  92. //
  93. WWDEBUG_SAY(("cNetwork::Server_Packet_Handler: flushing packet from invalid rhost_id %d\n",
  94. rhost_id));
  95. packet.Flush();
  96. return;
  97. }
  98. int network_obj_id = packet.Get (network_obj_id);
  99. BYTE dirty_bits = packet.Get (dirty_bits);
  100. bool is_delete_pending = packet.Get (is_delete_pending);
  101. //
  102. // Lookup the object this data belongs to
  103. //
  104. NetworkObjectClass *object = NetworkObjectMgrClass::Find_Object (network_obj_id);
  105. //
  106. // Do we need to create this object?
  107. //
  108. if ((dirty_bits & NetworkObjectClass::BIT_CREATION) == NetworkObjectClass::BIT_CREATION) {
  109. int net_classid = packet.Get (net_classid);
  110. //WWASSERT (object == NULL);
  111. if (object != NULL)
  112. {
  113. #ifdef WWDEBUG
  114. //sockaddr_in *actual_from_addr_ptr = (LPSOCKADDR_IN) &packet.Get_From_Address_Wrapper()->FromAddress;
  115. //sockaddr_in rhost_addr = PServerConnection->Get_Remote_Host(rhost_id)->Get_Address();
  116. #endif //WWDEBUG
  117. //#pragma message("(TSS) APPPACKETTYPE_CSDAMAGEEVENT workaround for unknown crash bug.\n")
  118. //WWDEBUG_SAY(("\n"));
  119. //WWDEBUG_SAY(("cNetwork::Server_Packet_Handler: received BIT_CREATION (id %d, is_delete_pending = %d, net_classid = %d, from rhost %d - %s) for existing object:\n",
  120. // network_obj_id, is_delete_pending, net_classid, rhost_id, Addr_As_String(&rhost_addr)));
  121. //WWDEBUG_SAY((" id = %d\n", object->Get_Network_ID()));
  122. //WWDEBUG_SAY((" AppPacketType = %s\n", cAppPacketStats::Interpret_Type(object->Get_App_Packet_Type())));
  123. //WWDEBUG_SAY((" IsDeletePending = %d\n", object->Is_Delete_Pending()));
  124. //WWDEBUG_SAY((" CreatedByPacketID = %d\n", object->Get_Created_By_Packet_ID()));
  125. //WWDEBUG_SAY(("Packet actually from address %s\n", Addr_As_String(actual_from_addr_ptr)));
  126. //WWDEBUG_SAY(("\n"));
  127. //DIE;
  128. //
  129. // So far we've only seen the problem manifest for APPPACKETTYPE_CSDAMAGEEVENT.
  130. // It should be safe to kill the existing conflicting object of this type.
  131. //
  132. if (object->Get_App_Packet_Type() == APPPACKETTYPE_CSDAMAGEEVENT) {
  133. object->Set_Delete_Pending();
  134. }
  135. //
  136. // Flush this packet and bail.
  137. //
  138. packet.Flush();
  139. return;
  140. }
  141. //
  142. // Create the network object
  143. //
  144. object = Create_Network_Object (packet, net_classid, network_obj_id);
  145. #ifdef WWDEBUG
  146. object->Set_Created_By_Packet_ID(packet.Get_Id());
  147. #endif //WWDEBUG
  148. object->Import_Creation (packet);
  149. /*
  150. //XXX
  151. WWDEBUG_SAY(("Created object # %-3d of type %-20s and id %d\n",
  152. NetworkObjectMgrClass::Get_Object_Count(),
  153. cAppPacketStats::Interpret_Type(object->Get_App_Packet_Type()),
  154. network_obj_id));
  155. */
  156. }
  157. //
  158. // TSS092301
  159. //
  160. if (PServerConnection->Get_Remote_Host(rhost_id) == NULL) {
  161. //
  162. // This was probably a quit packet. Bail !
  163. //
  164. packet.Flush();
  165. return;
  166. }
  167. if (object != NULL) {
  168. //
  169. // Do we need to modify this object?
  170. //
  171. if ((dirty_bits & NetworkObjectClass::BIT_RARE) == NetworkObjectClass::BIT_RARE) {
  172. object->Import_Rare (packet);
  173. }
  174. //
  175. // Do we need to modify this object?
  176. //
  177. if ((dirty_bits & NetworkObjectClass::BIT_OCCASIONAL) == NetworkObjectClass::BIT_OCCASIONAL) {
  178. object->Import_Occasional (packet);
  179. }
  180. //
  181. // Do we need to update this object?
  182. //
  183. if ((dirty_bits & NetworkObjectClass::BIT_FREQUENT) == NetworkObjectClass::BIT_FREQUENT) {
  184. object->Import_Frequent (packet);
  185. //object->Increment_Import_State_Count ();
  186. }
  187. object->Increment_Import_State_Count();
  188. //
  189. // Do we need to delete this object?
  190. //
  191. if (is_delete_pending) {
  192. //
  193. // Delete the object
  194. //
  195. if (object != NULL) {
  196. object->Set_Delete_Pending ();
  197. }
  198. }
  199. } else {
  200. packet.Flush();
  201. //Debug_Network_Basic(("Server received update for non-existent object %d.\n",
  202. // network_obj_id));
  203. }
  204. //
  205. // Did we read all the data contained in the packet?
  206. //
  207. //WWASSERT(packet.Is_Flushed());
  208. if (!packet.Is_Flushed())
  209. {
  210. WWDEBUG_SAY(("cNetwork::Client_Packet_Handler: packet not flushed for object of type %s\n",
  211. cAppPacketStats::Interpret_Type(object->Get_App_Packet_Type())));
  212. DIE;
  213. }
  214. #endif // not BETACLIENT
  215. }
  216. //-----------------------------------------------------------------------------
  217. void cNetwork::Client_Packet_Handler(cPacket & packet)
  218. {
  219. #ifndef FREEDEDICATEDSERVER
  220. WWASSERT(I_Am_Client());
  221. WWASSERT(Receiver != NULL);
  222. // TSS - hack
  223. if (g_is_loading) {
  224. Debug_Say(("Client flushing packet during loading\n"));
  225. packet.Flush();
  226. return;
  227. }
  228. //
  229. // This is presently needed for team object creation which isn't filtered out on send
  230. //
  231. if (cNetwork::I_Am_Server()) {
  232. packet.Flush ();
  233. return;
  234. }
  235. int network_obj_id = packet.Get (network_obj_id);
  236. BYTE dirty_bits = packet.Get (dirty_bits);
  237. bool is_delete_pending = packet.Get (is_delete_pending);
  238. //BYTE app_packet_type = packet.Get (app_packet_type);
  239. //
  240. // Lookup the object this data belongs to
  241. //
  242. NetworkObjectClass *object = NetworkObjectMgrClass::Find_Object (network_obj_id);
  243. //
  244. // Do we need to create this object?
  245. //
  246. if ((dirty_bits & NetworkObjectClass::BIT_CREATION) == NetworkObjectClass::BIT_CREATION) {
  247. //WWASSERT (object == NULL);
  248. if (object != NULL)
  249. {
  250. WWDEBUG_SAY(("cNetwork::Client_Packet_Handler: received BIT_CREATION for existing object of type %s\n",
  251. cAppPacketStats::Interpret_Type(object->Get_App_Packet_Type())));
  252. DIE;
  253. }
  254. //
  255. // Create the network object
  256. //
  257. int net_classid = packet.Get (net_classid);
  258. if (object == NULL) {
  259. object = Create_Network_Object (packet, net_classid, network_obj_id);
  260. }
  261. object->Import_Creation (packet);
  262. //
  263. // HACK - HACK
  264. //
  265. if (net_classid == NETCLASSID_GAMEOBJ) {
  266. BaseGameObj *game_obj = (BaseGameObj *)object;
  267. SmartGameObj *smart_game_obj = game_obj->As_SmartGameObj ();
  268. if (smart_game_obj != NULL) {
  269. int control_owner = smart_game_obj->Get_Control_Owner ();
  270. smart_game_obj->Set_Player_Data (cPlayerManager::Find_Player (control_owner));
  271. }
  272. }
  273. /*
  274. //XXX
  275. WWDEBUG_SAY(("Created object # %-3d of type %-20s and id %d\n",
  276. NetworkObjectMgrClass::Get_Object_Count(),
  277. cAppPacketStats::Interpret_Type(object->Get_App_Packet_Type()),
  278. network_obj_id));
  279. */
  280. }
  281. if (object != NULL) {
  282. //
  283. // Do we need to delete this object?
  284. //
  285. if (is_delete_pending && object != NULL) {
  286. object->Set_Delete_Pending ();
  287. }
  288. //
  289. // Do we need to modify this object?
  290. //
  291. if ((dirty_bits & NetworkObjectClass::BIT_RARE) == NetworkObjectClass::BIT_RARE) {
  292. object->Import_Rare (packet);
  293. }
  294. //
  295. // Do we need to modify this object?
  296. //
  297. if ((dirty_bits & NetworkObjectClass::BIT_OCCASIONAL) == NetworkObjectClass::BIT_OCCASIONAL) {
  298. object->Import_Occasional (packet);
  299. }
  300. //
  301. // Do we need to update this object?
  302. //
  303. if ((dirty_bits & NetworkObjectClass::BIT_FREQUENT) == NetworkObjectClass::BIT_FREQUENT) {
  304. object->Import_Frequent (packet);
  305. //object->Increment_Import_State_Count ();
  306. }
  307. object->Increment_Import_State_Count();
  308. object->Set_Last_Clientside_Update_Time(TIMEGETTIME());
  309. /*moving up
  310. //
  311. // Do we need to delete this object?
  312. //
  313. if (is_delete_pending) {
  314. //
  315. // Delete the object
  316. //
  317. if (object != NULL) {
  318. object->Set_Delete_Pending ();
  319. }
  320. }
  321. */
  322. } else {
  323. packet.Flush();
  324. //Debug_Network_Basic(("Client %d received update for non-existent object %d.\n",
  325. // Get_My_Id(), network_obj_id));
  326. }
  327. //
  328. // Did we read all the data contained in the packet?
  329. //
  330. //WWASSERT(packet.Is_Flushed());
  331. if (!packet.Is_Flushed())
  332. {
  333. WWDEBUG_SAY(("cNetwork::Client_Packet_Handler: packet not flushed for object of type %s\n",
  334. cAppPacketStats::Interpret_Type(object->Get_App_Packet_Type())));
  335. //DIE;
  336. }
  337. #endif // !FREEDEDICATEDSERVER
  338. }
  339. //BYTE app_packet_type = packet.Get (app_packet_type);