LLImageManager.cs 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664
  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 OpenMetaverse;
  30. using OpenSim.Framework;
  31. using OpenSim.Region.Environment.Interfaces;
  32. using C5;
  33. using OpenSim.Framework.Communications.Cache;
  34. using OpenMetaverse.Imaging;
  35. namespace OpenSim.Region.ClientStack.LindenUDP
  36. {
  37. /// <summary>
  38. /// Client image priority + discardlevel sender/manager
  39. /// </summary>
  40. public class LLImageManager
  41. {
  42. /// <summary>
  43. /// Priority Queue for images. Contains lots of data
  44. /// </summary>
  45. private readonly IPriorityQueue<Prio<J2KImage>> pq = new IntervalHeap<Prio<J2KImage>>();
  46. /// <summary>
  47. /// Dictionary of PriorityQueue handles by AssetId
  48. /// </summary>
  49. private readonly Dictionary<UUID, IPriorityQueueHandle<Prio<J2KImage>>> PQHandles =
  50. new Dictionary<UUID, IPriorityQueueHandle<Prio<J2KImage>>>();
  51. private LLClientView m_client;
  52. private readonly AssetCache m_assetCache;
  53. private bool m_shuttingdown = false;
  54. private readonly IJ2KDecoder m_j2kDecodeModule;
  55. private readonly AssetBase MissingSubstitute;
  56. /// <summary>
  57. /// Client image priority + discardlevel sender/manager
  58. /// </summary>
  59. /// <param name="client">LLClientView of client</param>
  60. /// <param name="pAssetCache">The Asset retrieval system</param>
  61. /// <param name="pJ2kDecodeModule">The Jpeg2000 Decoder</param>
  62. public LLImageManager(LLClientView client, AssetCache pAssetCache, IJ2KDecoder pJ2kDecodeModule)
  63. {
  64. m_client = client;
  65. m_assetCache = pAssetCache;
  66. if (pAssetCache != null)
  67. MissingSubstitute = pAssetCache.GetAsset(UUID.Parse("5748decc-f629-461c-9a36-a35a221fe21f"), true);
  68. m_j2kDecodeModule = pJ2kDecodeModule;
  69. }
  70. /// <summary>
  71. /// Enqueues a texture request
  72. /// </summary>
  73. /// <param name="req">Request from the client to get a texture</param>
  74. public void EnqueueReq(TextureRequestArgs req)
  75. {
  76. if (m_shuttingdown)
  77. return;
  78. //if (req.RequestType == 1) // avatar body texture!
  79. // return;
  80. AddQueueItem(req.RequestedAssetID, (int)req.Priority + 100000);
  81. //if (pq[PQHandles[req.RequestedAssetID]].data.Missing)
  82. //{
  83. // pq[PQHandles[req.RequestedAssetID]] -= 900000;
  84. //}
  85. //
  86. //if (pq[PQHandles[req.RequestedAssetID]].data.HasData && pq[PQHandles[req.RequestedAssetID]].data.Layers.Length > 0)
  87. //{
  88. //}
  89. pq[PQHandles[req.RequestedAssetID]].data.requestedUUID = req.RequestedAssetID;
  90. pq[PQHandles[req.RequestedAssetID]].data.Priority = (int)req.Priority;
  91. lock (pq[PQHandles[req.RequestedAssetID]].data)
  92. pq[PQHandles[req.RequestedAssetID]].data.Update(req.DiscardLevel, (int)req.PacketNumber);
  93. }
  94. /// <summary>
  95. /// Callback for the asset system
  96. /// </summary>
  97. /// <param name="assetID">UUID of the asset that we have received</param>
  98. /// <param name="asset">AssetBase of the asset that we've received</param>
  99. public void AssetDataCallback(UUID assetID, AssetBase asset)
  100. {
  101. if (m_shuttingdown)
  102. return;
  103. //Console.WriteLine("AssetCallback for assetId" + assetID);
  104. if (asset == null || asset.Data == null)
  105. {
  106. lock (pq)
  107. {
  108. //pq[PQHandles[assetID]].data.Missing = true;
  109. pq[PQHandles[assetID]].data.asset = MissingSubstitute;
  110. pq[PQHandles[assetID]].data.Missing = false;
  111. }
  112. }
  113. //else
  114. pq[PQHandles[assetID]].data.asset = asset;
  115. lock (pq[PQHandles[assetID]].data)
  116. pq[PQHandles[assetID]].data.Update((int)pq[PQHandles[assetID]].data.Priority, (int)pq[PQHandles[assetID]].data.CurrentPacket);
  117. }
  118. /// <summary>
  119. /// Processes the image queue. Pops count elements off and processes them
  120. /// </summary>
  121. /// <param name="count">number of images to peek off the queue</param>
  122. public void ProcessImageQueue(int count)
  123. {
  124. if (m_shuttingdown)
  125. return;
  126. IPriorityQueueHandle<Prio<J2KImage>> h = null;
  127. for (int j = 0; j < count; j++)
  128. {
  129. lock (pq)
  130. {
  131. if (!pq.IsEmpty)
  132. {
  133. //peek off the top
  134. Prio<J2KImage> process = pq.FindMax(out h);
  135. // Do we have the Asset Data?
  136. if (!process.data.HasData)
  137. {
  138. // Did we request the asset data?
  139. if (!process.data.dataRequested)
  140. {
  141. m_assetCache.GetAsset(process.data.requestedUUID, AssetDataCallback, true);
  142. pq[h].data.dataRequested = true;
  143. }
  144. // Is the asset missing?
  145. if (process.data.Missing)
  146. {
  147. //m_client.sendtextur
  148. pq[h] -= 90000;
  149. /*
  150. {
  151. OpenMetaverse.Packets.ImageNotInDatabasePacket imdback =
  152. new OpenMetaverse.Packets.ImageNotInDatabasePacket();
  153. imdback.ImageID =
  154. new OpenMetaverse.Packets.ImageNotInDatabasePacket.ImageIDBlock();
  155. imdback.ImageID.ID = process.data.requestedUUID;
  156. m_client.OutPacket(imdback, ThrottleOutPacketType.Texture);
  157. }
  158. */
  159. // Substitute a blank image
  160. process.data.asset = MissingSubstitute;
  161. process.data.Missing = false;
  162. // If the priority is less then -4billion, the client has forgotten about it.
  163. if (pq[h] < -400000000)
  164. {
  165. RemoveItemFromQueue(pq[h].data.requestedUUID);
  166. continue;
  167. }
  168. }
  169. // Lower the priority to give the next image a chance
  170. pq[h] -= 100000;
  171. }
  172. else if (process.data.HasData)
  173. {
  174. // okay, we've got the data
  175. lock (process.data)
  176. {
  177. if (!process.data.J2KDecode && !process.data.J2KDecodeWaiting)
  178. {
  179. process.data.J2KDecodeWaiting = true;
  180. // Do we have a jpeg decoder?
  181. if (m_j2kDecodeModule != null)
  182. {
  183. // Send it off to the jpeg decoder
  184. m_j2kDecodeModule.decode(process.data.requestedUUID, process.data.Data,
  185. j2kDecodedCallback);
  186. }
  187. else
  188. {
  189. // no module, no layers, full resolution only
  190. j2kDecodedCallback(process.data.AssetId, new OpenJPEG.J2KLayerInfo[0]);
  191. }
  192. } // Are we waiting?
  193. else if (!process.data.J2KDecodeWaiting)
  194. {
  195. // Send more data at a time for higher discard levels
  196. for (int i = 0; i < (2*(5 - process.data.DiscardLevel) + 1)*2; i++)
  197. if (!process.data.SendPacket(m_client))
  198. {
  199. pq[h] -= (500000*i);
  200. break;
  201. }
  202. }
  203. // If the priority is less then -4 billion, the client has forgotten about it, pop it off
  204. if (pq[h] < -400000000)
  205. {
  206. RemoveItemFromQueue(pq[h].data.requestedUUID);
  207. continue;
  208. }
  209. }
  210. //pq[h] = process;
  211. }
  212. // uncomment the following line to see the upper most asset and the priority
  213. //Console.WriteLine(process.ToString());
  214. // Lower priority to give the next image a chance to bubble up
  215. pq[h] -= 50000;
  216. }
  217. }
  218. }
  219. }
  220. /// <summary>
  221. /// Callback for when the image has been decoded
  222. /// </summary>
  223. /// <param name="AssetId">The UUID of the Asset</param>
  224. /// <param name="layers">The Jpeg2000 discard level Layer start and end byte offsets Array. 0 elements for failed or no decoder</param>
  225. public void j2kDecodedCallback(UUID AssetId, OpenJPEG.J2KLayerInfo[] layers)
  226. {
  227. // are we shutting down? if so, end.
  228. if (m_shuttingdown)
  229. return;
  230. lock (PQHandles)
  231. {
  232. // Update our asset data
  233. if (PQHandles.ContainsKey(AssetId))
  234. {
  235. pq[PQHandles[AssetId]].data.Layers = layers;
  236. pq[PQHandles[AssetId]].data.J2KDecode = true;
  237. pq[PQHandles[AssetId]].data.J2KDecodeWaiting = false;
  238. lock (pq[PQHandles[AssetId]].data)
  239. pq[PQHandles[AssetId]].data.Update((int)pq[PQHandles[AssetId]].data.Priority, (int)pq[PQHandles[AssetId]].data.CurrentPacket);
  240. // Send the first packet
  241. pq[PQHandles[AssetId]].data.SendPacket(m_client);
  242. }
  243. }
  244. }
  245. /// <summary>
  246. /// This image has had a good life. It's now expired. Remove it off the queue
  247. /// </summary>
  248. /// <param name="AssetId">UUID of asset to remove off the queue</param>
  249. private void RemoveItemFromQueue(UUID AssetId)
  250. {
  251. lock (PQHandles)
  252. {
  253. if (PQHandles.ContainsKey(AssetId))
  254. {
  255. IPriorityQueueHandle<Prio<J2KImage>> h = PQHandles[AssetId];
  256. PQHandles.Remove(AssetId);
  257. pq.Delete(h);
  258. }
  259. }
  260. }
  261. /// <summary>
  262. /// Adds an image to the queue and update priority
  263. /// if the item is already in the queue, just update the priority
  264. /// </summary>
  265. /// <param name="AssetId">UUID of the asset</param>
  266. /// <param name="priority">Priority to set</param>
  267. private void AddQueueItem(UUID AssetId, int priority)
  268. {
  269. IPriorityQueueHandle<Prio<J2KImage>> h = null;
  270. lock (PQHandles)
  271. {
  272. if (PQHandles.ContainsKey(AssetId))
  273. {
  274. h = PQHandles[AssetId];
  275. pq[h] = pq[h].SetPriority(priority);
  276. }
  277. else
  278. {
  279. J2KImage newreq = new J2KImage();
  280. newreq.requestedUUID = AssetId;
  281. pq.Add(ref h, new Prio<J2KImage>(newreq, priority));
  282. PQHandles.Add(AssetId, h);
  283. }
  284. }
  285. }
  286. /// <summary>
  287. /// Okay, we're ending. Clean up on isle 9
  288. /// </summary>
  289. public void Close()
  290. {
  291. m_shuttingdown = true;
  292. lock (pq)
  293. {
  294. while (!pq.IsEmpty)
  295. {
  296. pq.DeleteMin();
  297. }
  298. }
  299. lock (PQHandles)
  300. PQHandles.Clear();
  301. m_client = null;
  302. }
  303. }
  304. /// <summary>
  305. /// Image Data for this send
  306. /// Encapsulates the image sending data and method
  307. /// </summary>
  308. public class J2KImage
  309. {
  310. private AssetBase m_asset_ref = null;
  311. public volatile int LastPacketNum = 0;
  312. public volatile int DiscardLimit = 0;
  313. public volatile bool dataRequested = false;
  314. public OpenJPEG.J2KLayerInfo[] Layers = new OpenJPEG.J2KLayerInfo[0];
  315. public const int FIRST_IMAGE_PACKET_SIZE = 600;
  316. public const int IMAGE_PACKET_SIZE = 1000;
  317. public volatile int DiscardLevel;
  318. public float Priority;
  319. public volatile int CurrentPacket = 1;
  320. public volatile int StopPacket;
  321. public bool Missing = false;
  322. public bool J2KDecode = false;
  323. public bool J2KDecodeWaiting = false;
  324. private volatile bool sendFirstPacket = true;
  325. // Having this *AND* the AssetId allows us to remap asset data to AssetIds as necessary.
  326. public UUID requestedUUID = UUID.Zero;
  327. public J2KImage(AssetBase asset)
  328. {
  329. m_asset_ref = asset;
  330. }
  331. public J2KImage()
  332. {
  333. }
  334. public AssetBase asset
  335. {
  336. set { m_asset_ref = value; }
  337. }
  338. // We make the asset a reference so that we don't duplicate the byte[]
  339. // it's read only anyway, so no worries here
  340. // we want to avoid duplicating the byte[] for the images at all costs to avoid memory bloat! :)
  341. /// <summary>
  342. /// ID of the AssetBase
  343. /// </summary>
  344. public UUID AssetId
  345. {
  346. get { return m_asset_ref.FullID; }
  347. }
  348. /// <summary>
  349. /// Asset Data
  350. /// </summary>
  351. public byte[] Data
  352. {
  353. get { return m_asset_ref.Data; }
  354. }
  355. /// <summary>
  356. /// Returns true if we have the asset
  357. /// </summary>
  358. public bool HasData
  359. {
  360. get { return !(m_asset_ref == null); }
  361. }
  362. /// <summary>
  363. /// Called from the PriorityQueue handle .ToString(). Prints data on this asset
  364. /// </summary>
  365. /// <returns></returns>
  366. public override string ToString()
  367. {
  368. return string.Format("ID:{0}, RD:{1}, CP:{2}", requestedUUID, HasData, CurrentPacket);
  369. }
  370. /// <summary>
  371. /// Returns the total number of packets needed to transfer this texture,
  372. /// including the first packet of size FIRST_IMAGE_PACKET_SIZE
  373. /// </summary>
  374. /// <returns>Total number of packets needed to transfer this texture</returns>
  375. public int TexturePacketCount()
  376. {
  377. if (!HasData)
  378. return 0;
  379. return ((m_asset_ref.Data.Length - FIRST_IMAGE_PACKET_SIZE + IMAGE_PACKET_SIZE - 1) / IMAGE_PACKET_SIZE) + 1;
  380. }
  381. /// <summary>
  382. /// Returns the current byte offset for this transfer, calculated from
  383. /// the CurrentPacket
  384. /// </summary>
  385. /// <returns>Current byte offset for this transfer</returns>
  386. public int CurrentBytePosition()
  387. {
  388. if (CurrentPacket == 0)
  389. return 0;
  390. if (CurrentPacket == 1)
  391. return FIRST_IMAGE_PACKET_SIZE;
  392. int result = FIRST_IMAGE_PACKET_SIZE + (CurrentPacket - 2) * IMAGE_PACKET_SIZE;
  393. if (result < 0)
  394. {
  395. result = FIRST_IMAGE_PACKET_SIZE;
  396. }
  397. return result;
  398. }
  399. /// <summary>
  400. /// Returns the size, in bytes, of the last packet. This will be somewhere
  401. /// between 1 and IMAGE_PACKET_SIZE bytes
  402. /// </summary>
  403. /// <returns>Size of the last packet in the transfer</returns>
  404. public int LastPacketSize()
  405. {
  406. if (CurrentPacket == 1)
  407. return m_asset_ref.Data.Length;
  408. return (m_asset_ref.Data.Length - FIRST_IMAGE_PACKET_SIZE) % IMAGE_PACKET_SIZE; // m_asset_ref.Data.Length - (FIRST_IMAGE_PACKET_SIZE + ((TexturePacketCount() - 1) * IMAGE_PACKET_SIZE));
  409. }
  410. /// <summary>
  411. /// Find the packet number that contains a given byte position
  412. /// </summary>
  413. /// <param name="bytePosition">Byte position</param>
  414. /// <returns>Packet number that contains the given byte position</returns>
  415. int GetPacketForBytePosition(int bytePosition)
  416. {
  417. return ((bytePosition - FIRST_IMAGE_PACKET_SIZE + IMAGE_PACKET_SIZE - 1) / IMAGE_PACKET_SIZE) + 1;
  418. }
  419. /// <summary>
  420. /// Updates the Image sending limits based on the discard
  421. /// If we don't have any Layers, Send the full texture
  422. /// </summary>
  423. /// <param name="discardLevel">jpeg2000 discard level. 5-0</param>
  424. /// <param name="packet">Which packet to start from</param>
  425. public void Update(int discardLevel, int packet)
  426. {
  427. //Requests for 0 means that the client wants us to resend the whole image
  428. //Requests for -1 mean 'update priority but don't change discard level'
  429. if (packet == 0 || packet == -1)
  430. return;
  431. // Check if we've got layers
  432. if (Layers.Length > 0)
  433. {
  434. DiscardLevel = Util.Clamp<int>(discardLevel, 0, Layers.Length - 1);
  435. StopPacket = GetPacketForBytePosition(Layers[(Layers.Length - 1) - DiscardLevel].End);
  436. CurrentPacket = Util.Clamp<int>(packet, 1, TexturePacketCount() - 1);
  437. // sendFirstPacket = true;
  438. }
  439. else
  440. {
  441. // No layers, send full image
  442. DiscardLevel = 0;
  443. StopPacket = TexturePacketCount() - 1;
  444. CurrentPacket = Util.Clamp<int>(packet, 1, TexturePacketCount() - 1);
  445. }
  446. }
  447. /// <summary>
  448. /// Sends a texture packet to the client.
  449. /// </summary>
  450. /// <param name="client">Client to send texture to</param>
  451. /// <returns>true if a packet was sent, false if not</returns>
  452. public bool SendPacket(LLClientView client)
  453. {
  454. // If we've hit the end of the send or if the client set -1, return false.
  455. if (CurrentPacket > StopPacket || StopPacket == -1)
  456. return false;
  457. // The first packet contains up to 600 bytes and the details of the image. Number of packets, image size in bytes, etc.
  458. // This packet only gets sent once unless we're restarting the transfer from 0!
  459. if (sendFirstPacket)
  460. {
  461. sendFirstPacket = false;
  462. // Do we have less then 1 packet's worth of data?
  463. if (m_asset_ref.Data.Length <= FIRST_IMAGE_PACKET_SIZE)
  464. {
  465. // Send only 1 packet
  466. client.SendImageFirstPart(1, requestedUUID , (uint)m_asset_ref.Data.Length, m_asset_ref.Data, 2);
  467. CurrentPacket = 2; // Makes it so we don't come back to SendPacket and error trying to send a second packet
  468. return true;
  469. }
  470. else
  471. {
  472. // Send first packet
  473. byte[] firstImageData = new byte[FIRST_IMAGE_PACKET_SIZE];
  474. try { Buffer.BlockCopy(m_asset_ref.Data, 0, firstImageData, 0, FIRST_IMAGE_PACKET_SIZE); }
  475. catch (Exception)
  476. {
  477. Console.WriteLine(String.Format("Err: srcLen:{0}, BytePos:{1}, desLen:{2}, pktsize{3}", m_asset_ref.Data.Length, CurrentBytePosition(), firstImageData.Length, FIRST_IMAGE_PACKET_SIZE));
  478. //m_log.Error("Texture data copy failed on first packet for " + m_asset_ref.FullID.ToString());
  479. //m_cancel = true;
  480. //m_sending = false;
  481. return false;
  482. }
  483. client.SendImageFirstPart((ushort)TexturePacketCount(), requestedUUID, (uint)m_asset_ref.Data.Length, firstImageData, 2);
  484. ++CurrentPacket; // sets CurrentPacket to 1
  485. }
  486. }
  487. // figure out if we're on the last packet, if so, use the last packet size. If not, use 1000.
  488. // we know that the total image size is greater then 1000 if we're here
  489. int imagePacketSize = (CurrentPacket == (TexturePacketCount() ) ) ? LastPacketSize() : IMAGE_PACKET_SIZE;
  490. //if (imagePacketSize > 0)
  491. // imagePacketSize = IMAGE_PACKET_SIZE;
  492. //if (imagePacketSize != 1000)
  493. // Console.WriteLine("ENdPacket");
  494. //Console.WriteLine(String.Format("srcLen:{0}, BytePos:{1}, desLen:{2}, pktsize{3}", m_asset_ref.Data.Length, CurrentBytePosition(),0, imagePacketSize));
  495. byte[] imageData = new byte[imagePacketSize];
  496. try { Buffer.BlockCopy(m_asset_ref.Data, CurrentBytePosition(), imageData, 0, imagePacketSize); }
  497. catch (Exception e)
  498. {
  499. Console.WriteLine(String.Format("Err: srcLen:{0}, BytePos:{1}, desLen:{2}, pktsize:{3}, currpak:{4}, stoppak:{5}, totalpak:{6}", m_asset_ref.Data.Length, CurrentBytePosition(),
  500. imageData.Length, imagePacketSize, CurrentPacket,StopPacket,TexturePacketCount()));
  501. System.Console.WriteLine(e.ToString());
  502. //m_log.Error("Texture data copy failed for " + m_asset_ref.FullID.ToString());
  503. //m_cancel = true;
  504. //m_sending = false;
  505. return false;
  506. }
  507. // Send next packet to the client
  508. client.SendImageNextPart((ushort)(CurrentPacket - 1), requestedUUID, imageData);
  509. ++CurrentPacket;
  510. return true;
  511. }
  512. }
  513. /// <summary>
  514. /// Generic Priority Queue element
  515. /// Contains a Priority and a Reference type Data Element
  516. /// </summary>
  517. /// <typeparam name="D">Reference type data element</typeparam>
  518. struct Prio<D> : IComparable<Prio<D>> where D : class
  519. {
  520. public D data;
  521. private int priority;
  522. public Prio(D data, int priority)
  523. {
  524. this.data = data;
  525. this.priority = priority;
  526. }
  527. public int CompareTo(Prio<D> that)
  528. {
  529. return this.priority.CompareTo(that.priority);
  530. }
  531. public bool Equals(Prio<D> that)
  532. {
  533. return this.priority == that.priority;
  534. }
  535. public static Prio<D> operator +(Prio<D> tp, int delta)
  536. {
  537. return new Prio<D>(tp.data, tp.priority + delta);
  538. }
  539. public static bool operator <(Prio<D> tp, int check)
  540. {
  541. return (tp.priority < check);
  542. }
  543. public static bool operator >(Prio<D> tp, int check)
  544. {
  545. return (tp.priority > check);
  546. }
  547. public static Prio<D> operator -(Prio<D> tp, int delta)
  548. {
  549. if (tp.priority - delta < 0)
  550. return new Prio<D>(tp.data, tp.priority - delta);
  551. else
  552. return new Prio<D>(tp.data, 0);
  553. }
  554. public override String ToString()
  555. {
  556. return String.Format("{0}[{1}]", data, priority);
  557. }
  558. internal Prio<D> SetPriority(int pPriority)
  559. {
  560. return new Prio<D>(this.data, pPriority);
  561. }
  562. }
  563. }