TextureSender.cs 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  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. */
  28. using System;
  29. using libsecondlife;
  30. using libsecondlife.Packets;
  31. using OpenSim.Framework;
  32. using OpenSim.Framework.Console;
  33. namespace OpenSim.Region.Environment.Modules
  34. {
  35. public class TextureSender
  36. {
  37. private static readonly log4net.ILog m_log
  38. = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
  39. public int counter = 0;
  40. private AssetBase m_asset;
  41. public long DataPointer = 0;
  42. public int NumPackets = 0;
  43. public int PacketCounter = 0;
  44. public bool Cancel = false;
  45. public bool ImageLoaded = false;
  46. public bool Sending = false;
  47. public IClientAPI RequestUser;
  48. public LLUUID RequestedAssetID;
  49. public int RequestedDiscardLevel = -1;
  50. public uint StartPacketNumber = 0;
  51. // private int m_sentDiscardLevel = -1;
  52. public TextureSender(IClientAPI client, LLUUID textureID, int discardLevel, uint packetNumber)
  53. {
  54. RequestUser = client;
  55. RequestedAssetID = textureID;
  56. RequestedDiscardLevel = discardLevel;
  57. StartPacketNumber = packetNumber;
  58. }
  59. public void TextureReceived(AssetBase asset)
  60. {
  61. m_asset = asset;
  62. NumPackets = CalculateNumPackets(asset.Data.Length);
  63. PacketCounter = (int) StartPacketNumber;
  64. ImageLoaded = true;
  65. }
  66. public void UpdateRequest(int discardLevel, uint packetNumber)
  67. {
  68. RequestedDiscardLevel = discardLevel;
  69. StartPacketNumber = packetNumber;
  70. PacketCounter = (int) StartPacketNumber;
  71. }
  72. public bool SendTexturePacket()
  73. {
  74. SendPacket();
  75. counter++;
  76. if ((NumPackets == 0) || (RequestedDiscardLevel == -1) || (PacketCounter > NumPackets) ||
  77. ((RequestedDiscardLevel > 0) && (counter > 50 + (NumPackets/(RequestedDiscardLevel + 1)))))
  78. {
  79. return true;
  80. }
  81. return false;
  82. }
  83. private void SendPacket()
  84. {
  85. if (PacketCounter <= NumPackets)
  86. {
  87. if (PacketCounter == 0)
  88. {
  89. if (NumPackets == 0)
  90. {
  91. ImageDataPacket im = new ImageDataPacket();
  92. im.Header.Reliable = false;
  93. im.ImageID.Packets = 1;
  94. im.ImageID.ID = m_asset.FullID;
  95. im.ImageID.Size = (uint) m_asset.Data.Length;
  96. im.ImageData.Data = m_asset.Data;
  97. im.ImageID.Codec = 2;
  98. RequestUser.OutPacket(im, ThrottleOutPacketType.Texture);
  99. PacketCounter++;
  100. }
  101. else
  102. {
  103. ImageDataPacket im = new ImageDataPacket();
  104. im.Header.Reliable = false;
  105. im.ImageID.Packets = (ushort) (NumPackets);
  106. im.ImageID.ID = m_asset.FullID;
  107. im.ImageID.Size = (uint) m_asset.Data.Length;
  108. im.ImageData.Data = new byte[600];
  109. Array.Copy(m_asset.Data, 0, im.ImageData.Data, 0, 600);
  110. im.ImageID.Codec = 2;
  111. RequestUser.OutPacket(im, ThrottleOutPacketType.Texture);
  112. PacketCounter++;
  113. }
  114. }
  115. else
  116. {
  117. ImagePacketPacket im = new ImagePacketPacket();
  118. im.Header.Reliable = false;
  119. im.ImageID.Packet = (ushort) (PacketCounter);
  120. im.ImageID.ID = m_asset.FullID;
  121. int size = m_asset.Data.Length - 600 - (1000*(PacketCounter - 1));
  122. if (size > 1000) size = 1000;
  123. im.ImageData.Data = new byte[size];
  124. try
  125. {
  126. Array.Copy(m_asset.Data, 600 + (1000*(PacketCounter - 1)), im.ImageData.Data, 0, size);
  127. }
  128. catch (ArgumentOutOfRangeException)
  129. {
  130. m_log.Error("[TEXTURE]: Unable to separate texture into multiple packets: Array bounds failure on asset:" +
  131. m_asset.FullID.ToString() );
  132. return;
  133. }
  134. RequestUser.OutPacket(im, ThrottleOutPacketType.Texture);
  135. PacketCounter++;
  136. }
  137. }
  138. }
  139. private int CalculateNumPackets(int length)
  140. {
  141. int numPackets = 0;
  142. if (length > 600)
  143. {
  144. //over 600 bytes so split up file
  145. int restData = (length - 600);
  146. int restPackets = ((restData + 999)/1000);
  147. numPackets = restPackets;
  148. }
  149. return numPackets;
  150. }
  151. }
  152. }