Exemple #1
0
        public async Task Writing()
        {
            var ms    = new MemoryStream();
            var muxer = new Muxer {
                Channel = ms
            };
            var stream = new Substream {
                Muxer = muxer
            };
            var m1 = new byte[1];

            stream.AddData(new byte[] { 10 });
            Assert.IsTrue(stream.CanRead);
            Assert.IsTrue(stream.CanWrite);

            Assert.AreEqual(1, await stream.ReadAsync(m1, 0, 1));
            await stream.WriteAsync(m1, 0, 1);

            stream.WriteByte(11);
            await stream.FlushAsync();

            ms.Position = 0;
            var header = await Header.ReadAsync(ms);

            var length = await Varint.ReadVarint32Async(ms);

            var payload = new byte[length];

            ms.Read(payload, 0, length);
            Assert.AreEqual(stream.Id, header.StreamId);
            Assert.AreEqual(2, payload.Length);
            CollectionAssert.AreEqual(new byte[] { 10, 11 }, payload);
        }
Exemple #2
0
    private static async Task WriteAsync(Stream stream, byte[] buffer, CancellationToken cancellationToken)
    {
        using Substream s = stream.WriteSubstream();
        await s.WriteAsync(buffer, cancellationToken);

        await s.FlushAsync(cancellationToken);
    }