3

I am using a Client Server architecture to download files from a centralized server. Due to the Limits of the Client machines I'm using Framework 3.5 on them and 4.6 on the Server.

On the Server I'm doing the following:

public IHttpActionResult MyControllerMedhod()
{
    return MySecondMethod();
}

private HttpResponseMessage MySecondMethod()
{
     HttpResponseMessage result = new HttpResponseMessage(HttpStatusCode.OK);

     FileStream stream = new FileStream("c:\temp\tester.dat", FileMode.Open, FileAccess.Read, FileShare.ReadWrite);


     result.Content = new StreamContent(stream);
     result.Content.Headers.ContentDisposition = new ContentDispositionHeaderValue("attachment");
     result.Content.Headers.ContentDisposition.FileName = "c:\temp\tester.dat";
     result.Content.Headers.ContentType = new MediaTypeHeaderValue("application/octet-stream");
     result.Content.Headers.ContentLength = stream.Length;

return result;
}

On the Client machine I'm using:

webClient.DownloadFile("http://127.0.0.1/Download/MyControllerMethod", "c:\temp\test.dat");

It works quite fine BUT I have absolutely no control over how much bandwidth is used. For some of the Clients it would be of Advantage if less bandwidth is being used to download the files.

Now my question is: Is there any way to Limit the bandwidth that is being used per download? (either from Client side or Server side).

As remark: The Downloads can be data files as big as 800 or 900 MB.

kannan D.S
  • 1,107
  • 3
  • 17
  • 44
Thomas
  • 2,886
  • 3
  • 34
  • 78
  • `For some of the Clients it would be of Advantage if less bandwidth is being used to download the files.` Let's say the file was 400MB. What do you mean by `less bandwidth`? Do you mean 'download it more slowly'? – mjwills Aug 06 '18 at 12:52
  • yes download more slwoly (thus less bandwidth being used to download a single file). (our Network department wasn't too happy that I would....lets say block the whole Network if I updated too many Clients at once^^') – Thomas Aug 06 '18 at 12:53
  • https://www.amixa.com/blog/2015/06/24/throttling-bandwidth-on-an-iis-based-site/ – mjwills Aug 06 '18 at 12:56
  • @mjwills is that also bandwidth per download or only the General bandwidth available? – Thomas Aug 06 '18 at 12:58
  • 1
    You will need to use something more specific than WebClient. If you search for c# webClient throttling you'll get many examples like [this](https://www.codeproject.com/Articles/18243/Bandwidth-throttling) or [this](http://www.rovrov.com/blog/2014/12/06/limiting-file-download-speed-in-c%23/) – Renatas M. Aug 06 '18 at 13:03
  • WebClient is made to encapsule a lot of logic and to provide you an easy to use API. The cost of this is that you cannot access the underlying stream easily and therefore will not be able to throttle by using webclient. – Dominik Aug 06 '18 at 13:07
  • what would I have to use instead? – Thomas Aug 06 '18 at 13:12
  • Not used it myself, but maybe this BITS wrapper: https://code.msdn.microsoft.com/windowsdesktop/BITS-Wrapper-for-NET-56293860 – Joel Coehoorn Aug 06 '18 at 13:50
  • Also, it sounds like this code will run on your remote clients, where you really care what about your server/local infrastructure. Rather than making the clients more complicated, you should be able to limit this on the server via IIS. – Joel Coehoorn Aug 06 '18 at 13:52
  • @JoelCoehoorn how so? (iis 10 on Windows Server 2016). I know only how to throttle the TOTAL bandwidth on there not per Client. – Thomas Aug 06 '18 at 14:27
  • If your concern is only not saturating your local link, then why do you care about per-Client? If one client can use more bandwidth for a shorter time, as long as you configured that max to not impact your local environment that just lets you serve the client better – Joel Coehoorn Aug 06 '18 at 16:31
  • @JoelCoehoorn if the Clients are external yepp I wouldn't care. But in my own case they are Company internal Clients who also have other update Jobs running (not via websercvies) and have data Transfers, ... . If I would use the full bandwidth available to me I would block all of the other internal Jobs and...the Network admins being out for my head would be the lesser Problem for me (the higher ups would follow). – Thomas Aug 07 '18 at 08:20

1 Answers1

5

One way that this can be achieved from the server side, if you don't have the ability to adjust IIS settings is to use a ThrottledStream. This is a class that I found somewhere way back when, if I can located the original source I'll be sure to link back to it.

Throttled Stream:

public class ThrottledStream : Stream
{
    /// <summary>
    /// A constant used to specify an infinite number of bytes that can be transferred per second.
    /// </summary>
    public const long Infinite = 0;

    #region Private members
    /// <summary>
    /// The base stream.
    /// </summary>
    private Stream _baseStream;

    /// <summary>
    /// The maximum bytes per second that can be transferred through the base stream.
    /// </summary>
    private long _maximumBytesPerSecond;

    /// <summary>
    /// The number of bytes that has been transferred since the last throttle.
    /// </summary>
    private long _byteCount;

    /// <summary>
    /// The start time in milliseconds of the last throttle.
    /// </summary>
    private long _start;
    #endregion

    #region Properties
    /// <summary>
    /// Gets the current milliseconds.
    /// </summary>
    /// <value>The current milliseconds.</value>
    protected long CurrentMilliseconds
    {
        get
        {
            return Environment.TickCount;
        }
    }

    /// <summary>
    /// Gets or sets the maximum bytes per second that can be transferred through the base stream.
    /// </summary>
    /// <value>The maximum bytes per second.</value>
    public long MaximumBytesPerSecond
    {
        get
        {
            return _maximumBytesPerSecond;
        }
        set
        {
            if (MaximumBytesPerSecond != value)
            {
                _maximumBytesPerSecond = value;
                Reset();
            }
        }
    }

    /// <summary>
    /// Gets a value indicating whether the current stream supports reading.
    /// </summary>
    /// <returns>true if the stream supports reading; otherwise, false.</returns>
    public override bool CanRead
    {
        get
        {
            return _baseStream.CanRead;
        }
    }

    /// <summary>
    /// Gets a value indicating whether the current stream supports seeking.
    /// </summary>
    /// <value></value>
    /// <returns>true if the stream supports seeking; otherwise, false.</returns>
    public override bool CanSeek
    {
        get
        {
            return _baseStream.CanSeek;
        }
    }

    /// <summary>
    /// Gets a value indicating whether the current stream supports writing.
    /// </summary>
    /// <value></value>
    /// <returns>true if the stream supports writing; otherwise, false.</returns>
    public override bool CanWrite
    {
        get
        {
            return _baseStream.CanWrite;
        }
    }

    /// <summary>
    /// Gets the length in bytes of the stream.
    /// </summary>
    /// <value></value>
    /// <returns>A long value representing the length of the stream in bytes.</returns>
    /// <exception cref="T:System.NotSupportedException">The base stream does not support seeking. </exception>
    /// <exception cref="T:System.ObjectDisposedException">Methods were called after the stream was closed. </exception>
    public override long Length
    {
        get
        {
            return _baseStream.Length;
        }
    }

    /// <summary>
    /// Gets or sets the position within the current stream.
    /// </summary>
    /// <value></value>
    /// <returns>The current position within the stream.</returns>
    /// <exception cref="T:System.IO.IOException">An I/O error occurs. </exception>
    /// <exception cref="T:System.NotSupportedException">The base stream does not support seeking. </exception>
    /// <exception cref="T:System.ObjectDisposedException">Methods were called after the stream was closed. </exception>
    public override long Position
    {
        get
        {
            return _baseStream.Position;
        }
        set
        {
            _baseStream.Position = value;
        }
    }
    #endregion

    #region Ctor
    /// <summary>
    /// Initializes a new instance of the <see cref="T:ThrottledStream"/> class with an
    /// infinite amount of bytes that can be processed.
    /// </summary>
    /// <param name="baseStream">The base stream.</param>
    public ThrottledStream(Stream baseStream)
        : this(baseStream, ThrottledStream.Infinite)
    {
        // Nothing todo.
    }

    /// <summary>
    /// Initializes a new instance of the <see cref="T:ThrottledStream"/> class.
    /// </summary>
    /// <param name="baseStream">The base stream.</param>
    /// <param name="maximumBytesPerSecond">The maximum bytes per second that can be transferred through the base stream.</param>
    /// <exception cref="ArgumentNullException">Thrown when <see cref="baseStream"/> is a null reference.</exception>
    /// <exception cref="ArgumentOutOfRangeException">Thrown when <see cref="maximumBytesPerSecond"/> is a negative value.</exception>
    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
    /// <summary>
    /// Clears all buffers for this stream and causes any buffered data to be written to the underlying device.
    /// </summary>
    /// <exception cref="T:System.IO.IOException">An I/O error occurs.</exception>
    public override void Flush()
    {
        _baseStream.Flush();
    }

    /// <summary>
    /// Reads a sequence of bytes from the current stream and advances the position within the stream by the number of bytes read.
    /// </summary>
    /// <param name="buffer">An array of bytes. When this method returns, the buffer contains the specified byte array with the values between offset and (offset + count - 1) replaced by the bytes read from the current source.</param>
    /// <param name="offset">The zero-based byte offset in buffer at which to begin storing the data read from the current stream.</param>
    /// <param name="count">The maximum number of bytes to be read from the current stream.</param>
    /// <returns>
    /// The total number of bytes read into the buffer. This can be less than the number of bytes requested if that many bytes are not currently available, or zero (0) if the end of the stream has been reached.
    /// </returns>
    /// <exception cref="T:System.ArgumentException">The sum of offset and count is larger than the buffer length. </exception>
    /// <exception cref="T:System.ObjectDisposedException">Methods were called after the stream was closed. </exception>
    /// <exception cref="T:System.NotSupportedException">The base stream does not support reading. </exception>
    /// <exception cref="T:System.ArgumentNullException">buffer is null. </exception>
    /// <exception cref="T:System.IO.IOException">An I/O error occurs. </exception>
    /// <exception cref="T:System.ArgumentOutOfRangeException">offset or count is negative. </exception>
    public override int Read(byte[] buffer, int offset, int count)
    {
        Throttle(count);

        return _baseStream.Read(buffer, offset, count);
    }

    /// <summary>
    /// Sets the position within the current stream.
    /// </summary>
    /// <param name="offset">A byte offset relative to the origin parameter.</param>
    /// <param name="origin">A value of type <see cref="T:System.IO.SeekOrigin"></see> indicating the reference point used to obtain the new position.</param>
    /// <returns>
    /// The new position within the current stream.
    /// </returns>
    /// <exception cref="T:System.IO.IOException">An I/O error occurs. </exception>
    /// <exception cref="T:System.NotSupportedException">The base stream does not support seeking, such as if the stream is constructed from a pipe or console output. </exception>
    /// <exception cref="T:System.ObjectDisposedException">Methods were called after the stream was closed. </exception>
    public override long Seek(long offset, SeekOrigin origin)
    {
        return _baseStream.Seek(offset, origin);
    }

    /// <summary>
    /// Sets the length of the current stream.
    /// </summary>
    /// <param name="value">The desired length of the current stream in bytes.</param>
    /// <exception cref="T:System.NotSupportedException">The base stream does not support both writing and seeking, such as if the stream is constructed from a pipe or console output. </exception>
    /// <exception cref="T:System.IO.IOException">An I/O error occurs. </exception>
    /// <exception cref="T:System.ObjectDisposedException">Methods were called after the stream was closed. </exception>
    public override void SetLength(long value)
    {
        _baseStream.SetLength(value);
    }

    /// <summary>
    /// Writes a sequence of bytes to the current stream and advances the current position within this stream by the number of bytes written.
    /// </summary>
    /// <param name="buffer">An array of bytes. This method copies count bytes from buffer to the current stream.</param>
    /// <param name="offset">The zero-based byte offset in buffer at which to begin copying bytes to the current stream.</param>
    /// <param name="count">The number of bytes to be written to the current stream.</param>
    /// <exception cref="T:System.IO.IOException">An I/O error occurs. </exception>
    /// <exception cref="T:System.NotSupportedException">The base stream does not support writing. </exception>
    /// <exception cref="T:System.ObjectDisposedException">Methods were called after the stream was closed. </exception>
    /// <exception cref="T:System.ArgumentNullException">buffer is null. </exception>
    /// <exception cref="T:System.ArgumentException">The sum of offset and count is greater than the buffer length. </exception>
    /// <exception cref="T:System.ArgumentOutOfRangeException">offset or count is negative. </exception>
    public override void Write(byte[] buffer, int offset, int count)
    {
        Throttle(count);

        _baseStream.Write(buffer, offset, count);
    }

    /// <summary>
    /// Returns a <see cref="T:System.String"></see> that represents the current <see cref="T:System.Object"></see>.
    /// </summary>
    /// <returns>
    /// A <see cref="T:System.String"></see> that represents the current <see cref="T:System.Object"></see>.
    /// </returns>
    public override string ToString()
    {
        return _baseStream.ToString();
    }
    #endregion

    #region Protected methods
    /// <summary>
    /// Throttles for the specified buffer size in bytes.
    /// </summary>
    /// <param name="bufferSizeInBytes">The buffer size in bytes.</param>
    protected void Throttle(int bufferSizeInBytes)
    {
        // Make sure the buffer isn't empty.
        if (_maximumBytesPerSecond <= 0 || bufferSizeInBytes <= 0)
        {
            return;
        }

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

        if (elapsedMilliseconds > 0)
        {
            // Calculate the current bps.
            long bps = _byteCount * 1000L / elapsedMilliseconds;

            // If the bps are more then the maximum bps, try to throttle.
            if (bps > _maximumBytesPerSecond)
            {
                // Calculate the time to sleep.
                long wakeElapsed = _byteCount * 1000L / _maximumBytesPerSecond;
                int toSleep = (int)(wakeElapsed - elapsedMilliseconds);

                if (toSleep > 1)
                {
                    try
                    {
                        // The time to sleep is more then a millisecond, so sleep.
                        Thread.Sleep(toSleep);
                    }
                    catch (ThreadAbortException)
                    {
                        // Eatup ThreadAbortException.
                    }

                    // A sleep has been done, reset.
                    Reset();
                }
            }
        }
    }

    /// <summary>
    /// Will reset the bytecount to 0 and reset the start time to the current time.
    /// </summary>
    protected void Reset()
    {
        long difference = CurrentMilliseconds - _start;

        // Only reset counters when a known history is available of more then 1 second.
        if (difference > 1000)
        {
            _byteCount = 0;
            _start = CurrentMilliseconds;
        }
    }
    #endregion
}

This is used by placing the original stream into the constructor along with the maximum bytes per second that you would like to throttle it to. For example:

public IHttpActionResult MyControllerMedhod()
{
    return MySecondMethod();
}

private HttpResponseMessage MySecondMethod()
{
    HttpResponseMessage result = new HttpResponseMessage(HttpStatusCode.OK);

    FileStream stream = new FileStream("c:\temp\tester.dat", FileMode.Open, FileAccess.Read, FileShare.ReadWrite);


    result.Content = new ThrottledStream(stream, 153600);
    result.Content.Headers.ContentDisposition = new 
    ContentDispositionHeaderValue("attachment");
    result.Content.Headers.ContentDisposition.FileName = "c:\temp\tester.dat";
    result.Content.Headers.ContentType = new MediaTypeHeaderValue("application/octet-stream");
    result.Content.Headers.ContentLength = stream.Length;

    return result;
}

Edit:

Original Source: https://gist.github.com/passy/637319

DCCoder
  • 1,587
  • 4
  • 16
  • 29