Table of Contents

Class WebSocketImplementation

Namespace
Alis.Core.Network.Internal
Assembly
Alis.dll

Main implementation of the WebSocket abstract class

internal sealed class WebSocketImplementation : WebSocket, IDisposable

Inheritance

Implements

Inherited Members

Constructors

WebSocketImplementation(Guid, Func<MemoryStream>, Stream, TimeSpan, string, bool, bool, string)

Initializes a new instance of the Alis.Core.Network.Internal.WebSocketImplementation class

internal WebSocketImplementation(Guid guid, Func<MemoryStream> recycledStreamFactory, Stream stream, TimeSpan keepAliveInterval, string secWebSocketExtensions, bool includeExceptionInCloseResponse, bool isClient, string subProtocol)

Parameters

guid Guid

The guid

recycledStreamFactory Func<MemoryStream>

The recycled stream factory

stream Stream

The stream

keepAliveInterval TimeSpan

The keep alive interval

secWebSocketExtensions string

The sec web socket extensions

includeExceptionInCloseResponse bool

The include exception in close response

isClient bool

The is client

subProtocol string

The sub protocol

Exceptions

InvalidOperationException

KeepAliveInterval must be Zero or positive

Fields

PingPongPayloadLen

The max ping pong payload len

internal const int PingPongPayloadLen = 125

Field Value

int

_closeStatus

The close status

internal WebSocketCloseStatus? _closeStatus

Field Value

WebSocketCloseStatus?

_closeStatusDescription

The close status description

internal string _closeStatusDescription

Field Value

string

_continuationFrameMessageType

The binary

internal WebSocketMessageType _continuationFrameMessageType

Field Value

WebSocketMessageType

_guid

The guid

internal readonly Guid _guid

Field Value

Guid

_includeExceptionInCloseResponse

The include exception in close response

internal readonly bool _includeExceptionInCloseResponse

Field Value

bool

_internalReadCts

The internal read cts

internal readonly CancellationTokenSource _internalReadCts

Field Value

CancellationTokenSource

_isClient

The is client

internal readonly bool _isClient

Field Value

bool

_isContinuationFrame

The is continuation frame

internal bool _isContinuationFrame

Field Value

bool

_readCursor

The read cursor

internal WebSocketReadCursor _readCursor

Field Value

WebSocketReadCursor

_recycledStreamFactory

The recycled stream factory

internal readonly Func<MemoryStream> _recycledStreamFactory

Field Value

Func<MemoryStream>

_semaphore

The semaphore slim

internal readonly SemaphoreSlim _semaphore

Field Value

SemaphoreSlim

_state

The state

internal WebSocketState _state

Field Value

WebSocketState

_stream

The stream

internal readonly Stream _stream

Field Value

Stream

_tryGetBufferFailureLogged

The try get buffer failure logged

internal bool _tryGetBufferFailureLogged

Field Value

bool

_usePerMessageDeflate

The use per message deflate

internal readonly bool _usePerMessageDeflate

Field Value

bool

Properties

CloseStatus

Gets the value of the close status

public override WebSocketCloseStatus? CloseStatus { get; }

Property Value

WebSocketCloseStatus?

CloseStatusDescription

Gets the value of the close status description

public override string CloseStatusDescription { get; }

Property Value

string

KeepAliveInterval

Gets the value of the keep alive interval

public TimeSpan KeepAliveInterval { get; }

Property Value

TimeSpan

State

Gets the value of the state

public override WebSocketState State { get; }

Property Value

WebSocketState

SubProtocol

Gets the value of the sub protocol

public override string SubProtocol { get; }

Property Value

string

Methods

Abort()

Aborts the WebSocket without sending a Close frame

public override void Abort()

BuildClosePayload(WebSocketCloseStatus, string)

As per the spec, write the close status followed by the close reason

internal ArraySegment<byte> BuildClosePayload(WebSocketCloseStatus closeStatus, string statusDescription)

Parameters

closeStatus WebSocketCloseStatus

The close status

statusDescription string

Optional extra close details

Returns

ArraySegment<byte>

The payload to sent in the close frame

CloseAsync(WebSocketCloseStatus, string, CancellationToken)

Closes the close status

public override Task CloseAsync(WebSocketCloseStatus closeStatus, string statusDescription, CancellationToken cancellationToken)

Parameters

closeStatus WebSocketCloseStatus

The close status

statusDescription string

The status description

cancellationToken CancellationToken

The cancellation token

Returns

Task

CloseOutputAsync(WebSocketCloseStatus, string, CancellationToken)

Closes the output using the specified close status

public override Task CloseOutputAsync(WebSocketCloseStatus closeStatus, string statusDescription, CancellationToken cancellationToken)

Parameters

closeStatus WebSocketCloseStatus

The close status

statusDescription string

The status description

cancellationToken CancellationToken

The cancellation token

Returns

Task

CloseOutputAutoTimeoutAsync(WebSocketCloseStatus, string, Exception)

Automatic WebSocket close in response to some invalid data from the remote websocket host

internal Task CloseOutputAutoTimeoutAsync(WebSocketCloseStatus closeStatus, string statusDescription, Exception ex)

Parameters

closeStatus WebSocketCloseStatus

The close status to use

statusDescription string

A description of why we are closing

ex Exception

The exception (for logging)

Returns

Task

Dispose()

Dispose will send a close frame if the connection is still open

public override void Dispose()

GetBuffer(MemoryStream)

Note that the way in which the stream buffer is accessed can lead to significant performance problems You want to avoid a call to stream.ToArray to avoid extra memory allocation MemoryStream can be configured to have its internal buffer accessible.

internal ArraySegment<byte> GetBuffer(MemoryStream stream)

Parameters

stream MemoryStream

Returns

ArraySegment<byte>

GetOppCode(WebSocketMessageType)

Turns a spec websocket frame opcode into a WebSocketMessageType

internal WebSocketOpCode GetOppCode(WebSocketMessageType messageType)

Parameters

messageType WebSocketMessageType

Returns

WebSocketOpCode

HandleBinaryFrame(WebSocketFrame, bool)

Handles the binary frame using the specified frame

internal WebSocketReceiveResult HandleBinaryFrame(WebSocketFrame frame, bool endOfMessage)

Parameters

frame WebSocketFrame

The frame

endOfMessage bool

The end of message

Returns

WebSocketReceiveResult

The web socket receive result

HandleConnectionClose(WebSocketFrame, ArraySegment<byte>, CancellationToken)

Handles the connection close using the specified frame

internal Task<WebSocketReceiveResult> HandleConnectionClose(WebSocketFrame frame, ArraySegment<byte> buffer, CancellationToken token)

Parameters

frame WebSocketFrame

The frame

buffer ArraySegment<byte>

The buffer

token CancellationToken

The token

Returns

Task<WebSocketReceiveResult>

A task containing the web socket receive result

HandleContinuationFrame(WebSocketFrame, bool)

Handles the continuation frame using the specified frame

internal WebSocketReceiveResult HandleContinuationFrame(WebSocketFrame frame, bool endOfMessage)

Parameters

frame WebSocketFrame

The frame

endOfMessage bool

The end of message

Returns

WebSocketReceiveResult

The web socket receive result

HandleDefault(WebSocketFrame)

Handles the default using the specified frame

internal Task<WebSocketReceiveResult> HandleDefault(WebSocketFrame frame)

Parameters

frame WebSocketFrame

The frame

Returns

Task<WebSocketReceiveResult>

A task containing the web socket receive result

HandleExceptions(Exception)

Handles the exceptions using the specified catch all

internal Task<WebSocketReceiveResult> HandleExceptions(Exception catchAll)

Parameters

catchAll Exception

The catch all

Returns

Task<WebSocketReceiveResult>

A task containing the web socket receive result

HandlePing(WebSocketFrame, ArraySegment<byte>, CancellationTokenSource)

Handles the ping using the specified frame

internal Task<WebSocketReceiveResult> HandlePing(WebSocketFrame frame, ArraySegment<byte> buffer, CancellationTokenSource linkedCts)

Parameters

frame WebSocketFrame

The frame

buffer ArraySegment<byte>

The buffer

linkedCts CancellationTokenSource

The linked cts

Returns

Task<WebSocketReceiveResult>

A task containing the web socket receive result

HandlePong(WebSocketFrame, ArraySegment<byte>)

Handles the pong using the specified frame

internal WebSocketReceiveResult HandlePong(WebSocketFrame frame, ArraySegment<byte> buffer)

Parameters

frame WebSocketFrame

The frame

buffer ArraySegment<byte>

The buffer

Returns

WebSocketReceiveResult

The web socket receive result

HandleTextFrame(WebSocketFrame, bool)

Handles the text frame using the specified frame

internal WebSocketReceiveResult HandleTextFrame(WebSocketFrame frame, bool endOfMessage)

Parameters

frame WebSocketFrame

The frame

endOfMessage bool

The end of message

Returns

WebSocketReceiveResult

The web socket receive result

HandleWebSocketOpCodes(WebSocketFrame, ArraySegment<byte>, CancellationTokenSource, bool)

Handles the web socket op codes using the specified frame

internal Task<WebSocketReceiveResult> HandleWebSocketOpCodes(WebSocketFrame frame, ArraySegment<byte> buffer, CancellationTokenSource linkedCts, bool endOfMessage)

Parameters

frame WebSocketFrame

The frame

buffer ArraySegment<byte>

The buffer

linkedCts CancellationTokenSource

The linked cts

endOfMessage bool

The end of message

Returns

Task<WebSocketReceiveResult>

A task containing the web socket receive result

OnPong(PongEventArgs)

Called when a Pong frame is received

internal void OnPong(PongEventArgs e)

Parameters

e PongEventArgs

ReadWebSocketFrame(ArraySegment<byte>, CancellationToken)

Reads the web socket frame using the specified buffer

internal Task<WebSocketFrame> ReadWebSocketFrame(ArraySegment<byte> buffer, CancellationToken cancellationToken)

Parameters

buffer ArraySegment<byte>

The buffer

cancellationToken CancellationToken

The cancellation token

Returns

Task<WebSocketFrame>

A task containing the web socket frame

ReceiveAsync(ArraySegment<byte>, CancellationToken)

Receives the buffer

public override Task<WebSocketReceiveResult> ReceiveAsync(ArraySegment<byte> buffer, CancellationToken cancellationToken)

Parameters

buffer ArraySegment<byte>

The buffer

cancellationToken CancellationToken

The cancellation token

Returns

Task<WebSocketReceiveResult>

A task containing the web socket receive result

RespondToCloseFrame(WebSocketFrame, ArraySegment<byte>, CancellationToken)

Called when a Close frame is received Send a response close frame if applicable

internal Task<WebSocketReceiveResult> RespondToCloseFrame(WebSocketFrame frame, ArraySegment<byte> buffer, CancellationToken token)

Parameters

frame WebSocketFrame
buffer ArraySegment<byte>
token CancellationToken

Returns

Task<WebSocketReceiveResult>

SendAsync(ArraySegment<byte>, WebSocketMessageType, bool, CancellationToken)

Send data to the web socket

public override Task SendAsync(ArraySegment<byte> buffer, WebSocketMessageType messageType, bool endOfMessage, CancellationToken cancellationToken)

Parameters

buffer ArraySegment<byte>

the buffer containing data to send

messageType WebSocketMessageType

The message type. Can be Text or Binary

endOfMessage bool

True if this message is a standalone message (this is the norm) If it is a multi-part message then false (and true for the last message)

cancellationToken CancellationToken

the cancellation token

Returns

Task

SendPingAsync(ArraySegment<byte>, CancellationToken)

Sends the ping using the specified payload

public Task SendPingAsync(ArraySegment<byte> payload, CancellationToken cancellationToken)

Parameters

payload ArraySegment<byte>

The payload

cancellationToken CancellationToken

The cancellation token

Returns

Task

Exceptions

InvalidOperationException

Cannot send Ping: Max ping message size {PingPongPayloadLen} exceeded: {payload.Count}

SendPongAsync(ArraySegment<byte>, CancellationToken)

Sends the pong using the specified payload

internal Task SendPongAsync(ArraySegment<byte> payload, CancellationToken cancellationToken)

Parameters

payload ArraySegment<byte>

The payload

cancellationToken CancellationToken

The cancellation token

Returns

Task

SendPongFrame(ArraySegment<byte>, CancellationToken)

Sends the pong frame using the specified payload

internal Task SendPongFrame(ArraySegment<byte> payload, CancellationToken cancellationToken)

Parameters

payload ArraySegment<byte>

The payload

cancellationToken CancellationToken

The cancellation token

Returns

Task

ValidatePayloadSize(ArraySegment<byte>)

Validates the payload size using the specified payload

internal void ValidatePayloadSize(ArraySegment<byte> payload)

Parameters

payload ArraySegment<byte>

The payload

Exceptions

InvalidOperationException

Max ping message size {PingPongPayloadLen} exceeded: {payload.Count}

WriteStreamToNetwork(MemoryStream, CancellationToken)

Puts data on the wire

internal Task WriteStreamToNetwork(MemoryStream stream, CancellationToken cancellationToken)

Parameters

stream MemoryStream

The stream to read data from

cancellationToken CancellationToken

Returns

Task

Pong

public event EventHandler<PongEventArgs> Pong

Event Type

EventHandler<PongEventArgs>