AsyncInventorySender.cs 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  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.Threading;
  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 FetchHolder
  38. {
  39. public IClientAPI Client { get; private set; }
  40. public UUID ItemID { get; private set; }
  41. public FetchHolder(IClientAPI client, UUID itemID)
  42. {
  43. Client = client;
  44. ItemID = itemID;
  45. }
  46. }
  47. /// <summary>
  48. /// Send FetchInventoryReply information to clients asynchronously on a single thread rather than asynchronously via
  49. /// multiple threads.
  50. /// </summary>
  51. /// <remarks>
  52. /// If the main root inventory is right-clicked on a version 1 viewer for a user with a large inventory, a very
  53. /// very large number of FetchInventory requests are sent to the simulator. Each is handled on a separate thread
  54. /// by the IClientAPI, but the sheer number of requests overwhelms the number of threads available and ends up
  55. /// freezing the inbound packet handling.
  56. ///
  57. /// This class makes the first FetchInventory packet thread process the queue. If new requests come
  58. /// in while it is processing, then the subsequent threads just add the requests and leave it to the original
  59. /// thread to process them.
  60. ///
  61. /// This might slow down outbound packets but these are limited by the IClientAPI outbound queues
  62. /// anyway.
  63. ///
  64. /// It might be possible to ignore FetchInventory requests altogether, particularly as they are redundant wrt to
  65. /// FetchInventoryDescendents requests, but this would require more investigation.
  66. /// </remarks>
  67. public class AsyncInventorySender
  68. {
  69. // private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
  70. protected Scene m_scene;
  71. /// <summary>
  72. /// Queues fetch requests
  73. /// </summary>
  74. Queue<FetchHolder> m_fetchHolder = new Queue<FetchHolder>();
  75. /// <summary>
  76. /// Signal whether a queue is currently being processed or not.
  77. /// </summary>
  78. protected volatile bool m_processing;
  79. public AsyncInventorySender(Scene scene)
  80. {
  81. m_processing = false;
  82. m_scene = scene;
  83. }
  84. /// <summary>
  85. /// Handle a fetch inventory request from the client
  86. /// </summary>
  87. /// <param name="remoteClient"></param>
  88. /// <param name="itemID"></param>
  89. /// <param name="ownerID"></param>
  90. public void HandleFetchInventory(IClientAPI remoteClient, UUID itemID, UUID ownerID)
  91. {
  92. lock (m_fetchHolder)
  93. {
  94. // m_log.DebugFormat(
  95. // "[ASYNC INVENTORY SENDER]: Putting request from {0} for {1} on queue", remoteClient.Name, itemID);
  96. m_fetchHolder.Enqueue(new FetchHolder(remoteClient, itemID));
  97. }
  98. if (!m_processing)
  99. {
  100. m_processing = true;
  101. ProcessQueue();
  102. }
  103. }
  104. /// <summary>
  105. /// Process the queue of fetches
  106. /// </summary>
  107. protected void ProcessQueue()
  108. {
  109. FetchHolder fh = null;
  110. while (true)
  111. {
  112. lock (m_fetchHolder)
  113. {
  114. // m_log.DebugFormat("[ASYNC INVENTORY SENDER]: {0} items left to process", m_fetchHolder.Count);
  115. if (m_fetchHolder.Count == 0)
  116. {
  117. m_processing = false;
  118. return;
  119. }
  120. else
  121. {
  122. fh = m_fetchHolder.Dequeue();
  123. }
  124. }
  125. if (!fh.Client.IsActive)
  126. continue;
  127. // m_log.DebugFormat(
  128. // "[ASYNC INVENTORY SENDER]: Handling request from {0} for {1} on queue", fh.Client.Name, fh.ItemID);
  129. InventoryItemBase item = new InventoryItemBase(fh.ItemID, fh.Client.AgentId);
  130. item = m_scene.InventoryService.GetItem(item);
  131. if (item != null)
  132. fh.Client.SendInventoryItemDetails(item.Owner, item);
  133. // TODO: Possibly log any failure
  134. }
  135. }
  136. }
  137. }