wwpacket.cpp 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349
  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. // Filename: wwpacket.cpp
  20. // Project: wwnet
  21. // Author: Tom Spencer-Smith
  22. // Date: June 1998
  23. // Description: Adding and extracting data from byte streams
  24. //
  25. //------------------------------------------------------------------------------------
  26. #include "wwpacket.h"
  27. #include "win.h"
  28. #include "systimer.h"
  29. #include "crc.h"
  30. #include "quat.h"
  31. #include "fromaddress.h"
  32. #include "packettype.h"
  33. #include "bitpackids.h"
  34. #define PACKET_ID_BITS 28 // Enough for about 100 packets per second for about a month.
  35. #define PACKET_TYPE_BITS 4 // Enough for 16 packet types (we currently have 7 at 9/20/2001 11:33PM)
  36. DEFINE_AUTO_POOL(cPacket, 256)
  37. //
  38. // Class statics
  39. //
  40. #ifdef WRAPPER_CRC
  41. const USHORT cPacket::PACKET_HEADER_SIZE = 7;
  42. #else //WRAPPER_CRC
  43. const int cPacket::CRC_PLACEHOLDER = 99999;
  44. const USHORT cPacket::PACKET_HEADER_SIZE = 11;
  45. #endif //WRAPPER_CRC
  46. int cPacket::RefCount = 0;
  47. bool cPacket::EncoderInit = true;
  48. const unsigned long cPacket::DefSendTime = 0xffffffff;
  49. //------------------------------------------------------------------------------------
  50. cPacket::cPacket() :
  51. Type(UNDEFINED_TYPE),
  52. Id(UNDEFINED_ID),
  53. SenderId(UNDEFINED_ID),
  54. SendTime(DefSendTime),
  55. FirstSendTime(DefSendTime),
  56. ResendCount(-1), // so that first send doesn't count as a resend
  57. NumSends(1)
  58. {
  59. //cEncoderList::Set_Compression_Enabled(false);
  60. RefCount++;
  61. if (EncoderInit) {
  62. Init_Encoder();
  63. }
  64. }
  65. //---------------- --------------------------------------------------------------------
  66. cPacket::~cPacket()
  67. {
  68. RefCount--;
  69. }
  70. //------------------------------------------------------------------------------------
  71. //
  72. // Assignment operator
  73. //
  74. cPacket& cPacket::operator=(const cPacket& source)
  75. {
  76. PFromAddressWrapper = source.PFromAddressWrapper;
  77. Type = source.Type;
  78. Id = source.Id;
  79. SenderId = source.SenderId;
  80. SendTime = source.SendTime;
  81. FirstSendTime = source.FirstSendTime;
  82. ResendCount = source.ResendCount;
  83. #ifndef WRAPPER_CRC
  84. IsCrcCorrect = source.IsCrcCorrect;
  85. #endif //WRAPPER_CRC
  86. BitStreamClass::operator=(source);
  87. NumSends = source.NumSends;
  88. return * this;
  89. }
  90. //------------------------------------------------------------------------------------
  91. void cPacket::Add_Vector3(Vector3 & v)
  92. {
  93. WWASSERT(v.Is_Valid());
  94. Add(v.X);
  95. Add(v.Y);
  96. Add(v.Z);
  97. }
  98. //------------------------------------------------------------------------------------
  99. void cPacket::Get_Vector3(Vector3 & v)
  100. {
  101. Get(v.X);
  102. Get(v.Y);
  103. Get(v.Z);
  104. WWASSERT(v.Is_Valid());
  105. }
  106. //------------------------------------------------------------------------------------
  107. void cPacket::Add_Quaternion(Quaternion & q)
  108. {
  109. WWASSERT(q.Is_Valid());
  110. Add(q.X);
  111. Add(q.Y);
  112. Add(q.Z);
  113. Add(q.W);
  114. }
  115. //------------------------------------------------------------------------------------
  116. void cPacket::Get_Quaternion(Quaternion & q)
  117. {
  118. Get(q.X);
  119. Get(q.Y);
  120. Get(q.Z);
  121. Get(q.W);
  122. WWASSERT(q.Is_Valid());
  123. }
  124. //------------------------------------------------------------------------------------
  125. void cPacket::Set_Type(BYTE type)
  126. {
  127. WWASSERT(Type == UNDEFINED_TYPE || Type == type);
  128. WWASSERT(type != UNDEFINED_TYPE);
  129. Type = type;
  130. }
  131. //------------------------------------------------------------------------------------
  132. void cPacket::Set_Id(int id)
  133. {
  134. WWASSERT(id != UNDEFINED_ID);
  135. Id = id;
  136. }
  137. //------------------------------------------------------------------------------------
  138. void cPacket::Set_Sender_Id(int sender_id)
  139. {
  140. SenderId = sender_id;
  141. }
  142. //------------------------------------------------------------------------------------
  143. void cPacket::Set_Send_Time()
  144. {
  145. unsigned long time = TIMEGETTIME();
  146. if (SendTime == DefSendTime) {
  147. FirstSendTime = time;
  148. }
  149. SendTime = time;
  150. }
  151. //------------------------------------------------------------------------------------
  152. void cPacket::Set_Num_Sends(int num_sends)
  153. {
  154. WWASSERT(num_sends > 0);
  155. NumSends = num_sends;
  156. }
  157. /*
  158. //------------------------------------------------------------------------------------
  159. BYTE cPacket::Peek_Message_Type() const
  160. {
  161. //
  162. // Note that using Get is the only valid way to read data from a packet!
  163. //
  164. cPacket temp_packet;
  165. temp_packet = *this;
  166. BYTE message_type;
  167. temp_packet.Get(message_type);
  168. temp_packet.Flush();
  169. return message_type;
  170. }
  171. */
  172. void cPacket::Init_Encoder(void)
  173. {
  174. EncoderInit = false;
  175. cEncoderList::Set_Precision(BITPACK_PACKET_ID, PACKET_ID_BITS);
  176. cEncoderList::Set_Precision(BITPACK_PACKET_TYPE, PACKET_TYPE_BITS);
  177. }
  178. //------------------------------------------------------------------------------------
  179. void cPacket::Construct_Full_Packet(cPacket & full_packet, cPacket & src_packet)
  180. {
  181. WWASSERT(
  182. src_packet.Get_Type() >= PACKETTYPE_FIRST &&
  183. src_packet.Get_Type() <= PACKETTYPE_LAST);
  184. WWASSERT(src_packet.Get_Id() != UNDEFINED_ID);
  185. #ifndef WRAPPER_CRC
  186. full_packet.Add(CRC_PLACEHOLDER);
  187. #endif //WRAPPER_CRC
  188. full_packet.Add(src_packet.Get_Type(), BITPACK_PACKET_TYPE);
  189. full_packet.Add(src_packet.Get_Id(), BITPACK_PACKET_ID);
  190. full_packet.Add((BYTE)src_packet.Get_Sender_Id());
  191. full_packet.Add((USHORT)src_packet.Get_Bit_Length());
  192. int header_bit_length = full_packet.Get_Bit_Length();
  193. WWASSERT(header_bit_length == PACKET_HEADER_SIZE * 8);
  194. memcpy(
  195. full_packet.Get_Data() + PACKET_HEADER_SIZE,
  196. src_packet.Get_Data(),
  197. src_packet.Get_Compressed_Size_Bytes());
  198. unsigned int whole_bit_length = header_bit_length + src_packet.Get_Bit_Length();
  199. full_packet.Set_Bit_Length(whole_bit_length);
  200. //
  201. // Compute a CRC for all the data following the CRC placeholder
  202. //
  203. //ULONG crc = CRC::Memory(
  204. // (BYTE *) (full_packet.Get_Data() + sizeof(CRC_PLACEHOLDER)),
  205. // full_packet.Get_Max_Size() - sizeof(CRC_PLACEHOLDER));
  206. // Only CRC the meaningful data in the buffer - not the other 1300ish bytes as well. ST - 9/19/2001 11:18PM
  207. #ifndef WRAPPER_CRC
  208. ULONG crc = CRC::Memory((BYTE *) (full_packet.Get_Data() + sizeof(CRC_PLACEHOLDER)), (whole_bit_length / 8) - sizeof(CRC_PLACEHOLDER));
  209. //
  210. // Overwrite the crc placeholder with the computed crc.
  211. //
  212. cPacket temp_packet;
  213. temp_packet.Add(crc);
  214. memcpy(
  215. full_packet.Get_Data(),
  216. temp_packet.Get_Data(),
  217. temp_packet.Get_Compressed_Size_Bytes());
  218. #endif //WRAPPER_CRC
  219. }
  220. //------------------------------------------------------------------------------------
  221. void cPacket::Construct_App_Packet(cPacket & packet, cPacket & full_packet)
  222. {
  223. #ifndef WRAPPER_CRC
  224. int remote_crc;
  225. #endif //WRAPPER_CRC
  226. BYTE type;
  227. int packet_id;
  228. char sender_id;
  229. USHORT bit_size;
  230. #ifndef WRAPPER_CRC
  231. full_packet.Get(remote_crc);
  232. #endif //WRAPPER_CRC
  233. full_packet.Get(type, BITPACK_PACKET_TYPE);
  234. full_packet.Get(packet_id, BITPACK_PACKET_ID);
  235. full_packet.Get(sender_id);
  236. full_packet.Get(bit_size);
  237. #ifdef WRAPPER_CRC
  238. packet.Set_Type(type);
  239. packet.Set_Id(packet_id);
  240. packet.Set_Sender_Id(sender_id);
  241. packet.Set_Bit_Length(bit_size);
  242. packet.PFromAddressWrapper = full_packet.PFromAddressWrapper;
  243. memcpy(packet.Get_Data(), full_packet.Get_Data() + PACKET_HEADER_SIZE, packet.Get_Compressed_Size_Bytes());
  244. #else //WRAPPER_CRC
  245. if (((bit_size / 8) + PACKET_HEADER_SIZE) < MAX_BUFFER_SIZE) {
  246. //
  247. // Only CRC the meaningful data in the buffer - not the other 1300ish bytes as well. ST - 9/19/2001 11:29PM
  248. //
  249. int local_crc = CRC::Memory((BYTE *) (full_packet.Get_Data() + sizeof(CRC_PLACEHOLDER)), ((bit_size / 8) + PACKET_HEADER_SIZE) - sizeof(CRC_PLACEHOLDER));
  250. if (local_crc == remote_crc) {
  251. packet.Set_Is_Crc_Correct(true);
  252. packet.Set_Type(type);
  253. packet.Set_Id(packet_id);
  254. packet.Set_Sender_Id(sender_id);
  255. packet.Set_Bit_Length(bit_size);
  256. packet.PFromAddressWrapper = full_packet.PFromAddressWrapper;
  257. memcpy(packet.Get_Data(), full_packet.Get_Data() + PACKET_HEADER_SIZE, packet.Get_Compressed_Size_Bytes());
  258. } else {
  259. packet.Set_Is_Crc_Correct(false);
  260. }
  261. } else {
  262. packet.Set_Is_Crc_Correct(false);
  263. }
  264. #endif //WRAPPER_CRC
  265. }
  266. //ExecuteTime(0),
  267. //ReturnCode(0),
  268. //ExecuteTime = source.ExecuteTime;
  269. //ReturnCode = source.ReturnCode;
  270. //------------------------------------------------------------------------------------
  271. /*
  272. void cPacket::Set_Execute_Time(int execute_time)
  273. {
  274. WWASSERT(execute_time > 0);
  275. ExecuteTime = execute_time;
  276. }
  277. */
  278. /*
  279. if (!Is_Flushed()) {
  280. WWDEBUG_SAY(("*** cPacket::~cPacket: !Is_Flushed()\n"));
  281. DIE;
  282. }
  283. */