SceneViewer.cs 8.8 KB

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