Ejemplo n.º 1
0
        public static async Task WriteResponseAsync <TResponse>(Stream ms, TResponse response, ICompressionProvider?compressionProvider) where TResponse : IMessage <TResponse>
        {
            var compress = false;

            byte[]? data;
            if (compressionProvider != null)
            {
                compress = true;

                var output            = new MemoryStream();
                var compressionStream = compressionProvider.CreateCompressionStream(output, System.IO.Compression.CompressionLevel.Fastest);
                var compressedData    = response.ToByteArray();

                compressionStream.Write(compressedData, 0, compressedData.Length);
                compressionStream.Flush();
                compressionStream.Dispose();
                data = output.ToArray();
            }
            else
            {
                data = response.ToByteArray();
            }

            await ResponseUtils.WriteHeaderAsync(ms, data.Length, compress, CancellationToken.None);

            await ms.WriteAsync(data, 0, data.Length);
        }
Ejemplo n.º 2
0
        public static async Task WriteResponseAsync <TResponse>(Stream ms, TResponse response) where TResponse : IMessage <TResponse>
        {
            await ResponseUtils.WriteHeaderAsync(ms, response.CalculateSize(), false, CancellationToken.None);

            await ms.WriteAsync(response.ToByteArray());
        }