public static GetHeaderLength ( Byte frameStart, |
||
frameStart | Byte | |
offset | ||
return |
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 ParseHeaderAsync(int readed) { if (await TryReadHeaderUntilAsync(readed, 6).ConfigureAwait(false) == -1) { Close(WebSocketCloseReasons.ProtocolError); return; } var headerlength = WebSocketFrameHeader.GetHeaderLength(_headerBuffer.Array, _headerBuffer.Offset); if (await TryReadHeaderUntilAsync(readed, headerlength).ConfigureAwait(false) == -1) { Close(WebSocketCloseReasons.ProtocolError); return; } await ProcessHeaderFrameAsync(headerlength).ConfigureAwait(false); }
private void ParseHeader(int readed) { if (!TryReadHeaderUntil(ref readed, 6)) { Close(WebSocketCloseReasons.ProtocolError); return; } var headerlength = WebSocketFrameHeader.GetHeaderLength(_headerBuffer.Array, _headerBuffer.Offset); if (!TryReadHeaderUntil(ref readed, headerlength)) { Close(WebSocketCloseReasons.ProtocolError); return; } ProcessHeaderFrame(headerlength); }
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 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); } } }
public async Task AwaitHeaderAsync(CancellationToken cancellation) { this.CheckForDoubleRead(); try { while (this.IsConnected && this.CurrentHeader == null) { var buffered = 0; var estimatedHeaderLength = 2; // try read minimal frame first while (buffered < estimatedHeaderLength && !cancellation.IsCancellationRequested) { var read = await this.networkConnection.ReadAsync(this.headerBuffer.Array, this.headerBuffer.Offset + buffered, estimatedHeaderLength - buffered, cancellation).ConfigureAwait(false); if (read == 0) { buffered = 0; break; } buffered += read; if (buffered >= 2) { estimatedHeaderLength = WebSocketFrameHeader.GetHeaderLength(this.headerBuffer.Array, this.headerBuffer.Offset); } } if (buffered == 0 || cancellation.IsCancellationRequested) { if (buffered == 0) { if (this.log.IsDebugEnabled) { this.log.Debug($"({this.GetHashCode():X}) Connection has been closed while async awaiting header."); } } await this.CloseAsync(WebSocketCloseReasons.ProtocolError).ConfigureAwait(false); return; } await this.ParseHeaderAsync(buffered).ConfigureAwait(false); } } catch (Exception awaitHeaderError) when(awaitHeaderError.Unwrap() is ThreadAbortException == false) { if (!this.IsConnected) { return; } var awaitHeaderErrorUnwrap = awaitHeaderError.Unwrap(); if (this.log.IsDebugEnabled && awaitHeaderErrorUnwrap is OperationCanceledException == false && this.IsConnected) { this.log.Debug($"({this.GetHashCode():X}) An error occurred while async awaiting header.", awaitHeaderErrorUnwrap); } if (this.IsConnected) { await this.CloseAsync(WebSocketCloseReasons.ProtocolError).ConfigureAwait(false); } if (awaitHeaderErrorUnwrap is WebSocketException == false && awaitHeaderErrorUnwrap is OperationCanceledException == false) { throw new WebSocketException("Read operation on WebSocket stream is failed. More detailed information in inner exception.", awaitHeaderErrorUnwrap); } else { throw; } } }