AsyncSceneObjectGroupDeleter.cs 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  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.Collections.Concurrent;
  30. //using System.Reflection;
  31. using System.Threading;
  32. using log4net;
  33. using OpenMetaverse;
  34. using OpenSim.Framework;
  35. using OpenSim.Region.Framework.Interfaces;
  36. namespace OpenSim.Region.Framework.Scenes
  37. {
  38. class DeleteToInventoryHolder
  39. {
  40. public DeRezAction action;
  41. public IClientAPI remoteClient;
  42. public List<SceneObjectGroup> objectGroups;
  43. public UUID folderID;
  44. public bool permissionToDelete;
  45. }
  46. /// <summary>
  47. /// Asynchronously derez objects. This is used to derez large number of objects to inventory without holding
  48. /// up the main client thread.
  49. /// </summary>
  50. public class AsyncSceneObjectGroupDeleter
  51. {
  52. //private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
  53. /// <value>
  54. /// Is the deleter currently enabled?
  55. /// </value>
  56. public bool Enabled;
  57. private Scene m_scene;
  58. static private ConcurrentQueue<DeleteToInventoryHolder> m_inventoryDeletes = new ConcurrentQueue<DeleteToInventoryHolder>();
  59. static private object m_threadLock = new object();
  60. static private bool m_running;
  61. public AsyncSceneObjectGroupDeleter(Scene scene)
  62. {
  63. m_scene = scene;
  64. }
  65. /// <summary>
  66. /// Delete the given object from the scene
  67. /// </summary>
  68. public void DeleteToInventory(DeRezAction action, UUID folderID,
  69. List<SceneObjectGroup> objectGroups, IClientAPI remoteClient,
  70. bool permissionToDelete)
  71. {
  72. DeleteToInventoryHolder dtis = new DeleteToInventoryHolder();
  73. dtis.action = action;
  74. dtis.folderID = folderID;
  75. dtis.objectGroups = objectGroups;
  76. dtis.remoteClient = remoteClient;
  77. dtis.permissionToDelete = permissionToDelete;
  78. m_inventoryDeletes.Enqueue(dtis);
  79. if (permissionToDelete)
  80. {
  81. foreach (SceneObjectGroup g in objectGroups)
  82. g.DeleteGroupFromScene(false);
  83. }
  84. if(Monitor.TryEnter(m_threadLock))
  85. {
  86. if(!m_running)
  87. {
  88. if(Enabled)
  89. {
  90. m_running = true;
  91. Util.FireAndForget(x => InventoryDeQueueAndDelete());
  92. }
  93. else
  94. {
  95. m_running = true;
  96. InventoryDeQueueAndDelete();
  97. }
  98. }
  99. Monitor.Exit(m_threadLock);
  100. }
  101. }
  102. /// <summary>
  103. /// Move the next object in the queue to inventory. Then delete it properly from the scene.
  104. /// </summary>
  105. /// <returns></returns>
  106. public void InventoryDeQueueAndDelete()
  107. {
  108. lock (m_threadLock)
  109. {
  110. IInventoryAccessModule invAccess = m_scene.RequestModuleInterface<IInventoryAccessModule>();
  111. if (invAccess == null)
  112. return;
  113. int count = 0;
  114. while (m_inventoryDeletes.TryDequeue(out DeleteToInventoryHolder x))
  115. {
  116. // m_log.DebugFormat(
  117. // "[ASYNC DELETER]: Sending object to user's inventory, action {1}, count {2}, {0} item(s) remaining.",
  118. // left, x.action, x.objectGroups.Count);
  119. try
  120. {
  121. invAccess.CopyToInventory(x.action, x.folderID, x.objectGroups, x.remoteClient, false);
  122. if (x.permissionToDelete)
  123. {
  124. foreach (SceneObjectGroup g in x.objectGroups)
  125. m_scene.DeleteSceneObject(g, true);
  126. }
  127. count += x.objectGroups.Count;
  128. if(count > 256)
  129. {
  130. Thread.Sleep(50); // throttle
  131. count = 0;
  132. }
  133. }
  134. catch
  135. // catch (Exception e)
  136. {
  137. //m_log.ErrorFormat(
  138. // "[ASYNC OBJECT DELETER]: Exception background sending object: {0}{1}", e.Message, e.StackTrace);
  139. }
  140. }
  141. // m_log.Debug("[ASYNC DELETER]: No objects left in inventory send queue.");
  142. m_running = false;
  143. }
  144. }
  145. }
  146. }