Connection.h 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  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. * The Connection class handles queues for individual players, one connection per player.
  20. * Connections are identified by their names (m_name). This should accomodate changing IPs
  21. * in the face of modem disconnects, NAT irregularities, etc.
  22. * Messages can be guaranteed or non-guaranteed, sequenced or not.
  23. *
  24. *
  25. * So how this works is this:
  26. * The connection contains the incoming and outgoing commands to and from the player this
  27. * connection object represents. The incoming packets are separated into the different frames
  28. * of execution. This is done for efficiency reasons since we will be keeping track of commands
  29. * that are to be executed on many different frames, and they will need to be retrieved by the
  30. * connection manager on a per-frame basis. We don't really care what frame the outgoing
  31. * commands are executed on as they all need to be sent out right away to ensure that the commands
  32. * get to their destination in time.
  33. */
  34. #pragma once
  35. #ifndef __CONNECTION_H
  36. #define __CONNECTION_H
  37. #include "GameNetwork/NetCommandList.h"
  38. #include "GameNetwork/User.h"
  39. #include "GameNetwork/transport.h"
  40. #include "GameNetwork/NetPacket.h"
  41. #define CONNECTION_LATENCY_HISTORY_LENGTH 200
  42. class Connection : public MemoryPoolObject
  43. {
  44. MEMORY_POOL_GLUE_WITH_USERLOOKUP_CREATE(Connection, "Connection")
  45. public:
  46. Connection();
  47. //~Connection();
  48. void update();
  49. void init();
  50. void reset();
  51. UnsignedInt doSend();
  52. void doRecv();
  53. Bool allCommandsReady(UnsignedInt frame);
  54. Bool isQueueEmpty();
  55. void attachTransport(Transport *transport);
  56. void setUser(User *user);
  57. User *getUser();
  58. void setFrameGrouping(time_t frameGrouping);
  59. void sendNetCommandMsg(NetCommandMsg *msg, UnsignedByte relay);
  60. // These two processAck calls do the same thing, just take different types of ACK commands.
  61. NetCommandRef * processAck(NetAckBothCommandMsg *msg);
  62. NetCommandRef * processAck(NetAckStage1CommandMsg *msg);
  63. NetCommandRef * processAck(NetCommandMsg *msg);
  64. NetCommandRef * processAck(UnsignedShort commandID, UnsignedByte originalPlayerID);
  65. void clearCommandsExceptFrom( Int playerIndex );
  66. void setQuitting( void );
  67. Bool isQuitting( void ) { return m_isQuitting; }
  68. #if defined(_DEBUG) || defined(_INTERNAL)
  69. void debugPrintCommands();
  70. #endif
  71. protected:
  72. void doRetryMetrics();
  73. Bool m_isQuitting;
  74. UnsignedInt m_quitTime;
  75. Transport *m_transport;
  76. User *m_user;
  77. NetCommandList *m_netCommandList;
  78. time_t m_retryTime; ///< The time between sending retry packets for this connection. Time is in milliseconds.
  79. Real m_averageLatency; ///< The average time between sending a command and receiving an ACK.
  80. Real m_latencies[CONNECTION_LATENCY_HISTORY_LENGTH]; ///< List of the last 100 latencies.
  81. time_t m_frameGrouping; ///< The minimum time between packet sends.
  82. time_t m_lastTimeSent; ///< The time of the last packet send.
  83. Int m_numRetries; ///< The number of retries for the last second.
  84. time_t m_retryMetricsTime; ///< The start time of the current retry metrics thing.
  85. };
  86. #endif