ReplicationState.h 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  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 ComponentReplicationState;
  35. struct NodeReplicationState;
  36. struct SceneReplicationState;
  37. /// Dirty attribute bits structure for network replication.
  38. struct DirtyBits
  39. {
  40. /// Construct empty.
  41. DirtyBits() :
  42. count_(0)
  43. {
  44. memset(data_, 0, MAX_NETWORK_ATTRIBUTES / 8);
  45. }
  46. /// Copy-construct.
  47. DirtyBits(const DirtyBits& bits) :
  48. count_(bits.count_)
  49. {
  50. memcpy(data_, bits.data_, MAX_NETWORK_ATTRIBUTES / 8);
  51. }
  52. /// Set a bit.
  53. void Set(unsigned index)
  54. {
  55. if (index < MAX_NETWORK_ATTRIBUTES)
  56. {
  57. unsigned byteIndex = index >> 3;
  58. unsigned bit = 1 << (index & 7);
  59. if ((data_[byteIndex] & bit) == 0)
  60. {
  61. data_[byteIndex] |= bit;
  62. ++count_;
  63. }
  64. }
  65. }
  66. /// Clear a bit.
  67. void Clear(unsigned index)
  68. {
  69. if (index < MAX_NETWORK_ATTRIBUTES)
  70. {
  71. unsigned byteIndex = index >> 3;
  72. unsigned bit = 1 << (index & 7);
  73. if ((data_[byteIndex] & bit) != 0)
  74. {
  75. data_[byteIndex] &= ~bit;
  76. --count_;
  77. }
  78. }
  79. }
  80. /// Clear all bits.
  81. void ClearAll()
  82. {
  83. memset(data_, 0, MAX_NETWORK_ATTRIBUTES / 8);
  84. count_ = 0;
  85. }
  86. /// Return if bit is set.
  87. bool IsSet(unsigned index) const
  88. {
  89. if (index < MAX_NETWORK_ATTRIBUTES)
  90. {
  91. unsigned byteIndex = index >> 3;
  92. unsigned bit = 1 << (index & 7);
  93. return (data_[byteIndex] & bit) != 0;
  94. }
  95. else
  96. return false;
  97. }
  98. /// Return number of set bits.
  99. unsigned Count() const { return count_; }
  100. /// Bit data.
  101. unsigned char data_[MAX_NETWORK_ATTRIBUTES / 8];
  102. /// Number of set bits.
  103. unsigned char count_;
  104. };
  105. /// Per-user component network replication state.
  106. struct ComponentReplicationState
  107. {
  108. /// Parent network connection.
  109. Connection* connection_;
  110. /// Parent node replication state.
  111. NodeReplicationState* nodeState_;
  112. /// Link to the actual component.
  113. WeakPtr<Component> component_;
  114. /// Dirty attribute bits.
  115. DirtyBits dirtyAttributes_;
  116. };
  117. /// Last sent state of a node for network replication.
  118. struct NodeReplicationState
  119. {
  120. /// Construct.
  121. NodeReplicationState() :
  122. priorityAcc_(0.0f),
  123. markedDirty_(false)
  124. {
  125. }
  126. /// Parent network connection.
  127. Connection* connection_;
  128. /// Parent scene replication state.
  129. SceneReplicationState* sceneState_;
  130. /// Link to the actual node.
  131. WeakPtr<Node> node_;
  132. /// Dirty attribute bits.
  133. DirtyBits dirtyAttributes_;
  134. /// Dirty user vars.
  135. HashSet<ShortStringHash> dirtyVars_;
  136. /// Components by ID.
  137. HashMap<unsigned, ComponentReplicationState> componentStates_;
  138. /// Interest management priority accumulator.
  139. float priorityAcc_;
  140. /// Whether exists in the SceneState's dirty set.
  141. bool markedDirty_;
  142. };
  143. /// Per-user scene network replication state.
  144. struct SceneReplicationState
  145. {
  146. /// Parent network connection.
  147. Connection* connection_;
  148. /// Nodes by ID.
  149. HashMap<unsigned, NodeReplicationState> nodeStates_;
  150. /// Dirty node IDs.
  151. HashSet<unsigned> dirtyNodes_;
  152. void Clear()
  153. {
  154. nodeStates_.Clear();
  155. dirtyNodes_.Clear();
  156. }
  157. };