AsyncSceneObjectGroupDeleter.cs 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  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. namespace OpenSim.Region.Framework.Scenes
  35. {
  36. class DeleteToInventoryHolder
  37. {
  38. public DeRezAction action;
  39. public IClientAPI remoteClient;
  40. public SceneObjectGroup objectGroup;
  41. public UUID folderID;
  42. public bool permissionToDelete;
  43. }
  44. /// <summary>
  45. /// Asynchronously derez objects. This is used to derez large number of objects to inventory without holding
  46. /// up the main client thread.
  47. /// </summary>
  48. public class AsyncSceneObjectGroupDeleter
  49. {
  50. private static readonly ILog m_log
  51. = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
  52. /// <value>
  53. /// Is the deleter currently enabled?
  54. /// </value>
  55. public bool Enabled;
  56. private Timer m_inventoryTicker = new Timer(2000);
  57. private readonly Queue<DeleteToInventoryHolder> m_inventoryDeletes = new Queue<DeleteToInventoryHolder>();
  58. private Scene m_scene;
  59. public AsyncSceneObjectGroupDeleter(Scene scene)
  60. {
  61. m_scene = scene;
  62. m_inventoryTicker.AutoReset = false;
  63. m_inventoryTicker.Elapsed += InventoryRunDeleteTimer;
  64. }
  65. /// <summary>
  66. /// Delete the given object from the scene
  67. /// </summary>
  68. public void DeleteToInventory(DeRezAction action, UUID folderID,
  69. SceneObjectGroup objectGroup, IClientAPI remoteClient,
  70. bool permissionToDelete)
  71. {
  72. if (Enabled)
  73. m_inventoryTicker.Stop();
  74. lock (m_inventoryDeletes)
  75. {
  76. DeleteToInventoryHolder dtis = new DeleteToInventoryHolder();
  77. dtis.action = action;
  78. dtis.folderID = folderID;
  79. dtis.objectGroup = objectGroup;
  80. dtis.remoteClient = remoteClient;
  81. dtis.permissionToDelete = permissionToDelete;
  82. m_inventoryDeletes.Enqueue(dtis);
  83. }
  84. if (Enabled)
  85. m_inventoryTicker.Start();
  86. // Visually remove it, even if it isnt really gone yet. This means that if we crash before the object
  87. // has gone to inventory, it will reappear in the region again on restart instead of being lost.
  88. // This is not ideal since the object will still be available for manipulation when it should be, but it's
  89. // better than losing the object for now.
  90. if (permissionToDelete)
  91. objectGroup.DeleteGroup(false);
  92. }
  93. private void InventoryRunDeleteTimer(object sender, ElapsedEventArgs e)
  94. {
  95. m_log.Debug("[SCENE]: Starting send to inventory loop");
  96. while (InventoryDeQueueAndDelete())
  97. {
  98. //m_log.Debug("[SCENE]: Sent item successfully to inventory, continuing...");
  99. }
  100. }
  101. /// <summary>
  102. /// Move the next object in the queue to inventory. Then delete it properly from the scene.
  103. /// </summary>
  104. /// <returns></returns>
  105. public bool InventoryDeQueueAndDelete()
  106. {
  107. DeleteToInventoryHolder x = null;
  108. try
  109. {
  110. lock (m_inventoryDeletes)
  111. {
  112. int left = m_inventoryDeletes.Count;
  113. if (left > 0)
  114. {
  115. x = m_inventoryDeletes.Dequeue();
  116. if (!x.objectGroup.CanBeDeleted())
  117. {
  118. m_inventoryDeletes.Enqueue(x);
  119. return true;
  120. }
  121. m_log.DebugFormat(
  122. "[SCENE]: Sending object to user's inventory, {0} item(s) remaining.", left);
  123. try
  124. {
  125. m_scene.DeleteToInventory(x.action, x.folderID, x.objectGroup, x.remoteClient);
  126. if (x.permissionToDelete)
  127. m_scene.DeleteSceneObject(x.objectGroup, false);
  128. }
  129. catch (Exception e)
  130. {
  131. m_log.DebugFormat("Exception background sending object: " + e);
  132. }
  133. return true;
  134. }
  135. }
  136. }
  137. catch (Exception e)
  138. {
  139. // We can't put the object group details in here since the root part may have disappeared (which is where these sit).
  140. // FIXME: This needs to be fixed.
  141. m_log.ErrorFormat(
  142. "[SCENE]: Queued sending of scene object to agent {0} {1} failed: {2}",
  143. (x != null ? x.remoteClient.Name : "unavailable"), (x != null ? x.remoteClient.AgentId.ToString() : "unavailable"), e.ToString());
  144. }
  145. m_log.Debug("[SCENE]: No objects left in inventory send queue.");
  146. return false;
  147. }
  148. }
  149. }