using Google.Protobuf; using Google.Protobuf.Reflection; using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Text; using System.Threading.Tasks; namespace bts.api { public interface IImmutableMessage { MessageDescriptor Descriptor { get; } int CalculateSize(); void WriteTo(CodedOutputStream output); } public static class ImmutableProtoExtensions { public static void WriteTo(this IImmutableMessage message, Stream output) { var stream = new CodedOutputStream(output); message.WriteTo(stream); stream.Flush(); } public static byte[] ToByteArray(this IImmutableMessage message) { var result = new byte[message.CalculateSize()]; var output = new CodedOutputStream(result); message.WriteTo(output); output.CheckNoSpaceLeft(); return result; } } }