ReplicationState.h 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. //
  2. // Copyright (c) 2008-2014 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 "Attribute.h"
  24. #include "HashMap.h"
  25. #include "HashSet.h"
  26. #include "Ptr.h"
  27. #include "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() :
  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 = 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 = 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 = 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 URHO3D_API NetworkState
  110. {
  111. /// Cached network attribute infos.
  112. const Vector<AttributeInfo>* attributes_;
  113. /// Current network attribute values.
  114. Vector<Variant> currentValues_;
  115. /// Previous network attribute values.
  116. Vector<Variant> previousValues_;
  117. /// Replication states that are tracking this object.
  118. PODVector<ReplicationState*> replicationStates_;
  119. /// Previous user variables.
  120. VariantMap previousVars_;
  121. };
  122. /// Base class for per-user network replication states.
  123. struct URHO3D_API ReplicationState
  124. {
  125. /// Parent network connection.
  126. Connection* connection_;
  127. };
  128. /// Per-user component network replication state.
  129. struct URHO3D_API ComponentReplicationState : public ReplicationState
  130. {
  131. /// Parent node replication state.
  132. NodeReplicationState* nodeState_;
  133. /// Link to the actual component.
  134. WeakPtr<Component> component_;
  135. /// Dirty attribute bits.
  136. DirtyBits dirtyAttributes_;
  137. };
  138. /// Per-user node network replication state.
  139. struct URHO3D_API NodeReplicationState : public ReplicationState
  140. {
  141. /// Construct.
  142. NodeReplicationState() :
  143. ReplicationState(),
  144. priorityAcc_(0.0f),
  145. markedDirty_(false)
  146. {
  147. }
  148. /// Parent scene replication state.
  149. SceneReplicationState* sceneState_;
  150. /// Link to the actual node.
  151. WeakPtr<Node> node_;
  152. /// Dirty attribute bits.
  153. DirtyBits dirtyAttributes_;
  154. /// Dirty user vars.
  155. HashSet<StringHash> dirtyVars_;
  156. /// Components by ID.
  157. HashMap<unsigned, ComponentReplicationState> componentStates_;
  158. /// Interest management priority accumulator.
  159. float priorityAcc_;
  160. /// Whether exists in the SceneState's dirty set.
  161. bool markedDirty_;
  162. };
  163. /// Per-user scene network replication state.
  164. struct URHO3D_API SceneReplicationState : public ReplicationState
  165. {
  166. /// Nodes by ID.
  167. HashMap<unsigned, NodeReplicationState> nodeStates_;
  168. /// Dirty node IDs.
  169. HashSet<unsigned> dirtyNodes_;
  170. void Clear()
  171. {
  172. nodeStates_.Clear();
  173. dirtyNodes_.Clear();
  174. }
  175. };
  176. }