From 60d1d5cdee642ee9d5be7e91be5caeb9a1a756df Mon Sep 17 00:00:00 2001 From: Luke Pulverenti Date: Tue, 2 Sep 2014 22:30:24 -0400 Subject: restore nuget targets for mono build --- .../HttpServer/ThrottledStream.cs | 46 +++++++++++++++------- 1 file changed, 31 insertions(+), 15 deletions(-) (limited to 'MediaBrowser.Server.Implementations/HttpServer/ThrottledStream.cs') diff --git a/MediaBrowser.Server.Implementations/HttpServer/ThrottledStream.cs b/MediaBrowser.Server.Implementations/HttpServer/ThrottledStream.cs index 067e53571..4bde30dac 100644 --- a/MediaBrowser.Server.Implementations/HttpServer/ThrottledStream.cs +++ b/MediaBrowser.Server.Implementations/HttpServer/ThrottledStream.cs @@ -15,6 +15,8 @@ namespace MediaBrowser.Server.Implementations.HttpServer /// public const long Infinite = 0; + public Func ThrottleCallback { get; set; } + #region Private members /// /// The base stream. @@ -278,22 +280,42 @@ namespace MediaBrowser.Server.Implementations.HttpServer } #endregion - #region Protected methods - /// - /// Throttles for the specified buffer size in bytes. - /// - /// The buffer size in bytes. - protected void Throttle(int bufferSizeInBytes) + private bool ThrottleCheck(int bufferSizeInBytes) { if (_bytesWritten < MinThrottlePosition) { - return; + return false; } // Make sure the buffer isn't empty. if (_maximumBytesPerSecond <= 0 || bufferSizeInBytes <= 0) { - return; + return false; + } + + if (ThrottleCallback != null) + { + var val = ThrottleCallback(_maximumBytesPerSecond, _bytesWritten); + + if (val == 0) + { + return false; + } + } + + return true; + } + + #region Protected methods + /// + /// Throttles for the specified buffer size in bytes. + /// + /// The buffer size in bytes. + protected void Throttle(int bufferSizeInBytes) + { + if (!ThrottleCheck(bufferSizeInBytes)) + { + return ; } _byteCount += bufferSizeInBytes; @@ -332,13 +354,7 @@ namespace MediaBrowser.Server.Implementations.HttpServer protected async Task ThrottleAsync(int bufferSizeInBytes, CancellationToken cancellationToken) { - if (_bytesWritten < MinThrottlePosition) - { - return; - } - - // Make sure the buffer isn't empty. - if (_maximumBytesPerSecond <= 0 || bufferSizeInBytes <= 0) + if (!ThrottleCheck(bufferSizeInBytes)) { return; } -- cgit v1.2.3