ReplicationState.h 5.5 KB

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