ReplicationState.h 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. // Copyright (c) 2008-2023 the Urho3D project
  2. // License: MIT
  3. #pragma once
  4. #include "../Core/Attribute.h"
  5. #include "../Container/HashMap.h"
  6. #include "../Container/HashSet.h"
  7. #include "../Container/Ptr.h"
  8. #include "../Math/StringHash.h"
  9. #include <cstring>
  10. namespace Urho3D
  11. {
  12. static const unsigned MAX_NETWORK_ATTRIBUTES = 64;
  13. class Component;
  14. class Connection;
  15. class Node;
  16. class Scene;
  17. struct ReplicationState;
  18. struct ComponentReplicationState;
  19. struct NodeReplicationState;
  20. struct SceneReplicationState;
  21. /// Dirty attribute bits structure for network replication.
  22. struct URHO3D_API DirtyBits
  23. {
  24. /// Construct empty.
  25. DirtyBits() = default;
  26. /// Copy-construct.
  27. DirtyBits(const DirtyBits& bits) :
  28. count_(bits.count_)
  29. {
  30. memcpy(data_, bits.data_, MAX_NETWORK_ATTRIBUTES / 8);
  31. }
  32. /// Set a bit.
  33. void Set(unsigned index)
  34. {
  35. if (index < MAX_NETWORK_ATTRIBUTES)
  36. {
  37. unsigned byteIndex = index >> 3u;
  38. auto bit = (unsigned)(1u << (index & 7u));
  39. if ((data_[byteIndex] & bit) == 0)
  40. {
  41. data_[byteIndex] |= bit;
  42. ++count_;
  43. }
  44. }
  45. }
  46. /// Clear a bit.
  47. void Clear(unsigned index)
  48. {
  49. if (index < MAX_NETWORK_ATTRIBUTES)
  50. {
  51. unsigned byteIndex = index >> 3u;
  52. auto bit = (unsigned)(1u << (index & 7u));
  53. if ((data_[byteIndex] & bit) != 0)
  54. {
  55. data_[byteIndex] &= ~bit;
  56. --count_;
  57. }
  58. }
  59. }
  60. /// Clear all bits.
  61. void ClearAll()
  62. {
  63. memset(data_, 0, MAX_NETWORK_ATTRIBUTES / 8);
  64. count_ = 0;
  65. }
  66. /// Return if bit is set.
  67. bool IsSet(unsigned index) const
  68. {
  69. if (index < MAX_NETWORK_ATTRIBUTES)
  70. {
  71. unsigned byteIndex = index >> 3u;
  72. auto bit = (unsigned)(1u << (index & 7u));
  73. return (data_[byteIndex] & bit) != 0;
  74. }
  75. else
  76. return false;
  77. }
  78. /// Return number of set bits.
  79. unsigned Count() const { return count_; }
  80. /// Bit data.
  81. unsigned char data_[MAX_NETWORK_ATTRIBUTES / 8]{};
  82. /// Number of set bits.
  83. unsigned char count_{};
  84. };
  85. /// Per-object attribute state for network replication, allocated on demand.
  86. struct URHO3D_API NetworkState
  87. {
  88. /// Cached network attribute infos.
  89. const Vector<AttributeInfo>* attributes_{};
  90. /// Current network attribute values.
  91. Vector<Variant> currentValues_;
  92. /// Previous network attribute values.
  93. Vector<Variant> previousValues_;
  94. /// Replication states that are tracking this object.
  95. Vector<ReplicationState*> replicationStates_;
  96. /// Previous user variables.
  97. VariantMap previousVars_;
  98. /// Bitmask for intercepting network messages. Used on the client only.
  99. unsigned long long interceptMask_{};
  100. };
  101. /// Base class for per-user network replication states.
  102. struct URHO3D_API ReplicationState
  103. {
  104. /// Parent network connection.
  105. Connection* connection_;
  106. };
  107. /// Per-user component network replication state.
  108. struct URHO3D_API ComponentReplicationState : public ReplicationState
  109. {
  110. /// Parent node replication state.
  111. NodeReplicationState* nodeState_{};
  112. /// Link to the actual component.
  113. WeakPtr<Component> component_;
  114. /// Dirty attribute bits.
  115. DirtyBits dirtyAttributes_;
  116. };
  117. /// Per-user node network replication state.
  118. struct URHO3D_API NodeReplicationState : public ReplicationState
  119. {
  120. /// Parent scene replication state.
  121. SceneReplicationState* sceneState_;
  122. /// Link to the actual node.
  123. WeakPtr<Node> node_;
  124. /// Dirty attribute bits.
  125. DirtyBits dirtyAttributes_;
  126. /// Dirty user vars.
  127. HashSet<StringHash> dirtyVars_;
  128. /// Components by ID.
  129. HashMap<unsigned, ComponentReplicationState> componentStates_;
  130. /// Interest management priority accumulator.
  131. float priorityAcc_{};
  132. /// Whether exists in the SceneState's dirty set.
  133. bool markedDirty_{};
  134. };
  135. /// Per-user scene network replication state.
  136. struct URHO3D_API SceneReplicationState : public ReplicationState
  137. {
  138. /// Nodes by ID.
  139. HashMap<unsigned, NodeReplicationState> nodeStates_;
  140. /// Dirty node IDs.
  141. HashSet<unsigned> dirtyNodes_;
  142. void Clear()
  143. {
  144. nodeStates_.Clear();
  145. dirtyNodes_.Clear();
  146. }
  147. };
  148. }