// Placed object message payload into a segment from a buffer pool. When this get's too big, older blocks will be purged private ArraySegment <byte> SerializeMessageIntoPooledSegment(MemoryMessageData queueMessage) { // serialize payload int size = SegmentBuilder.CalculateAppendSize(queueMessage.Payload); // get segment from current block ArraySegment <byte> segment; if (currentBuffer == null || !currentBuffer.TryGetSegment(size, out segment)) { // no block or block full, get new block and try again currentBuffer = bufferPool.Allocate(); //call EvictionStrategy's OnBlockAllocated method this.evictionStrategy.OnBlockAllocated(currentBuffer); // if this fails with clean block, then requested size is too big if (!currentBuffer.TryGetSegment(size, out segment)) { string errmsg = String.Format(CultureInfo.InvariantCulture, "Message size is too big. MessageSize: {0}", size); throw new ArgumentOutOfRangeException(nameof(queueMessage), errmsg); } } // encode namespace, offset, partitionkey, properties and payload into segment int writeOffset = 0; SegmentBuilder.Append(segment, ref writeOffset, queueMessage.Payload); return(segment); }
// Placed object message payload into a segment from a buffer pool. When this get's too big, older blocks will be purged private ArraySegment <byte> SerializeMessageIntoPooledSegment(GeneratedBatchContainer queueMessage) { byte[] serializedPayload = this.serializationManager.SerializeToByteArray(queueMessage.Payload); // get size of namespace, offset, partitionkey, properties, and payload int size = SegmentBuilder.CalculateAppendSize(serializedPayload); // get segment ArraySegment <byte> segment; if (currentBuffer == null || !currentBuffer.TryGetSegment(size, out segment)) { // no block or block full, get new block and try again currentBuffer = bufferPool.Allocate(); //call EvictionStrategy's OnBlockAllocated method this.evictionStrategy.OnBlockAllocated(currentBuffer); // if this fails with clean block, then requested size is too big if (!currentBuffer.TryGetSegment(size, out segment)) { string errmsg = $"Message size is to big. MessageSize: {size}"; throw new ArgumentOutOfRangeException(nameof(queueMessage), errmsg); } } // encode namespace, offset, partitionkey, properties and payload into segment int writeOffset = 0; SegmentBuilder.Append(segment, ref writeOffset, serializedPayload); return(segment); }