ReplicationState.h 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. //
  2. // Copyright (c) 2008-2020 the Urho3D project.
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy
  5. // of this software and associated documentation files (the "Software"), to deal
  6. // in the Software without restriction, including without limitation the rights
  7. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. // copies of the Software, and to permit persons to whom the Software is
  9. // furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  19. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  20. // THE SOFTWARE.
  21. //
  22. #pragma once
  23. #include "../Core/Attribute.h"
  24. #include "../Container/HashMap.h"
  25. #include "../Container/HashSet.h"
  26. #include "../Container/Ptr.h"
  27. #include "../Math/StringHash.h"
  28. #include <cstring>
  29. namespace Urho3D
  30. {
  31. static const unsigned MAX_NETWORK_ATTRIBUTES = 64;
  32. class Component;
  33. class Connection;
  34. class Node;
  35. class Scene;
  36. struct ReplicationState;
  37. struct ComponentReplicationState;
  38. struct NodeReplicationState;
  39. struct SceneReplicationState;
  40. /// Dirty attribute bits structure for network replication.
  41. struct URHO3D_API DirtyBits
  42. {
  43. /// Construct empty.
  44. DirtyBits() = default;
  45. /// Copy-construct.
  46. DirtyBits(const DirtyBits& bits) :
  47. count_(bits.count_)
  48. {
  49. memcpy(data_, bits.data_, MAX_NETWORK_ATTRIBUTES / 8);
  50. }
  51. /// Set a bit.
  52. void Set(unsigned index)
  53. {
  54. if (index < MAX_NETWORK_ATTRIBUTES)
  55. {
  56. unsigned byteIndex = index >> 3u;
  57. auto bit = (unsigned)(1u << (index & 7u));
  58. if ((data_[byteIndex] & bit) == 0)
  59. {
  60. data_[byteIndex] |= bit;
  61. ++count_;
  62. }
  63. }
  64. }
  65. /// Clear a bit.
  66. void Clear(unsigned index)
  67. {
  68. if (index < MAX_NETWORK_ATTRIBUTES)
  69. {
  70. unsigned byteIndex = index >> 3u;
  71. auto bit = (unsigned)(1u << (index & 7u));
  72. if ((data_[byteIndex] & bit) != 0)
  73. {
  74. data_[byteIndex] &= ~bit;
  75. --count_;
  76. }
  77. }
  78. }
  79. /// Clear all bits.
  80. void ClearAll()
  81. {
  82. memset(data_, 0, MAX_NETWORK_ATTRIBUTES / 8);
  83. count_ = 0;
  84. }
  85. /// Return if bit is set.
  86. bool IsSet(unsigned index) const
  87. {
  88. if (index < MAX_NETWORK_ATTRIBUTES)
  89. {
  90. unsigned byteIndex = index >> 3u;
  91. auto bit = (unsigned)(1u << (index & 7u));
  92. return (data_[byteIndex] & bit) != 0;
  93. }
  94. else
  95. return false;
  96. }
  97. /// Return number of set bits.
  98. unsigned Count() const { return count_; }
  99. /// Bit data.
  100. unsigned char data_[MAX_NETWORK_ATTRIBUTES / 8]{};
  101. /// Number of set bits.
  102. unsigned char count_{};
  103. };
  104. /// Per-object attribute state for network replication, allocated on demand.
  105. struct URHO3D_API NetworkState
  106. {
  107. /// Cached network attribute infos.
  108. const Vector<AttributeInfo>* attributes_{};
  109. /// Current network attribute values.
  110. Vector<Variant> currentValues_;
  111. /// Previous network attribute values.
  112. Vector<Variant> previousValues_;
  113. /// Replication states that are tracking this object.
  114. PODVector<ReplicationState*> replicationStates_;
  115. /// Previous user variables.
  116. VariantMap previousVars_;
  117. /// Bitmask for intercepting network messages. Used on the client only.
  118. unsigned long long interceptMask_{};
  119. };
  120. /// Base class for per-user network replication states.
  121. struct URHO3D_API ReplicationState
  122. {
  123. /// Parent network connection.
  124. Connection* connection_;
  125. };
  126. /// Per-user component network replication state.
  127. struct URHO3D_API ComponentReplicationState : public ReplicationState
  128. {
  129. /// Parent node replication state.
  130. NodeReplicationState* nodeState_{};
  131. /// Link to the actual component.
  132. WeakPtr<Component> component_;
  133. /// Dirty attribute bits.
  134. DirtyBits dirtyAttributes_;
  135. };
  136. /// Per-user node network replication state.
  137. struct URHO3D_API NodeReplicationState : public ReplicationState
  138. {
  139. /// Parent scene replication state.
  140. SceneReplicationState* sceneState_;
  141. /// Link to the actual node.
  142. WeakPtr<Node> node_;
  143. /// Dirty attribute bits.
  144. DirtyBits dirtyAttributes_;
  145. /// Dirty user vars.
  146. HashSet<StringHash> dirtyVars_;
  147. /// Components by ID.
  148. HashMap<unsigned, ComponentReplicationState> componentStates_;
  149. /// Interest management priority accumulator.
  150. float priorityAcc_{};
  151. /// Whether exists in the SceneState's dirty set.
  152. bool markedDirty_{};
  153. };
  154. /// Per-user scene network replication state.
  155. struct URHO3D_API SceneReplicationState : public ReplicationState
  156. {
  157. /// Nodes by ID.
  158. HashMap<unsigned, NodeReplicationState> nodeStates_;
  159. /// Dirty node IDs.
  160. HashSet<unsigned> dirtyNodes_;
  161. void Clear()
  162. {
  163. nodeStates_.Clear();
  164. dirtyNodes_.Clear();
  165. }
  166. };
  167. }