Class MessageBuffer
- Namespace
- WebDriverBiDi.Protocol
- Assembly
- WebDriverBiDi.dll
Accumulates the pieces of a message that arrives in more than one read (WebSocket frames, or pipe reads that do not end on a message terminator) in memory rented from Shared, so that the completed message can be handed to an IncomingMessage as-is rather than being copied into a second buffer.
public sealed class MessageBuffer : IDisposable
- Inheritance
-
MessageBuffer
- Implements
- Inherited Members
Remarks
Ownership of the rented memory transfers to the caller of TakeOwnership(out int); after that call the buffer is empty and may be reused for the next message. Discard() returns partially accumulated data to the pool (for example when a fragmented message is abandoned) and likewise leaves the accumulator ready for reuse. Disposing the buffer discards any data it still holds.
This class is not thread-safe; it is intended to be owned by a single receive loop. It is public so that custom Connection implementations can reuse it, and so that it can be tested directly; most users will never need it.
Constructors
MessageBuffer()
public MessageBuffer()
Properties
HasData
Gets a value indicating whether any bytes have been accumulated.
public bool HasData { get; }
Property Value
Length
Gets the number of bytes accumulated so far.
public int Length { get; }
Property Value
Methods
Append(ReadOnlySpan<byte>)
Appends a piece of the message to the accumulated data, growing the pooled buffer if needed. Empty pieces are ignored and do not start an accumulation.
public void Append(ReadOnlySpan<byte> fragment)
Parameters
fragmentReadOnlySpan<byte>The bytes to append.
Discard()
Returns any partially accumulated data to the pool and resets the accumulator so it can be reused. Calling this method when nothing has been accumulated is a no-op.
public void Discard()
Dispose()
Discards any data still held by this accumulator.
public void Dispose()
TakeOwnership(out int)
Transfers ownership of the accumulated message to the caller and resets the accumulator so it can be reused for the next message.
public IMemoryOwner<byte> TakeOwnership(out int messageLength)
Parameters
messageLengthintWhen this method returns, contains the length of the accumulated message in bytes.
Returns
- IMemoryOwner<byte>
The IMemoryOwner<T> holding the message. Its Memory may be longer than
messageLength; the caller must dispose it to return the memory to the pool.
Exceptions
- InvalidOperationException
Thrown when no data has been accumulated.