TextureDownloadModule.cs 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  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 OpenSim 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. */
  28. using System;
  29. using System.Collections.Generic;
  30. using System.Threading;
  31. using libsecondlife;
  32. using libsecondlife.Packets;
  33. using Nini.Config;
  34. using OpenSim.Framework;
  35. using OpenSim.Framework.Console;
  36. using OpenSim.Region.Environment.Interfaces;
  37. using OpenSim.Region.Environment.Scenes;
  38. namespace OpenSim.Region.Environment.Modules
  39. {
  40. //this is a first attempt, to start breaking the mess thats called the assetcache up.
  41. // basically this should be the texture sending (to clients) code moved out of assetcache
  42. //and some small clean up
  43. public class TextureDownloadModule : IRegionModule
  44. {
  45. private static readonly log4net.ILog m_log
  46. = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
  47. private Scene m_scene;
  48. private List<Scene> m_scenes = new List<Scene>();
  49. private readonly BlockingQueue<TextureSender> m_queueSenders = new BlockingQueue<TextureSender>();
  50. private readonly Dictionary<LLUUID, UserTextureDownloadService> m_userTextureServices =
  51. new Dictionary<LLUUID, UserTextureDownloadService>();
  52. private Thread m_thread;
  53. public TextureDownloadModule()
  54. {
  55. }
  56. public void Initialise(Scene scene, IConfigSource config)
  57. {
  58. if (m_scene == null)
  59. {
  60. //Console.WriteLine("Creating Texture download module");
  61. m_thread = new Thread(new ThreadStart(ProcessTextureSenders));
  62. m_thread.IsBackground = true;
  63. m_thread.Start();
  64. }
  65. if (!m_scenes.Contains(scene))
  66. {
  67. m_scenes.Add(scene);
  68. m_scene = scene;
  69. m_scene.EventManager.OnNewClient += NewClient;
  70. m_scene.EventManager.OnRemovePresence += EventManager_OnRemovePresence;
  71. }
  72. }
  73. private void EventManager_OnRemovePresence(LLUUID agentId)
  74. {
  75. UserTextureDownloadService textureService;
  76. lock (m_userTextureServices)
  77. {
  78. if( m_userTextureServices.TryGetValue( agentId, out textureService ))
  79. {
  80. textureService.Close();
  81. m_userTextureServices.Remove(agentId);
  82. }
  83. }
  84. }
  85. public void PostInitialise()
  86. {
  87. }
  88. public void Close()
  89. {
  90. }
  91. public string Name
  92. {
  93. get { return "TextureDownloadModule"; }
  94. }
  95. public bool IsSharedModule
  96. {
  97. get { return false; }
  98. }
  99. public void NewClient(IClientAPI client)
  100. {
  101. client.OnRequestTexture += TextureRequest;
  102. }
  103. private bool TryGetUserTextureService(LLUUID userID, out UserTextureDownloadService textureService)
  104. {
  105. lock (m_userTextureServices)
  106. {
  107. if (m_userTextureServices.TryGetValue(userID, out textureService))
  108. {
  109. return true;
  110. }
  111. textureService = new UserTextureDownloadService(m_scene, m_queueSenders);
  112. m_userTextureServices.Add(userID, textureService);
  113. return true;
  114. }
  115. }
  116. public void TextureRequest(Object sender, TextureRequestArgs e)
  117. {
  118. IClientAPI client = (IClientAPI) sender;
  119. UserTextureDownloadService textureService;
  120. if (TryGetUserTextureService(client.AgentId, out textureService))
  121. {
  122. textureService.HandleTextureRequest(client, e);
  123. m_scene.AddPendingDownloads(1);
  124. }
  125. }
  126. public void ProcessTextureSenders()
  127. {
  128. TextureSender sender = null;
  129. while (true)
  130. {
  131. sender = m_queueSenders.Dequeue();
  132. if (sender.Cancel)
  133. {
  134. TextureSent(sender);
  135. sender.Cancel = false;
  136. }
  137. else
  138. {
  139. bool finished = sender.SendTexturePacket();
  140. if (finished)
  141. {
  142. TextureSent(sender);
  143. }
  144. else
  145. {
  146. m_queueSenders.Enqueue(sender);
  147. }
  148. }
  149. // Make sure that any sender we currently have can get garbage collected
  150. sender = null;
  151. //m_log.InfoFormat("[TEXTURE DOWNLOAD] Texture sender queue size: {0}", m_queueSenders.Count());
  152. }
  153. }
  154. private void TextureSent(TextureSender sender)
  155. {
  156. sender.Sending = false;
  157. m_scene.AddPendingDownloads(-1);
  158. }
  159. }
  160. }