Lawo.EmberPlusSharp.S101.DeframingStream.ReadAsync C# (CSharp) Method

ReadAsync() private method

private ReadAsync ( byte buffer, int offset, int count, CancellationToken cancellationToken ) : Task
buffer byte
offset int
count int
cancellationToken System.Threading.CancellationToken
return Task
        public sealed override async Task<int> ReadAsync(
            byte[] buffer, int offset, int count, CancellationToken cancellationToken)
        {
            if (this.state != State.AfterFrame)
            {
                // We're reading the full frame before touching the passed buffer, so that we have a chance to silently
                // throw away data if it turns out to be invalid. This is necessary so that we can correctly
                // communicate with consumers and providers which mix Ember+ data with other data. For example,
                // vsmStudio often sends a first Ember message followed by an Ember+ message.
                var readBuffer = this.ReadBuffer;

                while (((readBuffer.Index < readBuffer.Count) || await readBuffer.ReadAsync(cancellationToken)) &&
                    this.ReadByte(readBuffer))
                {
                }
            }

            var index = offset;
            var pastEnd = offset + count;

            // The last 2 bytes are the CRC
            while ((index < pastEnd) && (this.decodedQueue.Count > 2))
            {
                buffer[index++] = this.decodedQueue.Dequeue();
            }

            return index - offset;
        }