Exemplo n.º 1
0
        public async Task <Handshake> GetHandshakeMessage(PipeReader input)
        {
            ReadResult readAsync = await input.ReadAsync().ConfigureAwait(false);

            if (readAsync.Buffer.Length >= FrameHeaderSize)
            {
                ReadOnlySequence <byte> handshakeBytes =
                    readAsync.Buffer.Slice(0, FrameHeaderSize);

                ArraySegment <byte> arraySegment = handshakeBytes.ToArraySegment();

                if (Handshake.TryParse(arraySegment, out Handshake result))
                {
                    input.AdvanceTo(readAsync.Buffer.GetPosition(FrameHeaderSize));
                    return(result);
                }
            }

            input.AdvanceTo(readAsync.Buffer.GetPosition(0));
            return(null);
        }