Ejemplo n.º 1
0
        public ByteBuffer Serialize(out bool hasMetaData, object obj)
        {
            if (obj == null)
            {
                hasMetaData = false;
                _writer.WriteUInt8(0); // null
                return(_writer.GetDataAndRewind());
            }

            try
            {
                _newTypeFound = false;
                StoreObject(obj);
                _visited.Clear();
                if (!_newTypeFound)
                {
                    // No unknown metadata found - to be optimistic pays off
                    hasMetaData = false;
                    return(_writer.GetDataAndRewind());
                }

                StoreNewDescriptors(obj);
                if (_typeOrDescriptor2InfoNew.Count > 0)
                {
                    _writer.Rewind();
                    if (MergeTypesByShapeAndStoreNew())
                    {
                        _typeOrDescriptor2InfoNew.Clear();
                        _visited.Clear();
                        hasMetaData = true;
                        return(_writer.GetDataAndRewind());
                    }

                    _typeOrDescriptor2InfoNew.Clear();
                }

                _visited.Clear();
                _newTypeFound = false;
                StoreObject(obj);
                if (_newTypeFound)
                {
                    throw new BTDBException("Forgot descriptor or type");
                }

                _visited.Clear();
                hasMetaData = false;
                return(_writer.GetDataAndRewind());
            }
            catch
            {
                _visited.Clear();
                _typeOrDescriptor2InfoNew.Clear();
                _writer.Rewind();
                throw;
            }
        }
Ejemplo n.º 2
0
 public void Send(object dto)
 {
     lock (_sendLocker)
     {
         IDescriptorSerializerContext serializerContext = _sendingMapping;
         var writer = new ByteBufferWriter();
         writer.WriteUInt8(100);
         serializerContext = serializerContext.StoreNewDescriptors(writer, dto);
         serializerContext.FinishNewDescriptors(writer);
         serializerContext.StoreObject(writer, dto);
         var block = writer.Data;
         if (serializerContext.SomeTypeStored)
         {
             block[0] = 99;
         }
         _channel.Send(block);
         serializerContext.CommitNewDescriptors();
     }
 }