NetPacket.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  1. /*
  2. ** Command & Conquer Generals(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. // //
  20. // (c) 2001-2003 Electronic Arts Inc. //
  21. // //
  22. ////////////////////////////////////////////////////////////////////////////////
  23. /*
  24. Ok, how this should have been done is to make each of the command types
  25. have a bitmask telling which command message header information
  26. each command type required. That would make finding out the size of
  27. a particular command easier to find, without so much repetitious code.
  28. We would still need to have a separate function for each command type
  29. for the data, but at least that wouldn't be repeating code, that would
  30. be specialized code.
  31. */
  32. #pragma once
  33. #ifndef __NETPACKET_H
  34. #define __NETPACKET_H
  35. #include "NetworkDefs.h"
  36. #include "GameNetwork/NetCommandList.h"
  37. #include "Common/MessageStream.h"
  38. #include "Common/GameMemory.h"
  39. class NetPacket;
  40. typedef std::list<NetPacket *> NetPacketList;
  41. typedef std::list<NetPacket *>::iterator NetPacketListIter;
  42. class NetPacket : public MemoryPoolObject
  43. {
  44. MEMORY_POOL_GLUE_WITH_USERLOOKUP_CREATE(NetPacket, "NetPacket")
  45. public:
  46. NetPacket();
  47. NetPacket(TransportMessage *msg);
  48. //virtual ~NetPacket();
  49. void init();
  50. void reset();
  51. void setAddress(Int addr, Int port);
  52. Bool addCommand(NetCommandRef *msg);
  53. Int getNumCommands();
  54. NetCommandList *getCommandList();
  55. static NetCommandRef * ConstructNetCommandMsgFromRawData(UnsignedByte *data, UnsignedShort dataLength);
  56. static NetPacketList ConstructBigCommandPacketList(NetCommandRef *ref);
  57. UnsignedByte *getData();
  58. Int getLength();
  59. UnsignedInt getAddr();
  60. UnsignedShort getPort();
  61. protected:
  62. static UnsignedInt GetBufferSizeNeededForCommand(NetCommandMsg *msg);
  63. static void FillBufferWithCommand(UnsignedByte *buffer, NetCommandRef *msg);
  64. // These functions return the size of the command without any compression, repetition, etc.
  65. // i.e. All of the required fields are taken into account when returning the size.
  66. static UnsignedInt GetGameCommandSize(NetCommandMsg *msg);
  67. static UnsignedInt GetAckCommandSize(NetCommandMsg *msg);
  68. static UnsignedInt GetFrameCommandSize(NetCommandMsg *msg);
  69. static UnsignedInt GetPlayerLeaveCommandSize(NetCommandMsg *msg);
  70. static UnsignedInt GetRunAheadMetricsCommandSize(NetCommandMsg *msg);
  71. static UnsignedInt GetRunAheadCommandSize(NetCommandMsg *msg);
  72. static UnsignedInt GetDestroyPlayerCommandSize(NetCommandMsg *msg);
  73. static UnsignedInt GetKeepAliveCommandSize(NetCommandMsg *msg);
  74. static UnsignedInt GetDisconnectKeepAliveCommandSize(NetCommandMsg *msg);
  75. static UnsignedInt GetDisconnectPlayerCommandSize(NetCommandMsg *msg);
  76. static UnsignedInt GetPacketRouterQueryCommandSize(NetCommandMsg *msg);
  77. static UnsignedInt GetPacketRouterAckCommandSize(NetCommandMsg *msg);
  78. static UnsignedInt GetDisconnectChatCommandSize(NetCommandMsg *msg);
  79. static UnsignedInt GetDisconnectVoteCommandSize(NetCommandMsg *msg);
  80. static UnsignedInt GetChatCommandSize(NetCommandMsg *msg);
  81. static UnsignedInt GetProgressMessageSize(NetCommandMsg *msg);
  82. static UnsignedInt GetLoadCompleteMessageSize(NetCommandMsg *msg);
  83. static UnsignedInt GetTimeOutGameStartMessageSize(NetCommandMsg *msg);
  84. static UnsignedInt GetWrapperCommandSize(NetCommandMsg *msg);
  85. static UnsignedInt GetFileCommandSize(NetCommandMsg *msg);
  86. static UnsignedInt GetFileAnnounceCommandSize(NetCommandMsg *msg);
  87. static UnsignedInt GetFileProgressCommandSize(NetCommandMsg *msg);
  88. static UnsignedInt GetDisconnectFrameCommandSize(NetCommandMsg *msg);
  89. static UnsignedInt GetDisconnectScreenOffCommandSize(NetCommandMsg *msg);
  90. static UnsignedInt GetFrameResendRequestCommandSize(NetCommandMsg *msg);
  91. static void FillBufferWithGameCommand(UnsignedByte *buffer, NetCommandRef *msg);
  92. static void FillBufferWithAckCommand(UnsignedByte *buffer, NetCommandRef *msg);
  93. static void FillBufferWithFrameCommand(UnsignedByte *buffer, NetCommandRef *msg);
  94. static void FillBufferWithPlayerLeaveCommand(UnsignedByte *buffer, NetCommandRef *msg);
  95. static void FillBufferWithRunAheadMetricsCommand(UnsignedByte *buffer, NetCommandRef *msg);
  96. static void FillBufferWithRunAheadCommand(UnsignedByte *buffer, NetCommandRef *msg);
  97. static void FillBufferWithDestroyPlayerCommand(UnsignedByte *buffer, NetCommandRef *msg);
  98. static void FillBufferWithKeepAliveCommand(UnsignedByte *buffer, NetCommandRef *msg);
  99. static void FillBufferWithDisconnectKeepAliveCommand(UnsignedByte *buffer, NetCommandRef *msg);
  100. static void FillBufferWithDisconnectPlayerCommand(UnsignedByte *buffer, NetCommandRef *msg);
  101. static void FillBufferWithPacketRouterQueryCommand(UnsignedByte *buffer, NetCommandRef *msg);
  102. static void FillBufferWithPacketRouterAckCommand(UnsignedByte *buffer, NetCommandRef *msg);
  103. static void FillBufferWithDisconnectChatCommand(UnsignedByte *buffer, NetCommandRef *msg);
  104. static void FillBufferWithDisconnectVoteCommand(UnsignedByte *buffer, NetCommandRef *msg);
  105. static void FillBufferWithChatCommand(UnsignedByte *buffer, NetCommandRef *msg);
  106. static void FillBufferWithProgressMessage(UnsignedByte *buffer, NetCommandRef *msg);
  107. static void FillBufferWithLoadCompleteMessage(UnsignedByte *buffer, NetCommandRef *msg);
  108. static void FillBufferWithTimeOutGameStartMessage(UnsignedByte *buffer, NetCommandRef *msg);
  109. static void FillBufferWithFileMessage(UnsignedByte *buffer, NetCommandRef *msg);
  110. static void FillBufferWithFileProgressMessage(UnsignedByte *buffer, NetCommandRef *msg);
  111. static void FillBufferWithFileAnnounceMessage(UnsignedByte *buffer, NetCommandRef *msg);
  112. static void FillBufferWithDisconnectFrameMessage(UnsignedByte *buffer, NetCommandRef *msg);
  113. static void FillBufferWithDisconnectScreenOffMessage(UnsignedByte *buffer, NetCommandRef *msg);
  114. static void FillBufferWithFrameResendRequestMessage(UnsignedByte *buffer, NetCommandRef *msg);
  115. Bool addFrameCommand(NetCommandRef *msg);
  116. Bool isRoomForFrameMessage(NetCommandRef *msg);
  117. Bool addAckCommand(NetCommandRef *msg, UnsignedShort commandID, UnsignedByte originalPlayerID);
  118. Bool addAckStage1Command(NetCommandRef *msg);
  119. Bool addAckStage2Command(NetCommandRef *msg);
  120. Bool addAckBothCommand(NetCommandRef *msg);
  121. Bool isRoomForAckMessage(NetCommandRef *msg);
  122. Bool addGameCommand(NetCommandRef *msg);
  123. Bool isRoomForGameMessage(NetCommandRef *msg, GameMessage *gmsg);
  124. Bool addPlayerLeaveCommand(NetCommandRef *msg);
  125. Bool isRoomForPlayerLeaveMessage(NetCommandRef *msg);
  126. Bool addRunAheadMetricsCommand(NetCommandRef *msg);
  127. Bool isRoomForRunAheadMetricsMessage(NetCommandRef *msg);
  128. Bool addRunAheadCommand(NetCommandRef *msg);
  129. Bool isRoomForRunAheadMessage(NetCommandRef *msg);
  130. Bool addDestroyPlayerCommand(NetCommandRef *msg);
  131. Bool isRoomForDestroyPlayerMessage(NetCommandRef *msg);
  132. Bool addKeepAliveCommand(NetCommandRef *msg);
  133. Bool isRoomForKeepAliveMessage(NetCommandRef *msg);
  134. Bool addDisconnectKeepAliveCommand(NetCommandRef *msg);
  135. Bool isRoomForDisconnectKeepAliveMessage(NetCommandRef *msg);
  136. Bool addDisconnectPlayerCommand(NetCommandRef *msg);
  137. Bool isRoomForDisconnectPlayerMessage(NetCommandRef *msg);
  138. Bool addPacketRouterQueryCommand(NetCommandRef *msg);
  139. Bool isRoomForPacketRouterQueryMessage(NetCommandRef *msg);
  140. Bool addPacketRouterAckCommand(NetCommandRef *msg);
  141. Bool isRoomForPacketRouterAckMessage(NetCommandRef *msg);
  142. Bool addDisconnectChatCommand(NetCommandRef *msg);
  143. Bool isRoomForDisconnectChatMessage(NetCommandRef *msg);
  144. Bool addChatCommand(NetCommandRef *msg);
  145. Bool isRoomForChatMessage(NetCommandRef *msg);
  146. Bool addDisconnectVoteCommand(NetCommandRef *msg);
  147. Bool isRoomForDisconnectVoteMessage(NetCommandRef *msg);
  148. Bool addProgressMessage( NetCommandRef *msg );
  149. Bool isRoomForProgressMessage( NetCommandRef *msg );
  150. Bool addLoadCompleteMessage( NetCommandRef *msg );
  151. Bool isRoomForLoadCompleteMessage( NetCommandRef *msg );
  152. Bool addTimeOutGameStartMessage( NetCommandRef *msg );
  153. Bool isRoomForTimeOutGameStartMessage( NetCommandRef *msg );
  154. Bool addWrapperCommand(NetCommandRef *msg);
  155. Bool isRoomForWrapperMessage(NetCommandRef *msg);
  156. Bool addFileCommand(NetCommandRef *msg);
  157. Bool isRoomForFileMessage(NetCommandRef *msg);
  158. Bool addFileAnnounceCommand(NetCommandRef *msg);
  159. Bool isRoomForFileAnnounceMessage(NetCommandRef *msg);
  160. Bool addFileProgressCommand(NetCommandRef *msg);
  161. Bool isRoomForFileProgressMessage(NetCommandRef *msg);
  162. Bool addDisconnectFrameCommand(NetCommandRef *msg);
  163. Bool isRoomForDisconnectFrameMessage(NetCommandRef *msg);
  164. Bool addDisconnectScreenOffCommand(NetCommandRef *msg);
  165. Bool isRoomForDisconnectScreenOffMessage(NetCommandRef *msg);
  166. Bool addFrameResendRequestCommand(NetCommandRef *msg);
  167. Bool isRoomForFrameResendRequestMessage(NetCommandRef *msg);
  168. Bool isAckRepeat(NetCommandRef *msg);
  169. Bool isAckBothRepeat(NetCommandRef *msg);
  170. Bool isAckStage1Repeat(NetCommandRef *msg);
  171. Bool isAckStage2Repeat(NetCommandRef *msg);
  172. Bool isFrameRepeat(NetCommandRef *msg);
  173. static NetCommandMsg * readGameMessage(UnsignedByte *data, Int &i);
  174. static NetCommandMsg * readAckBothMessage(UnsignedByte *data, Int &i);
  175. static NetCommandMsg * readAckStage1Message(UnsignedByte *data, Int &i);
  176. static NetCommandMsg * readAckStage2Message(UnsignedByte *data, Int &i);
  177. static NetCommandMsg * readFrameMessage(UnsignedByte *data, Int &i);
  178. static NetCommandMsg * readPlayerLeaveMessage(UnsignedByte *data, Int &i);
  179. static NetCommandMsg * readRunAheadMetricsMessage(UnsignedByte *data, Int &i);
  180. static NetCommandMsg * readRunAheadMessage(UnsignedByte *data, Int &i);
  181. static NetCommandMsg * readDestroyPlayerMessage(UnsignedByte *data, Int &i);
  182. static NetCommandMsg * readKeepAliveMessage(UnsignedByte *data, Int &i);
  183. static NetCommandMsg * readDisconnectKeepAliveMessage(UnsignedByte *data, Int &i);
  184. static NetCommandMsg * readDisconnectPlayerMessage(UnsignedByte *data, Int &i);
  185. static NetCommandMsg * readPacketRouterQueryMessage(UnsignedByte *data, Int &i);
  186. static NetCommandMsg * readPacketRouterAckMessage(UnsignedByte *data, Int &i);
  187. static NetCommandMsg * readDisconnectChatMessage(UnsignedByte *data, Int &i);
  188. static NetCommandMsg * readDisconnectVoteMessage(UnsignedByte *data, Int &i);
  189. static NetCommandMsg * readChatMessage(UnsignedByte *data, Int &i);
  190. static NetCommandMsg * readProgressMessage(UnsignedByte *data, Int &i);
  191. static NetCommandMsg * readLoadCompleteMessage(UnsignedByte *data, Int &i);
  192. static NetCommandMsg * readTimeOutGameStartMessage(UnsignedByte *data, Int &i);
  193. static NetCommandMsg * readWrapperMessage(UnsignedByte *data, Int &i);
  194. static NetCommandMsg * readFileMessage(UnsignedByte *data, Int &i);
  195. static NetCommandMsg * readFileAnnounceMessage(UnsignedByte *data, Int &i);
  196. static NetCommandMsg * readFileProgressMessage(UnsignedByte *data, Int &i);
  197. static NetCommandMsg * readDisconnectFrameMessage(UnsignedByte *data, Int &i);
  198. static NetCommandMsg * readDisconnectScreenOffMessage(UnsignedByte *data, Int &i);
  199. static NetCommandMsg * readFrameResendRequestMessage(UnsignedByte *data, Int &i);
  200. void writeGameMessageArgumentToPacket(GameMessageArgumentDataType type, GameMessageArgumentType arg);
  201. static void readGameMessageArgumentFromPacket(GameMessageArgumentDataType type, NetGameCommandMsg *msg, UnsignedByte *data, Int &i);
  202. void dumpPacketToLog();
  203. protected:
  204. UnsignedByte m_packet[MAX_PACKET_SIZE];
  205. Int m_packetLen;
  206. UnsignedInt m_addr;
  207. Int m_numCommands;
  208. NetCommandRef* m_lastCommand;
  209. UnsignedInt m_lastFrame;
  210. UnsignedShort m_port;
  211. UnsignedShort m_lastCommandID;
  212. UnsignedByte m_lastPlayerID;
  213. UnsignedByte m_lastCommandType;
  214. UnsignedByte m_lastRelay;
  215. };
  216. #endif // __NETPACKET_H