Ejemplo n.º 1
0
 public byte[] Serialize(object Entity, DynamicBuffer buffer, Dictionary <object, BufferPtr> referenceMaping)
 {
     byte[] result = bitSerializer.Serialize(Entity);
     if (buffer.CurrentPoint == 0)
     {
         buffer.Alloc(result.Count()).Set(result);
     }
     return(result);
 }
Ejemplo n.º 2
0
        public byte[] Serialize(object entity, DynamicBuffer buffer, Dictionary <object, DynamicBuffer.BufferPtr> referenceMaping)
        {
            int size = sizeof(int);

            if (entity == null)
            {
                return(Сonstants.nullPtrBytres);
            }

            if (referenceMaping.ContainsKey(entity))
            {
                return(BitConverter.GetBytes(referenceMaping[entity].position));
            }

            if (typeInfo.Type == typeof(string))
            {
                entity = ((string)entity).ToArray();
            }

            Array array = entity as Array;

            var resolver = TypeInfo.instanse(typeInfo.ElementTypeInfo).Resolver;

            size += memberTypeInfo.SizeInBuffer * array.Length;

            byte[] entityBuffer = new byte[size];

            var ptr = buffer.Alloc(size);

            referenceMaping.Add(entity, ptr);


            int positionInBuffer = 0;

            byte[] lenghtBytes = BitConverter.GetBytes(array.Length);

            BlockCopy(lenghtBytes, 0, entityBuffer, 0, lenghtBytes.Length);

            positionInBuffer += lenghtBytes.Length;

            for (int i = 0; i < array.Length; i++)
            {
                object innerObject = array.GetValue(i);

                byte[] InnerObjectBytes = resolver.Serialize(innerObject, buffer, referenceMaping);

                BlockCopy(InnerObjectBytes, 0, entityBuffer, positionInBuffer, InnerObjectBytes.Length);

                positionInBuffer += InnerObjectBytes.Length;
            }

            ptr.Set(entityBuffer);

            return(BitConverter.GetBytes(ptr.position));
        }
        public byte[] Serialize(object Entity, DynamicBuffer buffer, Dictionary <object, BufferPtr> referenceMaping)
        {
            if (Entity == null)
            {
                return(Сonstants.nullPtrBytres);
            }
            BufferPtr ptr;

            if (referenceMaping.TryGetValue(Entity, out ptr))
            {
                return(BitConverter.GetBytes(ptr.position));
            }
            TypeInfo typeInfo = TypeInfo.instanse(Entity.GetType());

            ptr = buffer.Alloc(abstractTypeInfo.Size);

            byte[] objectBuffer = new byte[abstractTypeInfo.Size];

            objectBuffer[0] = (byte)ObjectFlag.AbstractClass;

            int positionInBuffer = 1;

            #region save information about type in  buffer
            string typeName = Entity.GetType().FullName;

            byte[] pointerToTypeInfo = stringTypeInfo.Resolver.Serialize(typeName, buffer, referenceMaping);

            BlockCopy(pointerToTypeInfo, 0, objectBuffer, positionInBuffer, pointerToTypeInfo.Length);

            positionInBuffer += pointerToTypeInfo.Length;

            #endregion

            // save ptr on instanse
            byte[] instansePtr = typeInfo.Resolver.Serialize(Entity, buffer, referenceMaping);

            BlockCopy(instansePtr, 0, objectBuffer, positionInBuffer, instansePtr.Length);

            ptr.Set(objectBuffer);

            return(BitConverter.GetBytes(ptr.position));
        }
Ejemplo n.º 4
0
        public byte[] Serialize(object Entity, DynamicBuffer buffer, Dictionary <object, BufferPtr> referenceMaping)
        {
            if (Entity == null)
            {
                return(Сonstants.nullPtrBytres);
            }
            BufferPtr ptr;

            if (referenceMaping.TryGetValue(Entity, out ptr))
            {
                return(BitConverter.GetBytes(ptr.position));
            }
            ptr = buffer.Alloc(typeInfo.Size);

            byte[] objectBuffer = new byte[typeInfo.Size];

            objectBuffer[0] = (byte)ObjectFlag.Normal;

            referenceMaping.Add(Entity, ptr);

            int positionInBuffer = 1;


            foreach (var member in typeInfo.Fields)
            {
                var memberTypeInfo = TypeInfo.instanse(member.FieldType);

                object innerObject = typeInfo.GetValue(Entity, member);

                byte[] InnerObjectBytes = memberTypeInfo.Resolver.Serialize(innerObject, buffer, referenceMaping);

                BlockCopy(InnerObjectBytes, 0, objectBuffer, positionInBuffer, InnerObjectBytes.Length);

                positionInBuffer += InnerObjectBytes.Length;
            }

            ptr.Set(objectBuffer);

            return(BitConverter.GetBytes(ptr.position));
        }