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