Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Don't allocate all the memory up front when receiving incoming sends (#…
- Loading branch information
Showing
2 changed files
with
74 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
66 changes: 66 additions & 0 deletions
66
src/Microsoft.AspNetCore.Sockets.Http/Internal/PipeWriterStream.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
// Copyright (c) .NET Foundation. All rights reserved. | ||
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. | ||
|
||
using System; | ||
using System.Buffers; | ||
using System.IO; | ||
using System.IO.Pipelines; | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
|
||
namespace Microsoft.AspNetCore.Sockets.Http.Internal | ||
{ | ||
// Write only stream implementation for efficiently writing bytes from the request body | ||
internal class PipeWriterStream : Stream | ||
{ | ||
private long _length; | ||
private readonly PipeWriter _pipeWriter; | ||
|
||
public PipeWriterStream(PipeWriter pipeWriter) | ||
{ | ||
_pipeWriter = pipeWriter; | ||
} | ||
|
||
public override bool CanRead => false; | ||
|
||
public override bool CanSeek => false; | ||
|
||
public override bool CanWrite => true; | ||
|
||
public override long Length => _length; | ||
|
||
public override long Position { get => throw new NotSupportedException(); set => throw new NotSupportedException(); } | ||
|
||
public override void Flush() | ||
{ | ||
throw new NotSupportedException(); | ||
} | ||
|
||
public override int Read(byte[] buffer, int offset, int count) | ||
{ | ||
throw new NotSupportedException(); | ||
} | ||
|
||
public override long Seek(long offset, SeekOrigin origin) | ||
{ | ||
throw new NotSupportedException(); | ||
} | ||
|
||
public override void SetLength(long value) | ||
{ | ||
throw new NotSupportedException(); | ||
} | ||
|
||
public override void Write(byte[] buffer, int offset, int count) | ||
{ | ||
_pipeWriter.Write(new ReadOnlySpan<byte>(buffer, offset, count)); | ||
_length += count; | ||
} | ||
|
||
public override Task WriteAsync(byte[] buffer, int offset, int count, CancellationToken cancellationToken) | ||
{ | ||
Write(buffer, offset, count); | ||
return Task.CompletedTask; | ||
} | ||
} | ||
} |