Пример #1
0
 void SerializeIntoBuffer(object metadata, IReadOnlyList<object> events, out int startOffset,
                          out IDescriptorSerializerContext serializerContext, out BlockType blockType,
                          out int lenWithoutEndPadding, out ByteBuffer block)
 {
     startOffset = (int)EndBufferLen + HeaderSize;
     var writer = new ByteBufferWriter();
     writer.WriteBlock(_zeroes, 0, startOffset);
     serializerContext = Mapping;
     if (metadata != null)
         serializerContext = serializerContext.StoreNewDescriptors(writer, metadata);
     if (events != null)
     {
         foreach (var o in events)
         {
             serializerContext = serializerContext.StoreNewDescriptors(writer, o);
         }
         if (events.Count == 0) events = null;
     }
     serializerContext.FinishNewDescriptors(writer);
     blockType = BlockType.FirstBlock;
     if (serializerContext.SomeTypeStored)
         blockType |= BlockType.HasTypeDeclaration;
     if (metadata != null)
     {
         serializerContext.StoreObject(writer, metadata);
         blockType |= BlockType.HasMetadata;
     }
     if (events != null)
     {
         if (events.Count == 1)
         {
             serializerContext.StoreObject(writer, events[0]);
             blockType |= BlockType.HasOneEvent;
         }
         else
         {
             writer.WriteVUInt32((uint)events.Count);
             foreach (var o in events)
             {
                 serializerContext.StoreObject(writer, o);
             }
             blockType |= BlockType.HasMoreEvents;
         }
     }
     lenWithoutEndPadding = (int)writer.GetCurrentPosition();
     writer.WriteBlock(_zeroes, 0, (int)(SectorSize - 1));
     block = writer.Data;
     if (CompressionStrategy.ShouldTryToCompress(lenWithoutEndPadding - startOffset))
     {
         var compressedBlock = ByteBuffer.NewSync(block.Buffer, startOffset, lenWithoutEndPadding - startOffset);
         if (CompressionStrategy.Compress(ref compressedBlock))
         {
             blockType |= BlockType.Compressed;
             Array.Copy(compressedBlock.Buffer, compressedBlock.Offset, block.Buffer, startOffset, compressedBlock.Length);
             lenWithoutEndPadding = startOffset + compressedBlock.Length;
             Array.Copy(_zeroes, 0, block.Buffer, lenWithoutEndPadding, (int)SectorSize - 1);
         }
     }
 }
Пример #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();
     }
 }
Пример #3
0
        void SerializeIntoBuffer(object metadata, IReadOnlyList <object> events, out int startOffset,
                                 out IDescriptorSerializerContext serializerContext, out BlockType blockType,
                                 out int lenWithoutEndPadding, out ByteBuffer block)
        {
            startOffset = (int)EndBufferLen + HeaderSize;
            var writer = new ByteBufferWriter();

            writer.WriteBlock(_zeroes.AsSpan(0, startOffset));
            serializerContext = Mapping;
            if (metadata != null)
            {
                serializerContext = serializerContext.StoreNewDescriptors(writer, metadata);
            }
            if (events != null)
            {
                foreach (var o in events)
                {
                    serializerContext = serializerContext.StoreNewDescriptors(writer, o);
                }
                if (events.Count == 0)
                {
                    events = null;
                }
            }
            serializerContext.FinishNewDescriptors(writer);
            blockType = BlockType.FirstBlock;
            if (serializerContext.SomeTypeStored)
            {
                blockType |= BlockType.HasTypeDeclaration;
            }
            if (metadata != null)
            {
                serializerContext.StoreObject(writer, metadata);
                blockType |= BlockType.HasMetadata;
            }
            if (events != null)
            {
                if (events.Count == 1)
                {
                    serializerContext.StoreObject(writer, events[0]);
                    blockType |= BlockType.HasOneEvent;
                }
                else
                {
                    writer.WriteVUInt32((uint)events.Count);
                    foreach (var o in events)
                    {
                        serializerContext.StoreObject(writer, o);
                    }
                    blockType |= BlockType.HasMoreEvents;
                }
            }
            lenWithoutEndPadding = (int)writer.GetCurrentPosition();
            writer.WriteBlock(_zeroes.AsSpan(0, (int)(SectorSize - 1)));
            block = writer.Data;
            if (CompressionStrategy.ShouldTryToCompress(lenWithoutEndPadding - startOffset))
            {
                var compressedBlock = ByteBuffer.NewSync(block.Buffer, startOffset, lenWithoutEndPadding - startOffset);
                if (CompressionStrategy.Compress(ref compressedBlock))
                {
                    blockType |= BlockType.Compressed;
                    Array.Copy(compressedBlock.Buffer, compressedBlock.Offset, block.Buffer, startOffset, compressedBlock.Length);
                    lenWithoutEndPadding = startOffset + compressedBlock.Length;
                    Array.Copy(_zeroes, 0, block.Buffer, lenWithoutEndPadding, (int)SectorSize - 1);
                }
            }
        }