AssetDownloadModule.cs 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335
  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;
  28. using System.Collections.Generic;
  29. using System.Threading;
  30. using libsecondlife;
  31. using libsecondlife.Packets;
  32. using Nini.Config;
  33. using OpenSim.Framework;
  34. using OpenSim.Region.Environment.Interfaces;
  35. using OpenSim.Region.Environment.Scenes;
  36. namespace OpenSim.Region.Environment.Modules
  37. {
  38. public class AssetDownloadModule : IRegionModule
  39. {
  40. private Scene m_scene;
  41. private Dictionary<LLUUID, Scene> RegisteredScenes = new Dictionary<LLUUID, Scene>();
  42. ///
  43. /// Assets requests (for each user) which are waiting for asset server data. This includes texture requests
  44. /// </summary>
  45. private Dictionary<LLUUID, Dictionary<LLUUID,AssetRequest>> RequestedAssets;
  46. /// <summary>
  47. /// Asset requests with data which are ready to be sent back to requesters. This includes textures.
  48. /// </summary>
  49. private List<AssetRequest> AssetRequests;
  50. public AssetDownloadModule()
  51. {
  52. RequestedAssets = new Dictionary<LLUUID, Dictionary<LLUUID, AssetRequest>>();
  53. AssetRequests = new List<AssetRequest>();
  54. }
  55. public void Initialise(Scene scene, IConfigSource config)
  56. {
  57. if (!RegisteredScenes.ContainsKey(scene.RegionInfo.RegionID))
  58. {
  59. RegisteredScenes.Add(scene.RegionInfo.RegionID, scene);
  60. // scene.EventManager.OnNewClient += NewClient;
  61. }
  62. if (m_scene == null)
  63. {
  64. m_scene = scene;
  65. // m_thread = new Thread(new ThreadStart(RunAssetQueue));
  66. // m_thread.Name = "AssetDownloadQueueThread";
  67. // m_thread.IsBackground = true;
  68. // m_thread.Start();
  69. // OpenSim.Framework.ThreadTracker.Add(m_thread);
  70. }
  71. }
  72. public void PostInitialise()
  73. {
  74. }
  75. public void Close()
  76. {
  77. }
  78. public string Name
  79. {
  80. get { return "AssetDownloadModule"; }
  81. }
  82. public bool IsSharedModule
  83. {
  84. get { return true; }
  85. }
  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. // TODO: unused
  163. // private void RunAssetQueue()
  164. // {
  165. // while (true)
  166. // {
  167. // try
  168. // {
  169. // ProcessAssetQueue();
  170. // Thread.Sleep(500);
  171. // }
  172. // catch (Exception)
  173. // {
  174. // // m_log.Error("[ASSET CACHE]: " + e.ToString());
  175. // }
  176. // }
  177. // }
  178. // TODO: unused
  179. // /// <summary>
  180. // /// Process the asset queue which sends packets directly back to the client.
  181. // /// </summary>
  182. // private void ProcessAssetQueue()
  183. // {
  184. // //should move the asset downloading to a module, like has been done with texture downloading
  185. // if (AssetRequests.Count == 0)
  186. // {
  187. // //no requests waiting
  188. // return;
  189. // }
  190. // // if less than 5, do all of them
  191. // int num = Math.Min(5, AssetRequests.Count);
  192. // AssetRequest req;
  193. // for (int i = 0; i < num; i++)
  194. // {
  195. // req = (AssetRequest)AssetRequests[i];
  196. // //Console.WriteLine("sending asset " + req.RequestAssetID);
  197. // TransferInfoPacket Transfer = new TransferInfoPacket();
  198. // Transfer.TransferInfo.ChannelType = 2;
  199. // Transfer.TransferInfo.Status = 0;
  200. // Transfer.TransferInfo.TargetType = 0;
  201. // if (req.AssetRequestSource == 2)
  202. // {
  203. // Transfer.TransferInfo.Params = new byte[20];
  204. // Array.Copy(req.RequestAssetID.GetBytes(), 0, Transfer.TransferInfo.Params, 0, 16);
  205. // int assType = (int)req.AssetInf.Type;
  206. // Array.Copy(Helpers.IntToBytes(assType), 0, Transfer.TransferInfo.Params, 16, 4);
  207. // }
  208. // else if (req.AssetRequestSource == 3)
  209. // {
  210. // Transfer.TransferInfo.Params = req.Params;
  211. // // Transfer.TransferInfo.Params = new byte[100];
  212. // //Array.Copy(req.RequestUser.AgentId.GetBytes(), 0, Transfer.TransferInfo.Params, 0, 16);
  213. // //Array.Copy(req.RequestUser.SessionId.GetBytes(), 0, Transfer.TransferInfo.Params, 16, 16);
  214. // }
  215. // Transfer.TransferInfo.Size = (int)req.AssetInf.Data.Length;
  216. // Transfer.TransferInfo.TransferID = req.TransferRequestID;
  217. // req.RequestUser.OutPacket(Transfer, ThrottleOutPacketType.Asset);
  218. // if (req.NumPackets == 1)
  219. // {
  220. // TransferPacketPacket TransferPacket = new TransferPacketPacket();
  221. // TransferPacket.TransferData.Packet = 0;
  222. // TransferPacket.TransferData.ChannelType = 2;
  223. // TransferPacket.TransferData.TransferID = req.TransferRequestID;
  224. // TransferPacket.TransferData.Data = req.AssetInf.Data;
  225. // TransferPacket.TransferData.Status = 1;
  226. // req.RequestUser.OutPacket(TransferPacket, ThrottleOutPacketType.Asset);
  227. // }
  228. // else
  229. // {
  230. // int processedLength = 0;
  231. // // libsecondlife hardcodes 1500 as the maximum data chunk size
  232. // int maxChunkSize = 1250;
  233. // int packetNumber = 0;
  234. // while (processedLength < req.AssetInf.Data.Length)
  235. // {
  236. // TransferPacketPacket TransferPacket = new TransferPacketPacket();
  237. // TransferPacket.TransferData.Packet = packetNumber;
  238. // TransferPacket.TransferData.ChannelType = 2;
  239. // TransferPacket.TransferData.TransferID = req.TransferRequestID;
  240. // int chunkSize = Math.Min(req.AssetInf.Data.Length - processedLength, maxChunkSize);
  241. // byte[] chunk = new byte[chunkSize];
  242. // Array.Copy(req.AssetInf.Data, processedLength, chunk, 0, chunk.Length);
  243. // TransferPacket.TransferData.Data = chunk;
  244. // // 0 indicates more packets to come, 1 indicates last packet
  245. // if (req.AssetInf.Data.Length - processedLength > maxChunkSize)
  246. // {
  247. // TransferPacket.TransferData.Status = 0;
  248. // }
  249. // else
  250. // {
  251. // TransferPacket.TransferData.Status = 1;
  252. // }
  253. // req.RequestUser.OutPacket(TransferPacket, ThrottleOutPacketType.Asset);
  254. // processedLength += chunkSize;
  255. // packetNumber++;
  256. // }
  257. // }
  258. // }
  259. // //remove requests that have been completed
  260. // for (int i = 0; i < num; i++)
  261. // {
  262. // AssetRequests.RemoveAt(0);
  263. // }
  264. // }
  265. /// <summary>
  266. /// Calculate the number of packets required to send the asset to the client.
  267. /// </summary>
  268. /// <param name="data"></param>
  269. /// <returns></returns>
  270. private int CalculateNumPackets(byte[] data)
  271. {
  272. const uint m_maxPacketSize = 600;
  273. int numPackets = 1;
  274. if (data.LongLength > m_maxPacketSize)
  275. {
  276. // over max number of bytes so split up file
  277. long restData = data.LongLength - m_maxPacketSize;
  278. int restPackets = (int)((restData + m_maxPacketSize - 1) / m_maxPacketSize);
  279. numPackets += restPackets;
  280. }
  281. return numPackets;
  282. }
  283. public class AssetRequest
  284. {
  285. public IClientAPI RequestUser;
  286. public LLUUID RequestAssetID;
  287. public AssetBase AssetInf;
  288. public AssetBase ImageInfo;
  289. public LLUUID TransferRequestID;
  290. public long DataPointer = 0;
  291. public int NumPackets = 0;
  292. public int PacketCounter = 0;
  293. public bool IsTextureRequest;
  294. public byte AssetRequestSource = 2;
  295. public byte[] Params = null;
  296. //public bool AssetInCache;
  297. //public int TimeRequested;
  298. public int DiscardLevel = -1;
  299. public AssetRequest()
  300. {
  301. }
  302. }
  303. }
  304. }