aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/OpenSim/Region/Environment/Modules/Agent/TextureSender/TextureSender.cs
blob: b1771037e32e4b1489ecbf7ce249f6033e4ab065 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
/*
 * Copyright (c) Contributors, http://opensimulator.org/
 * See CONTRIBUTORS.TXT for a full list of copyright holders.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *     * Redistributions of source code must retain the above copyright
 *       notice, this list of conditions and the following disclaimer.
 *     * Redistributions in binary form must reproduce the above copyright
 *       notice, this list of conditions and the following disclaimer in the
 *       documentation and/or other materials provided with the distribution.
 *     * Neither the name of the OpenSim Project nor the
 *       names of its contributors may be used to endorse or promote products
 *       derived from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE DEVELOPERS ``AS IS'' AND ANY
 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED. IN NO EVENT SHALL THE CONTRIBUTORS BE LIABLE FOR ANY
 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

using System;
using System.Reflection;
using OpenMetaverse.Packets;
using log4net;
using OpenSim.Framework;
using OpenSim.Region.Environment.Interfaces;

namespace OpenSim.Region.Environment.Modules.Agent.TextureSender
{
    public class ImageDownload
    {
        public const int FIRST_IMAGE_PACKET_SIZE = 600;
        public const int IMAGE_PACKET_SIZE = 1000;

        public OpenMetaverse.AssetTexture Texture;
        public int DiscardLevel;
        public float Priority;
        public int CurrentPacket;
        public int StopPacket;

        public ImageDownload(OpenMetaverse.AssetTexture texture, int discardLevel, float priority, int packet)
        {
            Texture = texture;
            Update(discardLevel, priority, packet);
        }

        /// <summary>
        /// Updates an image transfer with new information and recalculates
        /// offsets
        /// </summary>
        /// <param name="discardLevel">New requested discard level</param>
        /// <param name="priority">New requested priority</param>
        /// <param name="packet">New requested packet offset</param>
        public void Update(int discardLevel, float priority, int packet)
        {
            Priority = priority;
            DiscardLevel = Clamp(discardLevel, 0, Texture.LayerInfo.Length - 1);
            StopPacket = GetPacketForBytePosition(Texture.LayerInfo[(Texture.LayerInfo.Length - 1) - DiscardLevel].End);
            CurrentPacket = Clamp(packet, 1, TexturePacketCount());
        }

        /// <summary>
        /// Returns the total number of packets needed to transfer this texture,
        /// including the first packet of size FIRST_IMAGE_PACKET_SIZE
        /// </summary>
        /// <returns>Total number of packets needed to transfer this texture</returns>
        public int TexturePacketCount()
        {
            return ((Texture.AssetData.Length - FIRST_IMAGE_PACKET_SIZE + IMAGE_PACKET_SIZE - 1) / IMAGE_PACKET_SIZE) + 1;
        }

        /// <summary>
        /// Returns the current byte offset for this transfer, calculated from
        /// the CurrentPacket
        /// </summary>
        /// <returns>Current byte offset for this transfer</returns>
        public int CurrentBytePosition()
        {
            return FIRST_IMAGE_PACKET_SIZE + (CurrentPacket - 1) * IMAGE_PACKET_SIZE;
        }

        /// <summary>
        /// Returns the size, in bytes, of the last packet. This will be somewhere
        /// between 1 and IMAGE_PACKET_SIZE bytes
        /// </summary>
        /// <returns>Size of the last packet in the transfer</returns>
        public int LastPacketSize()
        {
            return Texture.AssetData.Length - (FIRST_IMAGE_PACKET_SIZE + ((TexturePacketCount() - 2) * IMAGE_PACKET_SIZE));
        }

        /// <summary>
        /// Find the packet number that contains a given byte position
        /// </summary>
        /// <param name="bytePosition">Byte position</param>
        /// <returns>Packet number that contains the given byte position</returns>
        int GetPacketForBytePosition(int bytePosition)
        {
            return ((bytePosition - FIRST_IMAGE_PACKET_SIZE + IMAGE_PACKET_SIZE - 1) / IMAGE_PACKET_SIZE);
        }

        /// <summary>
        /// Clamp a given value between a range
        /// </summary>
        /// <param name="value">Value to clamp</param>
        /// <param name="min">Minimum allowable value</param>
        /// <param name="max">Maximum allowable value</param>
        /// <returns>A value inclusively between lower and upper</returns>
        static int Clamp(int value, int min, int max)
        {
            // First we check to see if we're greater than the max
            value = (value > max) ? max : value;

            // Then we check to see if we're less than the min.
            value = (value < min) ? min : value;

            // There's no check to see if min > max.
            return value;
        }
    }

    /// <summary>
    /// A TextureSender handles the process of receiving a texture requested by the client from the
    /// AssetCache, and then sending that texture back to the client.
    /// </summary>
    public class TextureSender : ITextureSender
    {
        private static readonly ILog m_log
            = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);

        public bool ImageLoaded = false;

        /// <summary>
        /// Holds the texture asset to send.
        /// </summary>
        private AssetBase m_asset;
        private bool m_cancel = false;
        private bool m_sending = false;
        private bool sendFirstPacket = false;
        private int initialDiscardLevel = 0;
        private int initialPacketNum = 0;
        private float initialPriority = 0.0f;

        private ImageDownload download;
        private IClientAPI RequestUser;

        public TextureSender(IClientAPI client, int discardLevel, uint packetNumber, float priority)
        {
            RequestUser = client;
            initialDiscardLevel = discardLevel;
            initialPacketNum = (int)packetNumber;
            initialPriority = priority;
        }

        #region ITextureSender Members

        public bool Cancel
        {
            get { return m_cancel; }
            set { m_cancel = value; }
        }

        public bool Sending
        {
            get { return m_sending; }
            set { m_sending = value; }
        }

        // See ITextureSender
        public void UpdateRequest(int discardLevel, uint packetNumber)
        {
            if (download == null)
                return;

            lock (download)
            {
                if (discardLevel < download.DiscardLevel)
                    m_log.DebugFormat("Image download {0} is changing from DiscardLevel {1} to {2}",
                        m_asset.FullID, download.DiscardLevel, discardLevel);

                if (packetNumber != download.CurrentPacket)
                    m_log.DebugFormat("Image download {0} is changing from Packet {1} to {2}",
                        m_asset.FullID, download.CurrentPacket, packetNumber);

                download.Update(discardLevel, download.Priority, (int)packetNumber);

                sendFirstPacket = true;
            }
        }

        // See ITextureSender
        public bool SendTexturePacket()
        {
            if (!m_cancel && (sendFirstPacket || download.CurrentPacket <= download.StopPacket))
            {
                SendPacket();
                return false;
            }
            else
            {
                m_sending = false;
                m_cancel = true;
                sendFirstPacket = false;
                return true;
            }
        }

        #endregion

        /// <summary>
        /// Load up the texture data to send.
        /// </summary>
        /// <param name="asset">
        /// A <see cref="AssetBase"/>
        /// </param>
        public void TextureReceived(AssetBase asset)
        {
            m_asset = asset;

            try
            {
                OpenMetaverse.AssetTexture texture = new OpenMetaverse.AssetTexture(m_asset.FullID, m_asset.Data);
                if (texture.DecodeLayerBoundaries())
                {
                    bool sane = true;

                    // Sanity check all of the layers
                    for (int i = 0; i < texture.LayerInfo.Length; i++)
                    {
                        if (texture.LayerInfo[i].End > texture.AssetData.Length)
                        {
                            sane = false;
                            break;
                        }
                    }

                    if (sane)
                    {
                        download = new ImageDownload(texture, initialDiscardLevel, initialPriority, initialPacketNum);
                        ImageLoaded = true;
                        m_sending = true;
                        m_cancel = false;
                        sendFirstPacket = true;
                        return;
                    }
                    else
                    {
                        m_log.Error("JPEG2000 texture decoding succeeded, but sanity check failed for " +
                            m_asset.FullID.ToString());
                    }
                }
                else
                {
                    m_log.Error("JPEG2000 texture decoding failed for " + m_asset.FullID.ToString());
                }
            }
            catch (Exception ex)
            {
                m_log.Error("JPEG2000 texture decoding threw an exception for " + m_asset.FullID.ToString(), ex);
            }

            ImageLoaded = false;
            m_sending = false;
            m_cancel = true;
        }

        /// <summary>
        /// Sends a texture packet to the client.
        /// </summary>
        private void SendPacket()
        {
            lock (download)
            {
                if (sendFirstPacket)
                {
                    sendFirstPacket = false;

                    if (m_asset.Data.Length <= ImageDownload.FIRST_IMAGE_PACKET_SIZE)
                    {
                        RequestUser.SendImageFirstPart(1, m_asset.FullID, (uint)m_asset.Data.Length, m_asset.Data, 2);
                        return;
                    }
                    else
                    {
                        byte[] firstImageData = new byte[ImageDownload.FIRST_IMAGE_PACKET_SIZE];
                        try { Buffer.BlockCopy(m_asset.Data, 0, firstImageData, 0, ImageDownload.FIRST_IMAGE_PACKET_SIZE); }
                        catch (Exception)
                        {
                            m_log.Error("Texture data copy failed on first packet for " + m_asset.FullID.ToString());
                            m_cancel = true;
                            m_sending = false;
                            return;
                        }
                        RequestUser.SendImageFirstPart((ushort)download.TexturePacketCount(), m_asset.FullID, (uint)m_asset.Data.Length, firstImageData, 2);
                    }
                }

                int imagePacketSize = (download.CurrentPacket == download.TexturePacketCount() - 1) ?
                    download.LastPacketSize() : ImageDownload.IMAGE_PACKET_SIZE;

                byte[] imageData = new byte[imagePacketSize];
                try { Buffer.BlockCopy(m_asset.Data, download.CurrentBytePosition(), imageData, 0, imagePacketSize); }
                catch (Exception)
                {
                    m_log.Error("Texture data copy failed for " + m_asset.FullID.ToString());
                    m_cancel = true;
                    m_sending = false;
                    return;
                }

                RequestUser.SendImageNextPart((ushort)download.CurrentPacket, m_asset.FullID, imageData);
                ++download.CurrentPacket;
            }
        }
    }
}