OpenSimMirror/OpenSim/Region/ClientStack/Linden/UDP/LLImageManager.cs

360 lines
14 KiB
C#
Raw Normal View History

2009-02-12 09:53:12 +00:00
/*
2009-01-19 18:33:25 +00:00
* 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 OpenSimulator Project nor the
2009-01-19 18:33:25 +00:00
* 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;
From: Alan Webb <alan_webb@us.ibm.com> This change moves texture send processing out of the main packet processing loop and moves it to a timer based processing cycle. Texture packets are sent to the client consistently over time. The timer is discontinued whenever there are no textures to transmit. The behavior of the texture sending mechanism is controlled by three variables in the LLCLient section of the config file: [1] TextureRequestRate (mS) determines how many times per second texture send processing will occur. The default is 100mS. [2] TextureSendLimit determines how many different textures will be considered on each cycle. Textures are selected by priority. The old mechanism specified a value of 10 for this parameter and this is the default [3] TextureDataLimit determines how many packets will be sent for each of the selected textures. The old mechanism specified a value of 5, so this is the default. So the net effect is that TextureSendLimit*TextureDataLimit packets will be sent every TextureRequestRate mS. Once we have gotten a reasonable feeling for how these parameters affect overall processing, it would be nice to autonmically manage these values using information about the current status of the region and network. Note that this also resolves the pathologcal problem that previously existed which was that a seated avatar generated very few in-bound packets (theoretically) and would therefore be the least able to retrieve the images being displayed by a projector script.
2009-06-25 07:42:06 +00:00
using System.Threading;
using System.Collections;
2009-01-19 18:33:25 +00:00
using System.Collections.Generic;
using System.Reflection;
2009-01-19 18:33:25 +00:00
using OpenMetaverse;
2009-02-12 09:53:12 +00:00
using OpenMetaverse.Imaging;
2009-01-19 18:33:25 +00:00
using OpenSim.Framework;
using OpenSim.Region.Framework.Interfaces;
using OpenSim.Services.Interfaces;
using log4net;
2009-01-19 18:33:25 +00:00
namespace OpenSim.Region.ClientStack.LindenUDP
{
/// <summary>
/// This class handles UDP texture requests.
/// </summary>
2009-01-19 18:33:25 +00:00
public class LLImageManager
{
private sealed class J2KImageComparer : IComparer<J2KImage>
{
public int Compare(J2KImage x, J2KImage y)
{
return x.Priority.CompareTo(y.Priority);
}
}
private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
private bool m_shuttingdown;
private AssetBase m_missingImage;
private IAssetService m_assetCache;
private IJ2KDecoder m_j2kDecodeModule;
/// <summary>
/// Priority queue for determining which image to send first.
/// </summary>
private C5.IntervalHeap<J2KImage> m_priorityQueue = new C5.IntervalHeap<J2KImage>(10, new J2KImageComparer());
/// <summary>
/// Used to control thread access to the priority queue.
/// </summary>
private object m_syncRoot = new object();
/// <summary>
/// Client served by this image manager
/// </summary>
public IClientAPI Client { get; private set; }
public AssetBase MissingImage { get { return m_missingImage; } }
public LLImageManager(IClientAPI client, IAssetService pAssetCache, IJ2KDecoder pJ2kDecodeModule)
2009-01-19 18:33:25 +00:00
{
Client = client;
2009-01-19 18:33:25 +00:00
m_assetCache = pAssetCache;
if (pAssetCache != null)
m_missingImage = pAssetCache.Get("5748decc-f629-461c-9a36-a35a221fe21f");
if (m_missingImage == null)
m_log.Error("[ClientView] - Couldn't set missing image asset, falling back to missing image packet. This is known to crash the client");
2009-01-19 18:33:25 +00:00
m_j2kDecodeModule = pJ2kDecodeModule;
}
/// <summary>
/// Handles an incoming texture request or update to an existing texture request
/// </summary>
/// <param name="newRequest"></param>
public void EnqueueReq(TextureRequestArgs newRequest)
2009-01-19 18:33:25 +00:00
{
if (!m_shuttingdown)
{
J2KImage imgrequest;
// Do a linear search for this texture download
lock (m_syncRoot)
m_priorityQueue.Find(delegate(J2KImage img) { return img.TextureID == newRequest.RequestedAssetID; }, out imgrequest);
if (imgrequest != null)
{
if (newRequest.DiscardLevel == -1 && newRequest.Priority == 0f)
{
2009-10-02 01:12:13 +00:00
//m_log.Debug("[TEX]: (CAN) ID=" + newRequest.RequestedAssetID);
From: Alan Webb <alan_webb@us.ibm.com> This change moves texture send processing out of the main packet processing loop and moves it to a timer based processing cycle. Texture packets are sent to the client consistently over time. The timer is discontinued whenever there are no textures to transmit. The behavior of the texture sending mechanism is controlled by three variables in the LLCLient section of the config file: [1] TextureRequestRate (mS) determines how many times per second texture send processing will occur. The default is 100mS. [2] TextureSendLimit determines how many different textures will be considered on each cycle. Textures are selected by priority. The old mechanism specified a value of 10 for this parameter and this is the default [3] TextureDataLimit determines how many packets will be sent for each of the selected textures. The old mechanism specified a value of 5, so this is the default. So the net effect is that TextureSendLimit*TextureDataLimit packets will be sent every TextureRequestRate mS. Once we have gotten a reasonable feeling for how these parameters affect overall processing, it would be nice to autonmically manage these values using information about the current status of the region and network. Note that this also resolves the pathologcal problem that previously existed which was that a seated avatar generated very few in-bound packets (theoretically) and would therefore be the least able to retrieve the images being displayed by a projector script.
2009-06-25 07:42:06 +00:00
try
{
lock (m_syncRoot)
m_priorityQueue.Delete(imgrequest.PriorityQueueHandle);
}
catch (Exception) { }
}
else
{
// m_log.DebugFormat(
// "[LL IMAGE MANAGER]: Received duplicate of existing request for {0}, start packet {1} from {2}",
// newRequest.RequestedAssetID, newRequest.PacketNumber, m_client.Name);
// m_log.DebugFormat("[TEX]: (UPD) ID={0}: D={1}, S={2}, P={3}",
// newRequest.RequestedAssetID, newRequest.DiscardLevel, newRequest.PacketNumber, newRequest.Priority);
2009-01-19 18:33:25 +00:00
//Check the packet sequence to make sure this isn't older than
//one we've already received
if (newRequest.requestSequence > imgrequest.LastSequence)
From: Alan Webb <alan_webb@us.ibm.com> This change moves texture send processing out of the main packet processing loop and moves it to a timer based processing cycle. Texture packets are sent to the client consistently over time. The timer is discontinued whenever there are no textures to transmit. The behavior of the texture sending mechanism is controlled by three variables in the LLCLient section of the config file: [1] TextureRequestRate (mS) determines how many times per second texture send processing will occur. The default is 100mS. [2] TextureSendLimit determines how many different textures will be considered on each cycle. Textures are selected by priority. The old mechanism specified a value of 10 for this parameter and this is the default [3] TextureDataLimit determines how many packets will be sent for each of the selected textures. The old mechanism specified a value of 5, so this is the default. So the net effect is that TextureSendLimit*TextureDataLimit packets will be sent every TextureRequestRate mS. Once we have gotten a reasonable feeling for how these parameters affect overall processing, it would be nice to autonmically manage these values using information about the current status of the region and network. Note that this also resolves the pathologcal problem that previously existed which was that a seated avatar generated very few in-bound packets (theoretically) and would therefore be the least able to retrieve the images being displayed by a projector script.
2009-06-25 07:42:06 +00:00
{
//Update the sequence number of the last RequestImage packet
imgrequest.LastSequence = newRequest.requestSequence;
2009-01-19 18:33:25 +00:00
//Update the requested discard level
imgrequest.DiscardLevel = newRequest.DiscardLevel;
//Update the requested packet number
imgrequest.StartPacket = Math.Max(1, newRequest.PacketNumber);
//Update the requested priority
imgrequest.Priority = newRequest.Priority;
UpdateImageInQueue(imgrequest);
imgrequest.RunUpdate();
// J2KImage imgrequest2 = new J2KImage(this);
// imgrequest2.J2KDecoder = m_j2kDecodeModule;
// imgrequest2.AssetService = m_assetCache;
// imgrequest2.AgentID = m_client.AgentId;
// imgrequest2.InventoryAccessModule = m_client.Scene.RequestModuleInterface<IInventoryAccessModule>();
// imgrequest2.DiscardLevel = newRequest.DiscardLevel;
// imgrequest2.StartPacket = Math.Max(1, newRequest.PacketNumber);
// imgrequest2.Priority = newRequest.Priority;
// imgrequest2.TextureID = newRequest.RequestedAssetID;
// imgrequest2.Priority = newRequest.Priority;
//
// //Add this download to the priority queue
// AddImageToQueue(imgrequest2);
//
// imgrequest2.RunUpdate();
From: Alan Webb <alan_webb@us.ibm.com> This change moves texture send processing out of the main packet processing loop and moves it to a timer based processing cycle. Texture packets are sent to the client consistently over time. The timer is discontinued whenever there are no textures to transmit. The behavior of the texture sending mechanism is controlled by three variables in the LLCLient section of the config file: [1] TextureRequestRate (mS) determines how many times per second texture send processing will occur. The default is 100mS. [2] TextureSendLimit determines how many different textures will be considered on each cycle. Textures are selected by priority. The old mechanism specified a value of 10 for this parameter and this is the default [3] TextureDataLimit determines how many packets will be sent for each of the selected textures. The old mechanism specified a value of 5, so this is the default. So the net effect is that TextureSendLimit*TextureDataLimit packets will be sent every TextureRequestRate mS. Once we have gotten a reasonable feeling for how these parameters affect overall processing, it would be nice to autonmically manage these values using information about the current status of the region and network. Note that this also resolves the pathologcal problem that previously existed which was that a seated avatar generated very few in-bound packets (theoretically) and would therefore be the least able to retrieve the images being displayed by a projector script.
2009-06-25 07:42:06 +00:00
}
// else
// {
// m_log.DebugFormat(
// "[LL IMAGE MANAGER]: Ignoring duplicate of existing request for {0} (sequence {1}) from {2} as its request sequence {3} is not greater",
// newRequest.RequestedAssetID, imgrequest.LastSequence, m_client.Name, newRequest.requestSequence);
// }
}
2009-01-19 18:33:25 +00:00
}
else
{
if (newRequest.DiscardLevel == -1 && newRequest.Priority == 0f)
{
2009-10-02 01:12:13 +00:00
//m_log.DebugFormat("[TEX]: (IGN) ID={0}: D={1}, S={2}, P={3}",
// newRequest.RequestedAssetID, newRequest.DiscardLevel, newRequest.PacketNumber, newRequest.Priority);
}
else
{
// m_log.DebugFormat(
// "[LL IMAGE MANAGER]: Received request for {0}, start packet {1} from {2}",
// newRequest.RequestedAssetID, newRequest.PacketNumber, m_client.Name);
2009-10-02 01:12:13 +00:00
//m_log.DebugFormat("[TEX]: (NEW) ID={0}: D={1}, S={2}, P={3}",
// newRequest.RequestedAssetID, newRequest.DiscardLevel, newRequest.PacketNumber, newRequest.Priority);
imgrequest = new J2KImage(this);
imgrequest.J2KDecoder = m_j2kDecodeModule;
imgrequest.AssetService = m_assetCache;
imgrequest.AgentID = Client.AgentId;
imgrequest.InventoryAccessModule = Client.Scene.RequestModuleInterface<IInventoryAccessModule>();
imgrequest.DiscardLevel = newRequest.DiscardLevel;
imgrequest.StartPacket = Math.Max(1, newRequest.PacketNumber);
imgrequest.Priority = newRequest.Priority;
imgrequest.TextureID = newRequest.RequestedAssetID;
imgrequest.Priority = newRequest.Priority;
//Add this download to the priority queue
AddImageToQueue(imgrequest);
imgrequest.RunUpdate();
}
2009-01-19 18:33:25 +00:00
}
}
}
public bool HasUpdates()
{
J2KImage image = GetHighestPriorityImage();
return image != null && image.IsDecoded;
}
public bool ProcessImageQueue(int packetsToSend)
2009-01-19 18:33:25 +00:00
{
int packetsSent = 0;
while (packetsSent < packetsToSend)
{
J2KImage image = GetHighestPriorityImage();
// If null was returned, the texture priority queue is currently empty
if (image == null)
break;
if (image.IsDecoded)
{
int sent;
bool imageDone = image.SendPackets(Client, packetsToSend - packetsSent, out sent);
packetsSent += sent;
// If the send is complete, destroy any knowledge of this transfer
if (imageDone)
RemoveImageFromQueue(image);
}
else
{
// TODO: This is a limitation of how LLImageManager is currently
// written. Undecoded textures should not be going into the priority
// queue, because a high priority undecoded texture will clog up the
// pipeline for a client
// m_log.DebugFormat(
// "[LL IMAGE MANAGER]: Exiting image queue processing early on encountering undecoded image {0}",
// image.TextureID);
break;
}
}
// if (packetsSent != 0)
// m_log.DebugFormat("[LL IMAGE MANAGER]: Processed {0} packets from image queue", packetsSent);
return m_priorityQueue.Count > 0;
2009-01-19 18:33:25 +00:00
}
/// <summary>
/// Faux destructor
/// </summary>
2009-01-19 18:33:25 +00:00
public void Close()
{
m_shuttingdown = true;
}
/// <summary>
/// Clear the image queue.
/// </summary>
/// <returns>The number of requests cleared.</returns>
public int ClearImageQueue()
{
int requestsDeleted;
lock (m_priorityQueue)
{
requestsDeleted = m_priorityQueue.Count;
// Surprisingly, there doesn't seem to be a clear method at this time.
while (!m_priorityQueue.IsEmpty)
m_priorityQueue.DeleteMax();
}
return requestsDeleted;
}
/// <summary>
/// Returns an array containing all the images in the queue.
/// </summary>
/// <returns></returns>
public J2KImage[] GetImages()
{
lock (m_priorityQueue)
return m_priorityQueue.ToArray();
}
#region Priority Queue Helpers
private J2KImage GetHighestPriorityImage()
{
J2KImage image = null;
lock (m_syncRoot)
{
2009-10-04 21:05:58 +00:00
if (m_priorityQueue.Count > 0)
{
try
{
image = m_priorityQueue.FindMax();
}
2009-10-04 21:05:58 +00:00
catch (Exception) { }
}
}
return image;
}
private void AddImageToQueue(J2KImage image)
{
image.PriorityQueueHandle = null;
lock (m_syncRoot)
{
try
{
m_priorityQueue.Add(ref image.PriorityQueueHandle, image);
}
catch (Exception) { }
}
}
private void RemoveImageFromQueue(J2KImage image)
{
lock (m_syncRoot)
{
try
{
m_priorityQueue.Delete(image.PriorityQueueHandle);
}
catch (Exception) { }
}
}
private void UpdateImageInQueue(J2KImage image)
{
lock (m_syncRoot)
{
try
{
m_priorityQueue.Replace(image.PriorityQueueHandle, image);
}
catch (Exception)
{
image.PriorityQueueHandle = null;
m_priorityQueue.Add(ref image.PriorityQueueHandle, image);
}
}
}
#endregion Priority Queue Helpers
2009-01-19 18:33:25 +00:00
}
}