System.Net.WebSockets.WebSocketValidate.ValidateCloseStatus C# (CSharp) Method

ValidateCloseStatus() static private method

static private ValidateCloseStatus ( WebSocketCloseStatus closeStatus, string statusDescription ) : void
closeStatus WebSocketCloseStatus
statusDescription string
return void
        internal static void ValidateCloseStatus(WebSocketCloseStatus closeStatus, string statusDescription)
        {
            if (closeStatus == WebSocketCloseStatus.Empty && !string.IsNullOrEmpty(statusDescription))
            {
                throw new ArgumentException(SR.Format(SR.net_WebSockets_ReasonNotNull,
                    statusDescription,
                    WebSocketCloseStatus.Empty),
                    nameof(statusDescription));
            }

            int closeStatusCode = (int)closeStatus;

            if ((closeStatusCode >= InvalidCloseStatusCodesFrom &&
                closeStatusCode <= InvalidCloseStatusCodesTo) ||
                closeStatusCode == CloseStatusCodeAbort ||
                closeStatusCode == CloseStatusCodeFailedTLSHandshake)
            {
                // CloseStatus 1006 means Aborted - this will never appear on the wire and is reflected by calling WebSocket.Abort
                throw new ArgumentException(SR.Format(SR.net_WebSockets_InvalidCloseStatusCode,
                    closeStatusCode),
                    nameof(closeStatus));
            }

            int length = 0;
            if (!string.IsNullOrEmpty(statusDescription))
            {
                length = Encoding.UTF8.GetByteCount(statusDescription);
            }

            if (length > MaxControlFramePayloadLength)
            {
                throw new ArgumentException(SR.Format(SR.net_WebSockets_InvalidCloseStatusDescription,
                    statusDescription,
                    MaxControlFramePayloadLength),
                    nameof(statusDescription));
            }
        }

Usage Example

示例#1
0
 public override Task CloseOutputAsync(WebSocketCloseStatus closeStatus, string statusDescription,
                                       CancellationToken cancellationToken)
 {
     ThrowIfNotConnected();
     WebSocketValidate.ValidateCloseStatus(closeStatus, statusDescription);
     return(_innerWebSocket.CloseOutputAsync(closeStatus, statusDescription, cancellationToken));
 }
All Usage Examples Of System.Net.WebSockets.WebSocketValidate::ValidateCloseStatus