【问题标题】:Bandwidth control with LibCurlNet使用 LibCurlNet 进行带宽控制
【发布时间】:2012-02-21 13:25:21
【问题描述】:

我正在使用LibCurlNet 通过网络传输文件。我正在寻找类似使用 LibCurlNet 进行带宽限制的东西。有人对此有任何想法吗?

我发现我们可以使用“curlopt_max_recv_speed_large”来做到这一点。在 libcurlnet 中没有找到。

请注意我在 C# 中使用它。

谢谢

【问题讨论】:

  • 如果我的问题不清楚,请告诉我。我会提供每一个信息。

标签: c# .net libcurl bandwidth-throttling


【解决方案1】:

您可以找到有关带宽限制的信息 here 该网站的修改摘录

  using System;
  using System.IO;
  using System.Threading;
  using System.Diagnostics;

  namespace Born2Code.Net
  {
      /// <summary>
      /// Class for streaming data with throttling support.
      /// </summary>
      public class ThrottledStream : Stream
      {
          public const long Infinite = 0;

          #region Private members
          private Stream _baseStream;
          private long _maximumBytesPerSecond;
          private long _byteCount;
          private long _start;
          #endregion

          #region Properties
          protected long CurrentMilliseconds
          {
              get
              {
                  return Environment.TickCount;
              }
          }
          public long MaximumBytesPerSecond
          {
              get
              {
                  return _maximumBytesPerSecond;
              }
              set
              {
                  if (MaximumBytesPerSecond != value)
                  {
                      _maximumBytesPerSecond = value;
                      Reset();
                  }
              }
          }
          public override bool CanRead
          {
              get
              {
                  return _baseStream.CanRead;
              }
          }
          public override bool CanSeek
          {
              get
              {
                  return _baseStream.CanSeek;
              }
          }
          public override bool CanWrite
          {
              get
              {
                  return _baseStream.CanWrite;
              }
          }
          public override long Length
          {
              get
              {
                  return _baseStream.Length;
              }
          }
          public override long Position
          {
              get
              {
                  return _baseStream.Position;
              }
              set
              {
                  _baseStream.Position = value;
              }
          }
          #endregion

          #region Ctor
          public ThrottledStream(Stream baseStream)
              : this(baseStream, ThrottledStream.Infinite)
          {
              // Nothing todo.
          }

          public ThrottledStream(Stream baseStream, long maximumBytesPerSecond)
          {
              if (baseStream == null)
              {
                  throw new ArgumentNullException("baseStream");
              }

              if (maximumBytesPerSecond < 0)
              {
                  throw new ArgumentOutOfRangeException("maximumBytesPerSecond",
                      maximumBytesPerSecond, "The maximum number of bytes per second can't be negatie.");
              }

              _baseStream = baseStream;
              _maximumBytesPerSecond = maximumBytesPerSecond;
              _start = CurrentMilliseconds;
              _byteCount = 0;
          }
          #endregion

          #region Public methods
          public override void Flush()
          {
              _baseStream.Flush();
          }
          public override int Read(byte[] buffer, int offset, int count)
          {
              Throttle(count);

              return _baseStream.Read(buffer, offset, count);
          }
          public override long Seek(long offset, SeekOrigin origin)
          {
              return _baseStream.Seek(offset, origin);
          }
          public override void SetLength(long value)
          {
              _baseStream.SetLength(value);
          }
          public override void Write(byte[] buffer, int offset, int count)
          {
              Throttle(count);

              _baseStream.Write(buffer, offset, count);
          }
          public override string ToString()
          {
              return _baseStream.ToString();
          }
          #endregion

          #region Protected methods
          protected void Throttle(int bufferSizeInBytes)
          {
              if (_maximumBytesPerSecond <= 0 || bufferSizeInBytes <= 0)
              {
                  return;
              }

              _byteCount += bufferSizeInBytes;
              long elapsedMilliseconds = CurrentMilliseconds - _start;

              if (elapsedMilliseconds > 0)
              {
                  long bps = _byteCount * 1000L / elapsedMilliseconds;
                  if (bps > _maximumBytesPerSecond)
                  {
                      long wakeElapsed = _byteCount * 1000L / _maximumBytesPerSecond;
                      int toSleep = (int)(wakeElapsed - elapsedMilliseconds);

                      if (toSleep > 1)
                      {
                          try
                          {
                              Thread.Sleep(toSleep);
                          }
                          catch (ThreadAbortException)
                          {
                          }
                          Reset();
                      }
                  }
              }
          }
          protected void Reset()
          {
              long difference = CurrentMilliseconds - _start;
              if (difference > 1000)
              {
                  _byteCount = 0;
                  _start = CurrentMilliseconds;
              }
          }
          #endregion
      }
  }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-05-12
    • 1970-01-01
    • 2018-10-20
    • 2015-06-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多