ReplicationState.h 5.3 KB

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