AssetCache.cs 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755
  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.Reflection;
  30. using System.Threading;
  31. using libsecondlife;
  32. using libsecondlife.Packets;
  33. using log4net;
  34. using OpenSim.Framework.Statistics;
  35. namespace OpenSim.Framework.Communications.Cache
  36. {
  37. public delegate void AssetRequestCallback(LLUUID assetID, AssetBase asset);
  38. /// <summary>
  39. /// Manages local cache of assets and their sending to viewers.
  40. ///
  41. /// This class actually encapsulates two largely separate mechanisms. One mechanism fetches assets either
  42. /// synchronously or async and passes the data back to the requester. The second mechanism fetches assets and
  43. /// sends packetised data directly back to the client. The only point where they meet is AssetReceived() and
  44. /// AssetNotFound(), which means they do share the same asset and texture caches.
  45. ///
  46. /// TODO Assets in this cache are effectively immortal (they are never disposed off through old age).
  47. /// This is not a huge problem at the moment since other memory use usually dwarfs that used by assets
  48. /// but it's something to bear in mind.
  49. /// </summary>
  50. public class AssetCache : IAssetReceiver
  51. {
  52. private static readonly ILog m_log
  53. = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
  54. /// <summary>
  55. /// The cache of assets. This does not include textures.
  56. /// </summary>
  57. private Dictionary<LLUUID, AssetInfo> Assets;
  58. /// <summary>
  59. /// The cache of textures.
  60. /// </summary>
  61. private Dictionary<LLUUID, TextureImage> Textures;
  62. /// <summary>
  63. /// Assets requests which are waiting for asset server data. This includes texture requests
  64. /// </summary>
  65. private Dictionary<LLUUID, AssetRequest> RequestedAssets;
  66. /// <summary>
  67. /// Asset requests with data which are ready to be sent back to requesters. This includes textures.
  68. /// </summary>
  69. private List<AssetRequest> AssetRequests;
  70. /// <summary>
  71. /// Until the asset request is fulfilled, each asset request is associated with a list of requesters
  72. /// </summary>
  73. private Dictionary<LLUUID, AssetRequestsList> RequestLists;
  74. private readonly IAssetServer m_assetServer;
  75. private readonly Thread m_assetCacheThread;
  76. /// <summary>
  77. /// Report statistical data.
  78. /// </summary>
  79. public void ShowState()
  80. {
  81. m_log.InfoFormat("Assets:{0} Textures:{1} RequestLists:{2}",
  82. Assets.Count,
  83. Textures.Count,
  84. // AssetRequests.Count,
  85. // RequestedAssets.Count,
  86. RequestLists.Count);
  87. int temporaryImages = 0;
  88. int temporaryAssets = 0;
  89. long imageBytes = 0;
  90. long assetBytes = 0;
  91. foreach (TextureImage texture in Textures.Values)
  92. {
  93. if (texture != null)
  94. {
  95. if (texture.Temporary)
  96. {
  97. temporaryImages++;
  98. }
  99. imageBytes += texture.Data.GetLongLength(0);
  100. }
  101. }
  102. foreach (AssetInfo asset in Assets.Values)
  103. {
  104. if (asset != null)
  105. {
  106. if (asset.Temporary)
  107. {
  108. temporaryAssets++;
  109. }
  110. assetBytes += asset.Data.GetLongLength(0);
  111. }
  112. }
  113. m_log.InfoFormat("Temporary Images: {0} Temporary Assets: {1}",
  114. temporaryImages,
  115. temporaryAssets);
  116. m_log.InfoFormat("Image data: {0}kb Asset data: {1}kb",
  117. imageBytes / 1024,
  118. assetBytes / 1024);
  119. }
  120. /// <summary>
  121. /// Clear the asset cache.
  122. /// </summary>
  123. public void Clear()
  124. {
  125. m_log.Info("[ASSET CACHE]: Clearing Asset cache");
  126. if (StatsManager.SimExtraStats != null)
  127. StatsManager.SimExtraStats.ClearAssetCacheStatistics();
  128. Initialize();
  129. }
  130. /// <summary>
  131. /// Initialize the cache.
  132. /// </summary>
  133. private void Initialize()
  134. {
  135. Assets = new Dictionary<LLUUID, AssetInfo>();
  136. Textures = new Dictionary<LLUUID, TextureImage>();
  137. AssetRequests = new List<AssetRequest>();
  138. RequestedAssets = new Dictionary<LLUUID, AssetRequest>();
  139. RequestLists = new Dictionary<LLUUID, AssetRequestsList>();
  140. }
  141. /// <summary>
  142. /// Constructor. Initialize will need to be called separately.
  143. /// </summary>
  144. /// <param name="assetServer"></param>
  145. public AssetCache(IAssetServer assetServer)
  146. {
  147. m_log.Info("[ASSET CACHE]: Creating Asset cache");
  148. Initialize();
  149. m_assetServer = assetServer;
  150. m_assetServer.SetReceiver(this);
  151. m_assetCacheThread = new Thread(new ThreadStart(RunAssetManager));
  152. m_assetCacheThread.Name = "AssetCacheThread";
  153. m_assetCacheThread.IsBackground = true;
  154. m_assetCacheThread.Start();
  155. ThreadTracker.Add(m_assetCacheThread);
  156. }
  157. /// <summary>
  158. /// Process the asset queue which holds data which is packeted up and sent
  159. /// directly back to the client.
  160. /// </summary>
  161. public void RunAssetManager()
  162. {
  163. while (true)
  164. {
  165. try
  166. {
  167. ProcessAssetQueue();
  168. Thread.Sleep(500);
  169. }
  170. catch (Exception e)
  171. {
  172. m_log.Error("[ASSET CACHE]: " + e.ToString());
  173. }
  174. }
  175. }
  176. /// <summary>
  177. /// Only get an asset if we already have it in the cache.
  178. /// </summary>
  179. /// <param name="assetId"></param></param>
  180. /// <returns></returns>
  181. //private AssetBase GetCachedAsset(LLUUID assetId)
  182. //{
  183. // AssetBase asset = null;
  184. // if (Textures.ContainsKey(assetId))
  185. // {
  186. // asset = Textures[assetId];
  187. // }
  188. // else if (Assets.ContainsKey(assetId))
  189. // {
  190. // asset = Assets[assetId];
  191. // }
  192. // return asset;
  193. //}
  194. private bool TryGetCachedAsset(LLUUID assetId, out AssetBase asset)
  195. {
  196. if (Textures.ContainsKey(assetId))
  197. {
  198. asset = Textures[assetId];
  199. return true;
  200. }
  201. else if (Assets.ContainsKey(assetId))
  202. {
  203. asset = Assets[assetId];
  204. return true;
  205. }
  206. asset = null;
  207. return false;
  208. }
  209. /// <summary>
  210. /// Asynchronously retrieve an asset.
  211. /// </summary>
  212. /// <param name="assetId"></param>
  213. /// <param name="callback">
  214. /// A callback invoked when the asset has either been found or not found.
  215. /// If the asset was found this is called with the asset UUID and the asset data
  216. /// If the asset was not found this is still called with the asset UUID but with a null asset data reference</param>
  217. public void GetAsset(LLUUID assetId, AssetRequestCallback callback, bool isTexture)
  218. {
  219. //m_log.DebugFormat("[ASSET CACHE]: Requesting {0} {1}", isTexture ? "texture" : "asset", assetId);
  220. // Xantor 20080526:
  221. // if a request is made for an asset which is not in the cache yet, but has already been requested by
  222. // something else, queue up the callbacks on that requestor instead of swamping the assetserver
  223. // with multiple requests for the same asset.
  224. AssetBase asset;
  225. if (TryGetCachedAsset(assetId, out asset))
  226. {
  227. callback(assetId, asset);
  228. }
  229. else
  230. {
  231. #if DEBUG
  232. // m_log.DebugFormat("[ASSET CACHE]: Adding request for {0} {1}", isTexture ? "texture" : "asset", assetId);
  233. #endif
  234. NewAssetRequest req = new NewAssetRequest(assetId, callback);
  235. AssetRequestsList requestList;
  236. lock (RequestLists)
  237. {
  238. if (RequestLists.TryGetValue(assetId, out requestList)) // do we already have a request pending?
  239. {
  240. // m_log.DebugFormat("[ASSET CACHE]: Intercepted Duplicate request for {0} {1}", isTexture ? "texture" : "asset", assetId);
  241. // add to callbacks for this assetId
  242. RequestLists[assetId].Requests.Add(req);
  243. }
  244. else
  245. {
  246. // m_log.DebugFormat("[ASSET CACHE]: Adding request for {0} {1}", isTexture ? "texture" : "asset", assetId);
  247. requestList = new AssetRequestsList(assetId);
  248. RequestLists.Add(assetId, requestList);
  249. requestList.Requests.Add(req);
  250. m_assetServer.RequestAsset(assetId, isTexture);
  251. }
  252. }
  253. }
  254. }
  255. /// <summary>
  256. /// Synchronously retreive an asset. If the asset isn't in the cache, a request will be made to the persistent store to
  257. /// load it into the cache.
  258. ///
  259. /// XXX We'll keep polling the cache until we get the asset or we exceed
  260. /// the allowed number of polls. This isn't a very good way of doing things since a single thread
  261. /// is processing inbound packets, so if the asset server is slow, we could block this for up to
  262. /// the timeout period. What we might want to do is register asynchronous callbacks on asset
  263. /// receipt in the same manner as the TextureDownloadModule. Of course,
  264. /// a timeout before asset receipt usually isn't fatal, the operation will work on the retry when the
  265. /// asset is much more likely to have made it into the cache.
  266. /// </summary>
  267. /// <param name="assetID"></param>
  268. /// <param name="isTexture"></param>
  269. /// <returns>null if the asset could not be retrieved</returns>
  270. public AssetBase GetAsset(LLUUID assetID, bool isTexture)
  271. {
  272. // I'm not going over 3 seconds since this will be blocking processing of all the other inbound
  273. // packets from the client.
  274. int pollPeriod = 200;
  275. int maxPolls = 15;
  276. AssetBase asset;
  277. if (TryGetCachedAsset(assetID, out asset))
  278. {
  279. return asset;
  280. }
  281. else
  282. {
  283. m_assetServer.RequestAsset(assetID, isTexture);
  284. do
  285. {
  286. Thread.Sleep(pollPeriod);
  287. if (TryGetCachedAsset(assetID, out asset))
  288. {
  289. return asset;
  290. }
  291. } while (--maxPolls > 0);
  292. m_log.WarnFormat("[ASSET CACHE]: {0} {1} was not received before the retrieval timeout was reached",
  293. isTexture ? "texture" : "asset", assetID.ToString());
  294. return null;
  295. }
  296. }
  297. /// <summary>
  298. /// Add an asset to both the persistent store and the cache.
  299. /// </summary>
  300. /// <param name="asset"></param>
  301. public void AddAsset(AssetBase asset)
  302. {
  303. if (asset.Type == (int)AssetType.Texture)
  304. {
  305. if (!Textures.ContainsKey(asset.FullID))
  306. {
  307. TextureImage textur = new TextureImage(asset);
  308. Textures.Add(textur.FullID, textur);
  309. if (StatsManager.SimExtraStats != null)
  310. StatsManager.SimExtraStats.AddTexture(textur);
  311. if (!asset.Temporary)
  312. {
  313. m_assetServer.StoreAsset(asset);
  314. }
  315. }
  316. }
  317. else
  318. {
  319. if (!Assets.ContainsKey(asset.FullID))
  320. {
  321. AssetInfo assetInf = new AssetInfo(asset);
  322. Assets.Add(assetInf.FullID, assetInf);
  323. if (StatsManager.SimExtraStats != null)
  324. StatsManager.SimExtraStats.AddAsset(assetInf);
  325. if (!asset.Temporary)
  326. {
  327. m_assetServer.StoreAsset(asset);
  328. }
  329. }
  330. }
  331. }
  332. /// <summary>
  333. /// Allows you to clear a specific asset by uuid out
  334. /// of the asset cache. This is needed because the osdynamic
  335. /// texture code grows the asset cache without bounds. The
  336. /// real solution here is a much better cache archicture, but
  337. /// this is a stop gap measure until we have such a thing.
  338. /// </summary>
  339. public void ExpireAsset(LLUUID uuid)
  340. {
  341. // uuid is unique, so no need to worry about it showing up
  342. // in the 2 caches differently. Also, locks are probably
  343. // needed in all of this, or move to synchronized non
  344. // generic forms for Dictionaries.
  345. if (Textures.ContainsKey(uuid))
  346. {
  347. Textures.Remove(uuid);
  348. }
  349. else if (Assets.ContainsKey(uuid))
  350. {
  351. Assets.Remove(uuid);
  352. }
  353. }
  354. // See IAssetReceiver
  355. public void AssetReceived(AssetBase asset, bool IsTexture)
  356. {
  357. //check if it is a texture or not
  358. //then add to the correct cache list
  359. //then check for waiting requests for this asset/texture (in the Requested lists)
  360. //and move those requests into the Requests list.
  361. if (IsTexture)
  362. {
  363. TextureImage image = new TextureImage(asset);
  364. if (!Textures.ContainsKey(image.FullID))
  365. {
  366. Textures.Add(image.FullID, image);
  367. if (StatsManager.SimExtraStats != null)
  368. {
  369. StatsManager.SimExtraStats.AddTexture(image);
  370. }
  371. }
  372. }
  373. else
  374. {
  375. AssetInfo assetInf = new AssetInfo(asset);
  376. if (!Assets.ContainsKey(assetInf.FullID))
  377. {
  378. Assets.Add(assetInf.FullID, assetInf);
  379. if (StatsManager.SimExtraStats != null)
  380. {
  381. StatsManager.SimExtraStats.AddAsset(assetInf);
  382. }
  383. if (RequestedAssets.ContainsKey(assetInf.FullID))
  384. {
  385. AssetRequest req = RequestedAssets[assetInf.FullID];
  386. req.AssetInf = assetInf;
  387. req.NumPackets = CalculateNumPackets(assetInf.Data);
  388. RequestedAssets.Remove(assetInf.FullID);
  389. // If it's a direct request for a script, drop it
  390. // because it's a hacked client
  391. if(req.AssetRequestSource != 2 || assetInf.Type != 10)
  392. AssetRequests.Add(req);
  393. }
  394. }
  395. }
  396. // Notify requesters for this asset
  397. if (RequestLists.ContainsKey(asset.FullID))
  398. {
  399. AssetRequestsList reqList = null;
  400. lock (RequestLists)
  401. {
  402. //m_log.Info("AssetCache: Lock taken on requestLists (AssetReceived #1)");
  403. reqList = RequestLists[asset.FullID];
  404. }
  405. //m_log.Info("AssetCache: Lock released on requestLists (AssetReceived #1)");
  406. if (reqList != null)
  407. {
  408. //making a copy of the list is not ideal
  409. //but the old method of locking around this whole block of code was causing a multi-thread lock
  410. //between this and the TextureDownloadModule
  411. //while the localAsset thread running this and trying to send a texture to the callback in the
  412. //texturedownloadmodule , and hitting a lock in there. While the texturedownload thread (which was holding
  413. // the lock in the texturedownload module) was trying to
  414. //request a new asset and hitting a lock in here on the RequestLists.
  415. List<NewAssetRequest> theseRequests = new List<NewAssetRequest>(reqList.Requests);
  416. reqList.Requests.Clear();
  417. lock (RequestLists)
  418. {
  419. // m_log.Info("AssetCache: Lock taken on requestLists (AssetReceived #2)");
  420. RequestLists.Remove(asset.FullID);
  421. }
  422. //m_log.Info("AssetCache: Lock released on requestLists (AssetReceived #2)");
  423. foreach (NewAssetRequest req in theseRequests)
  424. {
  425. // Xantor 20080526 are we really calling all the callbacks if multiple queued for 1 request? -- Yes, checked
  426. // m_log.DebugFormat("[ASSET CACHE]: Callback for asset {0}", asset.FullID);
  427. req.Callback(asset.FullID, asset);
  428. }
  429. }
  430. }
  431. }
  432. // See IAssetReceiver
  433. public void AssetNotFound(LLUUID assetID, bool IsTexture)
  434. {
  435. //m_log.WarnFormat("[ASSET CACHE]: AssetNotFound for {0}", assetID);
  436. if (IsTexture)
  437. {
  438. Textures[assetID] = null;
  439. }
  440. else
  441. {
  442. Assets[assetID] = null;
  443. }
  444. // Notify requesters for this asset
  445. AssetRequestsList reqList = null;
  446. lock (RequestLists)
  447. {
  448. // m_log.Info("AssetCache: Lock taken on requestLists (AssetNotFound #1)");
  449. if (RequestLists.ContainsKey(assetID))
  450. {
  451. reqList = RequestLists[assetID];
  452. }
  453. }
  454. // m_log.Info("AssetCache: Lock released on requestLists (AssetNotFound #1)");
  455. if (reqList != null)
  456. {
  457. List<NewAssetRequest> theseRequests = new List<NewAssetRequest>(reqList.Requests);
  458. reqList.Requests.Clear();
  459. lock (RequestLists)
  460. {
  461. // m_log.Info("AssetCache: Lock taken on requestLists (AssetNotFound #2)");
  462. RequestLists.Remove(assetID);
  463. }
  464. // m_log.Info("AssetCache: Lock released on requestLists (AssetNotFound #2)");
  465. foreach (NewAssetRequest req in theseRequests)
  466. {
  467. req.Callback(assetID, null);
  468. }
  469. }
  470. }
  471. /// <summary>
  472. /// Calculate the number of packets required to send the asset to the client.
  473. /// </summary>
  474. /// <param name="data"></param>
  475. /// <returns></returns>
  476. private static int CalculateNumPackets(byte[] data)
  477. {
  478. const uint m_maxPacketSize = 600;
  479. int numPackets = 1;
  480. if (data.LongLength > m_maxPacketSize)
  481. {
  482. // over max number of bytes so split up file
  483. long restData = data.LongLength - m_maxPacketSize;
  484. int restPackets = (int)((restData + m_maxPacketSize - 1) / m_maxPacketSize);
  485. numPackets += restPackets;
  486. }
  487. return numPackets;
  488. }
  489. /// <summary>
  490. /// Handle an asset request from the client. The result will be sent back asynchronously.
  491. /// </summary>
  492. /// <param name="userInfo"></param>
  493. /// <param name="transferRequest"></param>
  494. public void AddAssetRequest(IClientAPI userInfo, TransferRequestPacket transferRequest)
  495. {
  496. LLUUID requestID = null;
  497. byte source = 2;
  498. if (transferRequest.TransferInfo.SourceType == 2)
  499. {
  500. //direct asset request
  501. requestID = new LLUUID(transferRequest.TransferInfo.Params, 0);
  502. }
  503. else if (transferRequest.TransferInfo.SourceType == 3)
  504. {
  505. //inventory asset request
  506. requestID = new LLUUID(transferRequest.TransferInfo.Params, 80);
  507. source = 3;
  508. //Console.WriteLine("asset request " + requestID);
  509. }
  510. //check to see if asset is in local cache, if not we need to request it from asset server.
  511. //Console.WriteLine("asset request " + requestID);
  512. if (!Assets.ContainsKey(requestID))
  513. {
  514. //not found asset
  515. // so request from asset server
  516. if (!RequestedAssets.ContainsKey(requestID))
  517. {
  518. AssetRequest request = new AssetRequest();
  519. request.RequestUser = userInfo;
  520. request.RequestAssetID = requestID;
  521. request.TransferRequestID = transferRequest.TransferInfo.TransferID;
  522. request.AssetRequestSource = source;
  523. request.Params = transferRequest.TransferInfo.Params;
  524. RequestedAssets.Add(requestID, request);
  525. m_assetServer.RequestAsset(requestID, false);
  526. }
  527. return;
  528. }
  529. // It has an entry in our cache
  530. AssetInfo asset = Assets[requestID];
  531. // FIXME: We never tell the client about assets which do not exist when requested by this transfer mechanism, which can't be right.
  532. if (null == asset)
  533. {
  534. //m_log.DebugFormat("[ASSET CACHE]: Asset transfer request for asset which is {0} already known to be missing. Dropping", requestID);
  535. return;
  536. }
  537. // Scripts cannot be retrieved by direct request
  538. if (transferRequest.TransferInfo.SourceType == 2 && asset.Type == 10)
  539. return;
  540. // The asset is knosn to exist and is in our cache, so add it to the AssetRequests list
  541. AssetRequest req = new AssetRequest();
  542. req.RequestUser = userInfo;
  543. req.RequestAssetID = requestID;
  544. req.TransferRequestID = transferRequest.TransferInfo.TransferID;
  545. req.AssetRequestSource = source;
  546. req.Params = transferRequest.TransferInfo.Params;
  547. req.AssetInf = asset;
  548. req.NumPackets = CalculateNumPackets(asset.Data);
  549. AssetRequests.Add(req);
  550. }
  551. /// <summary>
  552. /// Process the asset queue which sends packets directly back to the client.
  553. /// </summary>
  554. private void ProcessAssetQueue()
  555. {
  556. //should move the asset downloading to a module, like has been done with texture downloading
  557. if (AssetRequests.Count == 0)
  558. {
  559. //no requests waiting
  560. return;
  561. }
  562. // if less than 5, do all of them
  563. int num = Math.Min(5, AssetRequests.Count);
  564. AssetRequest req;
  565. AssetRequestToClient req2 = null;
  566. for (int i = 0; i < num; i++)
  567. {
  568. req = (AssetRequest)AssetRequests[i];
  569. if (req2 == null)
  570. {
  571. req2 = new AssetRequestToClient();
  572. }
  573. // Trying to limit memory usage by only creating AssetRequestToClient if needed
  574. //req2 = new AssetRequestToClient();
  575. req2.AssetInf = (AssetBase)req.AssetInf;
  576. req2.AssetRequestSource = req.AssetRequestSource;
  577. req2.DataPointer = req.DataPointer;
  578. req2.DiscardLevel = req.DiscardLevel;
  579. req2.ImageInfo = (AssetBase)req.ImageInfo;
  580. req2.IsTextureRequest = req.IsTextureRequest;
  581. req2.NumPackets = req.NumPackets;
  582. req2.PacketCounter = req.PacketCounter;
  583. req2.Params = req.Params;
  584. req2.RequestAssetID = req.RequestAssetID;
  585. req2.TransferRequestID = req.TransferRequestID;
  586. req.RequestUser.SendAsset(req2);
  587. }
  588. //remove requests that have been completed
  589. for (int i = 0; i < num; i++)
  590. {
  591. AssetRequests.RemoveAt(0);
  592. }
  593. }
  594. public class AssetRequest
  595. {
  596. public IClientAPI RequestUser;
  597. public LLUUID RequestAssetID;
  598. public AssetInfo AssetInf;
  599. public TextureImage ImageInfo;
  600. public LLUUID TransferRequestID;
  601. public long DataPointer = 0;
  602. public int NumPackets = 0;
  603. public int PacketCounter = 0;
  604. public bool IsTextureRequest;
  605. public byte AssetRequestSource = 2;
  606. public byte[] Params = null;
  607. //public bool AssetInCache;
  608. //public int TimeRequested;
  609. public int DiscardLevel = -1;
  610. public AssetRequest()
  611. {
  612. }
  613. }
  614. public class AssetInfo : AssetBase
  615. {
  616. public AssetInfo()
  617. {
  618. }
  619. public AssetInfo(AssetBase aBase)
  620. {
  621. Data = aBase.Data;
  622. FullID = aBase.FullID;
  623. Type = aBase.Type;
  624. Name = aBase.Name;
  625. Description = aBase.Description;
  626. }
  627. }
  628. public class TextureImage : AssetBase
  629. {
  630. public TextureImage()
  631. {
  632. }
  633. public TextureImage(AssetBase aBase)
  634. {
  635. Data = aBase.Data;
  636. FullID = aBase.FullID;
  637. Type = aBase.Type;
  638. Name = aBase.Name;
  639. Description = aBase.Description;
  640. }
  641. }
  642. public class AssetRequestsList
  643. {
  644. public LLUUID AssetID;
  645. public List<NewAssetRequest> Requests = new List<NewAssetRequest>();
  646. public AssetRequestsList(LLUUID assetID)
  647. {
  648. AssetID = assetID;
  649. }
  650. }
  651. public class NewAssetRequest
  652. {
  653. public LLUUID AssetID;
  654. public AssetRequestCallback Callback;
  655. public NewAssetRequest(LLUUID assetID, AssetRequestCallback callback)
  656. {
  657. AssetID = assetID;
  658. Callback = callback;
  659. }
  660. }
  661. }
  662. }