/// <inheritdoc /> public ReadOnlyMemory <byte> GetMessageBytes(ServiceMessage message) { var memoryBufferWriter = MemoryBufferWriter.Get(); try { var writer = new MessagePackWriter(memoryBufferWriter); // Write message to a buffer so we can get its length WriteMessageCore(ref writer, message); var dataLength = memoryBufferWriter.Length; var prefixLength = BinaryMessageFormatter.LengthPrefixLength(memoryBufferWriter.Length); var array = new byte[dataLength + prefixLength]; var span = array.AsSpan(); // Write length then message to output var written = BinaryMessageFormatter.WriteLengthPrefix(memoryBufferWriter.Length, span); Debug.Assert(written == prefixLength); memoryBufferWriter.CopyTo(span.Slice(prefixLength)); return(array); } finally { MemoryBufferWriter.Return(memoryBufferWriter); } }
/// <inheritdoc /> public void WriteMessage(ServiceMessage message, IBufferWriter <byte> output) { var memoryBufferWriter = MemoryBufferWriter.Get(); try { var writer = new MessagePackWriter(memoryBufferWriter); // Write message to a buffer so we can get its length WriteMessageCore(ref writer, message); // Write length then message to output BinaryMessageFormatter.WriteLengthPrefix(memoryBufferWriter.Length, output); memoryBufferWriter.CopyTo(output); } finally { MemoryBufferWriter.Return(memoryBufferWriter); } }