TextureSender.cs 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  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 libsecondlife;
  29. using libsecondlife.Packets;
  30. using OpenSim.Framework;
  31. using OpenSim.Framework.Console;
  32. using OpenSim.Region.Environment.Interfaces;
  33. namespace OpenSim.Region.Environment.Modules
  34. {
  35. /// <summary>
  36. /// A TextureSender handles the process of receiving a texture requested by the client from the
  37. /// AssetCache, and then sending that texture back to the client.
  38. /// </summary>
  39. public class TextureSender : ITextureSender
  40. {
  41. private static readonly log4net.ILog m_log
  42. = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
  43. /// <summary>
  44. /// Records the number of times texture send has been called.
  45. /// </summary>
  46. public int counter = 0;
  47. /// <summary>
  48. /// Holds the texture asset to send.
  49. /// </summary>
  50. private AssetBase m_asset;
  51. //public LLUUID assetID { get { return m_asset.FullID; } }
  52. /// <summary>
  53. /// This is actually the number of extra packets required to send the texture data! We always assume
  54. /// at least one is required.
  55. /// </summary>
  56. private int NumPackets = 0;
  57. /// <summary>
  58. /// Holds the packet number to send next. In this case, each packet is 1000 bytes long and starts
  59. /// at the 600th byte (0th indexed).
  60. /// </summary>
  61. private int PacketCounter = 0;
  62. // See ITextureSender
  63. public bool Cancel
  64. {
  65. get { return false; }
  66. set { m_cancel = value; }
  67. }
  68. private bool m_cancel = false;
  69. // See ITextureSender
  70. public bool Sending
  71. {
  72. get { return false; }
  73. set { m_sending = value; }
  74. }
  75. private bool m_sending = false;
  76. public bool ImageLoaded = false;
  77. private IClientAPI RequestUser;
  78. private int RequestedDiscardLevel = -1;
  79. private uint StartPacketNumber = 0;
  80. public TextureSender(IClientAPI client, int discardLevel, uint packetNumber)
  81. {
  82. RequestUser = client;
  83. RequestedDiscardLevel = discardLevel;
  84. StartPacketNumber = packetNumber;
  85. }
  86. /// <summary>
  87. /// Load up the texture data to send.
  88. /// </summary>
  89. /// <param name="asset">
  90. /// A <see cref="AssetBase"/>
  91. /// </param>
  92. public void TextureReceived(AssetBase asset)
  93. {
  94. m_asset = asset;
  95. NumPackets = CalculateNumPackets(asset.Data.Length);
  96. PacketCounter = (int) StartPacketNumber;
  97. ImageLoaded = true;
  98. }
  99. // See ITextureSender
  100. public void UpdateRequest(int discardLevel, uint packetNumber)
  101. {
  102. RequestedDiscardLevel = discardLevel;
  103. StartPacketNumber = packetNumber;
  104. PacketCounter = (int) StartPacketNumber;
  105. }
  106. // See ITextureSender
  107. public bool SendTexturePacket()
  108. {
  109. //m_log.DebugFormat("[TEXTURE SENDER]: Sending packet for {0}", m_asset.FullID);
  110. SendPacket();
  111. counter++;
  112. if ((NumPackets == 0) || (RequestedDiscardLevel == -1) || (PacketCounter > NumPackets) ||
  113. ((RequestedDiscardLevel > 0) && (counter > 50 + (NumPackets/(RequestedDiscardLevel + 1)))))
  114. {
  115. return true;
  116. }
  117. return false;
  118. }
  119. /// <summary>
  120. /// Sends a texture packet to the client.
  121. /// </summary>
  122. private void SendPacket()
  123. {
  124. if (PacketCounter <= NumPackets)
  125. {
  126. if (PacketCounter == 0)
  127. {
  128. if (NumPackets == 0)
  129. {
  130. ImageDataPacket im = new ImageDataPacket();
  131. im.Header.Reliable = false;
  132. im.ImageID.Packets = 1;
  133. im.ImageID.ID = m_asset.FullID;
  134. im.ImageID.Size = (uint) m_asset.Data.Length;
  135. im.ImageData.Data = m_asset.Data;
  136. im.ImageID.Codec = 2;
  137. RequestUser.OutPacket(im, ThrottleOutPacketType.Texture);
  138. PacketCounter++;
  139. }
  140. else
  141. {
  142. ImageDataPacket im = new ImageDataPacket();
  143. im.Header.Reliable = false;
  144. im.ImageID.Packets = (ushort) (NumPackets);
  145. im.ImageID.ID = m_asset.FullID;
  146. im.ImageID.Size = (uint) m_asset.Data.Length;
  147. im.ImageData.Data = new byte[600];
  148. Array.Copy(m_asset.Data, 0, im.ImageData.Data, 0, 600);
  149. im.ImageID.Codec = 2;
  150. RequestUser.OutPacket(im, ThrottleOutPacketType.Texture);
  151. PacketCounter++;
  152. }
  153. }
  154. else
  155. {
  156. ImagePacketPacket im = new ImagePacketPacket();
  157. im.Header.Reliable = false;
  158. im.ImageID.Packet = (ushort) (PacketCounter);
  159. im.ImageID.ID = m_asset.FullID;
  160. int size = m_asset.Data.Length - 600 - (1000*(PacketCounter - 1));
  161. if (size > 1000) size = 1000;
  162. im.ImageData.Data = new byte[size];
  163. try
  164. {
  165. Array.Copy(m_asset.Data, 600 + (1000*(PacketCounter - 1)), im.ImageData.Data, 0, size);
  166. }
  167. catch (ArgumentOutOfRangeException)
  168. {
  169. m_log.Error("[TEXTURE SENDER]: Unable to separate texture into multiple packets: Array bounds failure on asset:" +
  170. m_asset.FullID.ToString() );
  171. return;
  172. }
  173. RequestUser.OutPacket(im, ThrottleOutPacketType.Texture);
  174. PacketCounter++;
  175. }
  176. }
  177. }
  178. /// <summary>
  179. /// Calculate the number of packets that will be required to send the texture loaded into this sender
  180. /// This is actually the number of 1000 byte packets not including an initial 600 byte packet...
  181. /// </summary>
  182. /// <param name="length"></param>
  183. /// <returns></returns>
  184. private int CalculateNumPackets(int length)
  185. {
  186. int numPackets = 0;
  187. if (length > 600)
  188. {
  189. //over 600 bytes so split up file
  190. int restData = (length - 600);
  191. int restPackets = ((restData + 999)/1000);
  192. numPackets = restPackets;
  193. }
  194. return numPackets;
  195. }
  196. }
  197. }