Beispiel #1
0
        private static SubItemToken StartSubItem(object instance, ProtoWriter writer, bool allowFixed)
        {
            if (writer == null)
            {
                throw new ArgumentNullException("writer");
            }
            if (++writer.depth > 0x19)
            {
                writer.CheckRecursionStackAndPush(instance);
            }
            if (writer.packedFieldNumber != 0)
            {
                throw new InvalidOperationException("Cannot begin a sub-item while performing packed encoding");
            }
            switch (writer.wireType)
            {
            case WireType.String:
                writer.wireType = WireType.None;
                DemandSpace(0x20, writer);
                writer.flushLock++;
                writer.position++;
                return(new SubItemToken(writer.ioIndex++));

            case WireType.StartGroup:
                writer.wireType = WireType.None;
                return(new SubItemToken(-writer.fieldNumber));

            case WireType.Fixed32:
            {
                if (!allowFixed)
                {
                    throw CreateException(writer);
                }
                DemandSpace(0x20, writer);
                writer.flushLock++;
                SubItemToken token = new SubItemToken(writer.ioIndex);
                IncrementedAndReset(4, writer);
                return(token);
            }
            }
            throw CreateException(writer);
        }
Beispiel #2
0
        public static void WriteObject(object value, int key, ProtoWriter writer)
        {
            if (writer == null)
            {
                throw new ArgumentNullException("writer");
            }
            if (writer.model == null)
            {
                throw new InvalidOperationException("Cannot serialize sub-objects unless a model is provided");
            }
            SubItemToken token = StartSubItem(value, writer);

            if (key >= 0)
            {
                writer.model.Serialize(key, value, writer);
            }
            else if ((writer.model == null) || !writer.model.TrySerializeAuxiliaryType(writer, value.GetType(), DataFormat.Default, 1, value, false))
            {
                TypeModel.ThrowUnexpectedType(value.GetType());
            }
            EndSubItem(token, writer);
        }
Beispiel #3
0
        public static Guid ReadGuid(ProtoReader source)
        {
            int          num3;
            ulong        num   = 0L;
            ulong        num2  = 0L;
            SubItemToken token = ProtoReader.StartSubItem(source);

            while ((num3 = source.ReadFieldHeader()) > 0)
            {
                switch (num3)
                {
                case 1:
                {
                    num = source.ReadUInt64();
                    continue;
                }

                case 2:
                {
                    num2 = source.ReadUInt64();
                    continue;
                }
                }
                source.SkipField();
            }
            ProtoReader.EndSubItem(token, source);
            if ((num == 0L) && (num2 == 0L))
            {
                return(Guid.Empty);
            }
            uint num4 = (uint)(num >> 0x20);
            uint num5 = (uint)num;
            uint num6 = (uint)(num2 >> 0x20);
            uint num7 = (uint)num2;

            return(new Guid((int)num5, (short)num4, (short)(num4 >> 0x10), (byte)num7, (byte)(num7 >> 8), (byte)(num7 >> 0x10), (byte)(num7 >> 0x18), (byte)num6, (byte)(num6 >> 8), (byte)(num6 >> 0x10), (byte)(num6 >> 0x18)));
        }
Beispiel #4
0
        private static void EndSubItem(SubItemToken token, ProtoWriter writer, PrefixStyle style)
        {
            if (writer == null)
            {
                throw new ArgumentNullException("writer");
            }
            if (writer.wireType != WireType.None)
            {
                throw CreateException(writer);
            }
            int index = token.value;

            if (writer.depth <= 0)
            {
                throw CreateException(writer);
            }
            if (writer.depth-- > 0x19)
            {
                writer.PopRecursionStack();
            }
            writer.packedFieldNumber = 0;
            if (index < 0)
            {
                WriteHeaderCore(-index, WireType.EndGroup, writer);
                writer.wireType = WireType.None;
            }
            else
            {
                int num2;
                switch (style)
                {
                case PrefixStyle.Base128:
                {
                    num2 = (writer.ioIndex - index) - 1;
                    int  required = 0;
                    uint num5     = (uint)num2;
                    while ((num5 = num5 >> 7) != 0)
                    {
                        required++;
                    }
                    if (required == 0)
                    {
                        writer.ioBuffer[index] = (byte)(num2 & 0x7f);
                    }
                    else
                    {
                        DemandSpace(required, writer);
                        byte[] ioBuffer = writer.ioBuffer;
                        Helpers.BlockCopy(ioBuffer, index + 1, ioBuffer, (index + 1) + required, num2);
                        num5 = (uint)num2;
                        do
                        {
                            ioBuffer[index++] = (byte)((num5 & 0x7f) | 0x80);
                        }while ((num5 = num5 >> 7) != 0);
                        ioBuffer[index - 1] = (byte)(ioBuffer[index - 1] & -129);
                        writer.position    += required;
                        writer.ioIndex     += required;
                    }
                    break;
                }

                case PrefixStyle.Fixed32:
                    num2 = (writer.ioIndex - index) - 4;
                    WriteInt32ToBuffer(num2, writer.ioBuffer, index);
                    break;

                case PrefixStyle.Fixed32BigEndian:
                {
                    num2 = (writer.ioIndex - index) - 4;
                    byte[] buffer = writer.ioBuffer;
                    WriteInt32ToBuffer(num2, buffer, index);
                    byte num3 = buffer[index];
                    buffer[index]     = buffer[index + 3];
                    buffer[index + 3] = num3;
                    num3 = buffer[index + 1];
                    buffer[index + 1] = buffer[index + 2];
                    buffer[index + 2] = num3;
                    break;
                }

                default:
                    throw new ArgumentOutOfRangeException("style");
                }
                if ((--writer.flushLock == 0) && (writer.ioIndex >= 0x400))
                {
                    Flush(writer);
                }
            }
        }
Beispiel #5
0
 public static void EndSubItem(SubItemToken token, ProtoWriter writer)
 {
     EndSubItem(token, writer, PrefixStyle.Base128);
 }
Beispiel #6
0
        public static void WriteTimeSpan(TimeSpan timeSpan, ProtoWriter dest)
        {
            long          ticks;
            TimeSpanScale minMax;

            if (dest == null)
            {
                throw new ArgumentNullException("dest");
            }
            switch (dest.WireType)
            {
            case WireType.Fixed64:
                ProtoWriter.WriteInt64(timeSpan.Ticks, dest);
                return;

            case WireType.String:
            case WireType.StartGroup:
                ticks = timeSpan.Ticks;
                if (!(timeSpan == TimeSpan.MaxValue))
                {
                    if (timeSpan == TimeSpan.MinValue)
                    {
                        ticks  = -1L;
                        minMax = TimeSpanScale.MinMax;
                    }
                    else if ((ticks % 0xc92a69c000L) == 0L)
                    {
                        minMax = TimeSpanScale.Days;
                        ticks /= 0xc92a69c000L;
                    }
                    else if ((ticks % 0x861c46800L) == 0L)
                    {
                        minMax = TimeSpanScale.Hours;
                        ticks /= 0x861c46800L;
                    }
                    else if ((ticks % 0x23c34600L) == 0L)
                    {
                        minMax = TimeSpanScale.Minutes;
                        ticks /= 0x23c34600L;
                    }
                    else if ((ticks % 0x989680L) == 0L)
                    {
                        minMax = TimeSpanScale.Seconds;
                        ticks /= 0x989680L;
                    }
                    else if ((ticks % 0x2710L) == 0L)
                    {
                        minMax = TimeSpanScale.Milliseconds;
                        ticks /= 0x2710L;
                    }
                    else
                    {
                        minMax = TimeSpanScale.Ticks;
                    }
                    break;
                }
                ticks  = 1L;
                minMax = TimeSpanScale.MinMax;
                break;

            default:
                throw new ProtoException("Unexpected wire-type: " + dest.WireType.ToString());
            }
            SubItemToken token = ProtoWriter.StartSubItem(null, dest);

            if (ticks != 0L)
            {
                ProtoWriter.WriteFieldHeader(1, WireType.SignedVariant, dest);
                ProtoWriter.WriteInt64(ticks, dest);
            }
            if (minMax != TimeSpanScale.Days)
            {
                ProtoWriter.WriteFieldHeader(2, WireType.Variant, dest);
                ProtoWriter.WriteInt32((int)minMax, dest);
            }
            ProtoWriter.EndSubItem(token, dest);
        }
Beispiel #7
0
        private static long ReadTimeSpanTicks(ProtoReader source)
        {
            long num2;

            switch (source.WireType)
            {
            case WireType.Fixed64:
                return(source.ReadInt64());

            case WireType.String:
            case WireType.StartGroup:
            {
                int           num;
                SubItemToken  token = ProtoReader.StartSubItem(source);
                TimeSpanScale days  = TimeSpanScale.Days;
                num2 = 0L;
Label_0065:
                if ((num = source.ReadFieldHeader()) <= 0)
                {
                    ProtoReader.EndSubItem(token, source);
                    switch (days)
                    {
                    case TimeSpanScale.Days:
                        return(num2 * 0xc92a69c000L);

                    case TimeSpanScale.Hours:
                        return(num2 * 0x861c46800L);

                    case TimeSpanScale.Minutes:
                        return(num2 * 0x23c34600L);

                    case TimeSpanScale.Seconds:
                        return(num2 * 0x989680L);

                    case TimeSpanScale.Milliseconds:
                        return(num2 * 0x2710L);

                    case TimeSpanScale.Ticks:
                        return(num2);

                    case TimeSpanScale.MinMax:
                    {
                        long num4 = num2;
                        if ((num4 <= 1L) && (num4 >= -1L))
                        {
                            switch (((int)(num4 - -1L)))
                            {
                            case 0:
                                return(-9223372036854775808L);

                            case 2:
                                return(0x7fffffffffffffffL);
                            }
                        }
                        goto Label_0115;
                    }
                    }
                    throw new ProtoException("Unknown timescale: " + days.ToString());
                }
                switch (num)
                {
                case 1:
                    source.Assert(WireType.SignedVariant);
                    num2 = source.ReadInt64();
                    goto Label_0065;

                case 2:
                    days = (TimeSpanScale)source.ReadInt32();
                    goto Label_0065;
                }
                source.SkipField();
                goto Label_0065;
            }

            default:
                throw new ProtoException("Unexpected wire-type: " + source.WireType.ToString());
            }
Label_0115:
            throw new ProtoException("Unknown min/max value: " + num2.ToString());
        }
Beispiel #8
0
        public static object ReadNetObject(object value, ProtoReader source, int key, Type type, NetObjectOptions options)
        {
            int          num;
            SubItemToken token        = ProtoReader.StartSubItem(source);
            int          newObjectKey = -1;
            int          num3         = -1;

            while ((num = source.ReadFieldHeader()) > 0)
            {
                int    num4;
                bool   flag;
                bool   flag2;
                bool   flag3;
                object keyedObject;
                switch (num)
                {
                case 1:
                {
                    num4  = source.ReadInt32();
                    value = source.NetCache.GetKeyedObject(num4);
                    continue;
                }

                case 2:
                {
                    newObjectKey = source.ReadInt32();
                    continue;
                }

                case 3:
                {
                    num4 = source.ReadInt32();
                    type = (Type)source.NetCache.GetKeyedObject(num4);
                    key  = source.GetTypeKey(ref type);
                    continue;
                }

                case 4:
                {
                    num3 = source.ReadInt32();
                    continue;
                }

                case 8:
                {
                    string str = source.ReadString();
                    type = source.DeserializeType(str);
                    if (type == null)
                    {
                        throw new ProtoException("Unable to resolve type: " + str + " (you can use the TypeModel.DynamicTypeFormatting event to provide a custom mapping)");
                    }
                    break;
                }

                case 10:
                    flag  = type == typeof(string);
                    flag2 = value == null;
                    flag3 = flag2 && (flag || (((byte)(options & NetObjectOptions.LateSet)) != 0));
                    if ((newObjectKey < 0) || flag3)
                    {
                        goto Label_0180;
                    }
                    if (value != null)
                    {
                        goto Label_0162;
                    }
                    source.TrapNextObject(newObjectKey);
                    goto Label_016F;

                default:
                    goto Label_020D;
                }
                if (type == typeof(string))
                {
                    key = -1;
                    continue;
                }
                key = source.GetTypeKey(ref type);
                if (key >= 0)
                {
                    continue;
                }
                throw new InvalidOperationException("Dynamic type is not a contract-type: " + type.Name);
Label_0162:
                source.NetCache.SetKeyedObject(newObjectKey, value);
Label_016F:
                if (num3 >= 0)
                {
                    source.NetCache.SetKeyedObject(num3, type);
                }
Label_0180:
                keyedObject = value;
                if (flag)
                {
                    value = source.ReadString();
                }
                else
                {
                    value = ProtoReader.ReadTypedObject(keyedObject, key, source, type);
                }
                if (newObjectKey >= 0)
                {
                    if (flag2 && !flag3)
                    {
                        keyedObject = source.NetCache.GetKeyedObject(newObjectKey);
                    }
                    if (flag3)
                    {
                        source.NetCache.SetKeyedObject(newObjectKey, value);
                        if (num3 >= 0)
                        {
                            source.NetCache.SetKeyedObject(num3, type);
                        }
                    }
                }
                if (((newObjectKey >= 0) && !flag3) && !object.ReferenceEquals(keyedObject, value))
                {
                    throw new ProtoException("A reference-tracked object changed reference during deserialization");
                }
                if ((newObjectKey < 0) && (num3 >= 0))
                {
                    source.NetCache.SetKeyedObject(num3, type);
                }
                continue;
Label_020D:
                source.SkipField();
            }
            if ((newObjectKey >= 0) && (((byte)(options & NetObjectOptions.AsReference)) == 0))
            {
                throw new ProtoException("Object key in input stream, but reference-tracking was not expected");
            }
            ProtoReader.EndSubItem(token, source);
            return(value);
        }