public static bool TryParse(byte[] buffer, int offset, out WebSocketFrameHeaderFlags headerFlags) { headerFlags = null; if (buffer == null || buffer.Length - offset < 2) { return(false); } var optionByte = buffer[offset]; SetBit(ref optionByte, 7, false); SetBit(ref optionByte, 6, false); SetBit(ref optionByte, 5, false); SetBit(ref optionByte, 4, false); var options = (WebSocketFrameOption)optionByte; options = options > (WebSocketFrameOption)128 ? options - 128 : options; if (EnumHelper <WebSocketFrameOption> .IsDefined(options) == false) { return(false); } headerFlags = new WebSocketFrameHeaderFlags(buffer[offset], buffer[offset + 1], options); if (options > WebSocketFrameOption.Binary) { headerFlags.FIN = true; // control frames is always final } return(true); }
public static bool TryParse(byte[] buffer, int offset, out WebSocketFrameHeaderFlags headerFlags) { headerFlags = null; if (buffer == null || buffer.Length - offset < 2) { return(false); } var optionByte = buffer[0]; SetBit(ref optionByte, 7, false); SetBit(ref optionByte, 6, false); SetBit(ref optionByte, 5, false); SetBit(ref optionByte, 4, false); int value = optionByte; value = value > 128 ? value - 128 : value; if (!Enum.IsDefined(typeof(WebSocketFrameOption), value)) { return(false); } headerFlags = new WebSocketFrameHeaderFlags(buffer[0], buffer[1], (WebSocketFrameOption)value); return(true); }
public static WebSocketFrameHeader Create(Int64 count, Boolean isComplete, Boolean headerSent, WebSocketFrameOption option, WebSocketExtensionFlags extensionFlags) { var flags = new WebSocketFrameHeaderFlags(isComplete, headerSent ? WebSocketFrameOption.Continuation : option, extensionFlags); Int32 headerLength; if (count <= 125) { headerLength = 2; } else if (count < UInt16.MaxValue) { headerLength = 4; } else if ((UInt64)count < UInt64.MaxValue) { headerLength = 10; } else { throw new WebSocketException("Cannot create a header with a length of " + count); } return(new WebSocketFrameHeader() { HeaderLength = headerLength, ContentLength = (Int64)count, Flags = flags, RemainingBytes = (Int64)count }); }
public static WebSocketFrameHeader Create(long count, bool isComplete, bool headerSent, WebSocketFrameOption option, WebSocketExtensionFlags extensionFlags) { var flags = new WebSocketFrameHeaderFlags(isComplete, headerSent ? WebSocketFrameOption.Continuation : option, extensionFlags); int headerLength; if (count <= 125) { headerLength = 2; } else if (count <= ushort.MaxValue) { headerLength = 4; } else if ((ulong)count < ulong.MaxValue) { headerLength = 10; } else { throw new WebSocketException("Cannot create a header with a length of " + count); } return(new WebSocketFrameHeader() { HeaderLength = headerLength, ContentLength = count, Flags = flags, RemainingBytes = count }); }
private WebSocketExtensionFlags GetExtensionFlags(WebSocketFrameHeaderFlags webSocketFrameHeaderFlags) { var flags = new WebSocketExtensionFlags(); flags.Rsv1 = webSocketFrameHeaderFlags.RSV1; flags.Rsv2 = webSocketFrameHeaderFlags.RSV2; flags.Rsv3 = webSocketFrameHeaderFlags.RSV3; return flags; }
private WebSocketExtensionFlags GetExtensionFlags(WebSocketFrameHeaderFlags webSocketFrameHeaderFlags) { var flags = new WebSocketExtensionFlags(); flags.Rsv1 = webSocketFrameHeaderFlags.RSV1; flags.Rsv2 = webSocketFrameHeaderFlags.RSV2; flags.Rsv3 = webSocketFrameHeaderFlags.RSV3; return(flags); }
public static Boolean TryParse(Byte[] buffer, Int32 offset, out WebSocketFrameHeaderFlags headerFlags) { headerFlags = null; if (buffer == null || buffer.Length - offset < 2) return false; var optionByte = buffer[0]; SetBit(ref optionByte, 7, false); SetBit(ref optionByte, 6, false); SetBit(ref optionByte, 5, false); SetBit(ref optionByte, 4, false); Int32 value = optionByte; value = value > 128 ? value - 128 : value; if (!Enum.IsDefined(typeof(WebSocketFrameOption), value)) return false; headerFlags = new WebSocketFrameHeaderFlags(buffer[0],buffer[1],(WebSocketFrameOption)value); return true; }
public static WebSocketFrameHeader Create(long contentLength, bool isComplete, bool headerSent, uint maskKey, WebSocketFrameOption option, WebSocketExtensionFlags extensionFlags) { if (extensionFlags == null) { throw new ArgumentNullException(nameof(extensionFlags)); } var isMasked = maskKey != 0; var flags = new WebSocketFrameHeaderFlags(isComplete, isMasked, headerSent ? WebSocketFrameOption.Continuation : option, extensionFlags); int headerLength; if (contentLength <= 125) { headerLength = 2; } else if (contentLength <= ushort.MaxValue) { headerLength = 4; } else { headerLength = 10; } if (isMasked) { headerLength += 4; } return(new WebSocketFrameHeader { HeaderLength = headerLength, ContentLength = contentLength, Flags = flags, RemainingBytes = contentLength, MaskKey = maskKey }); }
public static WebSocketFrameHeader Create(Int32 count, Boolean isComplete, Boolean headerSent, WebSocketFrameOption option, WebSocketExtensionFlags extensionFlags) { var flags = new WebSocketFrameHeaderFlags(isComplete, headerSent ? WebSocketFrameOption.Continuation : option, extensionFlags); Int32 headerLength; if (count <= 125) headerLength = 2; else if (count < UInt16.MaxValue) headerLength = 4; else if ((UInt64)count < UInt64.MaxValue) headerLength = 10; else throw new WebSocketException("Cannot create a header with a length of " + count); return new WebSocketFrameHeader() { HeaderLength = headerLength, ContentLength = (UInt64)count, Flags = flags, RemainingBytes = (UInt64)count }; }
public static bool TryParse(byte[] frameStart, int offset, int headerLength, out WebSocketFrameHeader header) { if (frameStart == null) { throw new ArgumentNullException(nameof(frameStart)); } header = null; var availableLength = frameStart.Length - offset; if (availableLength < 6 || availableLength < headerLength) { return(false); } var value = frameStart[offset + 1]; var contentLength = (long)(value >= 128 ? value - 128 : value); if (contentLength <= 125) { // small frame } else if (contentLength == 126) { if (availableLength < headerLength) { return(false); } contentLength = EndianBitConverter.ToUInt16Be(frameStart, offset + 2); } else if (contentLength == 127) { if (availableLength < headerLength) { return(false); } var length = EndianBitConverter.ToUInt64Be(frameStart, offset + 2); if (length > long.MaxValue) { throw new WebSocketException("The maximum supported frame length is 9223372036854775807, current frame is " + length.ToString()); } contentLength = (long)length; } else { return(false); } WebSocketFrameHeaderFlags flags; if (!WebSocketFrameHeaderFlags.TryParse(frameStart, offset, out flags)) { return(false); } var maskKey = 0U; if (flags.MASK) { headerLength -= 4; maskKey = EndianBitConverter.ToUInt32Le(frameStart, offset + headerLength); } header = new WebSocketFrameHeader { ContentLength = contentLength, HeaderLength = headerLength, Flags = flags, RemainingBytes = contentLength, MaskKey = maskKey }; return(true); }
public static Boolean TryParse(Byte[] frameStart, Int32 offset, Int32 headerLength, ArraySegment <Byte> keySegment, out WebSocketFrameHeader header) { header = null; if (frameStart == null || frameStart.Length < 6 || frameStart.Length < (offset + headerLength)) { return(false); } Int32 value = frameStart[offset + 1]; Int64 contentLength = (Int64)(value >= 128?value - 128:value); if (contentLength <= 125) { // small frame } else if (contentLength == 126) { if (frameStart.Length < headerLength) { return(false); } if (BitConverter.IsLittleEndian) { frameStart.ReversePortion(offset + 2, 2); } contentLength = BitConverter.ToUInt16(frameStart, 2); } else if (contentLength == 127) { if (frameStart.Length < headerLength) { return(false); } if (BitConverter.IsLittleEndian) { frameStart.ReversePortion(offset + 2, 8); } UInt64 length = BitConverter.ToUInt64(frameStart, 2); if (length > Int64.MaxValue) { throw new WebSocketException("The maximum supported frame length is 9223372036854775807, current frame is " + length.ToString()); } contentLength = (Int64)length; } else { return(false); } WebSocketFrameHeaderFlags flags; if (WebSocketFrameHeaderFlags.TryParse(frameStart, offset, out flags)) { header = new WebSocketFrameHeader(keySegment) { ContentLength = contentLength, HeaderLength = headerLength, Flags = flags, RemainingBytes = contentLength }; if (flags.MASK) { headerLength -= 4; for (int i = 0; i < 4; i++) { header._key.Array[header._key.Offset + i] = frameStart[offset + i + headerLength]; } } return(true); } return(false); }
public static Boolean TryParse(Byte[] frameStart, Int32 offset, Int32 headerLength, ArraySegment <Byte> keySegment, out WebSocketFrameHeader header) { header = null; if (frameStart == null || frameStart.Length < 6 || frameStart.Length < (offset + headerLength)) { return(false); } Int32 value = frameStart[offset + 1]; UInt64 contentLength = (UInt64)(value >= 128?value - 128:value); if (contentLength <= 125) { // small frame } else if (contentLength == 126) { if (frameStart.Length < headerLength) { return(false); } if (BitConverter.IsLittleEndian) { frameStart.ReversePortion(offset + 2, 2); } contentLength = BitConverter.ToUInt16(frameStart, 2); } else if (contentLength == 127) { if (frameStart.Length < headerLength) { return(false); } if (BitConverter.IsLittleEndian) { frameStart.ReversePortion(offset + 2, 8); } contentLength = (UInt64)BitConverter.ToUInt64(frameStart, 2); } else { return(false); } WebSocketFrameHeaderFlags flags; if (WebSocketFrameHeaderFlags.TryParse(frameStart, offset, out flags)) { header = new WebSocketFrameHeader(keySegment) { ContentLength = contentLength, HeaderLength = headerLength, Flags = flags, RemainingBytes = contentLength }; if (flags.MASK) { headerLength -= 4; for (int i = 0; i < 4; i++) { header._key.Array[header._key.Offset + i] = frameStart[offset + i + headerLength]; } } return(true); } return(false); }