AssetDownloadModule.cs 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  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. using System.Collections.Generic;
  28. using libsecondlife;
  29. using libsecondlife.Packets;
  30. using Nini.Config;
  31. using OpenSim.Framework;
  32. using OpenSim.Region.Environment.Interfaces;
  33. using OpenSim.Region.Environment.Scenes;
  34. namespace OpenSim.Region.Environment.Modules.Agent.AssetDownload
  35. {
  36. public class AssetDownloadModule : IRegionModule
  37. {
  38. /// <summary>
  39. /// Asset requests with data which are ready to be sent back to requesters. This includes textures.
  40. /// </summary>
  41. private List<AssetRequest> AssetRequests;
  42. private Scene m_scene;
  43. private Dictionary<LLUUID, Scene> RegisteredScenes = new Dictionary<LLUUID, Scene>();
  44. ///
  45. /// Assets requests (for each user) which are waiting for asset server data. This includes texture requests
  46. /// </summary>
  47. private Dictionary<LLUUID, Dictionary<LLUUID, AssetRequest>> RequestedAssets;
  48. public AssetDownloadModule()
  49. {
  50. RequestedAssets = new Dictionary<LLUUID, Dictionary<LLUUID, AssetRequest>>();
  51. AssetRequests = new List<AssetRequest>();
  52. }
  53. #region IRegionModule Members
  54. public void Initialise(Scene scene, IConfigSource config)
  55. {
  56. if (!RegisteredScenes.ContainsKey(scene.RegionInfo.RegionID))
  57. {
  58. RegisteredScenes.Add(scene.RegionInfo.RegionID, scene);
  59. // scene.EventManager.OnNewClient += NewClient;
  60. }
  61. if (m_scene == null)
  62. {
  63. m_scene = scene;
  64. // m_thread = new Thread(new ThreadStart(RunAssetQueue));
  65. // m_thread.Name = "AssetDownloadQueueThread";
  66. // m_thread.IsBackground = true;
  67. // m_thread.Start();
  68. // OpenSim.Framework.ThreadTracker.Add(m_thread);
  69. }
  70. }
  71. public void PostInitialise()
  72. {
  73. }
  74. public void Close()
  75. {
  76. }
  77. public string Name
  78. {
  79. get { return "AssetDownloadModule"; }
  80. }
  81. public bool IsSharedModule
  82. {
  83. get { return true; }
  84. }
  85. #endregion
  86. public void NewClient(IClientAPI client)
  87. {
  88. // client.OnRequestAsset += AddAssetRequest;
  89. }
  90. /// <summary>
  91. /// Make an asset request the result of which will be packeted up and sent directly back to the client.
  92. /// </summary>
  93. /// <param name="userInfo"></param>
  94. /// <param name="transferRequest"></param>
  95. public void AddAssetRequest(IClientAPI userInfo, TransferRequestPacket transferRequest)
  96. {
  97. LLUUID requestID = null;
  98. byte source = 2;
  99. if (transferRequest.TransferInfo.SourceType == 2)
  100. {
  101. //direct asset request
  102. requestID = new LLUUID(transferRequest.TransferInfo.Params, 0);
  103. }
  104. else if (transferRequest.TransferInfo.SourceType == 3)
  105. {
  106. //inventory asset request
  107. requestID = new LLUUID(transferRequest.TransferInfo.Params, 80);
  108. source = 3;
  109. //Console.WriteLine("asset request " + requestID);
  110. }
  111. //not found asset
  112. // so request from asset server
  113. Dictionary<LLUUID, AssetRequest> userRequests = null;
  114. if (RequestedAssets.TryGetValue(userInfo.AgentId, out userRequests))
  115. {
  116. if (!userRequests.ContainsKey(requestID))
  117. {
  118. AssetRequest request = new AssetRequest();
  119. request.RequestUser = userInfo;
  120. request.RequestAssetID = requestID;
  121. request.TransferRequestID = transferRequest.TransferInfo.TransferID;
  122. request.AssetRequestSource = source;
  123. request.Params = transferRequest.TransferInfo.Params;
  124. userRequests[requestID] = request;
  125. m_scene.AssetCache.GetAsset(requestID, AssetCallback, false);
  126. }
  127. }
  128. else
  129. {
  130. userRequests = new Dictionary<LLUUID, AssetRequest>();
  131. AssetRequest request = new AssetRequest();
  132. request.RequestUser = userInfo;
  133. request.RequestAssetID = requestID;
  134. request.TransferRequestID = transferRequest.TransferInfo.TransferID;
  135. request.AssetRequestSource = source;
  136. request.Params = transferRequest.TransferInfo.Params;
  137. userRequests.Add(requestID, request);
  138. RequestedAssets[userInfo.AgentId] = userRequests;
  139. m_scene.AssetCache.GetAsset(requestID, AssetCallback, false);
  140. }
  141. }
  142. public void AssetCallback(LLUUID assetID, AssetBase asset)
  143. {
  144. if (asset != null)
  145. {
  146. foreach (Dictionary<LLUUID, AssetRequest> userRequests in RequestedAssets.Values)
  147. {
  148. if (userRequests.ContainsKey(assetID))
  149. {
  150. AssetRequest req = userRequests[assetID];
  151. if (req != null)
  152. {
  153. req.AssetInf = asset;
  154. req.NumPackets = CalculateNumPackets(asset.Data);
  155. userRequests.Remove(assetID);
  156. AssetRequests.Add(req);
  157. }
  158. }
  159. }
  160. }
  161. }
  162. /// <summary>
  163. /// Calculate the number of packets required to send the asset to the client.
  164. /// </summary>
  165. /// <param name="data"></param>
  166. /// <returns></returns>
  167. private int CalculateNumPackets(byte[] data)
  168. {
  169. const uint m_maxPacketSize = 600;
  170. int numPackets = 1;
  171. if (data.LongLength > m_maxPacketSize)
  172. {
  173. // over max number of bytes so split up file
  174. long restData = data.LongLength - m_maxPacketSize;
  175. int restPackets = (int) ((restData + m_maxPacketSize - 1) / m_maxPacketSize);
  176. numPackets += restPackets;
  177. }
  178. return numPackets;
  179. }
  180. #region Nested type: AssetRequest
  181. public class AssetRequest
  182. {
  183. public AssetBase AssetInf;
  184. public byte AssetRequestSource = 2;
  185. public long DataPointer = 0;
  186. public int DiscardLevel = -1;
  187. public AssetBase ImageInfo;
  188. public bool IsTextureRequest;
  189. public int NumPackets = 0;
  190. public int PacketCounter = 0;
  191. public byte[] Params = null;
  192. public LLUUID RequestAssetID;
  193. public IClientAPI RequestUser;
  194. public LLUUID TransferRequestID;
  195. //public bool AssetInCache;
  196. //public int TimeRequested;
  197. public AssetRequest()
  198. {
  199. }
  200. }
  201. #endregion
  202. }
  203. }