TextureSender.cs 7.5 KB

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