SceneViewer.cs 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  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 OpenMetaverse;
  30. using log4net;
  31. using OpenSim.Framework;
  32. using OpenSim.Framework.Client;
  33. using OpenSim.Region.Framework.Interfaces;
  34. using OpenSim.Region.Framework.Scenes.Types;
  35. namespace OpenSim.Region.Framework.Scenes
  36. {
  37. public class SceneViewer : ISceneViewer
  38. {
  39. /// <summary>
  40. /// Is this scene viewer enabled?
  41. /// </summary>
  42. private bool IsEnabled { get; set; }
  43. /// <summary>
  44. /// The scene presence serviced by this viewer.
  45. /// </summary>
  46. protected ScenePresence m_presence;
  47. /// <summary>
  48. /// The queue of parts for which we need to send out updates.
  49. /// </summary>
  50. protected UpdateQueue m_partsUpdateQueue = new UpdateQueue();
  51. /// <summary>
  52. /// The queue of objects for which we need to send out updates.
  53. /// </summary>
  54. protected Queue<SceneObjectGroup> m_pendingObjects;
  55. /// <summary>
  56. /// The last update assocated with a given part update.
  57. /// </summary>
  58. protected Dictionary<UUID, ScenePartUpdate> m_updateTimes = new Dictionary<UUID, ScenePartUpdate>();
  59. public SceneViewer(ScenePresence presence)
  60. {
  61. m_presence = presence;
  62. IsEnabled = true;
  63. }
  64. public void QueuePartForUpdate(SceneObjectPart part)
  65. {
  66. if (!IsEnabled)
  67. return;
  68. lock (m_partsUpdateQueue)
  69. {
  70. m_partsUpdateQueue.Enqueue(part);
  71. }
  72. }
  73. public void SendPrimUpdates()
  74. {
  75. if (m_pendingObjects == null)
  76. {
  77. if (!m_presence.IsChildAgent || (m_presence.Scene.m_seeIntoRegionFromNeighbor))
  78. {
  79. m_pendingObjects = new Queue<SceneObjectGroup>();
  80. lock (m_pendingObjects)
  81. {
  82. EntityBase[] entities = m_presence.Scene.Entities.GetEntities();
  83. foreach (EntityBase e in entities)
  84. {
  85. if (e != null && e is SceneObjectGroup)
  86. m_pendingObjects.Enqueue((SceneObjectGroup)e);
  87. }
  88. }
  89. }
  90. }
  91. lock (m_pendingObjects)
  92. {
  93. // We must do this under lock so that we don't suffer a race condition if another thread closes the
  94. // viewer
  95. if (!IsEnabled)
  96. return;
  97. while (m_pendingObjects != null && m_pendingObjects.Count > 0)
  98. {
  99. SceneObjectGroup g = m_pendingObjects.Dequeue();
  100. // Yes, this can really happen
  101. if (g == null)
  102. continue;
  103. // This is where we should check for draw distance
  104. // do culling and stuff. Problem with that is that until
  105. // we recheck in movement, that won't work right.
  106. // So it's not implemented now.
  107. //
  108. // Don't even queue if we have sent this one
  109. //
  110. if (!m_updateTimes.ContainsKey(g.UUID))
  111. g.ScheduleFullUpdateToAvatar(m_presence);
  112. }
  113. while (m_partsUpdateQueue.Count > 0)
  114. {
  115. SceneObjectPart part = m_partsUpdateQueue.Dequeue();
  116. if (part.ParentGroup.IsDeleted)
  117. continue;
  118. if (m_updateTimes.ContainsKey(part.UUID))
  119. {
  120. ScenePartUpdate update = m_updateTimes[part.UUID];
  121. // We deal with the possibility that two updates occur at
  122. // the same unix time at the update point itself.
  123. if ((update.LastFullUpdateTime < part.TimeStampFull) || part.ParentGroup.IsAttachment)
  124. {
  125. // m_log.DebugFormat(
  126. // "[SCENE PRESENCE]: Fully updating prim {0}, {1} - part timestamp {2}",
  127. // part.Name, part.UUID, part.TimeStampFull);
  128. part.SendFullUpdate(m_presence.ControllingClient,
  129. m_presence.GenerateClientFlags(part.UUID));
  130. // We'll update to the part's timestamp rather than
  131. // the current time to avoid the race condition
  132. // whereby the next tick occurs while we are doing
  133. // this update. If this happened, then subsequent
  134. // updates which occurred on the same tick or the
  135. // next tick of the last update would be ignored.
  136. update.LastFullUpdateTime = part.TimeStampFull;
  137. }
  138. else if (update.LastTerseUpdateTime <= part.TimeStampTerse)
  139. {
  140. // m_log.DebugFormat(
  141. // "[SCENE PRESENCE]: Tersely updating prim {0}, {1} - part timestamp {2}",
  142. // part.Name, part.UUID, part.TimeStampTerse);
  143. part.SendTerseUpdateToClient(m_presence.ControllingClient);
  144. update.LastTerseUpdateTime = part.TimeStampTerse;
  145. }
  146. }
  147. else
  148. {
  149. //never been sent to client before so do full update
  150. ScenePartUpdate update = new ScenePartUpdate();
  151. update.FullID = part.UUID;
  152. update.LastFullUpdateTime = part.TimeStampFull;
  153. m_updateTimes.Add(part.UUID, update);
  154. // Attachment handling
  155. //
  156. if (part.ParentGroup.RootPart.Shape.PCode == 9 && part.ParentGroup.RootPart.Shape.State != 0)
  157. {
  158. if (part != part.ParentGroup.RootPart)
  159. continue;
  160. part.ParentGroup.SendFullUpdateToClient(m_presence.ControllingClient);
  161. continue;
  162. }
  163. part.SendFullUpdate(m_presence.ControllingClient,
  164. m_presence.GenerateClientFlags(part.UUID));
  165. }
  166. }
  167. }
  168. }
  169. // public void Reset()
  170. // {
  171. // if (m_pendingObjects == null)
  172. // return;
  173. //
  174. // lock (m_pendingObjects)
  175. // {
  176. // if (m_pendingObjects != null)
  177. // {
  178. // m_pendingObjects.Clear();
  179. // m_pendingObjects = null;
  180. // }
  181. // }
  182. // }
  183. public void Close()
  184. {
  185. lock (m_pendingObjects)
  186. {
  187. // We perform this under the m_pendingObjects lock in order to avoid a race condition with another
  188. // thread on SendPrimUpdates()
  189. IsEnabled = false;
  190. lock (m_updateTimes)
  191. {
  192. m_updateTimes.Clear();
  193. }
  194. lock (m_partsUpdateQueue)
  195. {
  196. m_partsUpdateQueue.Clear();
  197. }
  198. }
  199. }
  200. public int GetPendingObjectsCount()
  201. {
  202. if (m_pendingObjects != null)
  203. lock (m_pendingObjects)
  204. return m_pendingObjects.Count;
  205. return 0;
  206. }
  207. public class ScenePartUpdate
  208. {
  209. public UUID FullID;
  210. public uint LastFullUpdateTime;
  211. public uint LastTerseUpdateTime;
  212. public ScenePartUpdate()
  213. {
  214. FullID = UUID.Zero;
  215. LastFullUpdateTime = 0;
  216. LastTerseUpdateTime = 0;
  217. }
  218. }
  219. }
  220. }