AssetCache.cs 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796
  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. Initialize();
  127. }
  128. /// <summary>
  129. /// Initialize the cache.
  130. /// </summary>
  131. private void Initialize()
  132. {
  133. Assets = new Dictionary<LLUUID, AssetInfo>();
  134. Textures = new Dictionary<LLUUID, TextureImage>();
  135. AssetRequests = new List<AssetRequest>();
  136. RequestedAssets = new Dictionary<LLUUID, AssetRequest>();
  137. RequestLists = new Dictionary<LLUUID, AssetRequestsList>();
  138. }
  139. /// <summary>
  140. /// Constructor. Initialize will need to be called separately.
  141. /// </summary>
  142. /// <param name="assetServer"></param>
  143. public AssetCache(IAssetServer assetServer)
  144. {
  145. m_log.Info("[ASSET CACHE]: Creating Asset cache");
  146. Initialize();
  147. m_assetServer = assetServer;
  148. m_assetServer.SetReceiver(this);
  149. m_assetCacheThread = new Thread(new ThreadStart(RunAssetManager));
  150. m_assetCacheThread.Name = "AssetCacheThread";
  151. m_assetCacheThread.IsBackground = true;
  152. m_assetCacheThread.Start();
  153. ThreadTracker.Add(m_assetCacheThread);
  154. }
  155. /// <summary>
  156. /// Process the asset queue which holds data which is packeted up and sent
  157. /// directly back to the client.
  158. /// </summary>
  159. public void RunAssetManager()
  160. {
  161. while (true)
  162. {
  163. try
  164. {
  165. ProcessAssetQueue();
  166. Thread.Sleep(500);
  167. }
  168. catch (Exception e)
  169. {
  170. m_log.Error("[ASSET CACHE]: " + e.ToString());
  171. }
  172. }
  173. }
  174. /// <summary>
  175. /// Only get an asset if we already have it in the cache.
  176. /// </summary>
  177. /// <param name="assetId"></param></param>
  178. /// <returns></returns>
  179. //private AssetBase GetCachedAsset(LLUUID assetId)
  180. //{
  181. // AssetBase asset = null;
  182. // if (Textures.ContainsKey(assetId))
  183. // {
  184. // asset = Textures[assetId];
  185. // }
  186. // else if (Assets.ContainsKey(assetId))
  187. // {
  188. // asset = Assets[assetId];
  189. // }
  190. // return asset;
  191. //}
  192. private bool TryGetCachedAsset(LLUUID assetId, out AssetBase asset)
  193. {
  194. if (Textures.ContainsKey(assetId))
  195. {
  196. asset = Textures[assetId];
  197. return true;
  198. }
  199. else if (Assets.ContainsKey(assetId))
  200. {
  201. asset = Assets[assetId];
  202. return true;
  203. }
  204. asset = null;
  205. return false;
  206. }
  207. /// <summary>
  208. /// Asynchronously retrieve an asset.
  209. /// </summary>
  210. /// <param name="assetId"></param>
  211. /// <param name="callback">
  212. /// A callback invoked when the asset has either been found or not found.
  213. /// If the asset was found this is called with the asset UUID and the asset data
  214. /// If the asset was not found this is still called with the asset UUID but with a null asset data reference</param>
  215. public void GetAsset(LLUUID assetId, AssetRequestCallback callback, bool isTexture)
  216. {
  217. //m_log.DebugFormat("[ASSET CACHE]: Requesting {0} {1}", isTexture ? "texture" : "asset", assetId);
  218. // Xantor 20080526:
  219. // if a request is made for an asset which is not in the cache yet, but has already been requested by
  220. // something else, queue up the callbacks on that requestor instead of swamping the assetserver
  221. // with multiple requests for the same asset.
  222. AssetBase asset;
  223. if (TryGetCachedAsset(assetId, out asset))
  224. {
  225. callback(assetId, asset);
  226. }
  227. else
  228. {
  229. #if DEBUG
  230. // m_log.DebugFormat("[ASSET CACHE]: Adding request for {0} {1}", isTexture ? "texture" : "asset", assetId);
  231. #endif
  232. NewAssetRequest req = new NewAssetRequest(assetId, callback);
  233. AssetRequestsList requestList;
  234. lock (RequestLists)
  235. {
  236. if (RequestLists.TryGetValue(assetId, out requestList)) // do we already have a request pending?
  237. {
  238. // m_log.DebugFormat("[ASSET CACHE]: Intercepted Duplicate request for {0} {1}", isTexture ? "texture" : "asset", assetId);
  239. // add to callbacks for this assetId
  240. RequestLists[assetId].Requests.Add(req);
  241. }
  242. else
  243. {
  244. // m_log.DebugFormat("[ASSET CACHE]: Adding request for {0} {1}", isTexture ? "texture" : "asset", assetId);
  245. requestList = new AssetRequestsList(assetId);
  246. RequestLists.Add(assetId, requestList);
  247. requestList.Requests.Add(req);
  248. m_assetServer.RequestAsset(assetId, isTexture);
  249. }
  250. }
  251. /* Old code doesn't handle duplicate requests right
  252. NewAssetRequest req = new NewAssetRequest(assetId, callback);
  253. // Make sure we always have a request list to which to add the asset
  254. AssetRequestsList requestList;
  255. lock (RequestLists)
  256. {
  257. // m_log.Info("AssetCache: Lock taken on requestLists (GetAsset)");
  258. if (!RequestLists.TryGetValue(assetId, out requestList))
  259. {
  260. requestList = new AssetRequestsList(assetId);
  261. RequestLists.Add(assetId, requestList);
  262. }
  263. }
  264. // m_log.Info("AssetCache: Lock released on requestLists (GetAsset)");
  265. requestList.Requests.Add(req);
  266. m_assetServer.RequestAsset(assetId, isTexture);
  267. */
  268. }
  269. }
  270. /// <summary>
  271. /// Synchronously retreive an asset. If the asset isn't in the cache, a request will be made to the persistent store to
  272. /// load it into the cache.
  273. ///
  274. /// XXX We'll keep polling the cache until we get the asset or we exceed
  275. /// the allowed number of polls. This isn't a very good way of doing things since a single thread
  276. /// is processing inbound packets, so if the asset server is slow, we could block this for up to
  277. /// the timeout period. What we might want to do is register asynchronous callbacks on asset
  278. /// receipt in the same manner as the TextureDownloadModule. Of course,
  279. /// a timeout before asset receipt usually isn't fatal, the operation will work on the retry when the
  280. /// asset is much more likely to have made it into the cache.
  281. /// </summary>
  282. /// <param name="assetID"></param>
  283. /// <param name="isTexture"></param>
  284. /// <returns>null if the asset could not be retrieved</returns>
  285. public AssetBase GetAsset(LLUUID assetID, bool isTexture)
  286. {
  287. // I'm not going over 3 seconds since this will be blocking processing of all the other inbound
  288. // packets from the client.
  289. int pollPeriod = 200;
  290. int maxPolls = 15;
  291. AssetBase asset;
  292. if (TryGetCachedAsset(assetID, out asset))
  293. {
  294. return asset;
  295. }
  296. else
  297. {
  298. m_assetServer.RequestAsset(assetID, isTexture);
  299. do
  300. {
  301. Thread.Sleep(pollPeriod);
  302. if (TryGetCachedAsset(assetID, out asset))
  303. {
  304. return asset;
  305. }
  306. } while (--maxPolls > 0);
  307. m_log.WarnFormat("[ASSET CACHE]: {0} {1} was not received before the retrieval timeout was reached",
  308. isTexture ? "texture" : "asset", assetID.ToString());
  309. return null;
  310. }
  311. }
  312. /// <summary>
  313. /// Add an asset to both the persistent store and the cache.
  314. /// </summary>
  315. /// <param name="asset"></param>
  316. public void AddAsset(AssetBase asset)
  317. {
  318. if (asset.Type == 0)
  319. {
  320. if (!Textures.ContainsKey(asset.FullID))
  321. {
  322. TextureImage textur = new TextureImage(asset);
  323. Textures.Add(textur.FullID, textur);
  324. if (StatsManager.SimExtraStats != null)
  325. StatsManager.SimExtraStats.AddTexture(textur);
  326. if (!asset.Temporary)
  327. {
  328. m_assetServer.StoreAndCommitAsset(asset);
  329. }
  330. }
  331. }
  332. else
  333. {
  334. if (!Assets.ContainsKey(asset.FullID))
  335. {
  336. AssetInfo assetInf = new AssetInfo(asset);
  337. Assets.Add(assetInf.FullID, assetInf);
  338. if (StatsManager.SimExtraStats != null)
  339. StatsManager.SimExtraStats.AddAsset(assetInf);
  340. if (!asset.Temporary)
  341. {
  342. m_assetServer.StoreAndCommitAsset(asset);
  343. }
  344. }
  345. }
  346. }
  347. // See IAssetReceiver
  348. public void AssetReceived(AssetBase asset, bool IsTexture)
  349. {
  350. //check if it is a texture or not
  351. //then add to the correct cache list
  352. //then check for waiting requests for this asset/texture (in the Requested lists)
  353. //and move those requests into the Requests list.
  354. if (IsTexture)
  355. {
  356. TextureImage image = new TextureImage(asset);
  357. if (!Textures.ContainsKey(image.FullID))
  358. {
  359. Textures.Add(image.FullID, image);
  360. if (StatsManager.SimExtraStats != null)
  361. {
  362. StatsManager.SimExtraStats.AddTexture(image);
  363. }
  364. }
  365. }
  366. else
  367. {
  368. AssetInfo assetInf = new AssetInfo(asset);
  369. if (!Assets.ContainsKey(assetInf.FullID))
  370. {
  371. Assets.Add(assetInf.FullID, assetInf);
  372. if (StatsManager.SimExtraStats != null)
  373. {
  374. StatsManager.SimExtraStats.AddAsset(assetInf);
  375. }
  376. if (RequestedAssets.ContainsKey(assetInf.FullID))
  377. {
  378. AssetRequest req = RequestedAssets[assetInf.FullID];
  379. req.AssetInf = assetInf;
  380. req.NumPackets = CalculateNumPackets(assetInf.Data);
  381. RequestedAssets.Remove(assetInf.FullID);
  382. AssetRequests.Add(req);
  383. }
  384. }
  385. }
  386. // Notify requesters for this asset
  387. if (RequestLists.ContainsKey(asset.FullID))
  388. {
  389. AssetRequestsList reqList = null;
  390. lock (RequestLists)
  391. {
  392. //m_log.Info("AssetCache: Lock taken on requestLists (AssetReceived #1)");
  393. reqList = RequestLists[asset.FullID];
  394. }
  395. //m_log.Info("AssetCache: Lock released on requestLists (AssetReceived #1)");
  396. if (reqList != null)
  397. {
  398. //making a copy of the list is not ideal
  399. //but the old method of locking around this whole block of code was causing a multi-thread lock
  400. //between this and the TextureDownloadModule
  401. //while the localAsset thread running this and trying to send a texture to the callback in the
  402. //texturedownloadmodule , and hitting a lock in there. While the texturedownload thread (which was holding
  403. // the lock in the texturedownload module) was trying to
  404. //request a new asset and hitting a lock in here on the RequestLists.
  405. List<NewAssetRequest> theseRequests = new List<NewAssetRequest>(reqList.Requests);
  406. reqList.Requests.Clear();
  407. lock (RequestLists)
  408. {
  409. // m_log.Info("AssetCache: Lock taken on requestLists (AssetReceived #2)");
  410. RequestLists.Remove(asset.FullID);
  411. }
  412. //m_log.Info("AssetCache: Lock released on requestLists (AssetReceived #2)");
  413. foreach (NewAssetRequest req in theseRequests)
  414. {
  415. // Xantor 20080526 are we really calling all the callbacks if multiple queued for 1 request? -- Yes, checked
  416. // m_log.DebugFormat("[ASSET CACHE]: Callback for asset {0}", asset.FullID);
  417. req.Callback(asset.FullID, asset);
  418. }
  419. }
  420. }
  421. }
  422. // See IAssetReceiver
  423. public void AssetNotFound(LLUUID assetID, bool IsTexture)
  424. {
  425. //m_log.WarnFormat("[ASSET CACHE]: AssetNotFound for {0}", assetID);
  426. if (IsTexture)
  427. {
  428. Textures[assetID] = null;
  429. }
  430. else
  431. {
  432. Assets[assetID] = null;
  433. }
  434. // Notify requesters for this asset
  435. AssetRequestsList reqList = null;
  436. lock (RequestLists)
  437. {
  438. // m_log.Info("AssetCache: Lock taken on requestLists (AssetNotFound #1)");
  439. if (RequestLists.ContainsKey(assetID))
  440. {
  441. reqList = RequestLists[assetID];
  442. }
  443. }
  444. // m_log.Info("AssetCache: Lock released on requestLists (AssetNotFound #1)");
  445. if (reqList != null)
  446. {
  447. List<NewAssetRequest> theseRequests = new List<NewAssetRequest>(reqList.Requests);
  448. reqList.Requests.Clear();
  449. lock (RequestLists)
  450. {
  451. // m_log.Info("AssetCache: Lock taken on requestLists (AssetNotFound #2)");
  452. RequestLists.Remove(assetID);
  453. }
  454. // m_log.Info("AssetCache: Lock released on requestLists (AssetNotFound #2)");
  455. foreach (NewAssetRequest req in theseRequests)
  456. {
  457. req.Callback(assetID, null);
  458. }
  459. }
  460. }
  461. /// <summary>
  462. /// Calculate the number of packets required to send the asset to the client.
  463. /// </summary>
  464. /// <param name="data"></param>
  465. /// <returns></returns>
  466. private static int CalculateNumPackets(byte[] data)
  467. {
  468. const uint m_maxPacketSize = 600;
  469. int numPackets = 1;
  470. if (data.LongLength > m_maxPacketSize)
  471. {
  472. // over max number of bytes so split up file
  473. long restData = data.LongLength - m_maxPacketSize;
  474. int restPackets = (int)((restData + m_maxPacketSize - 1) / m_maxPacketSize);
  475. numPackets += restPackets;
  476. }
  477. return numPackets;
  478. }
  479. /// <summary>
  480. /// Handle an asset request from the client. The result will be sent back asynchronously.
  481. /// </summary>
  482. /// <param name="userInfo"></param>
  483. /// <param name="transferRequest"></param>
  484. public void AddAssetRequest(IClientAPI userInfo, TransferRequestPacket transferRequest)
  485. {
  486. LLUUID requestID = null;
  487. byte source = 2;
  488. if (transferRequest.TransferInfo.SourceType == 2)
  489. {
  490. //direct asset request
  491. requestID = new LLUUID(transferRequest.TransferInfo.Params, 0);
  492. }
  493. else if (transferRequest.TransferInfo.SourceType == 3)
  494. {
  495. //inventory asset request
  496. requestID = new LLUUID(transferRequest.TransferInfo.Params, 80);
  497. source = 3;
  498. //Console.WriteLine("asset request " + requestID);
  499. }
  500. //check to see if asset is in local cache, if not we need to request it from asset server.
  501. //Console.WriteLine("asset request " + requestID);
  502. if (!Assets.ContainsKey(requestID))
  503. {
  504. //not found asset
  505. // so request from asset server
  506. if (!RequestedAssets.ContainsKey(requestID))
  507. {
  508. AssetRequest request = new AssetRequest();
  509. request.RequestUser = userInfo;
  510. request.RequestAssetID = requestID;
  511. request.TransferRequestID = transferRequest.TransferInfo.TransferID;
  512. request.AssetRequestSource = source;
  513. request.Params = transferRequest.TransferInfo.Params;
  514. RequestedAssets.Add(requestID, request);
  515. m_assetServer.RequestAsset(requestID, false);
  516. }
  517. return;
  518. }
  519. // It has an entry in our cache
  520. AssetInfo asset = Assets[requestID];
  521. // FIXME: We never tell the client about assets which do not exist when requested by this transfer mechanism, which can't be right.
  522. if (null == asset)
  523. {
  524. //m_log.DebugFormat("[ASSET CACHE]: Asset transfer request for asset which is {0} already known to be missing. Dropping", requestID);
  525. return;
  526. }
  527. // The asset is knosn to exist and is in our cache, so add it to the AssetRequests list
  528. AssetRequest req = new AssetRequest();
  529. req.RequestUser = userInfo;
  530. req.RequestAssetID = requestID;
  531. req.TransferRequestID = transferRequest.TransferInfo.TransferID;
  532. req.AssetRequestSource = source;
  533. req.Params = transferRequest.TransferInfo.Params;
  534. req.AssetInf = asset;
  535. req.NumPackets = CalculateNumPackets(asset.Data);
  536. AssetRequests.Add(req);
  537. }
  538. /// <summary>
  539. /// Process the asset queue which sends packets directly back to the client.
  540. /// </summary>
  541. private void ProcessAssetQueue()
  542. {
  543. //should move the asset downloading to a module, like has been done with texture downloading
  544. if (AssetRequests.Count == 0)
  545. {
  546. //no requests waiting
  547. return;
  548. }
  549. // if less than 5, do all of them
  550. int num = Math.Min(5, AssetRequests.Count);
  551. AssetRequest req;
  552. for (int i = 0; i < num; i++)
  553. {
  554. req = (AssetRequest)AssetRequests[i];
  555. //Console.WriteLine("sending asset " + req.RequestAssetID);
  556. TransferInfoPacket Transfer = new TransferInfoPacket();
  557. Transfer.TransferInfo.ChannelType = 2;
  558. Transfer.TransferInfo.Status = 0;
  559. Transfer.TransferInfo.TargetType = 0;
  560. if (req.AssetRequestSource == 2)
  561. {
  562. Transfer.TransferInfo.Params = new byte[20];
  563. Array.Copy(req.RequestAssetID.GetBytes(), 0, Transfer.TransferInfo.Params, 0, 16);
  564. int assType = (int)req.AssetInf.Type;
  565. Array.Copy(Helpers.IntToBytes(assType), 0, Transfer.TransferInfo.Params, 16, 4);
  566. }
  567. else if (req.AssetRequestSource == 3)
  568. {
  569. Transfer.TransferInfo.Params = req.Params;
  570. // Transfer.TransferInfo.Params = new byte[100];
  571. //Array.Copy(req.RequestUser.AgentId.GetBytes(), 0, Transfer.TransferInfo.Params, 0, 16);
  572. //Array.Copy(req.RequestUser.SessionId.GetBytes(), 0, Transfer.TransferInfo.Params, 16, 16);
  573. }
  574. Transfer.TransferInfo.Size = (int)req.AssetInf.Data.Length;
  575. Transfer.TransferInfo.TransferID = req.TransferRequestID;
  576. Transfer.Header.Zerocoded = true;
  577. req.RequestUser.OutPacket(Transfer, ThrottleOutPacketType.Asset);
  578. if (req.NumPackets == 1)
  579. {
  580. TransferPacketPacket TransferPacket = new TransferPacketPacket();
  581. TransferPacket.TransferData.Packet = 0;
  582. TransferPacket.TransferData.ChannelType = 2;
  583. TransferPacket.TransferData.TransferID = req.TransferRequestID;
  584. TransferPacket.TransferData.Data = req.AssetInf.Data;
  585. TransferPacket.TransferData.Status = 1;
  586. TransferPacket.Header.Zerocoded = true;
  587. req.RequestUser.OutPacket(TransferPacket, ThrottleOutPacketType.Asset);
  588. }
  589. else
  590. {
  591. int processedLength = 0;
  592. // libsecondlife hardcodes 1500 as the maximum data chunk size
  593. int maxChunkSize = 1250;
  594. int packetNumber = 0;
  595. while (processedLength < req.AssetInf.Data.Length)
  596. {
  597. TransferPacketPacket TransferPacket = new TransferPacketPacket();
  598. TransferPacket.TransferData.Packet = packetNumber;
  599. TransferPacket.TransferData.ChannelType = 2;
  600. TransferPacket.TransferData.TransferID = req.TransferRequestID;
  601. int chunkSize = Math.Min(req.AssetInf.Data.Length - processedLength, maxChunkSize);
  602. byte[] chunk = new byte[chunkSize];
  603. Array.Copy(req.AssetInf.Data, processedLength, chunk, 0, chunk.Length);
  604. TransferPacket.TransferData.Data = chunk;
  605. // 0 indicates more packets to come, 1 indicates last packet
  606. if (req.AssetInf.Data.Length - processedLength > maxChunkSize)
  607. {
  608. TransferPacket.TransferData.Status = 0;
  609. }
  610. else
  611. {
  612. TransferPacket.TransferData.Status = 1;
  613. }
  614. TransferPacket.Header.Zerocoded = true;
  615. req.RequestUser.OutPacket(TransferPacket, ThrottleOutPacketType.Asset);
  616. processedLength += chunkSize;
  617. packetNumber++;
  618. }
  619. }
  620. }
  621. //remove requests that have been completed
  622. for (int i = 0; i < num; i++)
  623. {
  624. AssetRequests.RemoveAt(0);
  625. }
  626. }
  627. public class AssetRequest
  628. {
  629. public IClientAPI RequestUser;
  630. public LLUUID RequestAssetID;
  631. public AssetInfo AssetInf;
  632. public TextureImage ImageInfo;
  633. public LLUUID TransferRequestID;
  634. public long DataPointer = 0;
  635. public int NumPackets = 0;
  636. public int PacketCounter = 0;
  637. public bool IsTextureRequest;
  638. public byte AssetRequestSource = 2;
  639. public byte[] Params = null;
  640. //public bool AssetInCache;
  641. //public int TimeRequested;
  642. public int DiscardLevel = -1;
  643. public AssetRequest()
  644. {
  645. }
  646. }
  647. public class AssetInfo : AssetBase
  648. {
  649. public AssetInfo()
  650. {
  651. }
  652. public AssetInfo(AssetBase aBase)
  653. {
  654. Data = aBase.Data;
  655. FullID = aBase.FullID;
  656. Type = aBase.Type;
  657. InvType = aBase.InvType;
  658. Name = aBase.Name;
  659. Description = aBase.Description;
  660. }
  661. }
  662. public class TextureImage : AssetBase
  663. {
  664. public TextureImage()
  665. {
  666. }
  667. public TextureImage(AssetBase aBase)
  668. {
  669. Data = aBase.Data;
  670. FullID = aBase.FullID;
  671. Type = aBase.Type;
  672. InvType = aBase.InvType;
  673. Name = aBase.Name;
  674. Description = aBase.Description;
  675. }
  676. }
  677. public class AssetRequestsList
  678. {
  679. public LLUUID AssetID;
  680. public List<NewAssetRequest> Requests = new List<NewAssetRequest>();
  681. public AssetRequestsList(LLUUID assetID)
  682. {
  683. AssetID = assetID;
  684. }
  685. }
  686. public class NewAssetRequest
  687. {
  688. public LLUUID AssetID;
  689. public AssetRequestCallback Callback;
  690. public NewAssetRequest(LLUUID assetID, AssetRequestCallback callback)
  691. {
  692. AssetID = assetID;
  693. Callback = callback;
  694. }
  695. }
  696. }
  697. }