예제 #1
0
        public static void WriteDateTime(DateTime value, ProtoWriter dest)
        {
            TimeSpan maxValue;

            if (dest == null)
            {
                throw new ArgumentNullException("dest");
            }
            switch (dest.WireType)
            {
            case WireType.String:
            case WireType.StartGroup:
            {
                if (value == DateTime.MaxValue)
                {
                    maxValue = TimeSpan.MaxValue;
                    break;
                }
                else if (value != DateTime.MinValue)
                {
                    maxValue = value - BclHelpers.EpochOrigin;
                    break;
                }
                else
                {
                    maxValue = TimeSpan.MinValue;
                    break;
                }
            }

            default:
            {
                maxValue = value - BclHelpers.EpochOrigin;
                break;
            }
            }
            BclHelpers.WriteTimeSpan(maxValue, dest);
        }
예제 #2
0
        /// <summary>
        /// Reads an *implementation specific* bundled .NET object, including (as options) type-metadata, identity/re-use, etc.
        /// </summary>
        public static object ReadNetObject(object value, ProtoReader source, int key, Type type, NetObjectOptions options)
        {
            SubItemToken token = ProtoReader.StartSubItem(source);
            int          fieldNumber;
            int          newObjectKey = -1, newTypeKey = -1, tmp;

            while ((fieldNumber = source.ReadFieldHeader()) > 0)
            {
                switch (fieldNumber)
                {
                case FieldExistingObjectKey:
                    tmp   = source.ReadInt32();
                    value = source.NetCache.GetKeyedObject(tmp);
                    break;

                case FieldNewObjectKey:
                    newObjectKey = source.ReadInt32();
                    break;

                case FieldExistingTypeKey:
                    tmp  = source.ReadInt32();
                    type = (Type)source.NetCache.GetKeyedObject(tmp);
                    key  = source.GetTypeKey(ref type);
                    break;

                case FieldNewTypeKey:
                    newTypeKey = source.ReadInt32();
                    break;

                case FieldTypeName:
                    string typeName = source.ReadString();
                    type = source.DeserializeType(typeName);
                    if (type == null)
                    {
                        throw new ProtoException("Unable to resolve type: " + typeName + " (you can use the TypeModel.DynamicTypeFormatting event to provide a custom mapping)");
                    }
                    key = source.GetTypeKey(ref type);
                    if (key < 0)
                    {
                        throw new InvalidOperationException("Dynamic type is not a contract-type: " + type.Name);
                    }
                    break;

                case FieldObject:
                    bool isString = type == typeof(string);
                    bool lateSet  = value == null && isString;
                    if (value == null && !lateSet)
                    {
                        try
                        {
                            value = ((options & NetObjectOptions.UseConstructor) == 0)
                                            ? BclHelpers.GetUninitializedObject(type)
                                            : Activator.CreateInstance(type);
                        } catch (Exception ex)
                        {
                            throw new ProtoException("Unable to create type " + (type == null ? "<null>" : type.FullName) + ": " + ex.Message, ex);
                        }
                    }
                    if (newObjectKey >= 0 && !lateSet)
                    {
                        source.NetCache.SetKeyedObject(newObjectKey, value);
                        if (newTypeKey >= 0)
                        {
                            source.NetCache.SetKeyedObject(newTypeKey, type);
                        }
                    }
                    object oldValue = value;
                    if (isString)
                    {
                        value = source.ReadString();
                    }
                    else
                    {
                        value = ProtoReader.ReadTypedObject(oldValue, key, source, type);
                    }

                    if (newObjectKey >= 0 && lateSet)
                    {
                        source.NetCache.SetKeyedObject(newObjectKey, value);
                        if (newTypeKey >= 0)
                        {
                            source.NetCache.SetKeyedObject(newTypeKey, type);
                        }
                    }
                    if (newObjectKey >= 0 && !lateSet && !ReferenceEquals(oldValue, value))
                    {
                        throw new ProtoException("A reference-tracked object changed reference during deserialization");
                    }
                    if (newObjectKey < 0 && newTypeKey >= 0)
                    {      // have a new type, but not a new object
                        source.NetCache.SetKeyedObject(newTypeKey, type);
                    }
                    break;

                default:
                    source.SkipField();
                    break;
                }
            }
            if (newObjectKey >= 0 && (options & NetObjectOptions.AsReference) == 0)
            {
                throw new ProtoException("Object key in input stream, but reference-tracking was not expected");
            }
            ProtoReader.EndSubItem(token, source);

            return(value);
        }
예제 #3
0
 // Token: 0x06003262 RID: 12898 RVA: 0x001263B4 File Offset: 0x001247B4
 public static void WriteTimeSpan(TimeSpan timeSpan, ProtoWriter dest)
 {
     BclHelpers.WriteTimeSpanImpl(timeSpan, dest, DateTimeKind.Unspecified);
 }
예제 #4
0
 // Token: 0x06003267 RID: 12903 RVA: 0x00126617 File Offset: 0x00124A17
 public static void WriteDateTimeWithKind(DateTime value, ProtoWriter dest)
 {
     BclHelpers.WriteDateTimeImpl(value, dest, true);
 }
예제 #5
0
 // Token: 0x06003266 RID: 12902 RVA: 0x0012660D File Offset: 0x00124A0D
 public static void WriteDateTime(DateTime value, ProtoWriter dest)
 {
     BclHelpers.WriteDateTimeImpl(value, dest, false);
 }
예제 #6
0
 public static void WriteNetObject(object value, ProtoWriter dest, int key, BclHelpers.NetObjectOptions options)
 {
     if (dest == null)
     {
         throw new ArgumentNullException("dest");
     }
     bool flag = (byte)(options & BclHelpers.NetObjectOptions.DynamicType) != 0;
     bool flag2 = (byte)(options & BclHelpers.NetObjectOptions.AsReference) != 0;
     WireType wireType = dest.WireType;
     SubItemToken token = ProtoWriter.StartSubItem(null, dest);
     bool flag3 = true;
     if (flag2)
     {
         bool flag4;
         int value2 = dest.NetCache.AddObjectKey(value, out flag4);
         ProtoWriter.WriteFieldHeader((!flag4) ? 2 : 1, WireType.Variant, dest);
         ProtoWriter.WriteInt32(value2, dest);
         if (flag4)
         {
             flag3 = false;
         }
     }
     if (flag3)
     {
         if (flag)
         {
             Type type = value.GetType();
             if (!(value is string))
             {
                 key = dest.GetTypeKey(ref type);
                 if (key < 0)
                 {
                     throw new InvalidOperationException("Dynamic type is not a contract-type: " + type.Name);
                 }
             }
             bool flag5;
             int value3 = dest.NetCache.AddObjectKey(type, out flag5);
             ProtoWriter.WriteFieldHeader((!flag5) ? 4 : 3, WireType.Variant, dest);
             ProtoWriter.WriteInt32(value3, dest);
             if (!flag5)
             {
                 ProtoWriter.WriteFieldHeader(8, WireType.String, dest);
                 ProtoWriter.WriteString(dest.SerializeType(type), dest);
             }
         }
         ProtoWriter.WriteFieldHeader(10, wireType, dest);
         if (value is string)
         {
             ProtoWriter.WriteString((string)value, dest);
         }
         else
         {
             ProtoWriter.WriteObject(value, key, dest);
         }
     }
     ProtoWriter.EndSubItem(token, dest);
 }
예제 #7
0
 public static object ReadNetObject(object value, ProtoReader source, int key, Type type, BclHelpers.NetObjectOptions options)
 {
     SubItemToken token = ProtoReader.StartSubItem(source);
     int num = -1;
     int num2 = -1;
     int num3;
     while ((num3 = source.ReadFieldHeader()) > 0)
     {
         switch (num3)
         {
         case 1:
         {
             int key2 = source.ReadInt32();
             value = source.NetCache.GetKeyedObject(key2);
             continue;
         }
         case 2:
             num = source.ReadInt32();
             continue;
         case 3:
         {
             int key2 = source.ReadInt32();
             type = (Type)source.NetCache.GetKeyedObject(key2);
             key = source.GetTypeKey(ref type);
             continue;
         }
         case 4:
             num2 = source.ReadInt32();
             continue;
         case 8:
         {
             string text = source.ReadString();
             type = source.DeserializeType(text);
             if (type == null)
             {
                 throw new ProtoException("Unable to resolve type: " + text + " (you can use the TypeModel.DynamicTypeFormatting event to provide a custom mapping)");
             }
             if (type == typeof(string))
             {
                 key = -1;
             }
             else
             {
                 key = source.GetTypeKey(ref type);
                 if (key < 0)
                 {
                     throw new InvalidOperationException("Dynamic type is not a contract-type: " + type.Name);
                 }
             }
             continue;
         }
         case 10:
         {
             bool flag = type == typeof(string);
             bool flag2 = value == null;
             bool flag3 = flag2 && (flag || (byte)(options & BclHelpers.NetObjectOptions.LateSet) != 0);
             if (num >= 0 && !flag3)
             {
                 if (value == null)
                 {
                     source.TrapNextObject(num);
                 }
                 else
                 {
                     source.NetCache.SetKeyedObject(num, value);
                 }
                 if (num2 >= 0)
                 {
                     source.NetCache.SetKeyedObject(num2, type);
                 }
             }
             object obj = value;
             if (flag)
             {
                 value = source.ReadString();
             }
             else
             {
                 value = ProtoReader.ReadTypedObject(obj, key, source, type);
             }
             if (num >= 0)
             {
                 if (flag2 && !flag3)
                 {
                     obj = source.NetCache.GetKeyedObject(num);
                 }
                 if (flag3)
                 {
                     source.NetCache.SetKeyedObject(num, value);
                     if (num2 >= 0)
                     {
                         source.NetCache.SetKeyedObject(num2, type);
                     }
                 }
             }
             if (num >= 0 && !flag3 && !object.ReferenceEquals(obj, value))
             {
                 throw new ProtoException("A reference-tracked object changed reference during deserialization");
             }
             if (num < 0 && num2 >= 0)
             {
                 source.NetCache.SetKeyedObject(num2, type);
             }
             continue;
         }
         }
         source.SkipField();
     }
     if (num >= 0 && (byte)(options & BclHelpers.NetObjectOptions.AsReference) == 0)
     {
         throw new ProtoException("Object key in input stream, but reference-tracking was not expected");
     }
     ProtoReader.EndSubItem(token, source);
     return value;
 }
예제 #8
0
        /// <summary>
        /// Reads an *implementation specific* bundled .NET object, including (as options) type-metadata, identity/re-use, etc.
        /// </summary>
        public static object ReadNetObject(object value, ProtoReader source, int key, Type type, NetObjectOptions options)
        {
            SubItemToken token = ProtoReader.StartSubItem(source);
            int          fieldNumber;
            int          newObjectKey = -1, newTypeKey = -1, tmp;

            while ((fieldNumber = source.ReadFieldHeader()) > 0)
            {
                switch (fieldNumber)
                {
                case FieldExistingObjectKey:
                    tmp   = source.ReadInt32();
                    value = source.NetCache.GetKeyedObject(tmp);
                    break;

                case FieldNewObjectKey:
                    newObjectKey = source.ReadInt32();
                    break;

                case FieldExistingTypeKey:
                    tmp  = source.ReadInt32();
                    type = (Type)source.NetCache.GetKeyedObject(tmp);
                    key  = source.GetTypeKey(ref type);
                    break;

                case FieldNewTypeKey:
                    newTypeKey = source.ReadInt32();
                    break;

                case FieldTypeName:
                    type = source.DeserializeType(source.ReadString());
                    key  = source.GetTypeKey(ref type);
                    break;

                case FieldObject:
                    bool isString = type == typeof(string);
                    bool lateSet  = value == null && isString;
                    if (value == null && !lateSet)
                    {
                        value = ((options & NetObjectOptions.UseConstructor) == 0) ? BclHelpers.GetUninitializedObject(type) : Activator.CreateInstance(type);
                    }
                    if (newObjectKey >= 0 && !lateSet)
                    {
                        source.NetCache.SetKeyedObject(newObjectKey, value);
                        if (newTypeKey >= 0)
                        {
                            source.NetCache.SetKeyedObject(newTypeKey, type);
                        }
                    }
                    object oldValue = value;
                    if (isString)
                    {
                        value = source.ReadString();
                    }
                    else
                    {
                        value = ProtoReader.ReadTypedObject(oldValue, key, source, type);
                    }

                    if (newObjectKey >= 0 && lateSet)
                    {
                        source.NetCache.SetKeyedObject(newObjectKey, value);
                        if (newTypeKey >= 0)
                        {
                            source.NetCache.SetKeyedObject(newTypeKey, type);
                        }
                    }
                    if (!lateSet && !ReferenceEquals(oldValue, value))
                    {
                        throw new ProtoException("A reference-tracked object changed reference during deserialization");
                    }
                    break;

                default:
                    source.SkipField();
                    break;
                }
            }
            ProtoReader.EndSubItem(token, source);

            return(value);
        }