public static TryParse ( Byte frameStart, |
||
frameStart | Byte | |
offset | ||
headerLength | ||
keySegment | ArraySegment |
|
header | ||
return | System.Boolean |
private void ParseHeader(Int32 readed) { if (!TryReadHeaderUntil(ref readed, 6)) { Close(WebSocketCloseReasons.ProtocolError); return; } Int32 headerlength = WebSocketFrameHeader.GetHeaderLength(_headerBuffer.Array, _headerBuffer.Offset); if (!TryReadHeaderUntil(ref readed, headerlength)) { Close(WebSocketCloseReasons.ProtocolError); return; } WebSocketFrameHeader header; if (!WebSocketFrameHeader.TryParse(_headerBuffer.Array, _headerBuffer.Offset, headerlength, _keyBuffer, out header)) { throw new WebSocketException("Cannot understand frame header"); } CurrentHeader = header; if (!header.Flags.Option.IsData()) { ProcessControlFrame(_clientStream); CurrentHeader = null; } else { _ongoingMessageAwaiting = 0; } }
private async Task ReadHeader(CancellationToken cancellation) { Int32 readed = 0; var success = await TryReadHeaderUntil(readed, 6, cancellation).ConfigureAwait(false); if (!success) { Close(WebSocketCloseReasons.ProtocolError); return; } readed += 6; Int32 headerlength = WebSocketFrameHeader.GetHeaderLength(_headerBuffer.Array, _headerBuffer.Offset); success = await TryReadHeaderUntil(readed, headerlength, cancellation).ConfigureAwait(false); if (!success) { Close(WebSocketCloseReasons.ProtocolError); return; } WebSocketFrameHeader header; if (!WebSocketFrameHeader.TryParse(_headerBuffer.Array, _headerBuffer.Offset, headerlength, _keyBuffer, out header)) { throw new WebSocketException("Cannot understand frame header"); } CurrentHeader = header; if (!header.Flags.Option.IsData()) { ProcessControlFrame(_clientStream); CurrentHeader = null; } else { _ongoingMessageAwaiting = 0; } _ping.NotifyActivity(); }
private void ProcessHeaderFrame(int headerlength) { if (!WebSocketFrameHeader.TryParse(_headerBuffer.Array, _headerBuffer.Offset, headerlength, _keyBuffer, out WebSocketFrameHeader header)) { throw new WebSocketException("Cannot understand frame header"); } CurrentHeader = header; if (!header.Flags.Option.IsData()) { ProcessControlFrame(); CurrentHeader = null; } else { _ongoingMessageAwaiting = 0; } _ping.NotifyActivity(); }
private async Task ParseHeaderAsync(int read) { if (read < 2) { if (this.log.IsWarningEnabled) { this.log.Warning($"{nameof(this.ParseHeaderAsync)} is called with only {read} bytes buffer. Minimal is 2 bytes."); } await this.CloseAsync(WebSocketCloseReasons.ProtocolError).ConfigureAwait(false); return; } var headerLength = WebSocketFrameHeader.GetHeaderLength(this.headerBuffer.Array, this.headerBuffer.Offset); if (read != headerLength) { if (this.log.IsWarningEnabled) { this.log.Warning($"{nameof(this.ParseHeaderAsync)} is called with {read} bytes buffer. While whole header is {headerLength} bytes length."); } await this.CloseAsync(WebSocketCloseReasons.ProtocolError).ConfigureAwait(false); return; } WebSocketFrameHeader header; if (!WebSocketFrameHeader.TryParse(this.headerBuffer.Array, this.headerBuffer.Offset, headerLength, out header)) { throw new WebSocketException("Frame header is malformed."); } if (this.log.IsDebugEnabled) { this.log.Debug($"({this.GetHashCode():X}) [FRAME<-] {header}"); } this.CurrentHeader = header; if (!header.Flags.Option.IsData()) { await this.ProcessControlFrameAsync().ConfigureAwait(false); this.CurrentHeader = null; } else { this.ongoingMessageAwaiting = 0; } try { this.pingHandler.NotifyActivity(); } catch (Exception notifyPingError) { if (this.log.IsWarningEnabled) { this.log.Warning($"({this.GetHashCode():X}) An error occurred while trying to call {this.pingHandler.GetType().Name}.{nameof(this.pingHandler.NotifyActivity)}() method.", notifyPingError); } } }