Class WebSocketStream
Represents a stream in WebSocket frames over another stream, binary frames for its data and an empty final one for its end, so the other stream can go on to carry the next stream.
public class WebSocketStream : ChunkStream, IAsyncDisposable, IDisposable, IDataStream
- Inheritance
-
WebSocketStream
- Implements
- Inherited Members
Remarks
Its graceful close, DisposeAsync(), ends it with its peer; one that cannot carry the next stream, as its peer ended it or a read, a write or its end failed, closes its source instead.
Constructors
WebSocketStream(Stream, string, bool, bool, bool)
Initializes a new instance of the WebSocketStream class.
public WebSocketStream(Stream sourceStream, string streamId, bool useBuffer, bool isServer, bool useStandardMasking = false)
Parameters
sourceStreamStreamThe stream that carries the frames, which a close leaves open where the stream can carry the next one.
streamIdstringThe stream's ID, which its logs carry.
useBufferbooltrue to read the source through a small read buffer.
isServerbooltrue for the server's side of the stream.
useStandardMaskingbooltrue to mask the client's frames as the WebSocket standard has it.
Properties
CloseStatus
Gets the status the peer's close frame carried, or null where it carried none.
public ushort? CloseStatus { get; }
Property Value
PreserveWriteBufferLength
Gets the length a buffer for WritePreservedAsync(Memory<byte>, CancellationToken) keeps free before its data, for the frame's header.
public int PreserveWriteBufferLength { get; }
Property Value
UsesStandardMasking
Gets a value indicating whether the client's frames are masked as the WebSocket standard has it.
public bool UsesStandardMasking { get; }
Property Value
Methods
Dispose(bool)
Closes the source synchronously, unless left open or closed already, when disposing.
protected override void Dispose(bool disposing)
Parameters
Remarks
It leaves the source to its owner: closing that synchronously waits for a QUIC stream's peer.
DisposeAsync()
Closes the source asynchronously, unless left open, then disposes this stream.
public override ValueTask DisposeAsync()
Returns
- ValueTask
A task that represents the asynchronous operation.
Remarks
It ends the stream with its peer where it can carry the next one, and closes the source where it cannot. A second call waits for the first one's end.
EnableInitialPadding()
Turns on the padding of the stream's first writes, which hides their lengths.
public void EnableInitialPadding()
Exceptions
- ObjectDisposedException
The stream is disposed.
- InvalidOperationException
The stream's masking is not the standard one, or it has written already.
ReadAsync(Memory<byte>, CancellationToken)
Asynchronously reads a sequence of bytes from the current stream, advances the position within the stream by the number of bytes read, and monitors cancellation requests.
public override ValueTask<int> ReadAsync(Memory<byte> buffer, CancellationToken cancellationToken = default)
Parameters
bufferMemory<byte>The region of memory to write the data into.
cancellationTokenCancellationTokenThe token to monitor for cancellation requests. The default value is None.
Returns
- ValueTask<int>
A task that represents the asynchronous read operation. The value of its Result property contains the total number of bytes read into the buffer. The result value can be less than the length of the buffer if that many bytes are not currently available, or it can be 0 (zero) if the length of the buffer is 0 or if the end of the stream has been reached.
Remarks
The source's.
Exceptions
- OperationCanceledException
The cancellation token was canceled. This exception is stored into the returned task.
- ObjectDisposedException
The stream is disposed.
- InvalidOperationException
A frame is longer than the stream reads.
- InvalidDataException
The frames break the WebSocket protocol.
- Exception
The source ended inside a frame.
WriteAsync(ReadOnlyMemory<byte>, CancellationToken)
Asynchronously writes a sequence of bytes to the current stream, advances the current position within this stream by the number of bytes written, and monitors cancellation requests.
public override ValueTask WriteAsync(ReadOnlyMemory<byte> buffer, CancellationToken cancellationToken = default)
Parameters
bufferReadOnlyMemory<byte>The region of memory to write data from.
cancellationTokenCancellationTokenThe token to monitor for cancellation requests. The default value is None.
Returns
- ValueTask
A task that represents the asynchronous write operation.
Remarks
The source's.
Exceptions
- OperationCanceledException
The cancellation token was canceled. This exception is stored into the returned task.
- ObjectDisposedException
The stream is disposed.
WriteClose(ushort, CancellationToken)
Writes a close frame with a status, which ends the connection, not just the stream.
public ValueTask WriteClose(ushort closeStatus, CancellationToken cancellationToken)
Parameters
closeStatusushortThe status.
cancellationTokenCancellationTokenThe token to monitor for cancellation requests.
Returns
- ValueTask
A task that completes once the frame is written; with the connection already closed, at once.
Exceptions
- OperationCanceledException
cancellationTokenwas canceled.- ObjectDisposedException
The stream is disposed.
- InvalidOperationException
The stream is the client's: only the server writes a close status, as a client's frame must be masked.
WritePreservedAsync(Memory<byte>, CancellationToken)
Writes data as one binary frame; with no padding, its header goes in the room the buffer keeps free before the data, so the frame goes out without a copy. With no data, it writes nothing.
public ValueTask WritePreservedAsync(Memory<byte> buffer, CancellationToken cancellationToken)
Parameters
bufferMemory<byte>The buffer: PreserveWriteBufferLength bytes of room, then the data. The write may overwrite both, even when it fails.
cancellationTokenCancellationTokenThe token to monitor for cancellation requests.
Returns
- ValueTask
A task that completes once the write ends.
Exceptions
- OperationCanceledException
cancellationTokenwas canceled.- ObjectDisposedException
The stream is disposed.
- ArgumentException
bufferis shorter than PreserveWriteBufferLength.