Example #1
0
        public void FrameHeadersShouldBeEncodeable()
        {
            var buf     = new byte[FrameHeader.HeaderSize];
            var bufView = new ArraySegment <byte>(buf);
            var frame   = new FrameHeader
            {
                Length   = 0x0340F1,
                Type     = (FrameType)0xFF,
                Flags    = 0x09,
                StreamId = 0x1AB83412u,
            };

            frame.EncodeInto(bufView);
            var expected = new byte[] { 0x03, 0x40, 0xF1, 0xFF, 0x09, 0x1A, 0xB8, 0x34, 0x12 };

            Assert.Equal(buf, expected);
            frame = new FrameHeader
            {
                Length   = (1 << 24) - 1,
                Type     = FrameType.Headers,
                Flags    = 0xFF,
                StreamId = 0x7FFFFFFFu,
            };
            frame.EncodeInto(bufView);
            expected = new byte[] { 0xFF, 0xFF, 0xFF, 0x01, 0xFF, 0x7F, 0xFF, 0xFF, 0xFF };
            Assert.Equal(buf, expected);
        }
        public static async Task WriteFrameHeader(
            this IWriteAndCloseableByteStream stream,
            FrameHeader fh)
        {
            var headerBytes = new byte[FrameHeader.HeaderSize];

            fh.EncodeInto(new ArraySegment <byte>(headerBytes));
            await stream.WriteAsync(new ArraySegment <byte>(headerBytes));
        }
Example #3
0
        private Task WriteSettingsAckAsync()
        {
            var fh = new FrameHeader
            {
                Type     = FrameType.Settings,
                StreamId = 0u,
                Length   = 0,
                Flags    = (byte)SettingsFrameFlags.Ack,
            };

            var headerView = new ArraySegment <byte>(outBuf, 0, FrameHeader.HeaderSize);

            fh.EncodeInto(headerView);
            return(this.outStream.WriteAsync(headerView));
        }
Example #4
0
        private Task WriteSettingsAsync(Settings settings)
        {
            var fh = new FrameHeader
            {
                Type     = FrameType.Settings,
                StreamId = 0u,
                Length   = settings.RequiredSize,
                Flags    = 0,
            };

            fh.EncodeInto(
                new ArraySegment <byte>(outBuf, 0, FrameHeader.HeaderSize));
            settings.EncodeInto(new ArraySegment <byte>(
                                    outBuf, FrameHeader.HeaderSize, settings.RequiredSize));
            var totalSize = FrameHeader.HeaderSize + settings.RequiredSize;
            var data      = new ArraySegment <byte>(outBuf, 0, totalSize);

            return(this.outStream.WriteAsync(data));
        }
Example #5
0
        private async Task WriteHeadersAsync(WriteRequest wr)
        {
            EnsureBuffer(FrameHeader.HeaderSize + MaxFrameSize);


            var maxFrameSize = Math.Min(
                MaxFrameSize,
                outBuf.Length - FrameHeader.HeaderSize);

            var headerView = new ArraySegment <byte>(
                outBuf, 0, FrameHeader.HeaderSize);

            var headers        = wr.Headers;
            var nrTotalHeaders = headers.Count();
            var sentHeaders    = 0;
            var isContinuation = false;

            while (true)
            {
                var headerBlockFragment = new ArraySegment <byte>(
                    outBuf, FrameHeader.HeaderSize, maxFrameSize);
                var encodeResult = this.hEncoder.EncodeInto(
                    headerBlockFragment, headers);

                if (encodeResult.FieldCount == 0 && (nrTotalHeaders - sentHeaders) != 0)
                {
                    throw new Exception(
                              "遇到过大的headerfield");
                }

                sentHeaders += encodeResult.FieldCount;
                var remaining = nrTotalHeaders - sentHeaders;

                FrameHeader hdr = wr.Header;
                hdr.Length = encodeResult.UsedBytes;
                if (!isContinuation)
                {
                    hdr.Type = FrameType.Headers;
                    if (remaining == 0)
                    {
                        hdr.Flags |= (byte)HeadersFrameFlags.EndOfHeaders;
                    }
                    else
                    {
                        var f = hdr.Flags & ~((byte)HeadersFrameFlags.EndOfHeaders);
                        hdr.Flags = (byte)f;
                    }
                }
                else
                {
                    hdr.Type  = FrameType.Continuation;
                    hdr.Flags = 0;
                    if (remaining == 0)
                    {
                        hdr.Flags = (byte)ContinuationFrameFlags.EndOfHeaders;
                    }
                }

                hdr.EncodeInto(headerView);
                var dataView = new ArraySegment <byte>(
                    outBuf, 0, FrameHeader.HeaderSize + encodeResult.UsedBytes);
                await this.outStream.WriteAsync(dataView);

                if (remaining == 0)
                {
                    break;
                }
                else
                {
                    isContinuation = true;

                    headers = wr.Headers.Skip(sentHeaders);
                }
            }
        }