/// <summary>
        /// Fire the events registered for this event type asynchronously
        /// </summary>
        /// <param name="capsEvent">Capability name</param>
        /// <param name="body">Decoded event body</param>
        /// <param name="simulator">Reference to the simulator that
        /// generated this event</param>
        internal void BeginRaiseEvent(string capsEvent, StructuredData.LLSD body, Simulator simulator)
        {
            bool specialHandler = false;

            Caps.EventQueueCallback callback;

            // Default handler first, if one exists
            if (_EventTable.TryGetValue(String.Empty, out callback))
            {
                if (callback != null)
                {
                    CapsCallbackWrapper wrapper;
                    wrapper.Callback  = callback;
                    wrapper.CapsEvent = capsEvent;
                    wrapper.Body      = body;
                    wrapper.Simulator = simulator;
                    ThreadPool.QueueUserWorkItem(_ThreadPoolCallback, wrapper);
                }
            }

            // Generic parser next, don't generic parse events we've manually registered for
            if (body.Type == StructuredData.LLSDType.Map && !_EventTable.ContainsKey(capsEvent))
            {
                StructuredData.LLSDMap map = (StructuredData.LLSDMap)body;
                Packet packet = Packet.BuildPacket(capsEvent, map);
                if (packet != null)
                {
                    NetworkManager.IncomingPacket incomingPacket;
                    incomingPacket.Simulator = simulator;
                    incomingPacket.Packet    = packet;

                    Logger.DebugLog("Serializing " + packet.Type.ToString() + " capability with generic handler", Client);

                    Client.Network.PacketInbox.Enqueue(incomingPacket);
                    specialHandler = true;
                }
            }

            // Explicit handler next
            if (_EventTable.TryGetValue(capsEvent, out callback) && callback != null)
            {
                CapsCallbackWrapper wrapper;
                wrapper.Callback  = callback;
                wrapper.CapsEvent = capsEvent;
                wrapper.Body      = body;
                wrapper.Simulator = simulator;
                ThreadPool.QueueUserWorkItem(_ThreadPoolCallback, wrapper);

                specialHandler = true;
            }

            if (!specialHandler)
            {
                Logger.Log("Unhandled CAPS event " + capsEvent, Helpers.LogLevel.Warning, Client);
            }
        }
        /// <summary>
        /// Fire the events registered for this event type synchronously
        /// </summary>
        /// <param name="capsEvent">Capability name</param>
        /// <param name="body">Decoded event body</param>
        /// <param name="simulator">Reference to the simulator that
        /// generated this event</param>
        internal void RaiseEvent(string capsEvent, StructuredData.LLSD body, Simulator simulator)
        {
            bool specialHandler = false;

            Caps.EventQueueCallback callback;

            // Default handler first, if one exists
            if (_EventTable.TryGetValue(capsEvent, out callback))
            {
                if (callback != null)
                {
                    try { callback(capsEvent, body, simulator); }
                    catch (Exception ex) { Logger.Log("CAPS Event Handler: " + ex.ToString(), Helpers.LogLevel.Error, Client); }
                }
            }

            // Generic parser next
            if (body.Type == StructuredData.LLSDType.Map)
            {
                StructuredData.LLSDMap map = (StructuredData.LLSDMap)body;
                Packet packet = Packet.BuildPacket(capsEvent, map);
                if (packet != null)
                {
                    NetworkManager.IncomingPacket incomingPacket;
                    incomingPacket.Simulator = simulator;
                    incomingPacket.Packet    = packet;

                    Logger.DebugLog("Serializing " + packet.Type.ToString() + " capability with generic handler", Client);

                    Client.Network.PacketInbox.Enqueue(incomingPacket);
                    specialHandler = true;
                }
            }

            // Explicit handler next
            if (_EventTable.TryGetValue(capsEvent, out callback) && callback != null)
            {
                try { callback(capsEvent, body, simulator); }
                catch (Exception ex) { Logger.Log("CAPS Event Handler: " + ex.ToString(), Helpers.LogLevel.Error, Client); }

                specialHandler = true;
            }

            if (!specialHandler)
            {
                Logger.Log("Unhandled CAPS event " + capsEvent, Helpers.LogLevel.Warning, Client);
            }
        }
        private static LLSD DeserializeNotationMap(StringReader reader)
        {
            int     character;
            LLSDMap llsdMap = new LLSDMap();

            while (((character = PeekAndSkipWhitespace(reader)) > 0) &&
                   ((char)character != mapEndNotationMarker))
            {
                LLSD llsdKey = DeserializeNotationElement(reader);
                if (llsdKey.Type != LLSDType.String)
                {
                    throw new LLSDException("Notation LLSD parsing: Invalid key in map");
                }
                string key = llsdKey.AsString();

                character = ReadAndSkipWhitespace(reader);
                if ((char)character != keyNotationDelimiter)
                {
                    throw new LLSDException("Notation LLSD parsing: Unexpected end of stream in map.");
                }
                if ((char)character != keyNotationDelimiter)
                {
                    throw new LLSDException("Notation LLSD parsing: Invalid delimiter in map.");
                }

                llsdMap[key] = DeserializeNotationElement(reader);
                character    = ReadAndSkipWhitespace(reader);
                if (character < 0)
                {
                    throw new LLSDException("Notation LLSD parsing: Unexpected end of map discovered.");
                }
                else if ((char)character == kommaNotationDelimiter)
                {
                    continue;
                }
                else if ((char)character == mapEndNotationMarker)
                {
                    break;
                }
            }
            if (character < 0)
            {
                throw new LLSDException("Notation LLSD parsing: Unexpected end of map discovered.");
            }

            return((LLSD)llsdMap);
        }
示例#4
0
        private static LLSD ParseNotationElement(string notationData, out int endPos)
        {
            if (notationData.Length == 0)
            {
                endPos = 0;
                return(null);
            }

            // Identify what type of object this is
            switch (notationData[0])
            {
            case '!':
                endPos = 1;
                return(new LLSD());

            case '1':
                endPos = 1;
                return(LLSD.FromBoolean(true));

            case '0':
                endPos = 1;
                return(LLSD.FromBoolean(false));

            case 'i':
            {
                if (notationData.Length < 2)
                {
                    endPos = notationData.Length;
                    return(LLSD.FromInteger(0));
                }

                int value;
                endPos = FindEnd(notationData, 1);

                if (Helpers.TryParse(notationData.Substring(1, endPos - 1), out value))
                {
                    return(LLSD.FromInteger(value));
                }
                else
                {
                    return(LLSD.FromInteger(0));
                }
            }

            case 'r':
            {
                if (notationData.Length < 2)
                {
                    endPos = notationData.Length;
                    return(LLSD.FromReal(0d));
                }

                double value;
                endPos = FindEnd(notationData, 1);

                if (Helpers.TryParse(notationData.Substring(1, endPos - 1), out value))
                {
                    return(LLSD.FromReal(value));
                }
                else
                {
                    return(LLSD.FromReal(0d));
                }
            }

            case 'u':
            {
                if (notationData.Length < 17)
                {
                    endPos = notationData.Length;
                    return(LLSD.FromUUID(LLUUID.Zero));
                }

                LLUUID value;
                endPos = FindEnd(notationData, 1);

                if (Helpers.TryParse(notationData.Substring(1, endPos - 1), out value))
                {
                    return(LLSD.FromUUID(value));
                }
                else
                {
                    return(LLSD.FromUUID(LLUUID.Zero));
                }
            }

            case 'b':
                throw new NotImplementedException("Notation binary type is unimplemented");

            case 's':
            case '"':
            case '\'':
                if (notationData.Length < 2)
                {
                    endPos = notationData.Length;
                    return(LLSD.FromString(String.Empty));
                }

                endPos = FindEnd(notationData, 1);
                return(LLSD.FromString(notationData.Substring(1, endPos - 1).Trim(new char[] { '"', '\'' })));

            case 'l':
                throw new NotImplementedException("Notation URI type is unimplemented");

            case 'd':
                throw new NotImplementedException("Notation date type is unimplemented");

            case '[':
            {
                if (notationData.IndexOf(']') == -1)
                {
                    throw new LLSDException("Invalid notation array");
                }

                int       pos   = 0;
                LLSDArray array = new LLSDArray();

                while (notationData[pos] != ']')
                {
                    ++pos;

                    // Advance past comma if need be
                    if (notationData[pos] == ',')
                    {
                        ++pos;
                    }

                    // Allow a single whitespace character
                    if (pos < notationData.Length && notationData[pos] == ' ')
                    {
                        ++pos;
                    }

                    int end;
                    array.Add(ParseNotationElement(notationData.Substring(pos), out end));
                    pos += end;
                }

                endPos = pos + 1;
                return(array);
            }

            case '{':
            {
                if (notationData.IndexOf('}') == -1)
                {
                    throw new LLSDException("Invalid notation map");
                }

                int     pos       = 0;
                LLSDMap hashtable = new LLSDMap();

                while (notationData[pos] != '}')
                {
                    ++pos;

                    // Advance past comma if need be
                    if (notationData[pos] == ',')
                    {
                        ++pos;
                    }

                    // Allow a single whitespace character
                    if (pos < notationData.Length && notationData[pos] == ' ')
                    {
                        ++pos;
                    }

                    if (notationData[pos] != '\'')
                    {
                        throw new LLSDException("Expected a map key");
                    }

                    int endquote = notationData.IndexOf('\'', pos + 1);
                    if (endquote == -1 || (endquote + 1) >= notationData.Length || notationData[endquote + 1] != ':')
                    {
                        throw new LLSDException("Invalid map format");
                    }

                    string key = notationData.Substring(pos, endquote - pos);
                    key  = key.Trim(new char[] { '"', '\'' });    //key.Replace("'", String.Empty);
                    pos += (endquote - pos) + 2;

                    int end;
                    hashtable[key] = ParseNotationElement(notationData.Substring(pos), out end);
                    pos           += end;
                }

                endPos = pos + 1;
                return(hashtable);
            }

            default:
                throw new LLSDException("Unknown notation value type");
            }
        }
示例#5
0
        public static void SerializeXmlElement(XmlTextWriter writer, LLSD data)
        {
            switch (data.Type)
            {
            case LLSDType.Unknown:
                writer.WriteStartElement(String.Empty, "undef", String.Empty);
                writer.WriteEndElement();
                break;

            case LLSDType.Boolean:
                writer.WriteStartElement(String.Empty, "boolean", String.Empty);
                writer.WriteString(data.AsString());
                writer.WriteEndElement();
                break;

            case LLSDType.Integer:
                writer.WriteStartElement(String.Empty, "integer", String.Empty);
                writer.WriteString(data.AsString());
                writer.WriteEndElement();
                break;

            case LLSDType.Real:
                writer.WriteStartElement(String.Empty, "real", String.Empty);
                writer.WriteString(data.AsString());
                writer.WriteEndElement();
                break;

            case LLSDType.String:
                writer.WriteStartElement(String.Empty, "string", String.Empty);
                writer.WriteString(data.AsString());
                writer.WriteEndElement();
                break;

            case LLSDType.UUID:
                writer.WriteStartElement(String.Empty, "uuid", String.Empty);
                writer.WriteString(data.AsString());
                writer.WriteEndElement();
                break;

            case LLSDType.Date:
                writer.WriteStartElement(String.Empty, "date", String.Empty);
                writer.WriteString(data.AsString());
                writer.WriteEndElement();
                break;

            case LLSDType.URI:
                writer.WriteStartElement(String.Empty, "uri", String.Empty);
                writer.WriteString(data.AsString());
                writer.WriteEndElement();
                break;

            case LLSDType.Binary:
                writer.WriteStartElement(String.Empty, "binary", String.Empty);
                writer.WriteStartAttribute(String.Empty, "encoding", String.Empty);
                writer.WriteString("base64");
                writer.WriteEndAttribute();
                writer.WriteString(data.AsString());
                writer.WriteEndElement();
                break;

            case LLSDType.Map:
                LLSDMap map = (LLSDMap)data;
                writer.WriteStartElement(String.Empty, "map", String.Empty);
                foreach (KeyValuePair <string, LLSD> kvp in map)
                {
                    writer.WriteStartElement(String.Empty, "key", String.Empty);
                    writer.WriteString(kvp.Key);
                    writer.WriteEndElement();

                    SerializeXmlElement(writer, kvp.Value);
                }
                writer.WriteEndElement();
                break;

            case LLSDType.Array:
                LLSDArray array = (LLSDArray)data;
                writer.WriteStartElement(String.Empty, "array", String.Empty);
                for (int i = 0; i < array.Count; i++)
                {
                    SerializeXmlElement(writer, array[i]);
                }
                writer.WriteEndElement();
                break;
            }
        }
示例#6
0
 public static byte[] SerializeXmlBytes(LLSD data)
 {
     return(Encoding.UTF8.GetBytes(SerializeXmlString(data)));
 }
示例#7
0
        private static LLSD ParseXmlElement(XmlTextReader reader)
        {
            SkipWhitespace(reader);

            if (reader.NodeType != XmlNodeType.Element)
            {
                throw new LLSDException("Expected an element");
            }

            string type = reader.LocalName;
            LLSD   ret;

            switch (type)
            {
            case "undef":
                if (reader.IsEmptyElement)
                {
                    reader.Read();
                    return(new LLSD());
                }

                reader.Read();
                SkipWhitespace(reader);
                ret = new LLSD();
                break;

            case "boolean":
                if (reader.IsEmptyElement)
                {
                    reader.Read();
                    return(LLSD.FromBoolean(false));
                }

                if (reader.Read())
                {
                    string s = reader.ReadString().Trim();

                    if (!String.IsNullOrEmpty(s) && (s == "true" || s == "1"))
                    {
                        ret = LLSD.FromBoolean(true);
                        break;
                    }
                }

                ret = LLSD.FromBoolean(false);
                break;

            case "integer":
                if (reader.IsEmptyElement)
                {
                    reader.Read();
                    return(LLSD.FromInteger(0));
                }

                if (reader.Read())
                {
                    int value = 0;
                    Helpers.TryParse(reader.ReadString().Trim(), out value);
                    ret = LLSD.FromInteger(value);
                    break;
                }

                ret = LLSD.FromInteger(0);
                break;

            case "real":
                if (reader.IsEmptyElement)
                {
                    reader.Read();
                    return(LLSD.FromReal(0d));
                }

                if (reader.Read())
                {
                    double value = 0d;
                    string str   = reader.ReadString().Trim().ToLower();

                    if (str == "nan")
                    {
                        value = Double.NaN;
                    }
                    else
                    {
                        Helpers.TryParse(str, out value);
                    }

                    ret = LLSD.FromReal(value);
                    break;
                }

                ret = LLSD.FromReal(0d);
                break;

            case "uuid":
                if (reader.IsEmptyElement)
                {
                    reader.Read();
                    return(LLSD.FromUUID(LLUUID.Zero));
                }

                if (reader.Read())
                {
                    LLUUID value = LLUUID.Zero;
                    LLUUID.TryParse(reader.ReadString().Trim(), out value);
                    ret = LLSD.FromUUID(value);
                    break;
                }

                ret = LLSD.FromUUID(LLUUID.Zero);
                break;

            case "date":
                if (reader.IsEmptyElement)
                {
                    reader.Read();
                    return(LLSD.FromDate(Helpers.Epoch));
                }

                if (reader.Read())
                {
                    DateTime value = Helpers.Epoch;
                    Helpers.TryParse(reader.ReadString().Trim(), out value);
                    ret = LLSD.FromDate(value);
                    break;
                }

                ret = LLSD.FromDate(Helpers.Epoch);
                break;

            case "string":
                if (reader.IsEmptyElement)
                {
                    reader.Read();
                    return(LLSD.FromString(String.Empty));
                }

                if (reader.Read())
                {
                    ret = LLSD.FromString(reader.ReadString());
                    break;
                }

                ret = LLSD.FromString(String.Empty);
                break;

            case "binary":
                if (reader.IsEmptyElement)
                {
                    reader.Read();
                    return(LLSD.FromBinary(new byte[0]));
                }

                if (reader.GetAttribute("encoding") != null && reader.GetAttribute("encoding") != "base64")
                {
                    throw new LLSDException("Unsupported binary encoding: " + reader.GetAttribute("encoding"));
                }

                if (reader.Read())
                {
                    try
                    {
                        ret = LLSD.FromBinary(Convert.FromBase64String(reader.ReadString().Trim()));
                        break;
                    }
                    catch (FormatException ex)
                    {
                        throw new LLSDException("Binary decoding exception: " + ex.Message);
                    }
                }

                ret = LLSD.FromBinary(new byte[0]);
                break;

            case "uri":
                if (reader.IsEmptyElement)
                {
                    reader.Read();
                    return(LLSD.FromUri(new Uri(String.Empty, UriKind.RelativeOrAbsolute)));
                }

                if (reader.Read())
                {
                    ret = LLSD.FromUri(new Uri(reader.ReadString(), UriKind.RelativeOrAbsolute));
                    break;
                }

                ret = LLSD.FromUri(new Uri(String.Empty, UriKind.RelativeOrAbsolute));
                break;

            case "map":
                return(ParseXmlMap(reader));

            case "array":
                return(ParseXmlArray(reader));

            default:
                reader.Read();
                ret = null;
                break;
            }

            if (reader.NodeType != XmlNodeType.EndElement || reader.LocalName != type)
            {
                throw new LLSDException("Expected </" + type + ">");
            }
            else
            {
                reader.Read();
                return(ret);
            }
        }
        /// <summary>
        ///
        /// </summary>
        /// <param name="reader"></param>
        /// <returns></returns>
        public static LLSD DeserializeNotation(StringReader reader)
        {
            LLSD llsd = DeserializeNotationElement(reader);

            return(llsd);
        }
        private static void SerializeNotationElementFormatted(StringWriter writer, string intend, LLSD llsd)
        {
            switch (llsd.Type)
            {
            case LLSDType.Unknown:
                writer.Write(undefNotationValue);
                break;

            case LLSDType.Boolean:
                if (llsd.AsBoolean())
                {
                    writer.Write(trueNotationValueTwo);
                }
                else
                {
                    writer.Write(falseNotationValueTwo);
                }
                break;

            case LLSDType.Integer:
                writer.Write(integerNotationMarker);
                writer.Write(llsd.AsString());
                break;

            case LLSDType.Real:
                writer.Write(realNotationMarker);
                writer.Write(llsd.AsString());
                break;

            case LLSDType.UUID:
                writer.Write(uuidNotationMarker);
                writer.Write(llsd.AsString());
                break;

            case LLSDType.String:
                writer.Write(singleQuotesNotationMarker);
                writer.Write(EscapeCharacter(llsd.AsString(), singleQuotesNotationMarker));
                writer.Write(singleQuotesNotationMarker);
                break;

            case LLSDType.Binary:
                writer.Write(binaryNotationMarker);
                writer.Write("64");
                writer.Write(doubleQuotesNotationMarker);
                writer.Write(llsd.AsString());
                writer.Write(doubleQuotesNotationMarker);
                break;

            case LLSDType.Date:
                writer.Write(dateNotationMarker);
                writer.Write(doubleQuotesNotationMarker);
                writer.Write(llsd.AsString());
                writer.Write(doubleQuotesNotationMarker);
                break;

            case LLSDType.URI:
                writer.Write(uriNotationMarker);
                writer.Write(doubleQuotesNotationMarker);
                writer.Write(EscapeCharacter(llsd.AsString(), doubleQuotesNotationMarker));
                writer.Write(doubleQuotesNotationMarker);
                break;

            case LLSDType.Array:
                SerializeNotationArrayFormatted(writer, intend + baseIntend, (LLSDArray)llsd);
                break;

            case LLSDType.Map:
                SerializeNotationMapFormatted(writer, intend + baseIntend, (LLSDMap)llsd);
                break;

            default:
                throw new LLSDException("Notation serialization: Not existing element discovered.");
            }
        }
示例#10
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="reader"></param>
        /// <returns></returns>
        private static LLSD DeserializeNotationElement(StringReader reader)
        {
            int character = ReadAndSkipWhitespace(reader);

            if (character < 0)
            {
                return(new LLSD()); // server returned an empty file, so we're going to pass along a null LLSD object
            }
            LLSD llsd;
            int  matching;

            switch ((char)character)
            {
            case undefNotationValue:
                llsd = new LLSD();
                break;

            case trueNotationValueOne:
                llsd = LLSD.FromBoolean(true);
                break;

            case trueNotationValueTwo:
                matching = BufferCharactersEqual(reader, trueNotationValueTwoFull, 1);
                if (matching > 1 && matching < trueNotationValueTwoFull.Length)
                {
                    throw new LLSDException("Notation LLSD parsing: True value parsing error:");
                }
                llsd = LLSD.FromBoolean(true);
                break;

            case trueNotationValueThree:
                matching = BufferCharactersEqual(reader, trueNotationValueThreeFull, 1);
                if (matching > 1 && matching < trueNotationValueThreeFull.Length)
                {
                    throw new LLSDException("Notation LLSD parsing: True value parsing error:");
                }
                llsd = LLSD.FromBoolean(true);
                break;

            case falseNotationValueOne:
                llsd = LLSD.FromBoolean(false);
                break;

            case falseNotationValueTwo:
                matching = BufferCharactersEqual(reader, falseNotationValueTwoFull, 1);
                if (matching > 1 && matching < falseNotationValueTwoFull.Length)
                {
                    throw new LLSDException("Notation LLSD parsing: True value parsing error:");
                }
                llsd = LLSD.FromBoolean(false);
                break;

            case falseNotationValueThree:
                matching = BufferCharactersEqual(reader, falseNotationValueThreeFull, 1);
                if (matching > 1 && matching < falseNotationValueThreeFull.Length)
                {
                    throw new LLSDException("Notation LLSD parsing: True value parsing error:");
                }
                llsd = LLSD.FromBoolean(false);
                break;

            case integerNotationMarker:
                llsd = DeserializeNotationInteger(reader);
                break;

            case realNotationMarker:
                llsd = DeserializeNotationReal(reader);
                break;

            case uuidNotationMarker:
                char[] uuidBuf = new char[36];
                if (reader.Read(uuidBuf, 0, 36) < 36)
                {
                    throw new LLSDException("Notation LLSD parsing: Unexpected end of stream in UUID.");
                }
                LLUUID lluuid;
                if (!LLUUID.TryParse(new String(uuidBuf), out lluuid))
                {
                    throw new LLSDException("Notation LLSD parsing: Invalid UUID discovered.");
                }
                llsd = LLSD.FromUUID(lluuid);
                break;

            case binaryNotationMarker:
                byte[] bytes = new byte[0];
                int    bChar = reader.Peek();
                if (bChar < 0)
                {
                    throw new LLSDException("Notation LLSD parsing: Unexpected end of stream in binary.");
                }
                if ((char)bChar == sizeBeginNotationMarker)
                {
                    throw new LLSDException("Notation LLSD parsing: Raw binary encoding not supported.");
                }
                else if (Char.IsDigit((char)bChar))
                {
                    char[] charsBaseEncoding = new char[2];
                    if (reader.Read(charsBaseEncoding, 0, 2) < 2)
                    {
                        throw new LLSDException("Notation LLSD parsing: Unexpected end of stream in binary.");
                    }
                    int baseEncoding;
                    if (!Int32.TryParse(new String(charsBaseEncoding), out baseEncoding))
                    {
                        throw new LLSDException("Notation LLSD parsing: Invalid binary encoding base.");
                    }
                    if (baseEncoding == 64)
                    {
                        if (reader.Read() < 0)
                        {
                            throw new LLSDException("Notation LLSD parsing: Unexpected end of stream in binary.");
                        }
                        string bytes64 = GetStringDelimitedBy(reader, doubleQuotesNotationMarker);
                        bytes = Convert.FromBase64String(bytes64);
                    }
                    else
                    {
                        throw new LLSDException("Notation LLSD parsing: Encoding base" + baseEncoding + " + not supported.");
                    }
                }
                llsd = LLSD.FromBinary(bytes);
                break;

            case stringNotationMarker:
                int numChars = GetLengthInBrackets(reader);
                if (reader.Read() < 0)
                {
                    throw new LLSDException("Notation LLSD parsing: Unexpected end of stream in string.");
                }
                char[] chars = new char[numChars];
                if (reader.Read(chars, 0, numChars) < numChars)
                {
                    throw new LLSDException("Notation LLSD parsing: Unexpected end of stream in string.");
                }
                if (reader.Read() < 0)
                {
                    throw new LLSDException("Notation LLSD parsing: Unexpected end of stream in string.");
                }
                llsd = LLSD.FromString(new String(chars));
                break;

            case singleQuotesNotationMarker:
                string sOne = GetStringDelimitedBy(reader, singleQuotesNotationMarker);
                llsd = LLSD.FromString(sOne);
                break;

            case doubleQuotesNotationMarker:
                string sTwo = GetStringDelimitedBy(reader, doubleQuotesNotationMarker);
                llsd = LLSD.FromString(sTwo);
                break;

            case uriNotationMarker:
                if (reader.Read() < 0)
                {
                    throw new LLSDException("Notation LLSD parsing: Unexpected end of stream in string.");
                }
                string sUri = GetStringDelimitedBy(reader, doubleQuotesNotationMarker);

                Uri uri;
                try
                {
                    uri = new Uri(sUri, UriKind.RelativeOrAbsolute);
                }
                catch
                {
                    throw new LLSDException("Notation LLSD parsing: Invalid Uri format detected.");
                }
                llsd = LLSD.FromUri(uri);
                break;

            case dateNotationMarker:
                if (reader.Read() < 0)
                {
                    throw new LLSDException("Notation LLSD parsing: Unexpected end of stream in date.");
                }
                string   date = GetStringDelimitedBy(reader, doubleQuotesNotationMarker);
                DateTime dt;
                if (!Helpers.TryParse(date, out dt))
                {
                    throw new LLSDException("Notation LLSD parsing: Invalid date discovered.");
                }
                llsd = LLSD.FromDate(dt);
                break;

            case arrayBeginNotationMarker:
                llsd = DeserializeNotationArray(reader);
                break;

            case mapBeginNotationMarker:
                llsd = DeserializeNotationMap(reader);
                break;

            default:
                throw new LLSDException("Notation LLSD parsing: Unknown type marker '" + (char)character + "'.");
            }
            return(llsd);
        }
示例#11
0
        private static LLSD ParseBinaryElement(MemoryStream stream)
        {
            SkipWhiteSpace(stream);
            LLSD llsd;

            int marker = stream.ReadByte();

            if (marker < 0)
            {
                throw new LLSDException("Binary LLSD parsing:Unexpected end of stream.");
            }

            switch ((byte)marker)
            {
            case undefBinaryValue:
                llsd = new LLSD();
                break;

            case trueBinaryValue:
                llsd = LLSD.FromBoolean(true);
                break;

            case falseBinaryValue:
                llsd = LLSD.FromBoolean(false);
                break;

            case integerBinaryMarker:
                int integer = NetworkToHostInt(ConsumeBytes(stream, int32Length));
                llsd = LLSD.FromInteger(integer);
                break;

            case realBinaryMarker:
                double dbl = NetworkToHostDouble(ConsumeBytes(stream, doubleLength));
                llsd = LLSD.FromReal(dbl);
                break;

            case uuidBinaryMarker:
                llsd = LLSD.FromUUID(new LLUUID(ConsumeBytes(stream, 16), 0));
                break;

            case binaryBinaryMarker:
                int binaryLength = NetworkToHostInt(ConsumeBytes(stream, int32Length));
                llsd = LLSD.FromBinary(ConsumeBytes(stream, binaryLength));
                break;

            case stringBinaryMarker:
                int    stringLength = NetworkToHostInt(ConsumeBytes(stream, int32Length));
                string ss           = Encoding.UTF8.GetString(ConsumeBytes(stream, stringLength));
                llsd = LLSD.FromString(ss);
                break;

            case uriBinaryMarker:
                int    uriLength = NetworkToHostInt(ConsumeBytes(stream, int32Length));
                string sUri      = Encoding.UTF8.GetString(ConsumeBytes(stream, uriLength));
                Uri    uri;
                try
                {
                    uri = new Uri(sUri, UriKind.RelativeOrAbsolute);
                }
                catch
                {
                    throw new LLSDException("Binary LLSD parsing: Invalid Uri format detected.");
                }
                llsd = LLSD.FromUri(uri);
                break;

            case dateBinaryMarker:
                double   timestamp = NetworkToHostDouble(ConsumeBytes(stream, doubleLength));
                DateTime dateTime  = DateTime.SpecifyKind(Helpers.Epoch, DateTimeKind.Utc);
                dateTime = dateTime.AddSeconds(timestamp);
                llsd     = LLSD.FromDate(dateTime.ToLocalTime());
                break;

            case arrayBeginBinaryMarker:
                llsd = ParseBinaryArray(stream);
                break;

            case mapBeginBinaryMarker:
                llsd = ParseBinaryMap(stream);
                break;

            default:
                throw new LLSDException("Binary LLSD parsing: Unknown type marker.");
            }
            return(llsd);
        }
示例#12
0
        private static void SerializeBinaryElement(MemoryStream stream, LLSD llsd)
        {
            switch (llsd.Type)
            {
            case LLSDType.Unknown:
                stream.WriteByte(undefBinaryValue);
                break;

            case LLSDType.Boolean:
                stream.Write(llsd.AsBinary(), 0, 1);
                break;

            case LLSDType.Integer:
                stream.WriteByte(integerBinaryMarker);
                stream.Write(llsd.AsBinary(), 0, int32Length);
                break;

            case LLSDType.Real:
                stream.WriteByte(realBinaryMarker);
                stream.Write(llsd.AsBinary(), 0, doubleLength);
                break;

            case LLSDType.UUID:
                stream.WriteByte(uuidBinaryMarker);
                stream.Write(llsd.AsBinary(), 0, 16);
                break;

            case LLSDType.String:
                stream.WriteByte(stringBinaryMarker);
                byte[] rawString          = llsd.AsBinary();
                byte[] stringLengthNetEnd = HostToNetworkIntBytes(rawString.Length);
                stream.Write(stringLengthNetEnd, 0, int32Length);
                stream.Write(rawString, 0, rawString.Length);
                break;

            case LLSDType.Binary:
                stream.WriteByte(binaryBinaryMarker);
                byte[] rawBinary          = llsd.AsBinary();
                byte[] binaryLengthNetEnd = HostToNetworkIntBytes(rawBinary.Length);
                stream.Write(binaryLengthNetEnd, 0, int32Length);
                stream.Write(rawBinary, 0, rawBinary.Length);
                break;

            case LLSDType.Date:
                stream.WriteByte(dateBinaryMarker);
                stream.Write(llsd.AsBinary(), 0, doubleLength);
                break;

            case LLSDType.URI:
                stream.WriteByte(uriBinaryMarker);
                byte[] rawURI          = llsd.AsBinary();
                byte[] uriLengthNetEnd = HostToNetworkIntBytes(rawURI.Length);
                stream.Write(uriLengthNetEnd, 0, int32Length);
                stream.Write(rawURI, 0, rawURI.Length);
                break;

            case LLSDType.Array:
                SerializeBinaryArray(stream, (LLSDArray)llsd);
                break;

            case LLSDType.Map:
                SerializeBinaryMap(stream, (LLSDMap)llsd);
                break;

            default:
                throw new LLSDException("Binary serialization: Not existing element discovered.");
            }
        }