首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >带宽节流

带宽节流
EN

Stack Overflow用户
提问于 2015-09-22 17:50:00
回答 2查看 1.4K关注 0票数 1

如何使用节流和webClient.DownloadFile方法?这是我的密码

代码语言:javascript
复制
WebClient webClient = new WebClient();
webClient.DownloadFile(new Uri("http://127.0.0.1/basic.xml"), "D:/basic.xml");
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2015-09-22 18:14:07

如果您想在客户端站点上节流,您可以创建您自己的ThrottledStream (实现stream )

代码语言:javascript
复制
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

namespace MyNamespace
{
    public class ThrottledStream : Stream
    {
        Stream _InputStream = null;
        int _Throttle = 0;
        Stopwatch _watch = Stopwatch.StartNew();
        long _TotalBytesRead = 0;

        public ThrottledStream(Stream @in, int throttleKb)
        {

            _Throttle = throttleKb * 1024;
            _InputStream = @in;
        }

        public override bool CanRead
        {
            get { return _InputStream.CanRead; }
        }

        public override bool CanSeek
        {
            get { return _InputStream.CanSeek; }
        }

        public override bool CanWrite
        {
            get { return false; }
        }

        public override void Flush()
        {
        }

        public override long Length
        {
            get { return _InputStream.Length; }
        }

        public override long Position
        {
            get
            {
                return _InputStream.Position;
            }
            set
            {
                _InputStream.Position = value;
            }
        }

        public override int Read(byte[] buffer, int offset, int count)
        {
            var newcount = GetBytesToReturn(count);
            int read = _InputStream.Read(buffer, offset, newcount);
            Interlocked.Add(ref _TotalBytesRead, read);
            return read;
        }

        public override long Seek(long offset, SeekOrigin origin)
        {
            return _InputStream.Seek(offset, origin);
        }

        public override void SetLength(long value)
        {
        }

        public override void Write(byte[] buffer, int offset, int count)
        {
        }

        int GetBytesToReturn(int count)
        {
            return GetBytesToReturnAsync(count).Result;
        }

        async Task<int> GetBytesToReturnAsync(int count)
        {
            if (_Throttle <= 0)
                return count;

            long canSend = (long)(_watch.ElapsedMilliseconds * (_Throttle / 1000.0));

            int diff = (int)(canSend - _TotalBytesRead);

            if (diff <= 0)
            {
                var waitInSec = ((diff * -1.0) / (_Throttle));

                await Task.Delay((int)(waitInSec * 1000)).ConfigureAwait(false);
            }

            if (diff >= count) return count;

            return diff > 0 ? diff : Math.Min(1024 * 8, count);
        }
    }
}

然后用它作为

代码语言:javascript
复制
using (HttpClient client = new HttpClient())
{
    var stream = await client.GetStreamAsync("http://stackoverflow.com");
    var throttledStream = new MyNamespace.ThrottledStream(stream, 128); //128Kb/s
    using (var fs = File.Create(@"d:/basic.xml"))
    {
        throttledStream.CopyTo(fs);
    }
}
票数 6
EN

Stack Overflow用户

发布于 2015-09-22 18:11:05

在给定的链接中,只需将目标与源流交换。

代码语言:javascript
复制
var originalDestinationStream = new FileStream(@"D:/basic.xml", 
                   FileMode.Open, FileAccess.ReadWrite, FileShare.ReadWrite);

// mySocket represents an instance to http://127.0.0.1/basic.xml
var sourceStream = new NetworkStream(mySocket, false);
var destinationStream = new ThrottledStream(originalDestinationStream, 51200)

byte[] buffer = new byte[1024];
int readCount = sourceStream.Read(buffer, 0, BufferSize);

while (readCount > 0)
{
    destinationStream.Write(buffer, 0, readCount);
    readCount = sourceStream.Read(buffer, 0, BufferSize);
}

当然,不要忘记引用ThrottledStream类,并将其包含在这里的项目中。

票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/32723561

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档