DisconnectManager.h 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. /*
  2. ** Command & Conquer Generals Zero Hour(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. #pragma once
  24. #ifndef __DISCONNECTMANAGER_H
  25. #define __DISCONNECTMANAGER_H
  26. #include "GameNetwork/NetCommandRef.h"
  27. #include "Lib/BaseType.h"
  28. //#include "GameNetwork/ConnectionManager.h"
  29. enum DisconnectStateType {
  30. DISCONNECTSTATETYPE_SCREENON,
  31. DISCONNECTSTATETYPE_SCREENOFF
  32. // DISCONNECTSTATETYPE_WAITINGFORPACKETROUTER
  33. };
  34. class ConnectionManager;
  35. struct DisconnectVoteStruct {
  36. Bool vote;
  37. UnsignedInt frame;
  38. };
  39. class DisconnectManager
  40. {
  41. public:
  42. DisconnectManager();
  43. virtual ~DisconnectManager();
  44. void init();
  45. void update(ConnectionManager *conMgr);
  46. void processDisconnectCommand(NetCommandRef *ref, ConnectionManager *conMgr);
  47. void allCommandsReady(UnsignedInt frame, ConnectionManager *conMgr, Bool waitForPacketRouter = TRUE);
  48. void nextFrame(UnsignedInt frame, ConnectionManager *conMgr);
  49. Bool allowedToContinue(); ///< Allow the next frame to go through?
  50. void playerHasAdvancedAFrame(Int slot, UnsignedInt frame); ///< this player has advanced to that frame.
  51. void voteForPlayerDisconnect(Int slot, ConnectionManager *conMgr);
  52. // For disconnect blame assignment
  53. UnsignedInt getPingFrame();
  54. Int getPingsSent();
  55. Int getPingsRecieved();
  56. protected:
  57. void sendKeepAlive(ConnectionManager *conMgr);
  58. void populateDisconnectScreen(ConnectionManager *conMgr);
  59. Int translatedSlotPosition(Int slot, Int localSlot);
  60. Int untranslatedSlotPosition(Int slot, Int localSlot);
  61. void resetPlayerTimeouts(ConnectionManager *conMgr);
  62. void resetPlayerTimeout(Int slot);
  63. void resetPacketRouterTimeout();
  64. void updateDisconnectStatus(ConnectionManager *conMgr);
  65. void disconnectPlayer(Int slot, ConnectionManager *conMgr);
  66. void sendDisconnectCommand(Int slot, ConnectionManager *conMgr);
  67. void sendVoteCommand(Int slot, ConnectionManager *conMgr);
  68. void updateWaitForPacketRouter(ConnectionManager *conMgr);
  69. void recalculatePacketRouterIndex(ConnectionManager *conMgr);
  70. Bool allOnSameFrame(ConnectionManager *conMgr); ///< returns true if all players are stuck on the same frame.
  71. Bool isLocalPlayerNextPacketRouter(ConnectionManager *conMgr); ///< returns true if the local player is next in line to be the packet router with all the players that have timed out being taken out of the picture.
  72. Bool hasPlayerTimedOut(Int slot); ///< returns true if this player has timed out.
  73. void sendPlayerDestruct(Int slot, ConnectionManager *conMgr); ///< send a destruct player network message.
  74. Bool isPlayerVotedOut(Int slot, ConnectionManager *conMgr); ///< returns true if this player has been voted out.
  75. Bool isPlayerInGame(Int slot, ConnectionManager *conMgr); ///< returns true if the player has neither timed out or been voted out.
  76. UnsignedInt getMaxDisconnectFrame(); ///< returns the highest frame that people have got to.
  77. Int countVotesForPlayer(Int slot); ///< return the number of disconnect votes a player has.
  78. void resetPlayersVotes(Int playerID, UnsignedInt frame, ConnectionManager *conMgr); ///< reset the votes for this player.
  79. void turnOnScreen(ConnectionManager *conMgr); ///< This gets called when the disconnect screen is first turned on.
  80. void processDisconnectKeepAlive(NetCommandMsg *msg, ConnectionManager *conMgr);
  81. void processDisconnectPlayer(NetCommandMsg *msg, ConnectionManager *conMgr);
  82. void processPacketRouterQuery(NetCommandMsg *msg, ConnectionManager *conMgr);
  83. void processPacketRouterAck(NetCommandMsg *msg, ConnectionManager *conMgr);
  84. void processDisconnectVote(NetCommandMsg *msg, ConnectionManager *conMgr);
  85. void processDisconnectFrame(NetCommandMsg *msg, ConnectionManager *conMgr);
  86. void processDisconnectScreenOff(NetCommandMsg *msg, ConnectionManager *conMgr);
  87. void applyDisconnectVote(Int slot, UnsignedInt frame, Int castingSlot, ConnectionManager *conMgr);
  88. UnsignedInt m_lastFrame;
  89. time_t m_lastFrameTime;
  90. DisconnectStateType m_disconnectState;
  91. UnsignedInt m_packetRouterFallback[MAX_SLOTS];
  92. UnsignedInt m_currentPacketRouterIndex;
  93. time_t m_lastKeepAliveSendTime;
  94. time_t m_playerTimeouts[MAX_SLOTS - 1];
  95. time_t m_packetRouterTimeout;
  96. DisconnectVoteStruct m_playerVotes[MAX_SLOTS][MAX_SLOTS];
  97. // Bool m_myVotes[MAX_SLOTS - 1];
  98. UnsignedInt m_disconnectFrames[MAX_SLOTS];
  99. Bool m_disconnectFramesReceived[MAX_SLOTS];
  100. Bool m_haveNotifiedOtherPlayersOfCurrentFrame;
  101. time_t m_timeOfDisconnectScreenOn;
  102. Int m_pingsSent;
  103. Int m_pingsRecieved;
  104. UnsignedInt m_pingFrame;
  105. };
  106. #endif // #ifndef __DISCONNECTMANAGER_H