* Sanity check in DeserializeLLSDXml() to return an empty LLSD block instead of throwing an exception

* Removing redundant code from OpenMetaverse.Utilities
* Using System.Diagnostics.StopWatch instead of Environment.TickCount for rate limiting, and speeding up packet sending from 75ms gaps to 10ms (might be slightly too fast, but it works for me)
* Cleaning up the logic a big in RequestAllSimParcels()
* Try/catch block in GridProxy around packet delegates

git-svn-id: http://libopenmetaverse.googlecode.com/svn/libopenmetaverse/trunk@2371 52acb1d6-8a22-11de-b505-999d5b087335
This commit is contained in:
John Hurliman
2008-12-05 01:34:24 +00:00
parent d816950d40
commit 4b1f441574
7 changed files with 40 additions and 371 deletions

View File

@@ -71,13 +71,20 @@ namespace OpenMetaverse.StructuredData
/// <returns></returns>
public static OSD DeserializeLLSDXml(XmlTextReader xmlData)
{
xmlData.Read();
SkipWhitespace(xmlData);
try
{
xmlData.Read();
SkipWhitespace(xmlData);
xmlData.Read();
OSD ret = ParseLLSDXmlElement(xmlData);
xmlData.Read();
OSD ret = ParseLLSDXmlElement(xmlData);
return ret;
return ret;
}
catch
{
return new OSD();
}
}
/// <summary>

View File

@@ -266,344 +266,4 @@ namespace OpenMetaverse.Utilities
}
}
}
/// <summary>
///
/// </summary>
[Obsolete("ParcelDownloader has been replaced by Parcels.RequestAllSimParcels()")]
public class ParcelDownloader
{
/// <summary>
///
/// </summary>
/// <param name="simulator">Simulator where the parcels are located</param>
/// <param name="Parcels">Mapping of parcel LocalIDs to Parcel objects</param>
/// <param name="map"></param>
public delegate void ParcelsDownloadedCallback(Simulator simulator, Dictionary<int, Parcel> Parcels, int[,] map);
/// <summary>
///
/// </summary>
public event ParcelsDownloadedCallback OnParcelsDownloaded;
private GridClient Client;
/// <summary>Dictionary of 64x64 arrays of parcels which have been successfully downloaded
/// for each simulator (and their LocalID's, 0 = Null)</summary>
private Dictionary<Simulator, int[,]> ParcelMarked = new Dictionary<Simulator, int[,]>();
private Dictionary<Simulator, Dictionary<int, Parcel>> Parcels = new Dictionary<Simulator, Dictionary<int, Parcel>>();
private List<Simulator> active_sims = new List<Simulator>();
/// <summary>
/// Default constructor
/// </summary>
/// <param name="client">A reference to the GridClient object</param>
public ParcelDownloader(GridClient client)
{
Client = client;
Client.Parcels.OnParcelProperties += new ParcelManager.ParcelPropertiesCallback(Parcels_OnParcelProperties);
Client.Parcels.OnAccessListReply += new ParcelManager.ParcelAccessListReplyCallback(Parcels_OnParcelAccessList);
}
public void DownloadSimParcels(Simulator simulator)
{
if (simulator == null)
{
Logger.Log("DownloadSimParcels() will not work with a null simulator", Helpers.LogLevel.Error, Client);
return;
}
lock (active_sims)
{
if (active_sims.Contains(simulator))
{
Logger.Log("DownloadSimParcels(" + simulator + ") called more than once?", Helpers.LogLevel.Error, Client);
return;
}
active_sims.Add(simulator);
}
lock (ParcelMarked)
{
if (!ParcelMarked.ContainsKey(simulator))
{
ParcelMarked[simulator] = new int[64, 64];
Parcels[simulator] = new Dictionary<int, Parcel>();
}
}
Client.Parcels.PropertiesRequest(simulator, 0.0f, 0.0f, 0.0f, 0.0f, 0, false);
}
/// <summary>
///
/// </summary>
/// <param name="map"></param>
/// <param name="localid"></param>
/// <returns></returns>
public float GetHeightRange(int[,] map, int localid)
{
float min = Single.MaxValue;
float max = 0.0f;
for (int y = 0; y < 64; y++)
{
for (int x = 0; x < 64; x++)
{
if (map[y, x] == localid)
{
for (int y1 = 0; y1 < 4; y1++)
{
for (int x1 = 0; x1 < 4; x1++)
{
float height;
int tries = 0;
CheckHeight:
if (Client.Terrain.TerrainHeightAtPoint(Client.Network.CurrentSim.Handle,
x * 4 + x1, y * 4 + y1, out height))
{
if (height < min)
min = height;
if (height > max)
max = height;
}
else if (tries > 4)
{
Logger.Log("Too many tries on this terrain block, skipping",
Helpers.LogLevel.Warning, Client);
continue;
}
else
{
Logger.Log(String.Format("Terrain height is null at {0},{1} retrying",
x * 4 + x1, y * 4 + y1), Helpers.LogLevel.Info, Client);
// Terrain at this point hasn't been downloaded, move the camera to this spot
// and try again
Vector3 position = new Vector3((float)(x * 4 + x1), (float)(y * 4 + y1),
Client.Self.SimPosition.Z);
Client.Self.Movement.Camera.Position = position;
Client.Self.Movement.SendUpdate(true);
Thread.Sleep(1000);
goto CheckHeight;
}
}
}
}
}
}
if (min != Single.MaxValue)
{
return max - min;
}
else
{
Logger.Log("Error decoding terrain for parcel " + localid, Helpers.LogLevel.Error, Client);
return Single.NaN;
}
}
/// <summary>
///
/// </summary>
/// <param name="map"></param>
/// <param name="localid"></param>
/// <returns></returns>
public WaterType GetWaterType(int[,] map, int localid)
{
if (!Client.Settings.STORE_LAND_PATCHES)
{
Logger.Log("GetWaterType() will not work without Settings.STORE_LAND_PATCHES set to true",
Helpers.LogLevel.Error, Client);
return WaterType.Unknown;
}
else if (!Client.Network.Connected && Client.Network.CurrentSim != null)
{
Logger.Log("GetWaterType() can only be used with an online client", Helpers.LogLevel.Error, Client);
return WaterType.Unknown;
}
bool underwater = false;
bool abovewater = false;
for (int y = 0; y < 64; y++)
{
for (int x = 0; x < 64; x++)
{
if (map[y, x] == localid)
{
for (int y1 = 0; y1 < 4; y1++)
{
for (int x1 = 0; x1 < 4; x1++)
{
float height;
int tries = 0;
CheckHeight:
tries++;
if (Client.Terrain.TerrainHeightAtPoint(Client.Network.CurrentSim.Handle,
x * 4 + x1, y * 4 + y1, out height))
{
if (height < Client.Network.CurrentSim.WaterHeight)
{
underwater = true;
}
else
{
abovewater = true;
}
}
else if (tries > 4)
{
Logger.Log("Too many tries on this terrain block, skipping",
Helpers.LogLevel.Warning, Client);
continue;
}
else
{
Logger.Log(String.Format("Terrain height is null at {0},{1} retrying",
x * 4 + x1, y * 4 + y1), Helpers.LogLevel.Info, Client);
// Terrain at this point hasn't been downloaded, move the camera to this spot
// and try again
Vector3 position = new Vector3((float)(x * 4 + x1), (float)(y * 4 + y1),
Client.Self.SimPosition.Z);
Client.Self.Movement.Camera.Position = position;
Client.Self.Movement.SendUpdate(true);
Thread.Sleep(1000);
goto CheckHeight;
}
}
}
}
}
}
if (underwater && abovewater)
{
return WaterType.Waterfront;
}
else if (abovewater)
{
return WaterType.Dry;
}
else if (underwater)
{
return WaterType.Underwater;
}
else
{
Logger.Log("Error decoding terrain for parcel " + localid, Helpers.LogLevel.Error, Client);
return WaterType.Unknown;
}
}
public int GetRectangularDeviation(Vector3 aabbmin, Vector3 aabbmax, int area)
{
int xlength = (int)(aabbmax.X - aabbmin.X);
int ylength = (int)(aabbmax.Y - aabbmin.Y);
int aabbarea = xlength * ylength;
return (aabbarea - area) / 16;
}
private void Parcels_OnParcelAccessList(Simulator simulator, int sequenceID, int localID, uint flags,
List<ParcelManager.ParcelAccessEntry> accessEntries)
{
if (simulator != null && Parcels.ContainsKey(simulator) && Parcels[simulator].ContainsKey(localID))
{
Parcel parcel = Parcels[simulator][localID];
//parcel.AccessList = accessEntries;
Parcels[simulator][localID] = parcel;
}
}
private void Parcels_OnParcelProperties(Simulator simulator, Parcel parcel, ParcelResult result,
int selectedPrims, int sequenceID, bool snapSelection)
{
// Check if this is for a simulator we're concerned with
if (!active_sims.Contains(simulator)) return;
// Warn about parcel property request errors and bail out
if (result == ParcelResult.NoData)
{
Logger.Log("ParcelDownloader received a NoData response, sequenceID " + sequenceID,
Helpers.LogLevel.Warning, Client);
return;
}
// Warn about unexpected data and bail out
if (!ParcelMarked.ContainsKey(simulator))
{
Logger.Log("ParcelDownloader received unexpected parcel data for " + simulator,
Helpers.LogLevel.Warning, Client);
return;
}
int x, y, index, bit;
int[,] markers = ParcelMarked[simulator];
// Add this parcel to the dictionary of LocalID -> Parcel mappings
lock (Parcels[simulator])
if (!Parcels[simulator].ContainsKey(parcel.LocalID))
Parcels[simulator][parcel.LocalID] = parcel;
// Request the access list for this parcel
Client.Parcels.AccessListRequest(simulator, parcel.LocalID,
AccessList.Both, 0);
// Mark this area as downloaded
for (y = 0; y < 64; y++)
{
for (x = 0; x < 64; x++)
{
if (markers[y, x] == 0)
{
index = (y * 64) + x;
bit = index % 8;
index >>= 3;
if ((parcel.Bitmap[index] & (1 << bit)) != 0)
markers[y, x] = parcel.LocalID;
}
}
}
// Request parcel information for the next missing area
for (y = 0; y < 64; y++)
{
for (x = 0; x < 64; x++)
{
if (markers[y, x] == 0)
{
Client.Parcels.PropertiesRequest(simulator,
(y + 1) * 4.0f, (x + 1) * 4.0f,
y * 4.0f, x * 4.0f, 0, false);
return;
}
}
}
// If we get here, there are no more zeroes in the markers map
lock (active_sims)
{
active_sims.Remove(simulator);
if (OnParcelsDownloaded != null)
{
// This map is complete, fire callback
try { OnParcelsDownloaded(simulator, Parcels[simulator], markers); }
catch (Exception e) { Logger.Log(e.Message, Helpers.LogLevel.Error, Client, e); }
}
}
}
}
}

View File

@@ -176,8 +176,8 @@ namespace OpenMetaverse
Logger.DebugLog("Starting event queue for " + Simulator.ToString(), Simulator.Client);
_EventQueueCap = new EventQueueClient(_Caps["EventQueueGet"]);
_EventQueueCap.OnConnected += new EventQueueClient.ConnectedCallback(EventQueueConnectedHandler);
_EventQueueCap.OnEvent += new EventQueueClient.EventCallback(EventQueueEventHandler);
_EventQueueCap.OnConnected += EventQueueConnectedHandler;
_EventQueueCap.OnEvent += EventQueueEventHandler;
_EventQueueCap.Start();
}
}

View File

@@ -675,8 +675,7 @@ namespace OpenMetaverse
OutgoingPacket outgoingPacket = null;
Simulator simulator = null;
Packet packet = null;
int now;
int lastPacketTime = Environment.TickCount;
System.Diagnostics.Stopwatch stopwatch = new System.Diagnostics.Stopwatch();
while (connected)
{
@@ -686,16 +685,15 @@ namespace OpenMetaverse
packet = outgoingPacket.Packet;
// Very primitive rate limiting, keeps a fixed buffer of time between each packet
now = Environment.TickCount;
int ms = now - lastPacketTime;
if (ms < 75)
stopwatch.Stop();
if (stopwatch.ElapsedMilliseconds < 10)
{
//Logger.DebugLog(String.Format("Rate limiting, last packet was {0}ms ago", ms));
Thread.Sleep(75 - ms);
Thread.Sleep(10 - (int)stopwatch.ElapsedMilliseconds);
}
lastPacketTime = now;
simulator.SendPacketUnqueued(outgoingPacket);
stopwatch.Start();
}
}
}

View File

@@ -935,8 +935,6 @@ namespace OpenMetaverse
WaitForSimParcel = new AutoResetEvent(false);
}
if (refresh)
{
for (int y = 0; y < 64; y++)
@@ -1509,30 +1507,37 @@ namespace OpenMetaverse
if (Client.Settings.PARCEL_TRACKING)
{
if(sequenceID.Equals(int.MaxValue))
WaitForSimParcel.Set();
lock (simulator.Parcels.Dictionary)
simulator.Parcels.Dictionary[parcel.LocalID] = parcel;
bool set = false;
int y, x, index, bit;
for (y = 0; y < simulator.ParcelMap.GetLength(0); y++)
for (y = 0; y < 64; y++)
{
for (x = 0; x < simulator.ParcelMap.GetLength(1); x++)
for (x = 0; x < 64; x++)
{
if (simulator.ParcelMap[y, x] == 0)
{
index = (y * 64) + x;
bit = index % 8;
index >>= 3;
index = (y * 64) + x;
bit = index % 8;
index >>= 3;
if ((parcel.Bitmap[index] & (1 << bit)) != 0)
simulator.ParcelMap[y, x] = parcel.LocalID;
if ((parcel.Bitmap[index] & (1 << bit)) != 0)
{
simulator.ParcelMap[y, x] = parcel.LocalID;
set = true;
}
}
}
if (!set)
{
Logger.Log("Received a parcel with a bitmap that did not map to any locations",
Helpers.LogLevel.Warning);
}
}
if (sequenceID.Equals(int.MaxValue) && WaitForSimParcel != null)
WaitForSimParcel.Set();
// auto request acl, will be stored in parcel tracking dictionary if enabled
if (Client.Settings.ALWAYS_REQUEST_PARCEL_ACL)
Client.Parcels.AccessListRequest(simulator, parcel.LocalID,

View File

@@ -342,8 +342,6 @@ namespace OpenMetaverse
/// <returns>true if map is full (contains no 0's)</returns>
public bool IsParcelMapFull()
{
//int ny = this.ParcelMap.GetLength(0);
//int nx = this.ParcelMap.GetLength(1);
for (int y = 0; y < 64; y++)
{
for (int x = 0; x < 64; x++)

View File

@@ -299,7 +299,8 @@ namespace GridProxy
PacketType origType = packet.Type;
foreach (PacketDelegate del in delegates[origType])
{
packet = del(packet, remoteEndPoint);
try { packet = del(packet, remoteEndPoint); }
catch (Exception ex) { Console.WriteLine("Error in packet delegate: " + ex.ToString()); }
// FIXME: how should we handle the packet type changing?
if (packet == null || packet.Type != origType) break;