Class WebSocketImplementation
- Namespace
- Alis.Core.Network.Internal
- Assembly
- Alis.dll
Main implementation of the WebSocket abstract class
internal sealed class WebSocketImplementation : WebSocket, IDisposableInheritance
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
guidGuid-
The guid
recycledStreamFactoryFunc<MemoryStream>-
The recycled stream factory
streamStream-
The stream
keepAliveIntervalTimeSpan-
The keep alive interval
secWebSocketExtensionsstring-
The sec web socket extensions
includeExceptionInCloseResponsebool-
The include exception in close response
isClientbool-
The is client
subProtocolstring-
The sub protocol
Exceptions
- InvalidOperationException
-
KeepAliveInterval must be Zero or positive
Fields
PingPongPayloadLen
The max ping pong payload len
internal const int PingPongPayloadLen = 125Field Value
_closeStatus
The close status
internal WebSocketCloseStatus? _closeStatusField Value
_closeStatusDescription
The close status description
internal string _closeStatusDescriptionField Value
_continuationFrameMessageType
The binary
internal WebSocketMessageType _continuationFrameMessageTypeField Value
_guid
The guid
internal readonly Guid _guidField Value
_includeExceptionInCloseResponse
The include exception in close response
internal readonly bool _includeExceptionInCloseResponseField Value
_internalReadCts
The internal read cts
internal readonly CancellationTokenSource _internalReadCtsField Value
_isClient
The is client
internal readonly bool _isClientField Value
_isContinuationFrame
The is continuation frame
internal bool _isContinuationFrameField Value
_readCursor
The read cursor
internal WebSocketReadCursor _readCursorField Value
_recycledStreamFactory
The recycled stream factory
internal readonly Func<MemoryStream> _recycledStreamFactoryField Value
_semaphore
The semaphore slim
internal readonly SemaphoreSlim _semaphoreField Value
_state
The state
internal WebSocketState _stateField Value
_stream
The stream
internal readonly Stream _streamField Value
_tryGetBufferFailureLogged
The try get buffer failure logged
internal bool _tryGetBufferFailureLoggedField Value
_usePerMessageDeflate
The use per message deflate
internal readonly bool _usePerMessageDeflateField Value
Properties
CloseStatus
Gets the value of the close status
public override WebSocketCloseStatus? CloseStatus { get; }Property Value
CloseStatusDescription
Gets the value of the close status description
public override string CloseStatusDescription { get; }Property Value
KeepAliveInterval
Gets the value of the keep alive interval
public TimeSpan KeepAliveInterval { get; }Property Value
State
Gets the value of the state
public override WebSocketState State { get; }Property Value
SubProtocol
Gets the value of the sub protocol
public override string SubProtocol { get; }Property Value
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
closeStatusWebSocketCloseStatus-
The close status
statusDescriptionstring-
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
closeStatusWebSocketCloseStatus-
The close status
statusDescriptionstring-
The status description
cancellationTokenCancellationToken-
The cancellation token
Returns
CloseOutputAsync(WebSocketCloseStatus, string, CancellationToken)
Closes the output using the specified close status
public override Task CloseOutputAsync(WebSocketCloseStatus closeStatus, string statusDescription, CancellationToken cancellationToken)Parameters
closeStatusWebSocketCloseStatus-
The close status
statusDescriptionstring-
The status description
cancellationTokenCancellationToken-
The cancellation token
Returns
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
closeStatusWebSocketCloseStatus-
The close status to use
statusDescriptionstring-
A description of why we are closing
exException-
The exception (for logging)
Returns
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
streamMemoryStream
Returns
GetOppCode(WebSocketMessageType)
Turns a spec websocket frame opcode into a WebSocketMessageType
internal WebSocketOpCode GetOppCode(WebSocketMessageType messageType)Parameters
messageTypeWebSocketMessageType
Returns
HandleBinaryFrame(WebSocketFrame, bool)
Handles the binary frame using the specified frame
internal WebSocketReceiveResult HandleBinaryFrame(WebSocketFrame frame, bool endOfMessage)Parameters
frameWebSocketFrame-
The frame
endOfMessagebool-
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
frameWebSocketFrame-
The frame
bufferArraySegment<byte>-
The buffer
tokenCancellationToken-
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
frameWebSocketFrame-
The frame
endOfMessagebool-
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
frameWebSocketFrame-
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
catchAllException-
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
frameWebSocketFrame-
The frame
bufferArraySegment<byte>-
The buffer
linkedCtsCancellationTokenSource-
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
frameWebSocketFrame-
The frame
bufferArraySegment<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
frameWebSocketFrame-
The frame
endOfMessagebool-
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
frameWebSocketFrame-
The frame
bufferArraySegment<byte>-
The buffer
linkedCtsCancellationTokenSource-
The linked cts
endOfMessagebool-
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
ReadWebSocketFrame(ArraySegment<byte>, CancellationToken)
Reads the web socket frame using the specified buffer
internal Task<WebSocketFrame> ReadWebSocketFrame(ArraySegment<byte> buffer, CancellationToken cancellationToken)Parameters
bufferArraySegment<byte>-
The buffer
cancellationTokenCancellationToken-
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
bufferArraySegment<byte>-
The buffer
cancellationTokenCancellationToken-
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
frameWebSocketFramebufferArraySegment<byte>tokenCancellationToken
Returns
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
bufferArraySegment<byte>-
the buffer containing data to send
messageTypeWebSocketMessageType-
The message type. Can be Text or Binary
endOfMessagebool-
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)
cancellationTokenCancellationToken-
the cancellation token
Returns
SendPingAsync(ArraySegment<byte>, CancellationToken)
Sends the ping using the specified payload
public Task SendPingAsync(ArraySegment<byte> payload, CancellationToken cancellationToken)Parameters
payloadArraySegment<byte>-
The payload
cancellationTokenCancellationToken-
The cancellation token
Returns
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
payloadArraySegment<byte>-
The payload
cancellationTokenCancellationToken-
The cancellation token
Returns
SendPongFrame(ArraySegment<byte>, CancellationToken)
Sends the pong frame using the specified payload
internal Task SendPongFrame(ArraySegment<byte> payload, CancellationToken cancellationToken)Parameters
payloadArraySegment<byte>-
The payload
cancellationTokenCancellationToken-
The cancellation token
Returns
ValidatePayloadSize(ArraySegment<byte>)
Validates the payload size using the specified payload
internal void ValidatePayloadSize(ArraySegment<byte> payload)Parameters
payloadArraySegment<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
streamMemoryStream-
The stream to read data from
cancellationTokenCancellationToken
Returns
Pong
public event EventHandler<PongEventArgs> Pong