Some work in progress LLSD serialise / de-serialise functions.

Sugilite
MW 2007-06-25 18:31:47 +00:00
parent bc1862ecde
commit 49b9913210
4 changed files with 215 additions and 24 deletions

View File

@ -0,0 +1,173 @@
using System;
using System.Collections;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Xml;
using libsecondlife;
namespace OpenSim.Framework
{
public class LLSDHelpers
{
public static string SerialiseLLSDReply(object obj)
{
StringWriter sw = new StringWriter();
XmlTextWriter writer = new XmlTextWriter(sw);
writer.Formatting = Formatting.None;
writer.WriteStartElement(String.Empty, "llsd", String.Empty);
LLSDHelpers.SerializeLLSDType(writer, obj);
writer.WriteEndElement();
writer.Close();
return sw.ToString();
}
public static void SerializeLLSDType(XmlTextWriter writer, object obj)
{
Type myType = obj.GetType();
LLSDType[] llsdattributes = (LLSDType[])myType.GetCustomAttributes(typeof(LLSDType), false);
if (llsdattributes.Length > 0)
{
switch (llsdattributes[0].ObjectType)
{
case "MAP":
writer.WriteStartElement(String.Empty, "map", String.Empty);
System.Reflection.FieldInfo[] fields = myType.GetFields();
for (int i = 0; i < fields.Length; i++)
{
object fieldValue = fields[i].GetValue(obj);
LLSDType[] fieldAttributes = (LLSDType[])fieldValue.GetType().GetCustomAttributes(typeof(LLSDType), false);
if (fieldAttributes.Length > 0)
{
writer.WriteStartElement(String.Empty, "key", String.Empty);
writer.WriteString(fields[i].Name);
writer.WriteEndElement();
SerializeLLSDType(writer, fieldValue);
}
else
{
//Console.WriteLine("LLSD field name" + fields[i].Name + " , " + fields[i].GetValue(obj).GetType());
writer.WriteStartElement(String.Empty, "key", String.Empty);
writer.WriteString(fields[i].Name);
writer.WriteEndElement();
LLSD.LLSDWriteOne(writer, fieldValue);
}
}
writer.WriteEndElement();
break;
case "ARRAY":
// LLSDArray arrayObject = obj as LLSDArray;
// ArrayList a = arrayObject.Array;
ArrayList a = (ArrayList)obj.GetType().GetField("Array").GetValue(obj);
writer.WriteStartElement(String.Empty, "array", String.Empty);
foreach (object item in a)
{
SerializeLLSDType(writer, item);
}
writer.WriteEndElement();
break;
}
}
else
{
LLSD.LLSDWriteOne(writer, obj);
}
}
public static object DeserialiseLLSDMap(Hashtable llsd, object obj)
{
Type myType = obj.GetType();
LLSDType[] llsdattributes = (LLSDType[])myType.GetCustomAttributes(typeof(LLSDType), false);
if (llsdattributes.Length > 0)
{
switch (llsdattributes[0].ObjectType)
{
case "MAP":
IDictionaryEnumerator enumerator = llsd.GetEnumerator();
while (enumerator.MoveNext())
{
System.Reflection.FieldInfo field = myType.GetField((string)enumerator.Key);
if (field != null)
{
field.SetValue(obj, enumerator.Value);
}
}
break;
}
}
return obj;
}
}
[LLSDType("MAP")]
public class LLSDMapLayer
{
public int Left = 0;
public int Right = 0;
public int Top = 0;
public int Bottom = 0;
public LLUUID ImageID = LLUUID.Zero;
public LLSDArray TestArray = new LLSDArray();
public LLSDMapLayer()
{
}
}
[LLSDType("ARRAY")]
public class LLSDArray
{
public ArrayList Array = new ArrayList();
public LLSDArray()
{
}
}
[LLSDType("MAP")]
public class LLSDMapRequest
{
public int Flags = 0;
public LLSDMapRequest()
{
}
}
[LLSDType("MAP")]
public class LLSDTest
{
public int Test1 = 20;
public int Test2 = 10;
public LLSDTest()
{
}
}
[AttributeUsage(AttributeTargets.Class)]
public class LLSDType : Attribute
{
private string myHandler;
public LLSDType(string type)
{
myHandler = type;
}
public string ObjectType
{
get
{
return myHandler;
}
}
}
}

View File

@ -1,4 +1,4 @@
<Project DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<Project DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup>
<ProjectType>Local</ProjectType>
<ProductVersion>8.0.50727</ProductVersion>
@ -6,7 +6,8 @@
<ProjectGuid>{8ACA2445-0000-0000-0000-000000000000}</ProjectGuid>
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
<ApplicationIcon></ApplicationIcon>
<ApplicationIcon>
</ApplicationIcon>
<AssemblyKeyContainerName>
</AssemblyKeyContainerName>
<AssemblyName>OpenSim.Framework</AssemblyName>
@ -15,9 +16,11 @@
<DefaultTargetSchema>IE50</DefaultTargetSchema>
<DelaySign>false</DelaySign>
<OutputType>Library</OutputType>
<AppDesignerFolder></AppDesignerFolder>
<AppDesignerFolder>
</AppDesignerFolder>
<RootNamespace>OpenSim.Framework</RootNamespace>
<StartupObject></StartupObject>
<StartupObject>
</StartupObject>
<FileUpgradeFlags>
</FileUpgradeFlags>
</PropertyGroup>
@ -28,7 +31,8 @@
<ConfigurationOverrideFile>
</ConfigurationOverrideFile>
<DefineConstants>TRACE;DEBUG</DefineConstants>
<DocumentationFile></DocumentationFile>
<DocumentationFile>
</DocumentationFile>
<DebugSymbols>True</DebugSymbols>
<FileAlignment>4096</FileAlignment>
<Optimize>False</Optimize>
@ -37,7 +41,8 @@
<RemoveIntegerChecks>False</RemoveIntegerChecks>
<TreatWarningsAsErrors>False</TreatWarningsAsErrors>
<WarningLevel>4</WarningLevel>
<NoWarn></NoWarn>
<NoWarn>
</NoWarn>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
<AllowUnsafeBlocks>False</AllowUnsafeBlocks>
@ -46,7 +51,8 @@
<ConfigurationOverrideFile>
</ConfigurationOverrideFile>
<DefineConstants>TRACE</DefineConstants>
<DocumentationFile></DocumentationFile>
<DocumentationFile>
</DocumentationFile>
<DebugSymbols>False</DebugSymbols>
<FileAlignment>4096</FileAlignment>
<Optimize>True</Optimize>
@ -55,22 +61,24 @@
<RemoveIntegerChecks>False</RemoveIntegerChecks>
<TreatWarningsAsErrors>False</TreatWarningsAsErrors>
<WarningLevel>4</WarningLevel>
<NoWarn></NoWarn>
<NoWarn>
</NoWarn>
</PropertyGroup>
<ItemGroup>
<Reference Include="Db4objects.Db4o.dll" >
<Reference Include="Db4objects.Db4o.dll">
<HintPath>..\..\bin\Db4objects.Db4o.dll</HintPath>
<Private>False</Private>
</Reference>
<Reference Include="libsecondlife.dll" >
<Reference Include="libsecondlife.dll">
<HintPath>..\..\bin\libsecondlife.dll</HintPath>
<Private>False</Private>
</Reference>
<Reference Include="System" >
<Reference Include="System">
<HintPath>System.dll</HintPath>
<Private>False</Private>
</Reference>
<Reference Include="System.Xml" >
<Reference Include="System.Data" />
<Reference Include="System.Xml">
<HintPath>System.Xml.dll</HintPath>
<Private>False</Private>
</Reference>
@ -80,13 +88,13 @@
<Name>OpenSim.Framework.Console</Name>
<Project>{A7CD0630-0000-0000-0000-000000000000}</Project>
<Package>{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}</Package>
<Private>False</Private>
<Private>False</Private>
</ProjectReference>
<ProjectReference Include="..\XmlRpcCS\XMLRPC.csproj">
<Name>XMLRPC</Name>
<Project>{8E81D43C-0000-0000-0000-000000000000}</Project>
<Package>{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}</Package>
<Private>False</Private>
<Private>False</Private>
</ProjectReference>
</ItemGroup>
<ItemGroup>
@ -102,6 +110,7 @@
<Compile Include="IRegionCommsListener.cs">
<SubType>Code</SubType>
</Compile>
<Compile Include="LLSDHelpers.cs" />
<Compile Include="Logger.cs">
<SubType>Code</SubType>
</Compile>

View File

@ -15,6 +15,7 @@
<include name="AuthenticateSessionBase.cs" />
<include name="BlockingQueue.cs" />
<include name="IRegionCommsListener.cs" />
<include name="LLSDHelpers.cs" />
<include name="Logger.cs" />
<include name="LoginService.cs" />
<include name="RegionCommsListener.cs" />

View File

@ -1,8 +1,11 @@
using System;
using System.Collections;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Xml;
using OpenSim.Servers;
using OpenSim.Framework;
using OpenSim.Framework.Utilities;
using OpenSim.Framework.Types;
using OpenSim.Caches;
@ -91,6 +94,11 @@ namespace OpenSim.Region
/// <returns></returns>
public string MapLayer(string request, string path, string param)
{
Encoding _enc = System.Text.Encoding.UTF8;
Hashtable hash =(Hashtable) LLSD.LLSDDeserialize(_enc.GetBytes(request));
LLSDMapRequest mapReq = new LLSDMapRequest();
LLSDHelpers.DeserialiseLLSDMap(hash, mapReq );
string res = "<llsd><map><key>AgentData</key><map><key>Flags</key><integer>0</integer></map><key>LayerData</key><array>";
res += this.BuildLLSDMapLayerResponse();
res += "</array></map></llsd>";
@ -123,14 +131,14 @@ namespace OpenSim.Region
public string ProcessEventQueue(string request, string path, string param)
{
// Console.WriteLine("event queue request " + request);
// Console.WriteLine("event queue request " + request);
string res = "";
int timer = 0;
/*while ((timer < 200) || (this.CapsEventQueue.Count < 1))
{
timer++;
}*/
/*while ((timer < 200) || (this.CapsEventQueue.Count < 1))
{
timer++;
}*/
if (this.CapsEventQueue.Count > 0)
{
lock (this.CapsEventQueue)
@ -152,9 +160,9 @@ namespace OpenSim.Region
res += "<key>events</key><array><map>";
res += "<key>message</key><string>EstablishAgentCommunication</string>";
res += "<key>body</key><map>";
res += "<key>sim-ip-and-port</key><string>"+ipAddressPort +"</string>";
res += "<key>seed-capability</key><string>"+caps+"</string>";
res += "<key>agent-id</key><uuid>"+this.agentID.ToStringHyphenated()+"</uuid>";
res += "<key>sim-ip-and-port</key><string>" + ipAddressPort + "</string>";
res += "<key>seed-capability</key><string>" + caps + "</string>";
res += "<key>agent-id</key><uuid>" + this.agentID.ToStringHyphenated() + "</uuid>";
res += "</map>";
res += "</map></array>";
res += "</map></llsd>";
@ -195,7 +203,7 @@ namespace OpenSim.Region
public void UploadHandler(LLUUID assetID, LLUUID inventoryItem, byte[] data)
{
// Console.WriteLine("upload handler called");
// Console.WriteLine("upload handler called");
AssetBase asset;
asset = new AssetBase();
asset.FullID = assetID;