Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 8 additions & 4 deletions src/libraries/Common/src/System/Net/ArrayBuffer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,7 @@ public ArrayBuffer(byte[] buffer)

public void Dispose()
{
_activeStart = 0;
_availableStart = 0;
DiscardAll();

byte[] array = _bytes;
_bytes = null!;
Expand All @@ -77,8 +76,7 @@ public void ClearAndReturnBuffer()
Debug.Assert(_usePool);
Debug.Assert(_bytes is not null);

_activeStart = 0;
_availableStart = 0;
DiscardAll();

byte[] bufferToReturn = _bytes;
_bytes = Array.Empty<byte>();
Expand Down Expand Up @@ -112,6 +110,12 @@ public void Discard(int byteCount)
}
}

public void DiscardAll()
{
_activeStart = 0;
_availableStart = 0;
}

public void Commit(int byteCount)
{
Debug.Assert(byteCount <= AvailableLength);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public PooledByteBufferWriter(int initialCapacity, Stream stream) : this(initial

public int Capacity => _buffer.Capacity;

public void Clear() => _buffer.Discard(_buffer.ActiveLength);
public void Clear() => _buffer.DiscardAll();

public void ClearAndReturnBuffers() => _buffer.ClearAndReturnBuffer();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -436,7 +436,7 @@ private void TryRewindStream(Stream stream)
{
// Rewind the stream to the exact end of the compressed data
stream.Seek(-unconsumedBytes, SeekOrigin.Current);
_buffer.Discard(unconsumedBytes);
_buffer.DiscardAll();
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -409,7 +409,7 @@ private async Task FlushOutgoingBytesAsync()
}

_lastPendingWriterShouldFlush = false;
_outgoingBuffer.Discard(_outgoingBuffer.ActiveLength);
_outgoingBuffer.DiscardAll();
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -558,7 +558,7 @@ private async ValueTask WriteRequestContentAsync(ReadOnlyMemory<byte> buffer, Ca
await _stream.WriteAsync(_sendBuffer.ActiveMemory, cancellationToken).ConfigureAwait(false);
await _stream.WriteAsync(buffer, cancellationToken).ConfigureAwait(false);

_sendBuffer.Discard(_sendBuffer.ActiveLength);
_sendBuffer.DiscardAll();

_singleDataFrameWritten = true;
}
Expand All @@ -577,14 +577,14 @@ private async ValueTask WriteRequestContentAsync(ReadOnlyMemory<byte> buffer, Ca
await _stream.WriteAsync(_sendBuffer.ActiveMemory, cancellationToken).ConfigureAwait(false);
await _stream.WriteAsync(buffer, cancellationToken).ConfigureAwait(false);

_sendBuffer.Discard(_sendBuffer.ActiveLength);
_sendBuffer.DiscardAll();
}
}

private ValueTask FlushSendBufferAsync(bool endStream, CancellationToken cancellationToken)
{
ReadOnlyMemory<byte> toSend = _sendBuffer.ActiveMemory;
_sendBuffer.Discard(toSend.Length);
_sendBuffer.DiscardAll();
return _stream.WriteAsync(toSend, endStream, cancellationToken);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1552,7 +1552,7 @@ private void Flush()
ReadOnlySpan<byte> bytes = _writeBuffer.ActiveSpan;
if (bytes.Length > 0)
{
_writeBuffer.Discard(bytes.Length);
_writeBuffer.DiscardAll();
WriteToStream(bytes);
}
}
Expand All @@ -1562,7 +1562,7 @@ private ValueTask FlushAsync(bool async)
ReadOnlyMemory<byte> bytes = _writeBuffer.ActiveMemory;
if (bytes.Length > 0)
{
_writeBuffer.Discard(bytes.Length);
_writeBuffer.DiscardAll();
return WriteToStreamAsync(bytes, async);
}
return default;
Expand Down Expand Up @@ -2080,7 +2080,7 @@ private void CompleteResponse()
Trace("Unexpected data on connection after response read.");
}

_readBuffer.Discard(_readBuffer.ActiveLength);
_readBuffer.DiscardAll();
_connectionClose = true;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,7 @@ internal void ReadPendingWrites(ref ProtocolToken token)
}

token.SetPayload(_outputBuffer.ActiveSpan);
_outputBuffer.Discard(_outputBuffer.ActiveLength);
_outputBuffer.DiscardAll();
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -319,7 +319,7 @@ internal void ReadPendingWrites(ref ProtocolToken token)
}

token.SetPayload(_outputBuffer.ActiveSpan);
_outputBuffer.Discard(_outputBuffer.ActiveLength);
_outputBuffer.DiscardAll();
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -92,10 +92,7 @@ partial void OnServerContextSet()
_pendingFdSocket = null;

// Discard any managed pre-fetch bytes; ownership transfers to the native BIO now.
if (_socketInBuffer.ActiveLength > 0)
{
_socketInBuffer.Discard(_socketInBuffer.ActiveLength);
}
_socketInBuffer.DiscardAll();

_useFdMode = true;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public Span<byte> GetSpan(int sizeHint = 0)
public ReadOnlyMemory<byte> WrittenMemory => _buffer.ActiveMemory;
public int Capacity => _buffer.Capacity;
public int WrittenCount => _buffer.ActiveLength;
public void Reset() => _buffer.Discard(_buffer.ActiveLength);
public void Reset() => _buffer.DiscardAll();
public void Dispose() => _buffer.Dispose();
}
}
Loading
Loading