NetworkMessage.h 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. /* Copyright The kNet Project.
  2. Licensed under the Apache License, Version 2.0 (the "License");
  3. you may not use this file except in compliance with the License.
  4. You may obtain a copy of the License at
  5. http://www.apache.org/licenses/LICENSE-2.0
  6. Unless required by applicable law or agreed to in writing, software
  7. distributed under the License is distributed on an "AS IS" BASIS,
  8. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  9. See the License for the specific language governing permissions and
  10. limitations under the License. */
  11. #pragma once
  12. /** @file NetworkMessage.h
  13. @brief The class NetworkMessage. Stores an outbound network message. */
  14. #include "kNetBuildConfig.h"
  15. #include "LockFreePoolAllocator.h"
  16. #include "FragmentedTransferManager.h"
  17. #include "Types.h"
  18. namespace kNet
  19. {
  20. /// Performs modular arithmetic comparison to see if newID refers to a PacketID that is *strictly* newer than oldID.
  21. /// @return True if newID is *strictly* newer than oldID, false otherwise.
  22. inline bool PacketIDIsNewerThan(packet_id_t newID, packet_id_t oldID)
  23. {
  24. packet_id_t diff = (packet_id_t)(newID - oldID);
  25. packet_id_t diff2 = (packet_id_t)(newID + 0x3FFFFF - oldID);
  26. return diff < 0x1FFFFF || diff2 < 0x1FFFFF;
  27. }
  28. /// Computes the PacketID for the packet (id + increment).
  29. inline packet_id_t AddPacketID(packet_id_t id, int increment)
  30. {
  31. return (id + increment) & ((1 << 22) - 1);
  32. }
  33. /// Computes the PacketID for the packet (id - sub).
  34. inline packet_id_t SubPacketID(packet_id_t id, int sub)
  35. {
  36. if ((int)id >= sub)
  37. return (packet_id_t)(id - sub);
  38. else
  39. return (packet_id_t)((1 << 22) - (sub-id));
  40. }
  41. /// NetworkMessage stores the serialized byte data of a single outbound network message, along
  42. /// with fields that specify how itreated by the network connection.
  43. class NetworkMessage : public PoolAllocatable<NetworkMessage>
  44. {
  45. public:
  46. /// To create a NetworkMessage, call MessageConnection::StartNewMessage() instead of directly instantiating
  47. /// a message structure. This is because each MessageConnection implements an internal pool of NetworkMessage
  48. /// structures which are reused between messages, to avoid excessive dynamic memory allocation.
  49. NetworkMessage();
  50. NetworkMessage &operator=(const NetworkMessage &rhs);
  51. NetworkMessage(const NetworkMessage &rhs);
  52. ~NetworkMessage();
  53. /// Stores the actual data of the message.
  54. /// When writing a new message, fill in the data bytes here. This buffer can hold Capacity() amount of bytes. If you need more,
  55. /// call Reallocate() with the desired amount of bytes.
  56. /// This field is read-only, do not change this pointer value.
  57. char *data;
  58. size_t Capacity() const { return dataCapacity; }
  59. size_t Size() const { return dataSize; }
  60. /// Resizes the message to the given amount of bytes. Reallocates the data buffer if it cannot fit into the new size.
  61. /// @param newBytes The new amount of bytes to use for the message.
  62. /// @param discard If true, any existing partially filled data will not be copied over, but the new buffer will have its
  63. /// bytes left uninitialized. If false, this function preserves any old partially filled data and works
  64. /// like std::vector::resize(). The intended purpose of this function is to be called prior to filling
  65. /// in any data, and so the default value is true.
  66. void Resize(size_t newBytes, bool discard = true);
  67. /// The send priority of this message with respect to other messages. Priority 0 is the lowest, and
  68. /// priority 0xFFFFFFFE is the highest. Priority 0xFFFFFFFF is a special one that means 'don't send this message'.
  69. unsigned long priority;
  70. /// The ID of this message. IDs 0 - 5 are reserved for the protocol and may not be used.
  71. /// Valid user range is [6, 1073741821 == 0x3FFFFFFD].
  72. message_id_t id;
  73. /// When sending out a message, the application can attach a content ID to the message,
  74. /// which will effectively replace all the older messages with the same messageID and
  75. /// contentID.
  76. unsigned long contentID;
  77. /// If true, this message should be delivered reliably, possibly resent to guarantee
  78. /// that the receiving party gets it.
  79. bool reliable;
  80. /// If true, this message should be delivered in-order with all the other in-order
  81. /// messages. The processing order of this message relative to non-in-ordered messages
  82. /// is not specified and can vary.
  83. bool inOrder;
  84. /// If this flag is set, the message will not be sent and will be deleted as soon
  85. /// as possible. It has been superceded by another message before it had the time
  86. /// to leave the outbound send queue.
  87. bool obsolete;
  88. #ifdef KNET_NETWORK_PROFILING
  89. std::string profilerName;
  90. #endif
  91. /// Checks if this message is newer than the other message.
  92. bool IsNewerThan(const NetworkMessage &rhs) const { return (unsigned long)(messageNumber - rhs.messageNumber) < 0x80000000; }
  93. /// A message with this priority is obsolete and should not be sent through the connection.
  94. static const unsigned long cPriorityDontSend = 0xFFFFFFFF;
  95. /// The maximum priority a message can have.
  96. static const unsigned long cMaxPriority = 0xFFFFFFFE;
  97. /// Returns the total number of bytes this datagram will take up space when it is serialized to an UDP channel.
  98. int GetTotalDatagramPackedSize() const;
  99. /// Returns the number of this message. The message number identifies the admission order of messages to the outbound queue.
  100. unsigned long MessageNumber() const { return messageNumber; }
  101. private:
  102. friend class MessageConnection;
  103. friend class UDPMessageConnection;
  104. friend class TCPMessageConnection;
  105. friend class FragmentedSendManager;
  106. friend struct FragmentedSendManager::FragmentedTransfer;
  107. /// A temporary storage area to remember the UDP packet ID this messages was received in.
  108. /// For TCP messages, this field is always zero.
  109. /// When sending out messages, this field is not used.
  110. packet_id_t receivedPacketID;
  111. /// A running number that is assigned to each message to distinguish the order
  112. /// the messages were added to the queue. The network layer manages this numbering,
  113. /// the application can not control it. This is used to break ties on packets
  114. /// that have the same priority.
  115. unsigned long messageNumber;
  116. /// A running number that is assigned to each reliable message. This is used in the
  117. /// network byte stream to implement ordering of messages.
  118. unsigned long reliableMessageNumber;
  119. /// The number of times this message has been sent and not been acked (reliable messages only).
  120. unsigned long sendCount;
  121. /// The index of this fragment, or not used (undefined) if totalNumFragments==0.
  122. unsigned long fragmentIndex;
  123. /// Tells the number of bytes that have been allocated and can be filled to the data array.
  124. size_t dataCapacity;
  125. /// Specifies the number of bytes actually used in the data array.
  126. size_t dataSize;
  127. /// If 0, this message is being sent unfragmented. Otherwise, this NetworkMessage is a fragment of the whole
  128. /// message and transfer points to the data structure that tracks the transfer of a fragmented message.
  129. FragmentedSendManager::FragmentedTransfer *transfer;
  130. };
  131. } // ~kNet