ConnectionManager.h 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  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. * The connection manager maintains a collection of Connections to each player. Messages are queued up
  25. * from here, as well as removed through here.
  26. */
  27. #pragma once
  28. #ifndef __CONNECTIONMANAGER_H
  29. #define __CONNECTIONMANAGER_H
  30. #include "GameNetwork/Connection.h"
  31. #include "GameNetwork/NetCommandList.h"
  32. #include "GameNetwork/Transport.h"
  33. #include "GameNetwork/FrameDataManager.h"
  34. #include "GameNetwork/FrameMetrics.h"
  35. #include "GameNetwork/NetworkDefs.h"
  36. #include "GameNetwork/DisconnectManager.h"
  37. class GameInfo;
  38. class NetCommandWrapperList;
  39. typedef std::map<UnsignedShort, AsciiString> FileCommandMap;
  40. typedef std::map<UnsignedShort, UnsignedByte> FileMaskMap;
  41. typedef std::map<UnsignedShort, Int> FileProgressMap;
  42. class ConnectionManager
  43. {
  44. public:
  45. ConnectionManager();
  46. ~ConnectionManager();
  47. virtual void init(); ///< Initialize this instance.
  48. virtual void reset(); ///< Take this instance back to the initial state.
  49. virtual void update(Bool isInGame); ///< Service the Connections being managed by this instance.
  50. // End SubsystemInterface functions
  51. void updateRunAhead(Int oldRunAhead, Int frameRate, Bool didSelfSlug, Int nextExecutionFrame); ///< Update the run ahead value. If we are the current packet router, issue the command.
  52. void attachTransport(Transport *transport);
  53. void parseUserList(const GameInfo *game);
  54. void sendChat(UnicodeString text, Int playerMask, UnsignedInt executionFrame);
  55. void sendDisconnectChat(UnicodeString text);
  56. void sendLocalCommand(NetCommandMsg *msg, UnsignedByte relay = 0xff); ///< Send command to the players specified in the relay, goes through packet router.
  57. void sendLocalCommandDirect(NetCommandMsg *msg, UnsignedByte relay); ///< Send command directly to the players specified, doesn't go through packet router.
  58. void sendLocalGameMessage(GameMessage *msg, UnsignedInt frame);
  59. void sendCommand(NetCommandMsg *msg);
  60. Bool allCommandsReady(UnsignedInt frame, Bool justTesting = FALSE);
  61. void handleAllCommandsReady(void);
  62. NetCommandList *getFrameCommandList(UnsignedInt frame);
  63. // void AddConnection(User *user, UnsignedInt slot);
  64. void determineRouterFallbackPlan();
  65. void zeroFrames(UnsignedInt startingFrame, UnsignedInt numFrames);
  66. void destroyGameMessages();
  67. // void createConnections(UnsignedInt numberOfPlayers, UnsignedInt localSlot);
  68. void setLocalAddress(UnsignedInt ip, UnsignedInt port);
  69. void initTransport();
  70. void processFrameTick(UnsignedInt frame);
  71. void handleLocalPlayerLeaving(UnsignedInt frame);
  72. void sendFile(AsciiString path, UnsignedByte playerMask, UnsignedShort commandID);
  73. UnsignedShort sendFileAnnounce(AsciiString path, UnsignedByte playerMask);
  74. Int getFileTransferProgress(Int playerID, AsciiString path);
  75. Bool areAllQueuesEmpty(void);
  76. UnsignedInt getLocalPlayerID();
  77. UnicodeString getPlayerName(Int playerNum);
  78. Int getNumPlayers();
  79. UnsignedInt getPacketRouterFallbackSlot(Int packetRouterNumber); ///< Returns the slot of the given packet router number in the fallback plan.
  80. UnsignedInt getPacketRouterSlot(); ///< Returns the current packet router's slot.
  81. PlayerLeaveCode disconnectPlayer(Int slot); ///< Disconnect this player immediately. This should only be called by the disconnect manager.
  82. void disconnectLocalPlayer(); ///< Does whatever is necessary to get TheNetwork to realize that it should be leaving the game now.
  83. void quitGame(); ///< Disconnect from the game RIGHT NOW!! Tell everyone else we are disconnecting.
  84. void voteForPlayerDisconnect(Int slot); ///< Register a vote for a player to be disconnected.
  85. void resendPendingCommands(); ///< Resend the pending commands to the packet router.
  86. void setFrameGrouping(time_t frameGrouping); ///< Set the number of frames that are grouped together into packets.
  87. PlayerLeaveCode processPlayerLeave(NetPlayerLeaveCommandMsg *msg);
  88. Bool canILeave(); ///< Returns true if the local player is allowed to leave.
  89. // Bandwidth metrics
  90. Real getIncomingBytesPerSecond( void );
  91. Real getIncomingPacketsPerSecond( void );
  92. Real getOutgoingBytesPerSecond( void );
  93. Real getOutgoingPacketsPerSecond( void );
  94. Real getUnknownBytesPerSecond( void );
  95. Real getUnknownPacketsPerSecond( void );
  96. UnsignedInt getPacketArrivalCushion( void );
  97. UnsignedInt getMinimumCushion();
  98. void flushConnections();
  99. void processChat(NetChatCommandMsg *msg); // this actually needs to be public because it is frame-synchronized
  100. void updateLoadProgress( Int progress );
  101. void loadProgressComplete( void );
  102. void sendTimeOutGameStart( void );
  103. Bool isPacketRouter( void );
  104. Bool isPlayerConnected( Int playerID );
  105. void notifyOthersOfCurrentFrame(Int frame);
  106. void sendFrameDataToPlayer(UnsignedInt playerID, UnsignedInt startingFrame);
  107. void sendSingleFrameToPlayer(UnsignedInt playerID, UnsignedInt frame);
  108. void notifyOthersOfNewFrame(UnsignedInt frame);
  109. UnsignedInt getNextPacketRouterSlot(UnsignedInt playerID); ///< returns the packet router player that comes after the given player.
  110. Int getAverageFPS( void );
  111. Int getSlotAverageFPS(Int slot);
  112. #if defined(_DEBUG) || defined(_INTERNAL)
  113. void debugPrintConnectionCommands();
  114. #endif
  115. // For disconnect blame assignment
  116. UnsignedInt getPingFrame();
  117. Int getPingsSent();
  118. Int getPingsRecieved();
  119. private:
  120. void doRelay();
  121. void doKeepAlive();
  122. void sendRemoteCommand(NetCommandRef *msg);
  123. void ackCommand(NetCommandRef *ref, UnsignedInt localSlot);
  124. Bool processNetCommand(NetCommandRef *ref);
  125. void processAckStage1(NetCommandMsg *msg);
  126. void processAckStage2(NetCommandMsg *msg);
  127. void processAck(NetCommandMsg *msg);
  128. void processFrameInfo(NetFrameCommandMsg *msg);
  129. void processRunAheadMetrics(NetRunAheadMetricsCommandMsg *msg);
  130. void processDisconnectChat(NetDisconnectChatCommandMsg *msg);
  131. void processProgress( NetProgressCommandMsg *msg );
  132. void processLoadComplete( NetCommandMsg *msg );
  133. void processTimeOutGameStart( NetCommandMsg *msg );
  134. void processWrapper(NetCommandRef *ref);
  135. void processFrameResendRequest(NetFrameResendRequestCommandMsg *msg);
  136. void processFile(NetFileCommandMsg *ref);
  137. void processFileAnnounce(NetFileAnnounceCommandMsg *ref);
  138. void processFileProgress(NetFileProgressCommandMsg *ref);
  139. // void doPerFrameMetrics(UnsignedInt frame);
  140. void getMinimumFps(Int &minFps, Int &minFpsPlayer); ///< Returns the smallest FPS in the m_fpsAverages list.
  141. Real getMaximumLatency(); ///< This actually sums the two biggest latencies in the m_latencyAverages list.
  142. void requestFrameDataResend(Int playerID, UnsignedInt frame); ///< request of this player that he send the specified frame's data.
  143. // The connections are set up like the slot list. The connection corresponding to the local
  144. // player's position in the slot list will be NULL. Connections corresponding to slots that
  145. // do not have a player will also be NULL.
  146. Connection *m_connections[MAX_SLOTS];
  147. Transport *m_transport;
  148. UnsignedInt m_localSlot;
  149. UnsignedInt m_packetRouterSlot;
  150. UnsignedInt m_packetRouterFallback[MAX_SLOTS];
  151. UnsignedInt m_localAddr;
  152. UnsignedInt m_localPort;
  153. User* m_localUser;
  154. DisconnectManager *m_disconnectManager; ///< Controls the disconnect dialog.
  155. FrameDataManager *m_frameData[MAX_SLOTS];
  156. NetCommandList *m_pendingCommands;
  157. NetCommandList *m_relayedCommands;
  158. FrameMetrics m_frameMetrics;
  159. NetCommandWrapperList *m_netCommandWrapperList;
  160. // These variables are used to keep track of the other players' average fps and latency.
  161. // yup.
  162. Real m_latencyAverages[MAX_SLOTS];
  163. Int m_fpsAverages[MAX_SLOTS];
  164. Int m_minFpsPlayer;
  165. Int m_minFps;
  166. UnsignedInt m_smallestPacketArrivalCushion;
  167. Bool m_didSelfSlug;
  168. // -----------------------------------------------------------------------------
  169. FileCommandMap s_fileCommandMap;
  170. FileMaskMap s_fileRecipientMaskMap;
  171. FileProgressMap s_fileProgressMap[MAX_SLOTS];
  172. // -----------------------------------------------------------------------------
  173. };
  174. #endif