TextureSender.cs 7.5 KB

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