ReplicationState.h 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  1. //
  2. // Copyright (c) 2008-2017 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 Atomic
  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 ATOMIC_API DirtyBits
  42. {
  43. /// Construct empty.
  44. DirtyBits() :
  45. count_(0)
  46. {
  47. memset(data_, 0, MAX_NETWORK_ATTRIBUTES / 8);
  48. }
  49. /// Copy-construct.
  50. DirtyBits(const DirtyBits& bits) :
  51. count_(bits.count_)
  52. {
  53. memcpy(data_, bits.data_, MAX_NETWORK_ATTRIBUTES / 8);
  54. }
  55. /// Set a bit.
  56. void Set(unsigned index)
  57. {
  58. if (index < MAX_NETWORK_ATTRIBUTES)
  59. {
  60. unsigned byteIndex = index >> 3;
  61. unsigned bit = (unsigned)(1 << (index & 7));
  62. if ((data_[byteIndex] & bit) == 0)
  63. {
  64. data_[byteIndex] |= bit;
  65. ++count_;
  66. }
  67. }
  68. }
  69. /// Clear a bit.
  70. void Clear(unsigned index)
  71. {
  72. if (index < MAX_NETWORK_ATTRIBUTES)
  73. {
  74. unsigned byteIndex = index >> 3;
  75. unsigned bit = (unsigned)(1 << (index & 7));
  76. if ((data_[byteIndex] & bit) != 0)
  77. {
  78. data_[byteIndex] &= ~bit;
  79. --count_;
  80. }
  81. }
  82. }
  83. /// Clear all bits.
  84. void ClearAll()
  85. {
  86. memset(data_, 0, MAX_NETWORK_ATTRIBUTES / 8);
  87. count_ = 0;
  88. }
  89. /// Return if bit is set.
  90. bool IsSet(unsigned index) const
  91. {
  92. if (index < MAX_NETWORK_ATTRIBUTES)
  93. {
  94. unsigned byteIndex = index >> 3;
  95. unsigned bit = (unsigned)(1 << (index & 7));
  96. return (data_[byteIndex] & bit) != 0;
  97. }
  98. else
  99. return false;
  100. }
  101. /// Return number of set bits.
  102. unsigned Count() const { return count_; }
  103. /// Bit data.
  104. unsigned char data_[MAX_NETWORK_ATTRIBUTES / 8];
  105. /// Number of set bits.
  106. unsigned char count_;
  107. };
  108. /// Per-object attribute state for network replication, allocated on demand.
  109. struct ATOMIC_API NetworkState
  110. {
  111. /// Construct with defaults.
  112. NetworkState() :
  113. interceptMask_(0)
  114. {
  115. }
  116. /// Cached network attribute infos.
  117. const Vector<AttributeInfo>* attributes_;
  118. /// Current network attribute values.
  119. Vector<Variant> currentValues_;
  120. /// Previous network attribute values.
  121. Vector<Variant> previousValues_;
  122. /// Replication states that are tracking this object.
  123. PODVector<ReplicationState*> replicationStates_;
  124. /// Previous user variables.
  125. VariantMap previousVars_;
  126. /// Bitmask for intercepting network messages. Used on the client only.
  127. unsigned long long interceptMask_;
  128. };
  129. /// Base class for per-user network replication states.
  130. struct ATOMIC_API ReplicationState
  131. {
  132. /// Parent network connection.
  133. Connection* connection_;
  134. };
  135. /// Per-user component network replication state.
  136. struct ATOMIC_API ComponentReplicationState : public ReplicationState
  137. {
  138. /// Parent node replication state.
  139. NodeReplicationState* nodeState_;
  140. /// Link to the actual component.
  141. WeakPtr<Component> component_;
  142. /// Dirty attribute bits.
  143. DirtyBits dirtyAttributes_;
  144. };
  145. /// Per-user node network replication state.
  146. struct ATOMIC_API NodeReplicationState : public ReplicationState
  147. {
  148. /// Construct.
  149. NodeReplicationState() :
  150. ReplicationState(),
  151. priorityAcc_(0.0f),
  152. markedDirty_(false)
  153. {
  154. }
  155. /// Parent scene replication state.
  156. SceneReplicationState* sceneState_;
  157. /// Link to the actual node.
  158. WeakPtr<Node> node_;
  159. /// Dirty attribute bits.
  160. DirtyBits dirtyAttributes_;
  161. /// Dirty user vars.
  162. HashSet<StringHash> dirtyVars_;
  163. /// Components by ID.
  164. HashMap<unsigned, ComponentReplicationState> componentStates_;
  165. /// Interest management priority accumulator.
  166. float priorityAcc_;
  167. /// Whether exists in the SceneState's dirty set.
  168. bool markedDirty_;
  169. };
  170. /// Per-user scene network replication state.
  171. struct ATOMIC_API SceneReplicationState : public ReplicationState
  172. {
  173. /// Nodes by ID.
  174. HashMap<unsigned, NodeReplicationState> nodeStates_;
  175. /// Dirty node IDs.
  176. HashSet<unsigned> dirtyNodes_;
  177. void Clear()
  178. {
  179. nodeStates_.Clear();
  180. dirtyNodes_.Clear();
  181. }
  182. };
  183. }