示例#1
0
        public static async Task <CommunicationResult> WriteSafelyAsync(
            this Stream stream,
            StreamWritable element,
            CancellationToken token,
            bool recycle = true)
        {
            try
            {
                await stream.WriteAsync(element.Buffer, 0, element.ContentLength, token);

                if (recycle)
                {
                    element.RecycleBuffer();
                }
                return(new CommunicationResult()
                {
                    Successful = true,
                });
            }
            catch (Exception exc)
            {
                return(new CommunicationResult()
                {
                    Successful = false,
                    Exception = exc,
                });
            }
        }
示例#2
0
            public static StreamWritable Combine(StreamWritable first, StreamWritable second)
            {
                var totalLength = first.ContentLength + second.ContentLength;

                if (first.Buffer.Length >= totalLength)
                {
                    Array.Copy(second.Buffer, 0, first.Buffer, first.ContentLength, second.ContentLength);
                    return(new StreamWritable(first.Buffer, totalLength, false));
                }
                var result = BufferPool.Rent(totalLength);

                Array.Copy(first.Buffer, 0, result, 0, first.ContentLength);
                Array.Copy(second.Buffer, 0, result, first.ContentLength, second.ContentLength);
                return(new StreamWritable(result, totalLength, true));
            }
示例#3
0
        public static async Task <CommunicationResult> WriteSafelyAsync(
            this Stream stream,
            StreamWritable first,
            StreamWritable second,
            CancellationToken token,
            bool recycle = true)
        {
            var combined = StreamWritable.Combine(first, second);
            var result   = await stream.WriteSafelyAsync(combined, token);

            if (recycle)
            {
                first.RecycleBuffer();
                second.RecycleBuffer();
            }
            return(result);
        }