C# Class System.Net.WebSockets.ManagedWebSocket

A managed implementation of a web socket that sends and receives data via a Stream.
Thread-safety: - It's acceptable to call ReceiveAsync and SendAsync in parallel. One of each may run concurrently. - It's acceptable to have a pending ReceiveAsync while CloseOutputAsync or CloseAsync is called. - Attemping to invoke any other operations in parallel may corrupt the instance. Attempting to invoke a send operation while another is in progress or a receive operation while another is in progress will result in an exception.
Inheritance: WebSocket
显示文件 Open project: dotnet/corefx Class Usage Examples

Private Properties

Property Type Description
ApplyMask int
ApplyMask int
CloseAsyncPrivate Task
CloseWithReceiveErrorAndThrowAsync Task
CombineMaskBytes int
ConsumeFromBuffer void
CreateOperationCanceledException Exception
DisposeCore void
EnsureBufferContainsAsync Task
EnsureBufferLength void
HandleReceivedCloseAsync Task
HandleReceivedPingPongAsync Task
IsValidCloseStatus bool
ManagedWebSocket System.Diagnostics
ReceiveAsyncPrivate Task
SendCloseFrameAsync Task
SendFrameAsync Task
SendFrameFallbackAsync Task
SendFrameLockAcquiredNonCancelableAsync Task
SendKeepAliveFrameAsync void
ThrowIfOperationInProgress void
TryParseMessageHeaderFromReceiveBuffer bool
TryValidateUtf8 bool
WriteFrameToSendBuffer int
WriteHeader int
WriteRandomMask void

Public Methods

Method Description
Abort ( ) : void
CloseAsync ( WebSocketCloseStatus closeStatus, string statusDescription, CancellationToken cancellationToken ) : Task
CloseOutputAsync ( WebSocketCloseStatus closeStatus, string statusDescription, CancellationToken cancellationToken ) : Task
CreateFromConnectedStream ( Stream stream, bool isServer, string subprotocol, TimeSpan keepAliveInterval, int receiveBufferSize, ArraySegment receiveBuffer = null ) : ManagedWebSocket

Creates a ManagedWebSocket from a Stream connected to a websocket endpoint.

Dispose ( ) : void
ReceiveAsync ( ArraySegment buffer, CancellationToken cancellationToken ) : Task
SendAsync ( ArraySegment buffer, WebSocketMessageType messageType, bool endOfMessage, CancellationToken cancellationToken ) : Task

Private Methods

Method Description
ApplyMask ( byte toMask, int toMaskOffset, byte mask, int maskOffset, int maskOffsetIndex, long count ) : int

Applies a mask to a portion of a byte array.

ApplyMask ( byte toMask, int toMaskOffset, int mask, int maskIndex, long count ) : int

Applies a mask to a portion of a byte array.

CloseAsyncPrivate ( WebSocketCloseStatus closeStatus, string statusDescription, CancellationToken cancellationToken ) : Task

Send a close message, then receive until we get a close response message.

CloseWithReceiveErrorAndThrowAsync ( WebSocketCloseStatus closeStatus, WebSocketError error, CancellationToken cancellationToken, Exception innerException = null ) : Task

Send a close message to the server and throw an exception, in response to getting bad data from the server.

CombineMaskBytes ( byte buffer, int maskOffset ) : int
ConsumeFromBuffer ( int count ) : void
CreateOperationCanceledException ( Exception innerException, CancellationToken cancellationToken = default(CancellationToken) ) : Exception

Creates an OperationCanceledException instance, using a default message and the specified inner exception and token.

DisposeCore ( ) : void
EnsureBufferContainsAsync ( int minimumRequiredBytes, CancellationToken cancellationToken, bool throwOnPrematureClosure = true ) : Task
EnsureBufferLength ( byte &buffer, int minLength ) : void

Grows the specified buffer if it's not at least the specified minimum length. Data is not copied if the buffer is grown.

HandleReceivedCloseAsync ( MessageHeader header, CancellationToken cancellationToken ) : Task

Processes a received close message.

HandleReceivedPingPongAsync ( MessageHeader header, CancellationToken cancellationToken ) : Task

Processes a received ping or pong message.

IsValidCloseStatus ( WebSocketCloseStatus closeStatus ) : bool

Check whether a close status is valid according to the RFC.

ManagedWebSocket ( Stream stream, bool isServer, string subprotocol, TimeSpan keepAliveInterval, int receiveBufferSize, ArraySegment receiveBuffer ) : System.Diagnostics

Initializes the websocket.

ReceiveAsyncPrivate ( ArraySegment payloadBuffer, CancellationToken cancellationToken ) : Task

Receive the next text, binary, continuation, or close message, returning information about it and writing its payload into the supplied buffer. Other control messages may be consumed and processed as part of this operation, but data about them will not be returned.

SendCloseFrameAsync ( WebSocketCloseStatus closeStatus, string closeStatusDescription, CancellationToken cancellationToken ) : Task

Sends a close message to the server.

SendFrameAsync ( MessageOpcode opcode, bool endOfMessage, ArraySegment payloadBuffer, CancellationToken cancellationToken ) : Task

Sends a websocket frame to the network.

SendFrameFallbackAsync ( MessageOpcode opcode, bool endOfMessage, ArraySegment payloadBuffer, CancellationToken cancellationToken ) : Task
SendFrameLockAcquiredNonCancelableAsync ( MessageOpcode opcode, bool endOfMessage, ArraySegment payloadBuffer ) : Task

Sends a websocket frame to the network. The caller must hold the sending lock.

SendKeepAliveFrameAsync ( ) : void
ThrowIfOperationInProgress ( Task operationTask, [ methodName = null ) : void

Aborts the websocket and throws an exception if an existing operation is in progress.

TryParseMessageHeaderFromReceiveBuffer ( MessageHeader &resultHeader ) : bool

Parses a message header from the buffer. This assumes the header is in the buffer.

TryValidateUtf8 ( ArraySegment arraySegment, bool endOfMessage, Utf8MessageState state ) : bool
WriteFrameToSendBuffer ( MessageOpcode opcode, bool endOfMessage, ArraySegment payloadBuffer ) : int

Writes a frame into the send buffer, which can then be sent over the network.

WriteHeader ( MessageOpcode opcode, byte sendBuffer, ArraySegment payload, bool endOfMessage, bool useMask ) : int
WriteRandomMask ( byte buffer, int offset ) : void

Writes a 4-byte random mask to the specified buffer at the specified offset.

Method Details

Abort() public method

public Abort ( ) : void
return void

CloseAsync() public method

public CloseAsync ( WebSocketCloseStatus closeStatus, string statusDescription, CancellationToken cancellationToken ) : Task
closeStatus WebSocketCloseStatus
statusDescription string
cancellationToken System.Threading.CancellationToken
return Task

CloseOutputAsync() public method

public CloseOutputAsync ( WebSocketCloseStatus closeStatus, string statusDescription, CancellationToken cancellationToken ) : Task
closeStatus WebSocketCloseStatus
statusDescription string
cancellationToken System.Threading.CancellationToken
return Task

CreateFromConnectedStream() public static method

Creates a ManagedWebSocket from a Stream connected to a websocket endpoint.
public static CreateFromConnectedStream ( Stream stream, bool isServer, string subprotocol, TimeSpan keepAliveInterval, int receiveBufferSize, ArraySegment receiveBuffer = null ) : ManagedWebSocket
stream System.IO.Stream The connected Stream.
isServer bool true if this is the server-side of the connection; false if this is the client-side of the connection.
subprotocol string The agreed upon subprotocol for the connection.
keepAliveInterval TimeSpan The interval to use for keep-alive pings.
receiveBufferSize int The buffer size to use for received data.
receiveBuffer ArraySegment Optional buffer to use for receives.
return ManagedWebSocket

Dispose() public method

public Dispose ( ) : void
return void

ReceiveAsync() public method

public ReceiveAsync ( ArraySegment buffer, CancellationToken cancellationToken ) : Task
buffer ArraySegment
cancellationToken System.Threading.CancellationToken
return Task

SendAsync() public method

public SendAsync ( ArraySegment buffer, WebSocketMessageType messageType, bool endOfMessage, CancellationToken cancellationToken ) : Task
buffer ArraySegment
messageType WebSocketMessageType
endOfMessage bool
cancellationToken System.Threading.CancellationToken
return Task