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