Packet.h 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. /* Copyright (c) 2002-2012 Croteam Ltd.
  2. This program is free software; you can redistribute it and/or modify
  3. it under the terms of version 2 of the GNU General Public License as published by
  4. the Free Software Foundation
  5. This program is distributed in the hope that it will be useful,
  6. but WITHOUT ANY WARRANTY; without even the implied warranty of
  7. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  8. GNU General Public License for more details.
  9. You should have received a copy of the GNU General Public License along
  10. with this program; if not, write to the Free Software Foundation, Inc.,
  11. 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. */
  12. #ifndef SE_INCL_PACKET_H
  13. #define SE_INCL_PACKET_H
  14. #ifdef PRAGMA_ONCE
  15. #pragma once
  16. #endif
  17. #include <Engine/Base/Lists.h>
  18. #include <Engine/Base/Timer.h>
  19. // The total size of the UDP packet should be below 1450 bytes, to reduce the
  20. // packet drop rate caused by routers dropping large UDP packets. UDP_BLOCK_SIZE is the
  21. // maximum length of real data, not counting the packet header (ulPacketSequence, bReliable,uwID)
  22. #define MAX_UDP_BLOCK_SIZE 1400
  23. #define MAX_HEADER_SIZE (sizeof(UBYTE) + sizeof(ULONG) + sizeof(UWORD) + sizeof(ULONG)) // pa_bReliable + pa_ulPacketSequence + pa_uwID + pa_ulTransferSize
  24. #define MAX_PACKET_SIZE (MAX_UDP_BLOCK_SIZE + MAX_HEADER_SIZE)
  25. // flags for different kinds of packets used by the netcode - note that the acknowledge packets are unreliable
  26. #define UDP_PACKET_UNRELIABLE 0
  27. #define UDP_PACKET_RELIABLE 1
  28. #define UDP_PACKET_RELIABLE_HEAD 2
  29. #define UDP_PACKET_RELIABLE_TAIL 4
  30. #define UDP_PACKET_ACKNOWLEDGE 8
  31. #define UDP_PACKET_CONNECT_REQUEST 16
  32. #define UDP_PACKET_CONNECT_RESPONSE 32
  33. // constants for CPacket::CanRetry() function - they describe the retry state of the packet
  34. #define RS_NOW 0 // the packet should be resent immediately
  35. #define RS_NOTNOW 1 // the packet should be resent at a later time
  36. #define RS_NOTATALL 2 // the packet has reached the maximum number of retries - give up
  37. class CAddress {
  38. public:
  39. ULONG adr_ulAddress; // host address
  40. UWORD adr_uwPort; // host port
  41. UWORD adr_uwID; // host id
  42. void MakeBroadcast(void);
  43. void Clear(void) {
  44. adr_ulAddress = 0;
  45. adr_uwPort = 0;
  46. adr_uwID = 0;
  47. }
  48. };
  49. /*
  50. * A class that contains a single UDP packet.
  51. */
  52. class CPacket {
  53. public:
  54. ULONG pa_ulSequence; // Sequence number of this packet
  55. UBYTE pa_ubReliable; // Is packet reliable or not
  56. SLONG pa_slSize; // Number of data bytes in packet (without header)
  57. SLONG pa_slTransferSize; // Number of data bytes in a data transfer unit this packet belongs to
  58. UBYTE pa_ubRetryNumber; // How many retries so far for this packet
  59. CTimerValue pa_tvSendWhen; // When to try sending this packet (includes latency bandwidth limitations
  60. // as well as retry intervals)
  61. UBYTE pa_pubPacketData[MAX_PACKET_SIZE]; // Packet header + actual data contained in the packet
  62. CListNode pa_lnListNode; // used to create a linked list of packets - buffer
  63. CAddress pa_adrAddress; // packet address, port and client ID
  64. // Constructors/destructors
  65. CPacket() { Clear(); } // Default Constructor
  66. CPacket(CPacket &paOriginal); // Copy constructor
  67. ~CPacket() { Clear(); }
  68. // Reset all packet data and free allocated memory
  69. void Clear();
  70. // Write data to the packet and add header data
  71. BOOL WriteToPacket(void* pv,SLONG slSize,UBYTE ubReliable,ULONG ulSequence,UWORD uwClientID,SLONG slTransferSize);
  72. // Write raw data to the packet and extract header data from the data
  73. BOOL WriteToPacketRaw(void* pv,SLONG slSize);
  74. // Read data from the packet (no header data)
  75. BOOL ReadFromPacket(void* pv,SLONG &slExpectedSize);
  76. // Is packet reliable
  77. BOOL IsReliable();
  78. // Is packet a head of a reliable stream?
  79. BOOL IsReliableHead();
  80. // Is packet a tail of a reliable stream?
  81. BOOL IsReliableTail();
  82. // Is the packet from a broadcast address (pa_uwID not assigned)
  83. BOOL IsBroadcast();
  84. // Get the sequence number of the packet (must be reliable)
  85. ULONG GetSequence();
  86. // What is the current retry status?
  87. UBYTE CanRetry();
  88. // Drop the packet from the list
  89. void Drop();
  90. // get the size of data transfer unit this packet belongs to
  91. SLONG CPacket::GetTransferSize();
  92. // Copy operator
  93. void operator=(const CPacket &paOriginal);
  94. };
  95. // data used to limit bandwidth/lantency and calculate statistics in packet-buffers
  96. class CPacketBufferStats {
  97. public:
  98. FLOAT pbs_fLatencyLimit; // minimum latency in seconds
  99. FLOAT pbs_fLatencyVariation;// additional latency variation
  100. FLOAT pbs_fBandwidthLimit; // maximum bandwidth in bps (bits per second)
  101. CTimerValue pbs_tvTimeNextPacketStart; // next point in time free for data receiving
  102. void Clear(void);
  103. // get time when the packet will be allowed to leave the buffer
  104. CTimerValue GetPacketSendTime(SLONG slSize);
  105. };
  106. class CPacketBuffer {
  107. public:
  108. ULONG pb_ulTotalSize; // Total size of data in packets stored in this buffer (no headers)
  109. ULONG pb_ulLastSequenceOut; // Sequence number of the last packet taken out of the buffer
  110. CListHead pb_lhPacketStorage;
  111. ULONG pb_ulNumOfPackets; // Total number of packets currently in storage
  112. ULONG pb_ulNumOfReliablePackets; // Number of reliable packets in storage (0 if no reliable stream in progress)
  113. CPacketBufferStats *pb_ppbsStats; // for bandwidth/latency emulation stats and limits
  114. CPacketBufferStats pb_pbsLimits; // maximum output BPS for the buffer, to prevent client flooding
  115. CPacketBuffer() { Clear(); };
  116. ~CPacketBuffer() { Clear(); };
  117. // Empty the packet buffer
  118. void Clear();
  119. // Is the packet buffer empty?
  120. BOOL IsEmpty();
  121. // Calculate when the packet can be output from the buffer
  122. CTimerValue GetPacketSendTime(SLONG slSize);
  123. // Adds a packet to the end of the packet buffer
  124. BOOL AppendPacket(CPacket &paPacket,BOOL bDelay);
  125. // Inserts the packet in the buffer, according to it's sequence number
  126. BOOL InsertPacket(CPacket &paPacket,BOOL bDelay);
  127. // Bumps up the retry count and time, and appends the packet to the buffer
  128. BOOL Retry(CPacket &paPacket);
  129. // Reads the data from the first packet in the bufffer, but does not remove it
  130. CPacket* PeekFirstPacket();
  131. // Reads the first packet in the bufffer
  132. CPacket* GetFirstPacket();
  133. // Reads the data from the packet with the requested sequence, but does not remove it
  134. CPacket* PeekPacket(ULONG ulSequence);
  135. // Reads the packet with the requested sequence
  136. CPacket* GetPacket(ULONG ulSequence);
  137. // Reads the first connection request packet from the buffer
  138. CPacket* GetConnectRequestPacket();
  139. // Removes the first packet from the buffer
  140. BOOL RemoveFirstPacket(BOOL bDelete);
  141. // Removes the packet with the requested sequence from the buffer
  142. BOOL RemovePacket(ULONG ulSequence,BOOL bDelete);
  143. // Remove connect response packets from the buffer
  144. BOOL RemoveConnectResponsePackets();
  145. // Gets the sequence number of the first packet in the buffer
  146. ULONG GetFirstSequence();
  147. // Gets the sequence number of the last packet in the buffer
  148. ULONG GetLastSequence();
  149. // Is the packet with the given sequence in the buffer?
  150. BOOL IsSequenceInBuffer(ULONG ulSequence);
  151. // Check if the buffer contains a complete sequence of reliable packets at the start of the buffer
  152. BOOL CheckSequence(SLONG &slSize);
  153. };
  154. #endif /* include-once check. */