Beispiel #1
0
        public static void Serialize(object o, BufferPollManager poolmanager, ref int bufferindex, ref long size, ref byte[] serbyte)
        {
            MemoryStreamWriter2 writer = new MemoryStreamWriter2(poolmanager);

            Serialize(o, writer);
            bufferindex = writer.Bufferindex;
            size        = writer.GetDataLen();

            if (bufferindex == -1)
            {
                serbyte = writer.GetBytes();
            }
        }
Beispiel #2
0
        private static void Serialize(object o, MemoryStreamWriter2 msWriter)
        {
            if (o == null)
            {
                EntityBufTypeFlag flag = EntityBufTypeFlag.VlaueNull;
                msWriter.WriteByte((byte)flag);
                return;
            }
            else
            {
                EntityBufTypeFlag flag = EntityBufTypeFlag.Empty;
                msWriter.WriteByte((byte)flag);
            }

            Tuple <EntityBufType, bool> tuple = GetTypeBufType(o.GetType());

            if (tuple.Item1.EntityType != EntityType.COMPLEX)
            {
                SerializeSimple(o, tuple.Item2, tuple.Item1, msWriter);
                return;
            }

            bool isArray;
            //PropertyInfo[] props = o.GetType().GetProperties();
            var entitybuftypelist = GetTypeEntityBufType(o.GetType());
            //foreach (var tp in entitybuftypelist)
            Tuple <EntityBufType, bool> tp = null;

            for (int i = 0; i < entitybuftypelist.Length; i++)
            {
                tp = entitybuftypelist[i];
                //EntityBufType buftype = MapBufType(prop.PropertyType, out isArray);
                isArray = tp.Item2;

                object val = tp.Item1.Property.GetValueMethed(o);//o.Eval(tp.Item1.Property);

                if (tp.Item1.EntityType == EntityType.COMPLEX)
                {
                    SerializeComplex(val, isArray, tp.Item1, msWriter);
                }
                else
                {
                    SerializeSimple(val, isArray, tp.Item1, msWriter);
                }
            }
        }
Beispiel #3
0
        public static byte[] Serialize(object o)
        {
            if (o != null && o is byte[])
            {
                return((byte[])o);
            }

            if (o is IEntityBufObject)
            {
                return(((IEntityBufObject)o).Serialize());
            }

            using (MemoryStream ms = new MemoryStream())
            {
                MemoryStreamWriter2 writer = new MemoryStreamWriter2(ms);
                Serialize(o, writer);
                var bytes = writer.GetBytes();

                return(bytes);
            }
        }
Beispiel #4
0
        private static void SerializeSimple(object val, bool isArray, EntityBufType bufType, MemoryStreamWriter2 msWriter)
        {
            //if (bufType.EntityType == EntityType.COMPLEX)
            //{
            //    throw new Exception("无法序列化复杂类型");
            //}

            switch (bufType.EntityType)
            {
            case EntityType.BYTE:
                if (isArray)
                {
                    msWriter.WriteByteArray((byte[])val);
                }
                else
                {
                    msWriter.WriteByte((byte)val);
                }
                break;

            case EntityType.STRING:
                if (isArray)
                {
                    msWriter.WriteStringArray((string[])val);
                }
                else
                {
                    msWriter.WriteString((string)val);
                }
                break;

            case EntityType.SHORT:
            case EntityType.INT16:
                if (isArray)
                {
                    msWriter.WriteInt16Array((Int16[])val);
                }
                else
                {
                    msWriter.WriteInt16((Int16)val);
                }
                break;

            case EntityType.USHORT:
                if (isArray)
                {
                    msWriter.WriteUInt16Array((UInt16[])val);
                }
                else
                {
                    msWriter.WriteUInt16((UInt16)val);
                }
                break;

            case EntityType.INT32:
                if (isArray)
                {
                    msWriter.WriteInt32Array((Int32[])val);
                }
                else
                {
                    msWriter.WriteInt32((Int32)val);
                }
                break;

            case EntityType.CHAR:
                if (isArray)
                {
                    msWriter.WriteCharArray((char[])val);
                }
                else
                {
                    msWriter.WriteChar((char)val);
                }
                break;

            case EntityType.DECIMAL:
                if (isArray)
                {
                    msWriter.WriteDeciamlArray((decimal[])val);
                }
                else
                {
                    msWriter.WriteDecimal((decimal)val);
                }
                break;

            case EntityType.DOUBLE:
                if (isArray)
                {
                    msWriter.WriteDoubleArray((double[])val);
                }
                else
                {
                    msWriter.WriteDouble((double)val);
                }
                break;

            case EntityType.FLOAT:
                if (isArray)
                {
                    msWriter.WriteFloatArray((float[])val);
                }
                else
                {
                    msWriter.WriteFloat((float)val);
                }
                break;

            case EntityType.INT64:
                if (isArray)
                {
                    msWriter.WriteInt64Array((Int64[])val);
                }
                else
                {
                    msWriter.WriteInt64((Int64)val);
                }
                break;

            case EntityType.DATETIME:
                if (isArray)
                {
                    msWriter.WriteDateTimeArray((DateTime[])val);
                }
                else
                {
                    msWriter.WriteDateTime((DateTime)val);
                }
                break;

            case EntityType.BOOL:
                if (isArray)
                {
                    msWriter.WriteBoolArray((bool[])val);
                }
                else
                {
                    msWriter.WriteBool((bool)val);
                }
                break;

            case EntityType.ENUM:
                if (isArray)
                {
                    Array    arr    = (Array)val;
                    string[] strarr = new string[arr.Length];
                    for (int i = 0; i < arr.Length; i++)
                    {
                        strarr[i] = arr.GetValue(i).ToString();
                    }
                    msWriter.WriteStringArray(strarr);
                }
                else
                {
                    msWriter.WriteString(val.ToString());
                }
                break;

            case EntityType.DICTIONARY:
                if (isArray)
                {
                    if (val == null)
                    {
                        msWriter.WriteInt32(-1);
                        break;
                    }
                    var dicArray = (Array)val;
                    msWriter.WriteInt32(dicArray.Length);
                    for (int i = 0; i < dicArray.Length; i++)
                    {
                        Serialize(dicArray.GetValue(i), msWriter);
                    }
                }
                else
                {
                    if (val == null)
                    {
                        msWriter.WriteInt32(-1);
                        break;
                    }
                    //
                    IDictionary idic = (IDictionary)val;
                    //写入长度
                    msWriter.WriteInt32(idic.Count);
                    int i = 0;
                    foreach (DictionaryEntry kv in idic)
                    {
                        //object k=kv.Eval("Key");
                        //object v = kv.Eval("Value");

                        Serialize(kv.Key, msWriter);
                        Serialize(kv.Value, msWriter);
                        i++;
                    }
                }
                break;

            case EntityType.LIST:
                if (isArray)
                {
                    if (val == null)
                    {
                        msWriter.WriteInt32(-1);
                        break;
                    }
                    var listarr = (Array)val;
                    msWriter.WriteInt32(listarr.Length);
                    for (int i = 0; i < listarr.Length; i++)
                    {
                        Serialize(listarr.GetValue(i), msWriter);
                    }
                }
                else
                {
                    if (val == null)
                    {
                        msWriter.WriteInt32(-1);
                        break;
                    }
                    var list = (IList)val;
                    msWriter.WriteInt32(list.Count);
                    foreach (var item in list)
                    {
                        Serialize(item, msWriter);
                    }
                }
                break;

            case EntityType.ARRAY:
                if (isArray)
                {
                    if (val == null)
                    {
                        msWriter.WriteInt32(-1);
                        break;
                    }
                    var listarr = (Array)val;
                    msWriter.WriteInt32(listarr.Length);
                    for (int i = 0; i < listarr.Length; i++)
                    {
                        Serialize(listarr.GetValue(i), msWriter);
                    }
                }
                else
                {
                    if (val == null)
                    {
                        msWriter.WriteInt32(-1);
                        break;
                    }
                    var arr = (Array)val;
                    msWriter.WriteInt32(arr.Length);
                    foreach (var item in arr)
                    {
                        Serialize(item, msWriter);
                    }
                }
                break;

            default:
                throw new EntityBufException("序列化错误");
            }
        }
Beispiel #5
0
        private static void SerializeComplex(object val, bool isArray, EntityBufType bufType, MemoryStreamWriter2 msWriter)
        {
            if (isArray)
            {
                var vals = (object[])val;
                int len  = -1;
                if (vals != null)
                {
                    len = vals.Length;
                }
                msWriter.WriteInt32(len);
                if (len > 0)
                {
                    foreach (object v in vals)
                    {
                        //写入标志
                        if (v != null)
                        {
                            EntityBufTypeFlag flag = EntityBufTypeFlag.Empty;
                            msWriter.WriteByte((byte)flag);
                            Serialize(v, msWriter);
                        }
                        else
                        {
                            EntityBufTypeFlag flag = EntityBufTypeFlag.VlaueNull;
                            msWriter.WriteByte((byte)flag);
                        }
                    }
                }
            }
            else
            {
                msWriter.WriteByte(complexchar);
                msWriter.WriteByte(complexchar);

                if (val != null)
                {
                    EntityBufTypeFlag flag = EntityBufTypeFlag.Empty;
                    msWriter.WriteByte((byte)flag);
                    Serialize(val, msWriter);
                }
                else
                {
                    EntityBufTypeFlag flag = EntityBufTypeFlag.VlaueNull;
                    msWriter.WriteByte((byte)flag);
                }
            }
        }