Example #1
0
        public static byte[] ClassToBytes(Object arg)
        {
            if (SerializeType.st_class != SerializeType.GetSerializeType(arg))
            {
                throw new Exception("ClassToBytes must get arguments with typeof class!");
            }

            List <LeaguerInfo> list = GetObjectPropertyInfos(arg);
            List <byte>        res  = new List <byte>();

            foreach (LeaguerInfo info in list)
            {
                res.AddRange(info.key.ToKeyBytes());
                res.AddRange(info.typeCode.ToBytes());
                res.AddRange(info.valBuffer.Length.ToBytes());
                res.AddRange(info.valBuffer);
            }
            res.InsertRange(0, res.Count.ToBytes());
            return(res.ToArray());
        }
Example #2
0
        static List <LeaguerInfo> GetObjectPropertyInfos(Object obj)
        {
            List <LeaguerInfo> list = new List <LeaguerInfo>();
            Type type = obj.GetType();

            FieldInfo[] fileds = type.GetFields(BindingFlags.Instance | BindingFlags.Public);
            foreach (FieldInfo info in fileds)
            {
                object val = info.GetValue(obj);
                if (null == val)
                {
                    continue;
                }
                LeaguerInfo pro = new LeaguerInfo();
                pro.key       = info.Name;
                pro.typeCode  = SerializeType.GetSerializeType(val.GetType());
                pro.valBuffer = Serializer.GetBytes(val);
                list.Add(pro);
            }

            PropertyInfo[] props = type.GetProperties(BindingFlags.Instance | BindingFlags.Public);
            foreach (PropertyInfo info in props)
            {
                object val = info.GetValue(obj, null);
                if (null == val)
                {
                    continue;
                }
                LeaguerInfo pro = new LeaguerInfo();
                pro.key       = info.Name;
                pro.typeCode  = SerializeType.GetSerializeType(val.GetType());
                pro.valBuffer = Serializer.GetBytes(val);
                list.Add(pro);
            }

            return(list);
        }
Example #3
0
        public static void Read(BinaryReader reader, Type type, ref object obj)
        {
            byte typeCode = SerializeType.GetSerializeType(type);

            switch (typeCode)
            {
            case SerializeType.st_bool:
            {
                obj = reader.ReadBoolean();
            }
            break;

            case SerializeType.st_byte:
            {
                obj = reader.ReadByte();
            }
            break;

            case SerializeType.st_char:
            {
                obj = reader.ReadChar();
            }
            break;

            case SerializeType.st_float:
            {
                obj = reader.ReadSingle();
            }
            break;

            case SerializeType.st_double:
            {
                obj = reader.ReadDouble();
            }
            break;

            case SerializeType.st_short:
            {
                obj = reader.ReadInt16();
            }
            break;

            case SerializeType.st_ushort:
            {
                obj = reader.ReadUInt16();
            }
            break;

            case SerializeType.st_int:
            {
                if (type.IsEnum)
                {
                    obj = Enum.Parse(type, Enum.GetName(type, reader.ReadInt32()));
                }
                else
                {
                    obj = reader.ReadInt32();
                }
            }
            break;

            case SerializeType.st_uint:
            {
                obj = reader.ReadUInt32();
            }
            break;

            case SerializeType.st_long:
            {
                obj = reader.ReadInt64();
            }
            break;

            case SerializeType.st_ulong:
            {
                obj = reader.ReadUInt64();
            }
            break;

            case SerializeType.st_string:
            {
                int    length = reader.ReadInt32();
                byte[] buffer = new byte[length];
                reader.Read(buffer, 0, length);
                obj = buffer.ToUTF8String();
            }
            break;

            case SerializeType.st_class:
            {
                int    size   = reader.ReadInt32();
                byte[] buffer = new byte[size];
                reader.Read(buffer, 0, size);
                ComplexSerializer.BytesToClass(buffer, type, ref obj);
            }
            break;

            case SerializeType.st_array:
            {
                int    length = reader.ReadInt32();
                byte[] buffer = new byte[length];
                reader.Read(buffer, 0, length);
                obj = ComplexSerializer.BytesToArray(buffer, type.GetElementType());
            }
            break;

            case SerializeType.st_list:
            {
                int    length = reader.ReadInt32();
                byte[] buffer = new byte[length];
                reader.Read(buffer, 0, length);
                obj = ComplexSerializer.BytesToList(buffer, type);
            }
            break;

            case SerializeType.st_dictionary:
            {
                int    length = reader.ReadInt32();
                byte[] buffer = new byte[length];
                reader.Read(buffer, 0, length);
                obj = ComplexSerializer.BytesToDictionary(buffer, type);
            }
            break;
            }
        }
Example #4
0
        public static byte[] GetBytes(Object arg)
        {
            byte typeCode = SerializeType.GetSerializeType(arg);

            switch (typeCode)
            {
            case SerializeType.st_bool:
            {
                return(Convert.ToBoolean(arg).ToBytes());
            }

            case SerializeType.st_byte:
            {
                return(Convert.ToByte(arg).ToBytes());
            }

            case SerializeType.st_char:
            {
                return(Convert.ToChar(arg).ToBytes());
            }

            case SerializeType.st_float:
            {
                return(Convert.ToSingle(arg).ToBytes());
            }

            case SerializeType.st_double:
            {
                return(Convert.ToDouble(arg).ToBytes());
            }

            case SerializeType.st_short:
            {
                return(Convert.ToInt16(arg).ToBytes());
            }

            case SerializeType.st_ushort:
            {
                return(Convert.ToUInt16(arg).ToBytes());
            }

            case SerializeType.st_int:
            {
                return(Convert.ToInt32(arg).ToBytes());
            }

            case SerializeType.st_uint:
            {
                return(Convert.ToUInt32(arg).ToBytes());
            }

            case SerializeType.st_long:
            {
                return(Convert.ToInt64(arg).ToBytes());
            }

            case SerializeType.st_ulong:
            {
                return(Convert.ToUInt64(arg).ToBytes());
            }

            case SerializeType.st_string:
            {
                return(Convert.ToString(arg).ToBytes());
            }

            case SerializeType.st_class:
            {
                return(ComplexSerializer.ClassToBytes(arg));
            }

            case SerializeType.st_array:
            {
                Array obj = (Array)arg;
                return(obj.ToBytes());
            }

            case SerializeType.st_list:
            {
                return(ComplexSerializer.ListToBytes(arg));
            }

            case SerializeType.st_dictionary:
            {
                return(ComplexSerializer.DicToBytes(arg));
            }
            }
            return(default(byte[]));
        }
Example #5
0
        public static void DeSerialize(byte[] buffer, Type type, ref object obj)
        {
            byte typeCode = SerializeType.GetSerializeType(type);

            switch (typeCode)
            {
            case SerializeType.st_bool:
            {
                obj = Convert.ChangeType(buffer.ToBoolean(), type);
            }
            break;

            case SerializeType.st_byte:
            {
                obj = Convert.ChangeType(buffer.ToByte(), type);
            }
            break;

            case SerializeType.st_char:
            {
                obj = Convert.ChangeType(buffer.ToChar(), type);
            }
            break;

            case SerializeType.st_float:
            {
                obj = Convert.ChangeType(buffer.ToFloat(), type);
            }
            break;

            case SerializeType.st_double:
            {
                obj = Convert.ChangeType(buffer.ToDouble(), type);
            }
            break;

            case SerializeType.st_short:
            {
                obj = Convert.ChangeType(buffer.ToShort(), type);
            }
            break;

            case SerializeType.st_ushort:
            {
                obj = Convert.ChangeType(buffer.ToUShort(), type);
            }
            break;

            case SerializeType.st_int:
            {
                if (type.IsEnum)
                {
                    obj = Enum.Parse(type, Enum.GetName(type, buffer.ToInt()));
                }
                else
                {
                    obj = Convert.ChangeType(buffer.ToInt(), type);
                }
            }
            break;

            case SerializeType.st_uint:
            {
                obj = Convert.ChangeType(buffer.ToUInt(), type);
            }
            break;

            case SerializeType.st_long:
            {
                obj = Convert.ChangeType(buffer.ToLong(), type);
            }
            break;

            case SerializeType.st_ulong:
            {
                obj = Convert.ChangeType(buffer.ToULong(), type);
            }
            break;

            case SerializeType.st_string:
            {
                byte[] realBuffer = new byte[buffer.Length - sizeof(int)];
                Array.Copy(buffer, sizeof(int), realBuffer, 0, realBuffer.Length);
                obj = Convert.ChangeType(realBuffer.ToUTF8String(), type);
            }
            break;

            case SerializeType.st_class:
            {
                byte[] realBuffer = new byte[buffer.Length - sizeof(int)];
                Array.Copy(buffer, sizeof(int), realBuffer, 0, realBuffer.Length);
                ComplexSerializer.BytesToClass(realBuffer, type, ref obj);
            }
            break;

            case SerializeType.st_array:
            {
                byte[] realBuffer = new byte[buffer.Length - sizeof(int)];
                Array.Copy(buffer, sizeof(int), realBuffer, 0, realBuffer.Length);
                obj = ComplexSerializer.BytesToArray(realBuffer, type.GetElementType());
            }
            break;

            case SerializeType.st_list:
            {
                byte[] realBuffer = new byte[buffer.Length - sizeof(int)];
                Array.Copy(buffer, sizeof(int), realBuffer, 0, realBuffer.Length);
                obj = ComplexSerializer.BytesToList(realBuffer, type);
            }
            break;

            case SerializeType.st_dictionary:
            {
                byte[] realBuffer = new byte[buffer.Length - sizeof(int)];
                Array.Copy(buffer, sizeof(int), realBuffer, 0, realBuffer.Length);
                obj = ComplexSerializer.BytesToDictionary(realBuffer, type);
            }
            break;
            }
        }
Example #6
0
        public static void BytesToClass(byte[] buffer, Type type, ref Object res)
        {
            byte typeCode = SerializeType.GetSerializeType(type);

            if (typeCode != SerializeType.st_class)
            {
                throw new Exception("Wrong call BytesToClass of:" + type.ToString());
            }

            using (MemoryStream stream = new MemoryStream(buffer))
            {
                try
                {
                    res = Activator.CreateInstance(type);
                    BinaryReader reader = new BinaryReader(stream);
                    Dictionary <string, LeaguerInfo> dicInfos = new Dictionary <string, LeaguerInfo>();
                    while (!reader.IsReadOver())
                    {
                        LeaguerInfo info      = new LeaguerInfo();
                        object      keyLength = 0;
                        Serializer.Read(reader, typeof(byte), ref keyLength);
                        byte[] keyBuffer = new byte[(byte)keyLength];
                        reader.Read(keyBuffer, 0, (byte)keyLength);
                        info.key = keyBuffer.ToUTF8String();
                        object typeCodeO = SerializeType.st_error;
                        Serializer.Read(reader, typeof(byte), ref typeCodeO);
                        info.typeCode = (byte)typeCodeO;
                        object valLengthO = 0;
                        Serializer.Read(reader, typeof(int), ref valLengthO);
                        int valLength = (int)valLengthO;
                        info.valBuffer = new byte[valLength];
                        reader.Read(info.valBuffer, 0, valLength);
                        dicInfos.Add(info.key, info);
                    }

                    FieldInfo[] fields = res.GetType().GetFields(BindingFlags.Instance | BindingFlags.Public);
                    foreach (FieldInfo field in fields)
                    {
                        if (dicInfos.ContainsKey(field.Name))
                        {
                            object obj = default(object);
                            Serializer.DeSerialize(dicInfos[field.Name].valBuffer, field.FieldType, ref obj);
                            field.SetValue(res, obj);
                        }
                    }

                    PropertyInfo[] props = type.GetProperties(BindingFlags.Instance | BindingFlags.Public);
                    foreach (PropertyInfo info in props)
                    {
                        if (dicInfos.ContainsKey(info.Name))
                        {
                            object obj = default(object);
                            Serializer.DeSerialize(dicInfos[info.Name].valBuffer, info.PropertyType, ref obj);
                            info.SetValue(res, obj, null);
                        }
                    }
                }
                catch (Exception e)
                {
                    GLog.LogError(e.ToString());
                }
            };
        }