Beispiel #1
0
        // Skips

        internal void Skip(MProto proto, MType type)
        {
            switch (type)
            {
            case MType.Binary: proto.ReadBinary(); break;

            case MType.Bool: proto.ReadBool(); break;

            case MType.Byte: proto.ReadByte(); break;

            case MType.Double: proto.ReadDouble(); break;

            case MType.Float: proto.ReadFloat(); break;

            case MType.I16: proto.ReadI16(); break;

            case MType.I32: proto.ReadI32(); break;

            case MType.I64: proto.ReadI64(); break;

            case MType.List: SkipList(proto, proto.ReadListBegin()); break;

            case MType.Map: SkipMap(proto, proto.ReadMapBegin()); break;

            case MType.Null: break;

            case MType.Set: SkipList(proto, proto.ReadListBegin()); break;

            case MType.String: proto.ReadString(); break;

            case MType.Struct: SkipStruct(proto, proto.ReadStructBegin()); break;
            }
        }
Beispiel #2
0
        internal List <object> ReadList(MProto proto, MioList meta)
        {
            if (meta == null)
            {
                return(null);
            }
            var list = proto.ReadListBegin();

            if (list.ElementType != meta.Element.Type)
            {
                SkipList(proto, list);
                return(null);
            }
            var res = new List <object>();

            for (int i = 0; i < list.Count; i++)
            {
                var obj = Read(proto, meta.Element);
                res.Add(obj);
            }
            return(res);
        }
Beispiel #3
0
 // Skips
 internal void Skip(MProto proto, MType type)
 {
     switch (type)
     {
         case MType.Binary: proto.ReadBinary(); break;
         case MType.Bool: proto.ReadBool(); break;
         case MType.Byte: proto.ReadByte(); break;
         case MType.Double: proto.ReadDouble(); break;
         case MType.Float: proto.ReadFloat(); break;
         case MType.I16: proto.ReadI16(); break;
         case MType.I32: proto.ReadI32(); break;
         case MType.I64: proto.ReadI64(); break;
         case MType.List: SkipList(proto, proto.ReadListBegin()); break;
         case MType.Map: SkipMap(proto, proto.ReadMapBegin()); break;
         case MType.Null: break;
         case MType.Set: SkipList(proto, proto.ReadListBegin()); break;
         case MType.String: proto.ReadString(); break;
         case MType.Struct: SkipStruct(proto, proto.ReadStructBegin()); break;
     }
 }
Beispiel #4
0
 internal List<object> ReadList(MProto proto, MioList meta)
 {
     if (meta == null)
         return null;
     var list = proto.ReadListBegin();
     if (list.ElementType != meta.Element.Type)
     {
         SkipList(proto, list);
         return null;
     }
     var res = new List<object>();
     for (int i = 0; i < list.Count; i++)
     {
         var obj = Read(proto, meta.Element);
         res.Add(obj);
     }
     return res;
 }
Beispiel #5
0
 private void Skip(MProto source, MType mType)
 {
     if (MType.Null == mType)
     {
         throw new MException("Unsupported Type: " + mType);
     }
     else if (MType.Bool == mType)
     {
         source.ReadBool();
     }
     else if (MType.Byte == mType)
     {
         source.ReadByte();
     }
     else if (MType.I16 == mType)
     {
         source.ReadI16();
     }
     else if (MType.I32 == mType)
     {
         source.ReadI32();
     }
     else if (MType.I64 == mType)
     {
         source.ReadI64();
     }
     else if (MType.Float == mType)
     {
         source.ReadFloat();
     }
     else if (MType.Double == mType)
     {
         source.ReadDouble();
     }
     else if (MType.Binary == mType)
     {
         source.ReadBinary();
     }
     else if (MType.String == mType)
     {
         source.ReadString();
     }
     else if (MType.Struct == mType)
     {
         MStruct struc = source.ReadStructBegin();
         while (true)
         {
             MField field = source.ReadFieldBegin();
             if (field.Type == MType.Null)
             {
                 break;
             }
             Skip(source, field.Type);
         }
     }
     else if (MType.Map == mType)
     {
         var map = source.ReadMapBegin();
         for (int i = 0; i < map.Count; i++)
         {
             Skip(source, map.KeyType);
             Skip(source, map.ValueType);
         }
     }
     else if (MType.List == mType)
     {
         var list = source.ReadListBegin();
         for (int i = 0; i < list.Count; i++)
         {
             Skip(source, list.ElementType);
         }
     }
     else if (MType.Set == mType)
     {
         var list = source.ReadListBegin();
         for (int i = 0; i < list.Count; i++)
         {
             Skip(source, list.ElementType);
         }
     }
     else
     {
         throw new MException("Should Not Reach Here, type = " + mType);
     }
 }
Beispiel #6
0
 internal object Read(MProto source, Type type, MMeta meta)
 {
     if (meta == null)
     {
         meta = MMeta.GetMMeta(type);
     }
     if (meta == null || MType.Null == meta.MType)
     {
         throw new MException("Unsupported Type: " + type);
     }
     else if (MType.Bool == meta.MType)
     {
         return(source.ReadBool());
     }
     else if (MType.Byte == meta.MType)
     {
         return(source.ReadByte());
     }
     else if (MType.I16 == meta.MType)
     {
         return(source.ReadI16());
     }
     else if (MType.I32 == meta.MType)
     {
         return(source.ReadI32());
     }
     else if (MType.I64 == meta.MType)
     {
         return(source.ReadI64());
     }
     else if (MType.Float == meta.MType)
     {
         return(source.ReadFloat());
     }
     else if (MType.Double == meta.MType)
     {
         return(source.ReadDouble());
     }
     else if (MType.Binary == meta.MType)
     {
         byte[] bytes = source.ReadBinary();
         if (meta.MainType.IsArray)
         {
             return(bytes);
         }
         else
         {
             return(new ArraySegment <byte>(bytes));
         }
     }
     else if (MType.String == meta.MType)
     {
         return(source.ReadString());
     }
     else if (MType.Struct == meta.MType)
     {
         MStruct struc  = source.ReadStructBegin();
         var     data   = Activator.CreateInstance(type, true);
         var     fields = GetStructFields(type);
         while (true)
         {
             MField field = source.ReadFieldBegin();
             if (field.Type == MType.Null)
             {
                 break;
             }
             if (fields.ContainsKey(field.ID))
             {
                 var prop     = fields[field.ID];
                 var propmeta = MMeta.GetMMeta(prop.PropertyType);
                 if (propmeta.MType != field.Type)
                 {
                     throw new MException("FieldErr, field = " + type.Name + ":"
                                          + prop.Name + ", got type = " + field.Type);
                 }
                 else
                 {
                     var val = Read(source, prop.PropertyType, propmeta);
                     prop.SetValue(data, val, null);
                 }
             }
             else
             {
                 Skip(source, field.Type);
             }
         }
         return(data);
     }
     else if (MType.Map == meta.MType)
     {
         var    map  = source.ReadMapBegin();
         object data = null;
         if (type.IsInterface)
         {
             var instType = typeof(Dictionary <,>);
             instType = instType.MakeGenericType(meta.KeyType, meta.ValueType);
             data     = Activator.CreateInstance(instType);
         }
         else
         {
             data = Activator.CreateInstance(type);
         }
         var dict = data as IDictionary;
         for (int i = 0; i < map.Count; i++)
         {
             var key = Read(source, meta.KeyType);
             var val = Read(source, meta.ValueType);
             dict.Add(key, val);
         }
         return(data);
     }
     else if (MType.List == meta.MType)
     {
         var    list = source.ReadListBegin();
         object data = null;
         if (type.IsArray)
         {
             data = Array.CreateInstance(meta.ValueType, list.Count);
             var arr = data as Array;
             for (int i = 0; i < list.Count; i++)
             {
                 var value = Read(source, meta.ValueType);
                 arr.SetValue(value, i);
             }
         }
         else
         {
             if (type.IsInterface)
             {
                 var instType = typeof(List <>);
                 instType = instType.MakeGenericType(meta.ValueType);
                 data     = Activator.CreateInstance(instType);
             }
             else
             {
                 data = Activator.CreateInstance(type);
             }
             var arr = data as IList;
             for (int i = 0; i < list.Count; i++)
             {
                 var value = Read(source, meta.ValueType);
                 arr.Add(value);
             }
         }
         return(data);
     }
     else if (MType.Set == meta.MType)
     {
         var    list     = source.ReadListBegin();
         object data     = null;
         var    instType = type;
         if (type.IsInterface)
         {
             instType = typeof(HashSet <>).MakeGenericType(meta.ValueType);
         }
         else if (type.IsGenericTypeDefinition)
         {
             instType = type.MakeGenericType(meta.ValueType);
         }
         data = Activator.CreateInstance(instType);
         var addMethod = type.GetMethod("Add");
         for (int i = 0; i < list.Count; i++)
         {
             var value = Read(source, meta.ValueType);
             addMethod.Invoke(data, new object[] { value });
         }
         return(data);
     }
     throw new MException("Should Not Reach Here");
 }