AsyncInventorySender.cs 6.6 KB

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