UserTextureDownloadService.cs 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  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 libsecondlife;
  31. using OpenSim.Framework;
  32. using OpenSim.Framework.Console;
  33. using OpenSim.Region.Environment.Scenes;
  34. namespace OpenSim.Region.Environment.Modules
  35. {
  36. public class UserTextureDownloadService
  37. {
  38. private static readonly log4net.ILog m_log
  39. = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
  40. private readonly Dictionary<LLUUID, TextureSender> m_textureSenders = new Dictionary<LLUUID, TextureSender>();
  41. private readonly BlockingQueue<TextureSender> m_sharedSendersQueue;
  42. private readonly Scene m_scene;
  43. public UserTextureDownloadService(Scene scene, BlockingQueue<TextureSender> sharedQueue)
  44. {
  45. m_scene = scene;
  46. m_sharedSendersQueue = sharedQueue;
  47. }
  48. public void HandleTextureRequest(IClientAPI client, TextureRequestArgs e)
  49. {
  50. TextureSender textureSender;
  51. //TODO: should be working out the data size/ number of packets to be sent for each discard level
  52. if ((e.DiscardLevel >= 0) || (e.Priority != 0))
  53. {
  54. lock (m_textureSenders)
  55. {
  56. if (m_textureSenders.TryGetValue(e.RequestedAssetID, out textureSender))
  57. {
  58. textureSender.UpdateRequest(e.DiscardLevel, e.PacketNumber);
  59. if ((textureSender.ImageLoaded) &&
  60. (textureSender.Sending == false))
  61. {
  62. EnqueueTextureSender(textureSender);
  63. }
  64. }
  65. else
  66. {
  67. TextureSender requestHandler =
  68. new TextureSender(client, e.RequestedAssetID, e.DiscardLevel, e.PacketNumber);
  69. m_textureSenders.Add(e.RequestedAssetID, requestHandler);
  70. m_scene.AssetCache.GetAsset(e.RequestedAssetID, TextureCallback);
  71. }
  72. }
  73. }
  74. else
  75. {
  76. lock (m_textureSenders)
  77. {
  78. if (m_textureSenders.TryGetValue(e.RequestedAssetID, out textureSender))
  79. {
  80. textureSender.Cancel = true;
  81. }
  82. }
  83. }
  84. }
  85. public void TextureCallback(LLUUID textureID, AssetBase asset)
  86. {
  87. lock (m_textureSenders)
  88. {
  89. TextureSender textureSender;
  90. if (m_textureSenders.TryGetValue(textureID, out textureSender))
  91. {
  92. if (!textureSender.ImageLoaded)
  93. {
  94. textureSender.TextureReceived(asset);
  95. EnqueueTextureSender(textureSender);
  96. }
  97. //m_log.InfoFormat("[TEXTURE SENDER] Removing texture sender with uuid {0}", textureID);
  98. m_textureSenders.Remove(textureID);
  99. //m_log.InfoFormat("[TEXTURE SENDER] Current texture senders in dictionary: {0}", m_textureSenders.Count);
  100. }
  101. else
  102. {
  103. throw new Exception("Got a texture with no sender object to handle it, this shouldn't happen");
  104. }
  105. }
  106. }
  107. private void EnqueueTextureSender(TextureSender textureSender)
  108. {
  109. textureSender.Cancel = false;
  110. textureSender.Sending = true;
  111. textureSender.counter = 0;
  112. if (!m_sharedSendersQueue.Contains(textureSender))
  113. {
  114. m_sharedSendersQueue.Enqueue(textureSender);
  115. }
  116. }
  117. internal void Close()
  118. {
  119. lock (m_textureSenders)
  120. {
  121. foreach( TextureSender textureSender in m_textureSenders.Values )
  122. {
  123. textureSender.Cancel = true;
  124. }
  125. m_textureSenders.Clear();
  126. }
  127. }
  128. }
  129. }