public void ResetRead() { _receiveBytesLeft = -1; _stateLength = -1; _state = ErrorFrameState.Flags; _readPayloadBuffer = WriterContext.EmptySegment; _readOffset = 0; }
public bool Read(byte[] buffer, ref int offset, ref int bytesToProcess) { var numberOfBytesTransferredFromStart = bytesToProcess; var allCompleted = false; while (bytesToProcess > 0 && !allCompleted) { bool isBufferCopyCompleted; switch (_state) { case ErrorFrameState.Flags: if (buffer[offset] == 32) { throw new BackTrackException("", offset); } _flags = (FrameFlags)buffer[offset]; _state = ErrorFrameState.Length; ++offset; --bytesToProcess; _receiveBytesLeft = 4; break; case ErrorFrameState.Length: if ((_flags & FrameFlags.LargeFrame) == 0) { _messageLength = buffer[offset]; ++offset; --bytesToProcess; } else { isBufferCopyCompleted = CopyToReadBuffer(buffer, ref offset, ref bytesToProcess); if (isBufferCopyCompleted) { if (BitConverter.IsLittleEndian) { Array.Reverse(_readBuffer, 0, 4); } _messageLength = BitConverter.ToInt32(_readBuffer, 0); } } _receiveBytesLeft = _messageLength; _state = ErrorFrameState.Message; break; case ErrorFrameState.Message: isBufferCopyCompleted = CopyToReadBuffer(buffer, ref offset, ref bytesToProcess); if (isBufferCopyCompleted) { ErrorMessage = Encoding.UTF8.GetString(_readBuffer, 0, _messageLength); allCompleted = true; } break; } } if (offset < 0) { throw new InvalidOperationException(); } return(allCompleted); }