Exemplo n.º 1
0
        public static async Task CopyToAsync_AllDataCopied(int byteCount, bool useAsync)
        {
            using StreamPair streams = await CreateConnectedStreamsAsync();

            (Stream writeable, Stream readable) = GetReadWritePair(streams);

            var results = new MemoryStream();

            byte[] dataToCopy = RandomNumberGenerator.GetBytes(byteCount);

            Task copyTask;

            if (useAsync)
            {
                copyTask = readable.CopyToAsync(results);
                await writeable.WriteAsync(dataToCopy);
            }
            else
            {
                copyTask = Task.Run(() => readable.CopyTo(results));
                writeable.Write(new ReadOnlySpan <byte>(dataToCopy));
            }

            writeable.Dispose();
            await copyTask;
        }
Exemplo n.º 2
0
        internal static async Task ReadWrite_Success(ReadWriteMode mode, int writeSize, bool startWithFlush)
        {
            foreach (CancellationToken nonCanceledToken in new[] { CancellationToken.None, new CancellationTokenSource().Token })
            {
                using StreamPair streams = await CreateConnectedStreamsAsync();

                foreach ((Stream writeable, Stream readable) in GetReadWritePairs(streams))
                {
                    if (startWithFlush)
                    {
                        await FlushAsync(mode, writeable, nonCanceledToken);
                    }

                    byte[] writerBytes = RandomNumberGenerator.GetBytes(writeSize);
                    var    readerBytes = new byte[writerBytes.Length];

                    Task writes = Task.Run(async() =>
                    {
                        await WriteAsync(mode, writeable, writerBytes, 0, writerBytes.Length, nonCanceledToken);

                        if (FlushRequiredToWriteData)
                        {
                            if (FlushGuaranteesAllDataWritten)
                            {
                                await writeable.FlushAsync();
                            }
                            else
                            {
                                await writeable.DisposeAsync();
                            }
                        }
                    });

                    int n = 0;
                    while (n < readerBytes.Length)
                    {
                        int r = await ReadAsync(mode, readable, readerBytes, n, readerBytes.Length - n);

                        n += r;
                    }

                    await writes;

                    if (!FlushGuaranteesAllDataWritten)
                    {
                        break;
                    }
                }
            }
        }
Exemplo n.º 3
0
 protected static (Stream writeable, Stream readable) GetReadWritePair(StreamPair streams) =>
Exemplo n.º 4
0
        protected static IEnumerable <(Stream writeable, Stream readable)> GetReadWritePairs(StreamPair streams)
        {
            var pairs = new List <(Stream, Stream)>(2);

            if (streams.Stream1.CanWrite)
            {
                pairs.Add((streams.Stream1, streams.Stream2));
            }

            if (streams.Stream2.CanWrite)
            {
                pairs.Add((streams.Stream2, streams.Stream1));
            }

            return(pairs);
        }
Exemplo n.º 5
0
        protected static async Task <StreamPair> CreateConnectedStreamsAsync()
        {
            QuicImplementationProvider provider = ImplementationProvider;
            var protocol = new SslApplicationProtocol("quictest");

            QuicListener listener = new QuicListener(provider, new QuicListenerOptions()
            {
                ListenEndPoint = new IPEndPoint(IPAddress.Loopback, 0),
                ServerAuthenticationOptions = new SslServerAuthenticationOptions {
                    ApplicationProtocols = new List <SslApplicationProtocol> {
                        protocol
                    }
                },
                CertificateFilePath = "Certs/cert.crt",
                PrivateKeyFilePath  = "Certs/cert.key"
            });

            listener.Start();

            QuicConnection connection1 = null, connection2 = null;
            QuicStream     stream1 = null, stream2 = null;

            await Task.WhenAll(
                Task.Run(async() =>
            {
                connection1 = await listener.AcceptConnectionAsync();
                stream1     = await connection1.AcceptStreamAsync();

                // Hack to force stream creation
                byte[] buffer = new byte[1];
                await stream1.ReadAsync(buffer);
            }),
                Task.Run(async() =>
            {
                connection2 = new QuicConnection(
                    provider,
                    listener.ListenEndPoint,
                    new SslClientAuthenticationOptions()
                {
                    ApplicationProtocols = new List <SslApplicationProtocol>()
                    {
                        protocol
                    }
                });
                await connection2.ConnectAsync();
                stream2 = connection2.OpenBidirectionalStream();

                // Hack to force stream creation
                byte[] buffer = new byte[1];
                await stream2.WriteAsync(buffer);
                await stream2.FlushAsync();
            }));

            var result = new StreamPair(stream1, stream2);

            result.Disposables.Add(connection1);
            result.Disposables.Add(connection2);
            result.Disposables.Add(listener);

            return(result);
        }