Example #1
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();
        }
Example #2
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();
 }
Example #3
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);
         }
     }
 }