/// <summary> /// Append specified buffer into the encoder, but break the /// buffer into multiple AppendAsync() calls of 'bufferSize' /// </summary> internal static Task AppendAsync( this IHttpResponseEncoder encoder, byte[] buffer, int bufferSizeEachCall) { return(AppendAsync( encoder, buffer, 0, buffer.Length, bufferSizeEachCall)); }
public override async Task WriteAsync( byte[] buffer, int offset, int count, CancellationToken cancellationToken) { RequireNonCompleted(); // Header wasn't sent, // We suppose to store this write into memory if (false == _response.IsHeaderSent) { // Is the type of body explicitly specified in header? // (Content-length or Transfer-Encoding) // Or, // Will this write overflows the internal buffer? // If so, send the header. if (WillInternalBufferFull(additionalBytes: count) || _response.IsBodyTypeExplicitlySpecified()) { await SendHeaderAndCreateEncoderAsync(); // Dump this write request into it. await _encoder.AppendAsync(buffer, offset, count); } // Otherwise, all writes go to the internal buffer else { _buffer.Write(buffer, offset, count); } } // Header was sent, // We direct all writes to the current encoder. else { // If we don't have an encoder, // which means the header was sent by someone else, // we should have no data held in memory // (if we do, encoder was created already), // so just create an encoder and use it. if (null == _encoder) { _encoder = GetEncoderImpl(_rawResponseStream); } await _encoder.AppendAsync(buffer, offset, count); } }
async Task SendHeaderAndCreateEncoderAsync() { if (_response.IsHeaderSent) { throw new InvalidOperationException(); } // First, crete the encoder _encoder = GetEncoderImpl(_rawResponseStream); // Flush headers. // This need tobe done before any data written into the encoder, // and after the encoder is created. await _response.SendHeaderAsync(); // Transfer bytes held in memory to the encoder await _encoder.AppendAsync( _buffer.GetBuffer(), 0, (int)_buffer.Length, _tcpSettings.ReadWriteBufferSize); }
/// <summary> /// Append specified buffer into the encoder, but break the /// buffer into multiple AppendAsync() calls of 'bufferSize' /// </summary> internal static async Task AppendAsync( this IHttpResponseEncoder encoder, byte[] buffer, int offset, int count, int bufferSizeEachCall) { var bytesWrote = 0; while (bytesWrote < count) { var remain = count - bytesWrote; var bytesToWrite = Math.Min(remain, bufferSizeEachCall); await encoder.AppendAsync( buffer, offset + bytesWrote, bytesToWrite); bytesWrote += bytesToWrite; } }