Exemple #1
0
        public override object ReadObject(GaeaStream inStream, Type defType)
        {
            var typeId = inStream.ReadUInt32();

            if (typeId == 0)
            {
                return(null);
            }
            byte isRef    = (byte)inStream.ReadByte();
            int  hashcode = inStream.ReadInt32();

            if (isRef > 0)
            {
                return(inStream.GetRef(hashcode));
            }
            var    type      = defType;
            var    keyTypeId = inStream.ReadUInt32();
            var    keyType   = keyTypeId.ToType();
            object key       = SerializerFactory.GetSerializer(keyType).ReadObject(inStream, defType.GetGenericArguments()[0]);

            object value       = null;
            var    valueTypeId = inStream.ReadUInt32();
            var    valueType   = valueTypeId.ToType();

            if (valueTypeId != 0)
            {
                value = SerializerFactory.GetSerializer(valueType).ReadObject(inStream, defType.GetGenericArguments()[1]);
            }
            object obj = Activator.CreateInstance(type, key, value);

            inStream.SetRef(hashcode, obj);
            return(obj);
        }
Exemple #2
0
        public override void WriteObject(object obj, GaeaStream outStream)
        {
            if (obj == null)
            {
                SerializerFactory.GetSerializer(null).WriteObject(null, outStream);
            }
            Type type   = obj.GetType();
            uint typeId = type.GetTypeId();

            outStream.WriteUInt32(typeId);
            if (outStream.WriteRef(obj))
            {
                return;
            }
            object key     = type.GetProperty("Key").GetValue(obj, null);
            object value   = type.GetProperty("Value").GetValue(obj, null);
            var    keyType = key.GetType();

            outStream.Write(keyType.GetTypeId().GetBytes(), 0, 4);
            SerializerFactory.GetSerializer(keyType).WriteObject(key, outStream);
            if (value == null)
            {
                SerializerFactory.GetSerializer(null).WriteObject(value, outStream);
            }
            else
            {
                var valueType = value.GetType();
                outStream.Write(valueType.GetTypeId().GetBytes(), 0, 4);
                SerializerFactory.GetSerializer(valueType).WriteObject(value, outStream);
            }
        }
Exemple #3
0
        public override object ReadObject(GaeaStream inStream, Type defType)
        {
            byte[] head = new byte[5];
            inStream.SafeRead(head, 0, 5);
            int isRef    = head.ToByte(0);
            int hashcode = head.ToInt32(1);

            if (isRef > 0)
            {
                object o = inStream.GetRef(hashcode);
                if (o == null)
                {
                    return(string.Empty);
                }
                if (!(o is string))
                {
                    throw new Exception("Type error!");
                }
                return(o);
            }
            int len = inStream.ReadInt32();

            if (len == 0)
            {
                inStream.SetRef(hashcode, string.Empty);
                return(string.Empty);
            }
            byte[] buffer = new byte[len];
            inStream.SafeRead(buffer, 0, len);
            string str = inStream.Encoder.GetString(buffer);

            inStream.SetRef(hashcode, str);
            return(str);
        }
Exemple #4
0
        public override void WriteObject(object obj, GaeaStream outStream)
        {
            if (obj == null)
            {
                SerializerFactory.GetSerializer(null).WriteObject(null, outStream);
            }
            IDictionary dic    = (IDictionary)obj;
            Type        type   = typeof(IDictionary);
            uint        typeId = type.GetTypeId();

            outStream.WriteUInt32(typeId);
            if (outStream.WriteRef(obj))
            {
                return;
            }
            outStream.WriteInt32(dic.Count);
            foreach (DictionaryEntry item in dic)
            {
                var keyType = item.Key.GetType();
                outStream.Write(keyType.GetTypeId().GetBytes(), 0, 4);
                SerializerFactory.GetSerializer(keyType).WriteObject(item.Key, outStream);
                if (item.Value == null)
                {
                    SerializerFactory.GetSerializer(null).WriteObject(item.Value, outStream);
                }
                else
                {
                    var valueType = item.Value.GetType();
                    outStream.Write(valueType.GetTypeId().GetBytes(), 0, 4);
                    SerializerFactory.GetSerializer(valueType).WriteObject(item.Value, outStream);
                }
            }
        }
Exemple #5
0
        public override void WriteObject(object obj, GaeaStream outStream)
        {
            if (obj == null)
            {
                SerializerFactory.GetSerializer(null).WriteObject(null, outStream);
            }
            IList list   = (IList)obj;
            Type  type   = typeof(IList);
            uint  typeId = type.GetTypeId();

            outStream.WriteUInt32(typeId);
            if (outStream.WriteRef(obj))
            {
                return;
            }
            outStream.WriteInt32(list.Count);
            foreach (var item in list)
            {
                if (item == null)
                {
                    SerializerFactory.GetSerializer(null).WriteObject(item, outStream);
                }
                else
                {
                    var itemType = item.GetType();
                    outStream.Write(itemType.GetTypeId().GetBytes(), 0, 4);
                    SerializerFactory.GetSerializer(itemType).WriteObject(item, outStream);
                }
            }
        }
Exemple #6
0
        public override object ReadObject(GaeaStream inStream, Type defType)
        {
            uint typeId = inStream.ReadUInt32();

            if (typeId == 0)
            {
                return(null);
            }
            byte isRef    = (byte)inStream.ReadByte();
            int  hashCode = inStream.ReadInt32();

            if (isRef > 0)
            {
                return(inStream.GetRef(hashCode));
            }
            int  len  = inStream.ReadInt32();
            Type type = typeId.ToType();

            if (type == typeof(byte))
            {
                byte[] buffer = new byte[len];
                inStream.SafeRead(buffer, 0, len);
                return(buffer);
            }
            if (type.IsGenericType)
            {
                type = GenericHelper.GetRealGenType(type, defType.GetElementType());
            }
            var array = Array.CreateInstance(type, len);

            if (!type.IsPrimitive)
            {
                for (int i = 0; i < len; i++)
                {
                    var itemTypeId = inStream.ReadUInt32();
                    if (itemTypeId == 0)
                    {
                        array.SetValue(null, i);
                    }
                    else
                    {
                        Type itemType = itemTypeId.ToType();
                        var  value    = SerializerFactory.GetSerializer(itemType).ReadObject(inStream, defType.GetElementType());
                        array.SetValue(value, i);
                    }
                }
            }
            else
            {
                for (int i = 0; i < len; i++)
                {
                    var value = SerializerFactory.GetSerializer(type).ReadObject(inStream, defType.GetElementType());
                    array.SetValue(value, i);
                }
            }
            inStream.SetRef(hashCode, array);
            return(array);
        }
Exemple #7
0
        public override void WriteObject(object obj, GaeaStream outStream)
        {
            bool tem = (bool)obj;

            if (tem)
            {
                outStream.WriteByte((byte)1);
            }
            else
            {
                outStream.WriteByte((byte)0);
            }
        }
Exemple #8
0
        public override object ReadObject(GaeaStream inStream, Type defType)
        {
            byte tem = (byte)inStream.ReadByte();

            if (tem > 0)
            {
                return(true);
            }
            else
            {
                return(false);
            }
        }
Exemple #9
0
 public override object ReadObject(GaeaStream inStream, Type defType)
 {
     byte[] buffer = new byte[BYTE_LEN];
     inStream.SafeRead(buffer, 0, BYTE_LEN);
     if (defType == typeof(UInt16))
     {
         return(buffer.ToInt16(0));
     }
     else
     {
         return(buffer.ToInt16(0));
     }
 }
Exemple #10
0
        public override void WriteObject(object obj, GaeaStream outStream)
        {
            if (outStream.WriteRef(obj))
            {
                return;
            }
            var buffer = outStream.Encoder.GetBytes(obj.ToString());
            var bLen   = buffer.Length.GetBytes();
            var bytes  = new byte[buffer.Length + 4];

            Array.Copy(bLen, 0, bytes, 0, 4);
            Array.Copy(buffer, 0, bytes, 4, buffer.Length);
            outStream.Write(bytes, 0, bytes.Length);
        }
Exemple #11
0
        public override void WriteObject(object obj, GaeaStream outStream)
        {
            if (obj == null)
            {
                SerializerFactory.GetSerializer(null).WriteObject(null, outStream);
                return;
            }
            Array array  = (Array)obj;
            Type  t      = obj.GetType();
            Type  type   = t.GetElementType();
            uint  typeId = type.GetTypeId();

            outStream.WriteUInt32(typeId);
            if (outStream.WriteRef(obj))
            {
                return;
            }
            outStream.WriteInt32(array.Length);
            if (t == (typeof(byte)))
            {
                outStream.Write((byte[])obj, 0, array.Length);
                return;
            }
            if (!type.IsPrimitives())
            {
                foreach (var item in array)
                {
                    if (item == null)
                    {
                        SerializerFactory.GetSerializer(null).WriteObject(item, outStream); //if item is null write 0x00000000 to type
                    }
                    else
                    {
                        Type itemT      = item.GetType();
                        uint itemTypeId = itemT.GetTypeId();
                        outStream.WriteUInt32(itemTypeId);
                        SerializerFactory.GetSerializer(itemT).WriteObject(item, outStream);
                    }
                }
            }
            else
            {
                foreach (var item in array)
                {
                    SerializerFactory.GetSerializer(type).WriteObject(item, outStream);
                }
            }
        }
Exemple #12
0
        public override void WriteObject(object obj, GaeaStream outStream)
        {
            if (obj == null)
            {
                SerializerFactory.GetSerializer(null).WriteObject(null, outStream);
                return;
            }
            Type     type     = obj.GetType();
            TypeInfo typeInfo = GetTypeInfo(type);
            var      typId    = typeInfo.TypeId.GetBytes();

            outStream.Write(typId, 0, 4);
            if (outStream.WriteRef(obj))
            {
                return;
            }
            foreach (var p in typeInfo.Properies)
            {
                var value = p.GetValue(obj, null);
                if (value == null)
                {
                    SerializerFactory.GetSerializer(null).WriteObject(value, outStream);
                }
                else
                {
                    outStream.WriteUInt32(value.GetType().GetTypeId());
                    if (value is IGaeaSerializer)
                    {
                        ((IGaeaSerializer)value).Serialize(outStream);
                    }
                    else
                    {
                        try
                        {
                            SerializerFactory.GetSerializer(value.GetType()).WriteObject(value, outStream);
                        }
                        catch (Exception err)
                        {
                            var e = new SerializeException(err);
                            e.Type         = type;
                            e.PropertyName = p.Name;
                            throw e;
                        }
                    }
                }
            }
        }
Exemple #13
0
        public object Derialize(byte[] buffer, Type type)
        {
            using (GaeaStream stream = new GaeaStream(buffer))
            {
                stream.Encoder = _encoder;

                if (type.GetInterface("IGaeaSerializer") != null)
                {
                    var obj = ((IGaeaSerializer)Activator.CreateInstance(type));
                    obj.Derialize(stream);
                    return(obj);
                }
                else
                {
                    object obj = SerializerFactory.GetSerializer(type).ReadObject(stream, type);
                    return(obj);
                }
            }
        }
Exemple #14
0
 public byte[] Serialize(object obj)
 {
     using (GaeaStream stream = new GaeaStream())
     {
         stream.Encoder = _encoder;
         if (obj == null)
         {
             SerializerFactory.GetSerializer(null).WriteObject(obj, stream);
         }
         else
         {
             if (obj is IGaeaSerializer)
             {
                 ((IGaeaSerializer)obj).Serialize(stream);
             }
             else
             {
                 SerializerFactory.GetSerializer(obj.GetType()).WriteObject(obj, stream);
             }
         }
         return(stream.ToArray());
     }
 }
Exemple #15
0
 public override void WriteObject(object obj, GaeaStream outStream)
 {
     byte[] buffer = ((char)obj).GetBytes();
     outStream.Write(buffer, 0, BYTE_LEN);
 }
Exemple #16
0
        public override object ReadObject(GaeaStream inStream, Type defType)
        {
            uint typeId = inStream.ReadUInt32();

            if (typeId == 0)
            {
                return(null);
            }
            Type type = typeId.ToType();

            if (type == null)
            {
                throw new NotFindTypeException("Not find the type from current application.type:" + defType.ToString() + ".typeId:" + typeId);
            }
            if (type.IsGenericType)
            {
                type = type.MakeGenericType(defType.GetGenericArguments());
            }
            int fbyte    = inStream.ReadByte();
            int hashcode = inStream.ReadInt32();

            if (fbyte > 0)
            {
                return(inStream.GetRef(hashcode));
            }
            TypeInfo typeInfo = GetTypeInfo(type);
            object   obj      = Activator.CreateInstance(type, true);

            foreach (var p in typeInfo.Properies)
            {
                var ptypeId = inStream.ReadUInt32();
                if (ptypeId == 0)
                {
                    p.SetValue(obj, null, null);
                    continue;
                }
                Type t = ptypeId.ToType();
                if (t == null)
                {
                    throw new NotFindTypeException("Not find the type from current application.type:" + p.PropertyType.Name + ",propery name:" + p.Name);
                }
                if (t.GetInterface("IGaeaSerializer") != null)
                {
                    var value = ((IGaeaSerializer)Activator.CreateInstance(t));
                    value.Derialize(inStream);
                    p.SetValue(obj, value, null);
                }
                else
                {
                    try
                    {
                        var    value  = SerializerFactory.GetSerializer(t).ReadObject(inStream, p.PropertyType);
                        object pValue = value;
                        if (p.PropertyType == typeof(byte))
                        {
                            pValue = Convert.ToByte(value);
                        }
                        else if (p.PropertyType == typeof(int))
                        {
                            pValue = Convert.ToInt32(value);
                        }
                        else if (p.PropertyType == typeof(long))
                        {
                            pValue = Convert.ToInt64(value);
                        }
                        p.SetValue(obj, pValue, null);
                    }
                    catch (Exception err)
                    {
                        var e = new SerializeException(err);
                        e.Type         = type;
                        e.PropertyName = p.Name;
                        throw e;
                    }
                }
            }
            inStream.SetRef(hashcode, obj);
            return(obj);
        }
Exemple #17
0
 public override object ReadObject(GaeaStream inStream, Type defType)
 {
     return((byte)inStream.ReadByte());
 }
Exemple #18
0
        public override void WriteObject(object obj, GaeaStream outStream)
        {
            var buffer = Convert.ToInt16(obj).GetBytes();

            outStream.Write(buffer, 0, BYTE_LEN);
        }
Exemple #19
0
 public override object ReadObject(GaeaStream inStream, Type defType)
 {
     byte[] buffer = new byte[BYTE_LEN];
     inStream.SafeRead(buffer, 0, BYTE_LEN);
     return(buffer.ToDouble(0));
 }
Exemple #20
0
 public override void WriteObject(object obj, GaeaStream outStream)
 {
     outStream.WriteByte((byte)obj);
 }
Exemple #21
0
 public abstract object ReadObject(GaeaStream inStream, Type defType);
Exemple #22
0
        public override void WriteObject(object obj, GaeaStream outStream)
        {
            var buffer = ((double)obj).GetBytes();

            outStream.Write(buffer, 0, BYTE_LEN);
        }
 public override void WriteObject(object obj, GaeaStream outStream)
 {
     byte[] buffer = ConvertToBinary((DateTime)obj);
     outStream.Write(buffer, 0, BYTE_LEN);
 }
Exemple #24
0
 public abstract void WriteObject(object obj, GaeaStream outStream);
 public override object ReadObject(GaeaStream inStream, Type defType)
 {
     byte[] buffer = new byte[BYTE_LEN];
     inStream.SafeRead(buffer, 0, BYTE_LEN);
     return(GetDateTime(buffer));
 }
Exemple #26
0
        public override object ReadObject(GaeaStream inStream, Type defType)
        {
            var typeId = inStream.ReadUInt32();

            if (typeId == 0)
            {
                return(null);
            }
            byte isRef    = (byte)inStream.ReadByte();
            int  hashcode = inStream.ReadInt32();

            if (isRef > 0)
            {
                return(inStream.GetRef(hashcode));
            }
            var type = typeId.ToType();

            if (type == null)
            {
                throw new NotFindTypeException("Type id can't convert to type !typeId:" + typeId);
            }
            if (type != typeof(IDictionary))
            {
                throw new TypeNoMatchException("Type must be IDictionary!type:" + type.FullName);
            }
            if (!defType.IsAbstract && !defType.IsInterface && typeof(IDictionary).IsAssignableFrom(defType))
            {
                type = defType;
            }
            else
            {
                if (defType.IsGenericType)
                {
                    type = typeof(Dictionary <,>).MakeGenericType(defType.GetGenericArguments());
                }
                else
                {
                    type = typeof(Hashtable);
                }
                if (!defType.IsAssignableFrom(type))
                {
                    throw new TypeNoMatchException("Defind type and value type not match !defind type:" + defType.FullName + ",value type:" + type.FullName);
                }
            }
            var         len = inStream.ReadInt32();
            IDictionary obj = (IDictionary)Activator.CreateInstance(type, true);

            Type[] gtypes = null;
            if (type.IsGenericType)
            {
                gtypes = type.GetGenericArguments();
            }
            for (int i = 0; i < len; i++)
            {
                var keyTypeId = inStream.ReadUInt32();
                var keyType   = keyTypeId.ToType();
                var dkeyType  = keyType;
                if (type.IsGenericType)
                {
                    dkeyType = gtypes[0];
                }
                object key             = SerializerFactory.GetSerializer(keyTypeId.ToType()).ReadObject(inStream, dkeyType);
                byte[] valueTypeBuffer = new byte[4];
                inStream.SafeRead(valueTypeBuffer, 0, 4);
                var valueTypeId = valueTypeBuffer.ToUInt32(0);
                if (valueTypeId == 0)
                {
                    obj[key] = null;
                }
                else
                {
                    var valueType  = valueTypeId.ToType();
                    var dvalueType = valueType;
                    if (type.IsGenericType)
                    {
                        dvalueType = gtypes[1];
                    }
                    object value = SerializerFactory.GetSerializer(valueTypeId.ToType()).ReadObject(inStream, dvalueType);
                    obj[key] = value;
                }
            }
            inStream.SetRef(hashcode, obj);
            return(obj);
        }
Exemple #27
0
 public override void WriteObject(object obj, GaeaStream outStream)
 {
     outStream.Write(new byte[] { 0, 0, 0, 0 }, 0, 4);
 }
Exemple #28
0
        public override object ReadObject(GaeaStream inStream, Type defType)
        {
            var typeId = inStream.ReadUInt32();

            if (typeId == 0)
            {
                return(null);
            }
            byte  isRef    = (byte)inStream.ReadByte();
            int   hashcode = inStream.ReadInt32();
            IList obj;

            if (isRef > 0)
            {
                obj = (IList)inStream.GetRef(hashcode);
            }
            else
            {
                var type = typeId.ToType();
                if (type == null)
                {
                    throw new TypeNoMatchException("Type id can't convert to type !typeId:" + typeId);
                }
                if (type != typeof(IList))
                {
                    throw new TypeNoMatchException("Type must be IList!type:" + type.FullName);
                }
                if (!defType.IsAbstract && !defType.IsInterface && typeof(IList).IsAssignableFrom(defType))
                {
                    type = defType;
                }
                else
                {
                    if (defType.IsGenericType)
                    {
                        var gtypes = defType.GetGenericArguments();
                        if (gtypes.Length != 1)
                        {
                            throw new TypeNoMatchException("Defind type Generic parameters length error!deftype:" + defType.FullName);
                        }
                        type = typeof(List <>).MakeGenericType(gtypes);
                    }
                    else
                    {
                        type = typeof(ArrayList);
                    }
                    if (!defType.IsAssignableFrom(type))
                    {
                        throw new TypeNoMatchException("Defind type and value type not match !defind type:" + defType.FullName + ",value type:" + type.FullName);
                    }
                }
                var len = inStream.ReadInt32();
                obj = (IList)Activator.CreateInstance(type, true);
                for (int i = 0; i < len; i++)
                {
                    var itemTypeId = inStream.ReadUInt32();
                    if (itemTypeId == 0)
                    {
                        obj.Add(null);
                    }
                    else
                    {
                        var itemType = itemTypeId.ToType();
                        if (itemType == null)
                        {
                            throw new NotFindTypeException("Not find the type from current application.typeId:" + itemTypeId);
                        }
                        Type dType = itemType;//item define type
                        if (type.IsGenericType)
                        {
                            dType = type.GetGenericArguments()[0];
                        }
                        object value = SerializerFactory.GetSerializer(dType).ReadObject(inStream, dType);
                        obj.Add(value);
                    }
                }
                inStream.SetRef(hashcode, obj);
            }

            return(obj);
        }
Exemple #29
0
 public override object ReadObject(GaeaStream inStream, Type defType)
 {
     return(null);
 }