EntityUpdates.cs 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  1. /*
  2. * Copyright (c) Contributors, http://opensimulator.org/
  3. * See CONTRIBUTORS.TXT for a full list of copyright holders.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. * * Redistributions of source code must retain the above copyright
  8. * notice, this list of conditions and the following disclaimer.
  9. * * Redistributions in binary form must reproduce the above copyright
  10. * notice, this list of conditions and the following disclaimer in the
  11. * documentation and/or other materials provided with the distribution.
  12. * * Neither the name of the OpenSimulator Project nor the
  13. * names of its contributors may be used to endorse or promote products
  14. * derived from this software without specific prior written permission.
  15. *
  16. * THIS SOFTWARE IS PROVIDED BY THE DEVELOPERS ``AS IS'' AND ANY
  17. * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  18. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  19. * DISCLAIMED. IN NO EVENT SHALL THE CONTRIBUTORS BE LIABLE FOR ANY
  20. * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  21. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  22. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  23. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  24. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  25. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  26. */
  27. using System;
  28. using System.Collections.Generic;
  29. using OpenSim.Framework;
  30. namespace OpenSim.Region.Framework.Scenes
  31. {
  32. /// <summary>
  33. /// Specifies the fields that have been changed when sending a prim or
  34. /// avatar update
  35. /// </summary>
  36. [Flags]
  37. public enum ObjectPropertyUpdateFlags : byte
  38. {
  39. None = 0,
  40. Family = 1,
  41. Object = 2,
  42. NoFamily = unchecked((byte)~Family),
  43. NoObject = unchecked((byte)~Object)
  44. }
  45. public class EntityUpdate
  46. {
  47. // for priority queue
  48. public int PriorityQueue;
  49. public int PriorityQueueIndex;
  50. public ulong EntryOrder;
  51. private ISceneEntity m_entity;
  52. private PrimUpdateFlags m_flags;
  53. public ObjectPropertyUpdateFlags m_propsFlags;
  54. public ObjectPropertyUpdateFlags PropsFlags
  55. {
  56. get
  57. {
  58. return m_propsFlags;
  59. }
  60. set
  61. {
  62. m_propsFlags = value;
  63. }
  64. }
  65. public ISceneEntity Entity
  66. {
  67. get
  68. {
  69. return m_entity;
  70. }
  71. internal set
  72. {
  73. m_entity = value;
  74. }
  75. }
  76. public PrimUpdateFlags Flags
  77. {
  78. get { return m_flags; }
  79. set { m_flags = value; }
  80. }
  81. [System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.AggressiveInlining)]
  82. public void Update(int pqueue, ulong entry)
  83. {
  84. if ((m_flags & PrimUpdateFlags.CancelKill) != 0)
  85. {
  86. if ((m_flags & PrimUpdateFlags.UpdateProbe) != 0)
  87. m_flags = PrimUpdateFlags.UpdateProbe;
  88. else
  89. m_flags = PrimUpdateFlags.FullUpdatewithAnim;
  90. }
  91. PriorityQueue = pqueue;
  92. EntryOrder = entry;
  93. }
  94. [System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.AggressiveInlining)]
  95. public void UpdateFromNew(EntityUpdate newupdate, int pqueue)
  96. {
  97. m_propsFlags |= newupdate.PropsFlags;
  98. PrimUpdateFlags newFlags = newupdate.Flags;
  99. if ((newFlags & PrimUpdateFlags.UpdateProbe) != 0)
  100. m_flags &= ~PrimUpdateFlags.UpdateProbe;
  101. if ((newFlags & PrimUpdateFlags.CancelKill) != 0)
  102. {
  103. if ((newFlags & PrimUpdateFlags.UpdateProbe) != 0)
  104. m_flags = PrimUpdateFlags.UpdateProbe;
  105. else
  106. newFlags = PrimUpdateFlags.FullUpdatewithAnim;
  107. }
  108. else
  109. m_flags |= newFlags;
  110. PriorityQueue = pqueue;
  111. }
  112. [System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.AggressiveInlining)]
  113. public void Free()
  114. {
  115. m_entity = null;
  116. PriorityQueueIndex = -1;
  117. EntityUpdatesPool.Free(this);
  118. }
  119. public EntityUpdate(ISceneEntity entity, PrimUpdateFlags flags)
  120. {
  121. m_entity = entity;
  122. m_flags = flags;
  123. }
  124. public EntityUpdate(ISceneEntity entity, PrimUpdateFlags flags, bool sendfam, bool sendobj)
  125. {
  126. m_entity = entity;
  127. m_flags = flags;
  128. if (sendfam)
  129. m_propsFlags |= ObjectPropertyUpdateFlags.Family;
  130. if (sendobj)
  131. m_propsFlags |= ObjectPropertyUpdateFlags.Object;
  132. }
  133. public override string ToString()
  134. {
  135. return String.Format("[{0},{1},{2}]", PriorityQueue, EntryOrder, m_entity.LocalId);
  136. }
  137. }
  138. public static class EntityUpdatesPool
  139. {
  140. const int MAXSIZE = 32768;
  141. const int PREALLOC = 16384;
  142. private static readonly EntityUpdate[] m_pool = new EntityUpdate[MAXSIZE];
  143. private static readonly object m_poollock = new object();
  144. private static int m_poolPtr;
  145. //private static int m_min = int.MaxValue;
  146. //private static int m_max = int.MinValue;
  147. static EntityUpdatesPool()
  148. {
  149. for(int i = 0; i < PREALLOC; ++i)
  150. m_pool[i] = new EntityUpdate(null, 0);
  151. m_poolPtr = PREALLOC - 1;
  152. }
  153. public static EntityUpdate Get(ISceneEntity entity, PrimUpdateFlags flags)
  154. {
  155. lock (m_poollock)
  156. {
  157. if (m_poolPtr >= 0)
  158. {
  159. EntityUpdate eu = m_pool[m_poolPtr];
  160. m_pool[m_poolPtr] = null;
  161. m_poolPtr--;
  162. //if (m_min > m_poolPtr)
  163. // m_min = m_poolPtr;
  164. eu.Entity = entity;
  165. eu.Flags = flags;
  166. return eu;
  167. }
  168. }
  169. return new EntityUpdate(entity, flags);
  170. }
  171. public static EntityUpdate Get(ISceneEntity entity, PrimUpdateFlags flags, bool sendfam, bool sendobj)
  172. {
  173. lock (m_poollock)
  174. {
  175. if (m_poolPtr >= 0)
  176. {
  177. EntityUpdate eu = m_pool[m_poolPtr];
  178. m_pool[m_poolPtr] = null;
  179. m_poolPtr--;
  180. //if (m_min > m_poolPtr)
  181. // m_min = m_poolPtr;
  182. eu.Entity = entity;
  183. eu.Flags = flags;
  184. ObjectPropertyUpdateFlags tmp = 0;
  185. if (sendfam)
  186. tmp |= ObjectPropertyUpdateFlags.Family;
  187. if (sendobj)
  188. tmp |= ObjectPropertyUpdateFlags.Object;
  189. eu.PropsFlags = tmp;
  190. return eu;
  191. }
  192. }
  193. return new EntityUpdate(entity, flags, sendfam, sendobj);
  194. }
  195. public static void Free(EntityUpdate eu)
  196. {
  197. lock (m_poollock)
  198. {
  199. if (m_poolPtr < MAXSIZE - 1)
  200. {
  201. m_poolPtr++;
  202. //if (m_max < m_poolPtr)
  203. // m_max = m_poolPtr;
  204. m_pool[m_poolPtr] = eu;
  205. }
  206. }
  207. }
  208. }
  209. }