AsyncSceneObjectGroupDeleter.cs 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  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 System.Reflection;
  30. using System.Timers;
  31. using log4net;
  32. using OpenMetaverse;
  33. using OpenSim.Framework;
  34. using OpenSim.Region.Framework.Interfaces;
  35. namespace OpenSim.Region.Framework.Scenes
  36. {
  37. class DeleteToInventoryHolder
  38. {
  39. public DeRezAction action;
  40. public IClientAPI remoteClient;
  41. public List<SceneObjectGroup> objectGroups;
  42. public UUID folderID;
  43. public bool permissionToDelete;
  44. }
  45. /// <summary>
  46. /// Asynchronously derez objects. This is used to derez large number of objects to inventory without holding
  47. /// up the main client thread.
  48. /// </summary>
  49. public class AsyncSceneObjectGroupDeleter
  50. {
  51. private static readonly ILog m_log
  52. = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
  53. /// <value>
  54. /// Is the deleter currently enabled?
  55. /// </value>
  56. public bool Enabled;
  57. private Timer m_inventoryTicker = new Timer(2000);
  58. private readonly Queue<DeleteToInventoryHolder> m_inventoryDeletes = new Queue<DeleteToInventoryHolder>();
  59. private Scene m_scene;
  60. public AsyncSceneObjectGroupDeleter(Scene scene)
  61. {
  62. m_scene = scene;
  63. m_inventoryTicker.AutoReset = false;
  64. m_inventoryTicker.Elapsed += InventoryRunDeleteTimer;
  65. }
  66. /// <summary>
  67. /// Delete the given object from the scene
  68. /// </summary>
  69. public void DeleteToInventory(DeRezAction action, UUID folderID,
  70. List<SceneObjectGroup> objectGroups, IClientAPI remoteClient,
  71. bool permissionToDelete)
  72. {
  73. if (Enabled)
  74. lock (m_inventoryTicker)
  75. m_inventoryTicker.Stop();
  76. lock (m_inventoryDeletes)
  77. {
  78. DeleteToInventoryHolder dtis = new DeleteToInventoryHolder();
  79. dtis.action = action;
  80. dtis.folderID = folderID;
  81. dtis.objectGroups = objectGroups;
  82. dtis.remoteClient = remoteClient;
  83. dtis.permissionToDelete = permissionToDelete;
  84. m_inventoryDeletes.Enqueue(dtis);
  85. }
  86. if (Enabled)
  87. lock (m_inventoryTicker)
  88. m_inventoryTicker.Start();
  89. // Visually remove it, even if it isnt really gone yet. This means that if we crash before the object
  90. // has gone to inventory, it will reappear in the region again on restart instead of being lost.
  91. // This is not ideal since the object will still be available for manipulation when it should be, but it's
  92. // better than losing the object for now.
  93. if (permissionToDelete)
  94. {
  95. List<uint> killIDs = new List<uint>();
  96. foreach (SceneObjectGroup g in objectGroups)
  97. { killIDs.Add(g.LocalId);
  98. g.DeleteGroupFromScene(true);
  99. }
  100. m_scene.SendKillObject(killIDs);
  101. }
  102. }
  103. private void InventoryRunDeleteTimer(object sender, ElapsedEventArgs e)
  104. {
  105. // m_log.Debug("[ASYNC DELETER]: Starting send to inventory loop");
  106. // We must set appearance parameters in the en_US culture in order to avoid issues where values are saved
  107. // in a culture where decimal points are commas and then reloaded in a culture which just treats them as
  108. // number seperators.
  109. Culture.SetCurrentCulture();
  110. while (InventoryDeQueueAndDelete())
  111. {
  112. //m_log.Debug("[ASYNC DELETER]: Sent item successfully to inventory, continuing...");
  113. }
  114. }
  115. /// <summary>
  116. /// Move the next object in the queue to inventory. Then delete it properly from the scene.
  117. /// </summary>
  118. /// <returns></returns>
  119. public bool InventoryDeQueueAndDelete()
  120. {
  121. DeleteToInventoryHolder x = null;
  122. try
  123. {
  124. lock (m_inventoryDeletes)
  125. {
  126. int left = m_inventoryDeletes.Count;
  127. if (left > 0)
  128. {
  129. x = m_inventoryDeletes.Dequeue();
  130. // m_log.DebugFormat(
  131. // "[ASYNC DELETER]: Sending object to user's inventory, action {1}, count {2}, {0} item(s) remaining.",
  132. // left, x.action, x.objectGroups.Count);
  133. try
  134. {
  135. IInventoryAccessModule invAccess = m_scene.RequestModuleInterface<IInventoryAccessModule>();
  136. if (invAccess != null)
  137. invAccess.CopyToInventory(x.action, x.folderID, x.objectGroups, x.remoteClient, false);
  138. if (x.permissionToDelete)
  139. {
  140. foreach (SceneObjectGroup g in x.objectGroups)
  141. m_scene.DeleteSceneObject(g, false);
  142. }
  143. }
  144. catch (Exception e)
  145. {
  146. m_log.ErrorFormat(
  147. "[ASYNC DELETER]: Exception background sending object: {0}{1}", e.Message, e.StackTrace);
  148. }
  149. return true;
  150. }
  151. }
  152. }
  153. catch (Exception e)
  154. {
  155. // We can't put the object group details in here since the root part may have disappeared (which is where these sit).
  156. // FIXME: This needs to be fixed.
  157. m_log.ErrorFormat(
  158. "[ASYNC DELETER]: Queued sending of scene object to agent {0} {1} failed: {2} {3}",
  159. (x != null ? x.remoteClient.Name : "unavailable"),
  160. (x != null ? x.remoteClient.AgentId.ToString() : "unavailable"),
  161. e.Message,
  162. e.StackTrace);
  163. }
  164. // m_log.Debug("[ASYNC DELETER]: No objects left in inventory send queue.");
  165. return false;
  166. }
  167. }
  168. }