ReplicationState.h 5.4 KB

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