Transport.h 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  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. // Transport.h ///////////////////////////////////////////////////////////////
  24. // Transport layer - a thin layer around a UDP socket, with queues.
  25. // Author: Matthew D. Campbell, July 2001
  26. #pragma once
  27. #ifndef _TRANSPORT_H_
  28. #define _TRANSPORT_H_
  29. #include "GameNetwork/udp.h"
  30. #include "GameNetwork/NetworkDefs.h"
  31. /**
  32. * The transport layer handles the UDP socket for the game, and will packetize and
  33. * de-packetize multiple ACK/CommandPacket/etc packets into larger aggregates.
  34. */
  35. // we only ever allocate one of there, and it is quite large, so we really DON'T want
  36. // it to be a MemoryPoolObject (srj)
  37. class Transport //: public MemoryPoolObject
  38. {
  39. //MEMORY_POOL_GLUE_WITH_USERLOOKUP_CREATE(Transport, "Transport")
  40. public:
  41. Transport();
  42. ~Transport();
  43. Bool init( AsciiString ip, UnsignedShort port );
  44. Bool init( UnsignedInt ip, UnsignedShort port );
  45. void reset( void );
  46. Bool update( void ); ///< Call this once a GameEngine tick, regardless of whether the frame advances.
  47. Bool doRecv( void ); ///< call this to service the receive packets
  48. Bool doSend( void ); ///< call this to service the send queue.
  49. Bool queueSend(UnsignedInt addr, UnsignedShort port, const UnsignedByte *buf, Int len /*,
  50. NetMessageFlags flags, Int id */); ///< Queue a packet for sending to the specified address and port. This will be sent on the next update() call.
  51. inline Bool allowBroadcasts(Bool val) { if (!m_udpsock) return false; return (m_udpsock->AllowBroadcasts(val))?true:false; }
  52. // Latency insertion and packet loss
  53. void setLatency( Bool val ) { m_useLatency = val; }
  54. void setPacketLoss( Bool val ) { m_usePacketLoss = val; }
  55. // Bandwidth metrics
  56. Real getIncomingBytesPerSecond( void );
  57. Real getIncomingPacketsPerSecond( void );
  58. Real getOutgoingBytesPerSecond( void );
  59. Real getOutgoingPacketsPerSecond( void );
  60. Real getUnknownBytesPerSecond( void );
  61. Real getUnknownPacketsPerSecond( void );
  62. TransportMessage m_outBuffer[MAX_MESSAGES];
  63. TransportMessage m_inBuffer[MAX_MESSAGES];
  64. #if defined(_DEBUG) || defined(_INTERNAL)
  65. DelayedTransportMessage m_delayedInBuffer[MAX_MESSAGES];
  66. #endif
  67. UnsignedShort m_port;
  68. private:
  69. Bool m_winsockInit;
  70. UDP *m_udpsock;
  71. // Latency insertion and packet loss
  72. Bool m_useLatency;
  73. Bool m_usePacketLoss;
  74. // Bandwidth metrics
  75. UnsignedInt m_incomingBytes[MAX_TRANSPORT_STATISTICS_SECONDS];
  76. UnsignedInt m_unknownBytes[MAX_TRANSPORT_STATISTICS_SECONDS];
  77. UnsignedInt m_outgoingBytes[MAX_TRANSPORT_STATISTICS_SECONDS];
  78. UnsignedInt m_incomingPackets[MAX_TRANSPORT_STATISTICS_SECONDS];
  79. UnsignedInt m_unknownPackets[MAX_TRANSPORT_STATISTICS_SECONDS];
  80. UnsignedInt m_outgoingPackets[MAX_TRANSPORT_STATISTICS_SECONDS];
  81. Int m_statisticsSlot;
  82. UnsignedInt m_lastSecond;
  83. Bool isGeneralsPacket( TransportMessage *msg );
  84. };
  85. #endif // _TRANSPORT_H_