Ejemplo n.º 1
0
 internal void SkipList(MProto proto, MList list)
 {
     for (int i = 0; i < list.Count; i++)
     {
         Skip(proto, list.ElementType);
     }
 }
Ejemplo n.º 2
0
        internal MioObject ReadStruct(MProto proto, MioStruct meta)
        {
            if (meta == null)
            {
                return(null);
            }
            var struc = proto.ReadStructBegin();
            var res   = new MioObject();

            while (true)
            {
                var field = proto.ReadFieldBegin();
                if (field.Type == MType.Null)
                {
                    break;
                }
                if (!meta.Fields.ContainsKey(field.ID))
                {
                    Skip(proto, field.Type);
                    continue;
                }
                var miofield = meta.Fields[field.ID];
                if (miofield.Meta.Type != field.Type)
                {
                    Skip(proto, field.Type);
                    continue;
                }
                var obj = Read(proto, miofield.Meta);
                res.SetObject(miofield.ID, obj);
            }
            return(res);
        }
Ejemplo n.º 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;
            }
        }
Ejemplo n.º 4
0
 internal void SkipMap(MProto proto, MMap map)
 {
     for (int i = 0; i < map.Count; i++)
     {
         Skip(proto, map.KeyType);
         Skip(proto, map.ValueType);
     }
 }
Ejemplo n.º 5
0
 internal void SkipStruct(MProto proto, MStruct struc)
 {
     while (true)
     {
         var field = proto.ReadFieldBegin();
         if (field.Type == MType.Null)
         {
             break;
         }
         Skip(proto, field.Type);
     }
 }
Ejemplo n.º 6
0
        internal void WriteList(MProto proto, MioList meta, List <object> data)
        {
            if (data == null)
            {
                proto.WriteListBegin(new MList(meta.Element.Type, 0));
                return;
            }
            var list = new MList(meta.Element.Type, data.Count);

            proto.WriteListBegin(list);
            foreach (var elem in data)
            {
                Write(proto, meta.Element, elem);
            }
        }
Ejemplo n.º 7
0
        internal void WriteMap(MProto proto, MioMap meta, Dictionary <object, object> data)
        {
            if (data == null)
            {
                proto.WriteMapBegin(new MMap(meta.Key.Type, meta.Value.Type, 0));
                return;
            }
            var map = new MMap(meta.Key.Type, meta.Value.Type, data.Count);

            proto.WriteMapBegin(map);
            foreach (var entry in data)
            {
                Write(proto, meta.Key, entry.Key);
                Write(proto, meta.Value, entry.Value);
            }
        }
Ejemplo n.º 8
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);
        }
Ejemplo n.º 9
0
        // Reads

        internal object Read(MProto proto, Mio meta)
        {
            if (meta == null)
            {
                return(null);
            }
            switch (meta.Type)
            {
            case MType.Binary: return(proto.ReadBinary());

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

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

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

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

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

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

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

            case MType.List: return(ReadList(proto, meta as MioList));

            case MType.Map: return(ReadMap(proto, meta as MioMap));

            case MType.Null: return(null);

            case MType.Set: return(ReadList(proto, meta as MioList));

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

            case MType.Struct: return(ReadStruct(proto, meta as MioStruct));
            }
            return(null);
        }
Ejemplo n.º 10
0
        internal Dictionary <object, object> ReadMap(MProto proto, MioMap meta)
        {
            if (meta == null)
            {
                return(null);
            }
            var map = proto.ReadMapBegin();

            if (map.KeyType != meta.Key.Type || map.ValueType != meta.Value.Type)
            {
                SkipMap(proto, map);
                return(null);
            }
            var res = new Dictionary <object, object>();

            for (int i = 0; i < map.Count; i++)
            {
                var key = Read(proto, meta.Key);
                var val = Read(proto, meta.Value);
                res.Add(key, val);
            }
            return(res);
        }
Ejemplo n.º 11
0
        internal void WriteStruct(MProto proto, MioStruct meta, MioObject data)
        {
            if (data == null)
            {
                proto.WriteFieldStop();
                return;
            }
            var struc = new MStruct();

            proto.WriteStructBegin(struc);
            foreach (var field in meta.Fields.Values)
            {
                var obj = data.GetObject(field.ID);
                if (obj == null)
                {
                    continue;
                }
                var f = new MField(string.Empty, field.Meta.Type, field.ID);
                proto.WriteFieldBegin(f);
                Write(proto, field.Meta, obj);
            }
            proto.WriteFieldStop();
        }
Ejemplo n.º 12
0
        // Writes

        internal void Write(MProto proto, Mio meta, object data)
        {
            if (meta == null)
            {
                return;
            }
            switch (meta.Type)
            {
            case MType.Binary: proto.WriteBinary((byte[])data); break;

            case MType.Bool: proto.WriteBool(Convert.ToBoolean(data)); break;

            case MType.Byte: proto.WriteByte((sbyte)Convert.ToByte(data)); break;

            case MType.Double: proto.WriteDouble(Convert.ToDouble(data)); break;

            case MType.Float: proto.WriteFloat(Convert.ToSingle(data)); break;

            case MType.I16: proto.WriteI16(Convert.ToInt16(data)); break;

            case MType.I32: proto.WriteI32(Convert.ToInt32(data)); break;

            case MType.I64: proto.WriteI64(Convert.ToInt64(data)); break;

            case MType.List: WriteList(proto, meta as MioList, data as List <object>); break;

            case MType.Map: WriteMap(proto, meta as MioMap, data as Dictionary <object, object>); break;

            case MType.Null: break;

            case MType.Set: WriteList(proto, meta as MioList, data as List <object>); break;

            case MType.String: proto.WriteString(Convert.ToString(data)); break;

            case MType.Struct: WriteStruct(proto, meta as MioStruct, data as MioObject); break;
            }
        }
Ejemplo n.º 13
0
 internal MioObject ReadStruct(MProto proto, MioStruct meta)
 {
     if (meta == null)
         return null;
     var struc = proto.ReadStructBegin();
     var res = new MioObject();
     while (true)
     {
         var field = proto.ReadFieldBegin();
         if (field.Type == MType.Null)
             break;
         if (!meta.Fields.ContainsKey(field.ID))
         {
             Skip(proto, field.Type);
             continue;
         }
         var miofield = meta.Fields[field.ID];
         if (miofield.Meta.Type != field.Type)
         {
             Skip(proto, field.Type);
             continue;
         }
         var obj = Read(proto, miofield.Meta);
         res.SetObject(miofield.ID, obj);
     }
     return res;
 }
Ejemplo n.º 14
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;
 }
Ejemplo n.º 15
0
 internal Dictionary<object, object> ReadMap(MProto proto, MioMap meta)
 {
     if (meta == null)
         return null;
     var map = proto.ReadMapBegin();
     if (map.KeyType != meta.Key.Type || map.ValueType != meta.Value.Type)
     {
         SkipMap(proto, map);
         return null;
     }
     var res = new Dictionary<object, object>();
     for (int i = 0; i < map.Count; i++)
     {
         var key = Read(proto, meta.Key);
         var val = Read(proto, meta.Value);
         res.Add(key, val);
     }
     return res;
 }
Ejemplo n.º 16
0
 internal void Write(MProto target, object data, Type type, MMeta meta)
 {
     if (type == null)
     {
         type = data.GetType();
     }
     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)
     {
         target.WriteBool((bool)data);
     }
     else if (MType.Byte == meta.MType)
     {
         target.WriteByte((sbyte)data);
     }
     else if (MType.I16 == meta.MType)
     {
         target.WriteI16((short)data);
     }
     else if (MType.I32 == meta.MType)
     {
         target.WriteI32((int)data);
     }
     else if (MType.I64 == meta.MType)
     {
         target.WriteI64((long)data);
     }
     else if (MType.Float == meta.MType)
     {
         target.WriteFloat((float)data);
     }
     else if (MType.Double == meta.MType)
     {
         target.WriteDouble((double)data);
     }
     else if (MType.Binary == meta.MType)
     {
         if (meta.MainType.IsArray)
         {
             target.WriteBinary((byte[])data);
         }
         else
         {
             var seg = (ArraySegment <byte>)data;
             target.WriteBinary(seg.Array, seg.Offset, seg.Count);
         }
     }
     else if (MType.String == meta.MType)
     {
         target.WriteString((string)data);
     }
     else if (MType.Struct == meta.MType)
     {
         MStruct struc = new MStruct();
         struc.Name = type.Name;
         var fields = GetStructFields(type);
         target.WriteStructBegin(struc);
         //
         foreach (var entry in fields)
         {
             var proptype  = entry.Value.PropertyType;
             var fieldmeta = MMeta.GetMMeta(proptype);
             var value     = entry.Value.GetValue(data, null);
             if (value == null)
             {
                 continue;
             }
             var field = new MField(entry.Value.Name, fieldmeta.MType, entry.Key);
             target.WriteFieldBegin(field);
             Write(target, value, proptype, fieldmeta);
         }
         target.WriteFieldStop();
     }
     else if (MType.Map == meta.MType)
     {
         var keymeta = MMeta.GetMMeta(meta.KeyType);
         var valmeta = MMeta.GetMMeta(meta.ValueType);
         var dict    = data as IDictionary;
         var map     = new MMap(keymeta.MType, valmeta.MType, dict.Count);
         target.WriteMapBegin(map);
         foreach (var key in dict.Keys)
         {
             Write(target, key, meta.KeyType, keymeta);
             var value = dict[key];
             Write(target, value, meta.ValueType, valmeta);
         }
     }
     else if (MType.List == meta.MType)
     {
         var valmeta = MMeta.GetMMeta(meta.ValueType);
         var arr     = data as IEnumerable;
         int count   = 0;
         if (meta.MainType.IsArray)
         {
             count = (data as Array).Length;
         }
         else
         {
             count = (data as IList).Count;
         }
         //
         var list = new MList(valmeta.MType, count);
         target.WriteListBegin(list);
         foreach (var entry in arr)
         {
             Write(target, entry, meta.ValueType, valmeta);
         }
     }
     else if (MType.Set == meta.MType)
     {
         var valmeta = MMeta.GetMMeta(meta.ValueType);
         int count   = (int)meta.ContainerType.GetProperty("Count").GetValue(data, null);
         var arr     = data as IEnumerable;
         var list    = new MList(valmeta.MType, count);
         target.WriteListBegin(list);
         foreach (var entry in arr)
         {
             Write(target, entry, meta.ValueType, valmeta);
         }
     }
 }
Ejemplo n.º 17
0
 // Reads
 internal object Read(MProto proto, Mio meta)
 {
     if (meta == null)
         return null;
     switch (meta.Type)
     {
         case MType.Binary: return proto.ReadBinary();
         case MType.Bool: return proto.ReadBool();
         case MType.Byte: return proto.ReadByte();
         case MType.Double: return proto.ReadDouble();
         case MType.Float: return proto.ReadFloat();
         case MType.I16: return proto.ReadI16();
         case MType.I32: return proto.ReadI32();
         case MType.I64: return proto.ReadI64();
         case MType.List: return ReadList(proto, meta as MioList);
         case MType.Map: return ReadMap(proto, meta as MioMap);
         case MType.Null: return null;
         case MType.Set: return ReadList(proto, meta as MioList);
         case MType.String: return proto.ReadString();
         case MType.Struct: return ReadStruct(proto, meta as MioStruct);
     }
     return null;
 }
Ejemplo n.º 18
0
 internal void Write(MProto target, object data, Type type)
 {
     Write(target, data, type, null);
 }
Ejemplo n.º 19
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;
     }
 }
Ejemplo n.º 20
0
 internal void WriteMap(MProto proto, MioMap meta, Dictionary<object, object> data)
 {
     if (data == null)
     {
         proto.WriteMapBegin(new MMap(meta.Key.Type, meta.Value.Type, 0));
         return;
     }
     var map = new MMap(meta.Key.Type, meta.Value.Type, data.Count);
     proto.WriteMapBegin(map);
     foreach (var entry in data)
     {
         Write(proto, meta.Key, entry.Key);
         Write(proto, meta.Value, entry.Value);
     }
 }
Ejemplo n.º 21
0
 // Writes
 internal void Write(MProto proto, Mio meta, object data)
 {
     if (meta == null)
         return;
     switch (meta.Type)
     {
         case MType.Binary: proto.WriteBinary((byte[])data); break;
         case MType.Bool: proto.WriteBool(Convert.ToBoolean(data)); break;
         case MType.Byte: proto.WriteByte((sbyte)Convert.ToByte(data)); break;
         case MType.Double: proto.WriteDouble(Convert.ToDouble(data)); break;
         case MType.Float: proto.WriteFloat(Convert.ToSingle(data)); break;
         case MType.I16: proto.WriteI16(Convert.ToInt16(data)); break;
         case MType.I32: proto.WriteI32(Convert.ToInt32(data)); break;
         case MType.I64: proto.WriteI64(Convert.ToInt64(data)); break;
         case MType.List: WriteList(proto, meta as MioList, data as List<object>); break;
         case MType.Map: WriteMap(proto, meta as MioMap, data as Dictionary<object,object>); break;
         case MType.Null: break;
         case MType.Set: WriteList(proto, meta as MioList, data as List<object>); break;
         case MType.String: proto.WriteString(Convert.ToString(data)); break;
         case MType.Struct: WriteStruct(proto, meta as MioStruct, data as MioObject); break;
     }
 }
Ejemplo n.º 22
0
 internal void SkipMap(MProto proto, MMap map)
 {
     for (int i = 0; i < map.Count; i++)
     {
         Skip(proto, map.KeyType);
         Skip(proto, map.ValueType);
     }
 }
Ejemplo n.º 23
0
 internal void SkipList(MProto proto, MList list)
 {
     for (int i = 0; i < list.Count; i++)
     {
         Skip(proto, list.ElementType);
     }
 }
Ejemplo n.º 24
0
 internal object Read(MProto source, Type type)
 {
     return(Read(source, type, null));
 }
Ejemplo n.º 25
0
 internal void SkipStruct(MProto proto, MStruct struc)
 {
     while (true)
     {
         var field = proto.ReadFieldBegin();
         if (field.Type == MType.Null)
             break;
         Skip(proto, field.Type);
     }
 }
Ejemplo n.º 26
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");
 }
Ejemplo n.º 27
0
 internal void WriteList(MProto proto, MioList meta, List<object> data)
 {
     if (data == null)
     {
         proto.WriteListBegin(new MList(meta.Element.Type, 0));
         return;
     }
     var list = new MList(meta.Element.Type, data.Count);
     proto.WriteListBegin(list);
     foreach (var elem in data)
     {
         Write(proto, meta.Element, elem);
     }
 }
Ejemplo n.º 28
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);
     }
 }
Ejemplo n.º 29
0
 internal void WriteStruct(MProto proto, MioStruct meta, MioObject data)
 {
     if (data == null)
     {
         proto.WriteFieldStop();
         return;
     }
     var struc = new MStruct();
     proto.WriteStructBegin(struc);
     foreach (var field in meta.Fields.Values)
     {
         var obj = data.GetObject(field.ID);
         if (obj == null)
             continue;
         var f = new MField(string.Empty, field.Meta.Type, field.ID);
         proto.WriteFieldBegin(f);
         Write(proto, field.Meta, obj);
     }
     proto.WriteFieldStop();
 }
Ejemplo n.º 30
0
 internal void Write(MProto target, object data)
 {
     Write(target, data, null, null);
 }