Exemple #1
0
        public async Task TestReadAsync()
        {
            using (var stream = new ProgressStream(new DummyNetworkStream(), Update)) {
                var buffer   = new byte[1024];
                int expected = 517;

                stream.Source.Write(buffer, 0, expected);
                stream.Source.Position = 0;

                progress = 0;
                int n = await stream.ReadAsync(buffer, 0, buffer.Length);

                Assert.AreEqual(expected, n, "nread");
                Assert.AreEqual(expected, progress, "progress");
            }
        }
Exemple #2
0
        internal static async Task WriteToRequestAsync(HttpWebRequest request, ProgressStream progressStream)
        {
            log.Info("Writing data to request asynchronously.");

            if (progressStream == null)
            {
                return;
            }
            try
            {
                log.Info("Reporting progress requested.");
                long length            = progressStream.Length;
                var  cancellationToken = new CancellationToken();
                var  buffer            = new byte[CHUNK_SIZE];

                using (var requestStream = await request.GetRequestStreamAsync().ConfigureAwait(false))
                {
                    progressStream.Position = 0;

                    int read = 0;
                    for (int i = 0; i < length; i += read)
                    {
                        read = await progressStream.ReadAsync(buffer, 0, CHUNK_SIZE, cancellationToken).ConfigureAwait(false);

                        await requestStream.WriteAsync(buffer, 0, read, cancellationToken).ConfigureAwait(false);

                        await requestStream.FlushAsync(cancellationToken).ConfigureAwait(false); // flushing is required or else we jump to 100% very fast

                        progressStream.Position += read;
                    }
                }
            }
            catch (WebException ex)
            {
                HandleWebException(ex);
            }
        }