AssetCache.cs 30 KB

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