How to lock a buffer reference - c#

I write a buffer class for asynchronous socket which is multi-threading. And I want to ensure that any operation on the buffer is not allowed until other operation is finished (read, write). How to do this? The code is like:
public class ByteBuffer {
private static ManualResetEvent mutex =
new ManualResetEvent(false);
byte[] buff;
int capacity;
int size;
int startIdx;
public byte[] Buffer {
get { return buff; }
}
public int StartIndex {
get { return startIdx; }
}
public int Capacity {
get { return capacity; }
}
public int Length {
get { return size; }
}
// Ctor
public ByteBuffer() {
capacity = 1024;
buff = new byte[capacity];
size = startIdx = 0;
}
// Read data from buff without deleting
public byte[] Peek(int s){
// read s bytes data
}
// Read data and delete it
public byte[] Read(int s) {
// read s bytes data & delete it
}
public void Append(byte[] data) {
// Add data to buff
}
private void Resize() {
// resize the buff
}
}
And how to lock the getter?

I suggest using lock for example
public class A
{
private static object lockObj = new object();
public MyCustomClass sharedObject;
public void Foo()
{
lock(lockObj)
{
//codes here are safe
//shareObject.....
}
}
}

Related

Using Named Pipes in C#

i've been trying to use Named Pipes in C# for a while but can't get it to work.
I'm working with the following code:
internal class SplitManager
{
public static void initialize()
{
Debug.Log("Initializing LiveSplit pipe");
SplitManager.pipeClientStream = new NamedPipeClientStream("//.//pipe//LiveSplit");
Debug.Log("Successfully initialized LiveSplit pipe");
}
public static void startRun()
{
Debug.Log("[PIPE]: start");
SplitManager.WriteString("start");
}
public static void performSplit()
{
Debug.Log("[PIPE]: split");
SplitManager.WriteString("split");
}
private static void WriteString(string str)
{
SplitManager.pipeClientStream.Connect();
new StreamString(SplitManager.pipeClientStream).WriteString(str);
SplitManager.pipeClientStream.Close();
}
private static NamedPipeClientStream pipeClientStream;
}
public class StreamString
{
public StreamString(Stream ioStream)
{
this.ioStream = ioStream;
this.streamEncoding = new UnicodeEncoding();
}
public string ReadString()
{
int num = this.ioStream.ReadByte() * 256;
num += this.ioStream.ReadByte();
byte[] array = new byte[num];
this.ioStream.Read(array, 0, num);
return this.streamEncoding.GetString(array);
}
public int WriteString(string outString)
{
byte[] bytes = this.streamEncoding.GetBytes(outString);
int num = bytes.Length;
if (num > 65535)
{
num = 65535;
}
this.ioStream.WriteByte((byte)(num / 256));
this.ioStream.WriteByte((byte)(num & 255));
this.ioStream.Write(bytes, 0, num);
this.ioStream.Flush();
return bytes.Length + 2;
}
private Stream ioStream;
private UnicodeEncoding streamEncoding;
}
When i run this code i get Win32Exception with an error message that it cannot find the specified file. I'm 100% sure the path is fine, since i checked it with powershell command [System.IO.Directory]::GetFiles("\\.\\pipe\\"). Any ideas why does this error happen?
It turns out that C# actually provides the "//.//pipe//" prefix to the string, so simply replacing SplitManager.pipeClientStream = new NamedPipeClientStream("//.//pipe//LiveSplit"); to SplitManager.pipeClientStream = new NamedPipeClientStream("LiveSplit"); worked!

How can I throttle/set the upload speed with an HttpClient? [duplicate]

I am developing a program that continually sends a stream of data in the background and I want to allow the user to set a cap for both upload and download limit.
I have read up on the token bucket and leaky bucket alghorhithms, and seemingly the latter seems to fit the description since this is not a matter of maximizing the network bandwidth but rather being as unobtrusive as possible.
I am however a bit unsure on how I would implement this. A natural approach is to extend the abstract Stream class to make it simple to extend existing traffic, but would this not require the involvement of extra threads to send the data while simultaneously receiving (leaky bucket)? Any hints on other implementations that do the same would be appreciated.
Also, although I can modify how much data the program receives, how well does bandwidth throttling work at the C# level? Will the computer still receive the data and simply save it, effectively canceling the throttling effect or will it wait until I ask to receive more?
EDIT: I am interested in throttling both incoming and outgoing data, where I have no control over the opposite end of the stream.
Based on #0xDEADBEEF's solution I created the following (testable) solution based on Rx schedulers:
public class ThrottledStream : Stream
{
private readonly Stream parent;
private readonly int maxBytesPerSecond;
private readonly IScheduler scheduler;
private readonly IStopwatch stopwatch;
private long processed;
public ThrottledStream(Stream parent, int maxBytesPerSecond, IScheduler scheduler)
{
this.maxBytesPerSecond = maxBytesPerSecond;
this.parent = parent;
this.scheduler = scheduler;
stopwatch = scheduler.StartStopwatch();
processed = 0;
}
public ThrottledStream(Stream parent, int maxBytesPerSecond)
: this (parent, maxBytesPerSecond, Scheduler.Immediate)
{
}
protected void Throttle(int bytes)
{
processed += bytes;
var targetTime = TimeSpan.FromSeconds((double)processed / maxBytesPerSecond);
var actualTime = stopwatch.Elapsed;
var sleep = targetTime - actualTime;
if (sleep > TimeSpan.Zero)
{
using (var waitHandle = new AutoResetEvent(initialState: false))
{
scheduler.Sleep(sleep).GetAwaiter().OnCompleted(() => waitHandle.Set());
waitHandle.WaitOne();
}
}
}
public override bool CanRead
{
get { return parent.CanRead; }
}
public override bool CanSeek
{
get { return parent.CanSeek; }
}
public override bool CanWrite
{
get { return parent.CanWrite; }
}
public override void Flush()
{
parent.Flush();
}
public override long Length
{
get { return parent.Length; }
}
public override long Position
{
get
{
return parent.Position;
}
set
{
parent.Position = value;
}
}
public override int Read(byte[] buffer, int offset, int count)
{
var read = parent.Read(buffer, offset, count);
Throttle(read);
return read;
}
public override long Seek(long offset, SeekOrigin origin)
{
return parent.Seek(offset, origin);
}
public override void SetLength(long value)
{
parent.SetLength(value);
}
public override void Write(byte[] buffer, int offset, int count)
{
Throttle(count);
parent.Write(buffer, offset, count);
}
}
and some tests that just take some milliseconds:
[TestMethod]
public void ShouldThrottleReading()
{
var content = Enumerable
.Range(0, 1024 * 1024)
.Select(_ => (byte)'a')
.ToArray();
var scheduler = new TestScheduler();
var source = new ThrottledStream(new MemoryStream(content), content.Length / 8, scheduler);
var target = new MemoryStream();
var t = source.CopyToAsync(target);
t.Wait(10).Should().BeFalse();
scheduler.AdvanceTo(TimeSpan.FromSeconds(4).Ticks);
t.Wait(10).Should().BeFalse();
scheduler.AdvanceTo(TimeSpan.FromSeconds(8).Ticks - 1);
t.Wait(10).Should().BeFalse();
scheduler.AdvanceTo(TimeSpan.FromSeconds(8).Ticks);
t.Wait(10).Should().BeTrue();
}
[TestMethod]
public void ShouldThrottleWriting()
{
var content = Enumerable
.Range(0, 1024 * 1024)
.Select(_ => (byte)'a')
.ToArray();
var scheduler = new TestScheduler();
var source = new MemoryStream(content);
var target = new ThrottledStream(new MemoryStream(), content.Length / 8, scheduler);
var t = source.CopyToAsync(target);
t.Wait(10).Should().BeFalse();
scheduler.AdvanceTo(TimeSpan.FromSeconds(4).Ticks);
t.Wait(10).Should().BeFalse();
scheduler.AdvanceTo(TimeSpan.FromSeconds(8).Ticks - 1);
t.Wait(10).Should().BeFalse();
scheduler.AdvanceTo(TimeSpan.FromSeconds(8).Ticks);
t.Wait(10).Should().BeTrue();
}
I came up with a different implementation of the ThrottledStream-Class mentioned by arul. My version uses a WaitHandle and a Timer with a 1s Interval:
public ThrottledStream(Stream parentStream, int maxBytesPerSecond=int.MaxValue)
{
MaxBytesPerSecond = maxBytesPerSecond;
parent = parentStream;
processed = 0;
resettimer = new System.Timers.Timer();
resettimer.Interval = 1000;
resettimer.Elapsed += resettimer_Elapsed;
resettimer.Start();
}
protected void Throttle(int bytes)
{
try
{
processed += bytes;
if (processed >= maxBytesPerSecond)
wh.WaitOne();
}
catch
{
}
}
private void resettimer_Elapsed(object sender, ElapsedEventArgs e)
{
processed = 0;
wh.Set();
}
Whenever the bandwidth-limit exceeds the Thread will sleep until the next second begins. No need to calculate the optimal sleep duration.
Full Implementation:
public class ThrottledStream : Stream
{
#region Properties
private int maxBytesPerSecond;
/// <summary>
/// Number of Bytes that are allowed per second
/// </summary>
public int MaxBytesPerSecond
{
get { return maxBytesPerSecond; }
set
{
if (value < 1)
throw new ArgumentException("MaxBytesPerSecond has to be >0");
maxBytesPerSecond = value;
}
}
#endregion
#region Private Members
private int processed;
System.Timers.Timer resettimer;
AutoResetEvent wh = new AutoResetEvent(true);
private Stream parent;
#endregion
/// <summary>
/// Creates a new Stream with Databandwith cap
/// </summary>
/// <param name="parentStream"></param>
/// <param name="maxBytesPerSecond"></param>
public ThrottledStream(Stream parentStream, int maxBytesPerSecond=int.MaxValue)
{
MaxBytesPerSecond = maxBytesPerSecond;
parent = parentStream;
processed = 0;
resettimer = new System.Timers.Timer();
resettimer.Interval = 1000;
resettimer.Elapsed += resettimer_Elapsed;
resettimer.Start();
}
protected void Throttle(int bytes)
{
try
{
processed += bytes;
if (processed >= maxBytesPerSecond)
wh.WaitOne();
}
catch
{
}
}
private void resettimer_Elapsed(object sender, ElapsedEventArgs e)
{
processed = 0;
wh.Set();
}
#region Stream-Overrides
public override void Close()
{
resettimer.Stop();
resettimer.Close();
base.Close();
}
protected override void Dispose(bool disposing)
{
resettimer.Dispose();
base.Dispose(disposing);
}
public override bool CanRead
{
get { return parent.CanRead; }
}
public override bool CanSeek
{
get { return parent.CanSeek; }
}
public override bool CanWrite
{
get { return parent.CanWrite; }
}
public override void Flush()
{
parent.Flush();
}
public override long Length
{
get { return parent.Length; }
}
public override long Position
{
get
{
return parent.Position;
}
set
{
parent.Position = value;
}
}
public override int Read(byte[] buffer, int offset, int count)
{
Throttle(count);
return parent.Read(buffer, offset, count);
}
public override long Seek(long offset, SeekOrigin origin)
{
return parent.Seek(offset, origin);
}
public override void SetLength(long value)
{
parent.SetLength(value);
}
public override void Write(byte[] buffer, int offset, int count)
{
Throttle(count);
parent.Write(buffer, offset, count);
}
#endregion
}

Multithreading SslStream C#

I'm writing an https server which inherit from an existing http server.
The Http server using a ImprovedTCPServerSocket which wrap Socket and when send/receive he is locking the scope as you can see:
public class ImprovedTCPServerSocket
{
private readonly Object m_SocketLocker = new Object();
protected Socket m_Socket;
public ImprovedTCPServerSocket(Socket mSocket)
{
m_Socket = mSocket;
}
public void Send(byte[] buffer, int offset, int size)
{
Socket mySocket;
lock (m_SocketLocker)
{
mySocket = m_Socket;
}
mySocket.Send(buffer, offset, size, SocketFlags.None);
}
protected int Receive(int requiredBytesToRead, byte[] receivedBytes, TimeSpan timeout, bool doReceive)
{
try
{
Socket mySocket;
lock (m_SocketLocker)
{
mySocket = m_Socket;
}
while (some condition){
currentBytesOffset += InternalReceive(mySocket, receivedBytes);
}
}
catch() {
.....
}
}
protected virtual int InternalReceive(Socket mySocket, byte[] receivedBytesת int currentBytesOffset)
{
currentBytesOffset += mySocket.Receive(receivedBytes, // collect given input.
currentBytesOffset, // offset.
bytesAvailableToRead, // number of bytes to read
flags,
out socketError);
return currentBytesOffset;
}
public virtual void Close()
{
m_Socket.Close();
}
}
Now in order to implement an https server I created a new class ImprovedSslStream which wrap SslStream and inherit from ImprovedTCPServerSocket
I'm using SslStream Do I need to do the same lock to m_SslStream as we made to m_Socket or is it ok to use it like this?
Here is my code:
public class ImprovedSslStream : ImprovedTCPServerSocket
{
private X509Certificate m_Certificate;
private SslStream m_SslStream;
public ImprovedSslStream(Socket mSocket, X509Certificate serverCertificate) : base(mSocket)
{
m_Certificate = serverCertificate;
}
public void Send(byte[] buffer, int offset, int size)
{
//
sslStream.Write(.....)
}
protected override int GetCurrentByteOffset(Socket mySocket, byte[] receivedBytes, int currentBytesOffset,
int bytesAvailableToRead, bool doReceive)
{
m_SslStream=new SslStream(new NetworkStream(mySocket),false); //Do I need to lock it ?
try
{
m_SslStream.AuthenticateAsServer(m_Certificate,
false, SslProtocols.Tls, true);
currentBytesOffset+=m_SslStream.Read(receivedBytes, currentBytesOffset, bytesAvailableToRead);
}
catch (AuthenticationException e)
{
}
return currentBytesOffset;
}
}
}

Streaming input to System.Speech.Recognition.SpeechRecognitionEngine

I am trying to do "streaming" speech recognition in C# from a TCP socket. The problem I am having is that SpeechRecognitionEngine.SetInputToAudioStream() seems to require a Stream of a defined length which can seek. Right now the only way I can think to make this work is to repeatedly run the recognizer on a MemoryStream as more input comes in.
Here's some code to illustrate:
SpeechRecognitionEngine appRecognizer = new SpeechRecognitionEngine();
System.Speech.AudioFormat.SpeechAudioFormatInfo formatInfo = new System.Speech.AudioFormat.SpeechAudioFormatInfo(8000, System.Speech.AudioFormat.AudioBitsPerSample.Sixteen, System.Speech.AudioFormat.AudioChannel.Mono);
NetworkStream stream = new NetworkStream(socket,true);
appRecognizer.SetInputToAudioStream(stream, formatInfo);
// At the line above a "NotSupportedException" complaining that "This stream does not support seek operations."
Does anyone know how to get around this? It must support streaming input of some sort, since it works fine with the microphone using SetInputToDefaultAudioDevice().
Thanks, Sean
I got live speech recognition working by overriding the stream class:
class SpeechStreamer : Stream
{
private AutoResetEvent _writeEvent;
private List<byte> _buffer;
private int _buffersize;
private int _readposition;
private int _writeposition;
private bool _reset;
public SpeechStreamer(int bufferSize)
{
_writeEvent = new AutoResetEvent(false);
_buffersize = bufferSize;
_buffer = new List<byte>(_buffersize);
for (int i = 0; i < _buffersize;i++ )
_buffer.Add(new byte());
_readposition = 0;
_writeposition = 0;
}
public override bool CanRead
{
get { return true; }
}
public override bool CanSeek
{
get { return false; }
}
public override bool CanWrite
{
get { return true; }
}
public override long Length
{
get { return -1L; }
}
public override long Position
{
get { return 0L; }
set { }
}
public override long Seek(long offset, SeekOrigin origin)
{
return 0L;
}
public override void SetLength(long value)
{
}
public override int Read(byte[] buffer, int offset, int count)
{
int i = 0;
while (i<count && _writeEvent!=null)
{
if (!_reset && _readposition >= _writeposition)
{
_writeEvent.WaitOne(100, true);
continue;
}
buffer[i] = _buffer[_readposition+offset];
_readposition++;
if (_readposition == _buffersize)
{
_readposition = 0;
_reset = false;
}
i++;
}
return count;
}
public override void Write(byte[] buffer, int offset, int count)
{
for (int i = offset; i < offset+count; i++)
{
_buffer[_writeposition] = buffer[i];
_writeposition++;
if (_writeposition == _buffersize)
{
_writeposition = 0;
_reset = true;
}
}
_writeEvent.Set();
}
public override void Close()
{
_writeEvent.Close();
_writeEvent = null;
base.Close();
}
public override void Flush()
{
}
}
... and using an instance of that as the stream input to the SetInputToAudioStream method. As soon as the stream returns a length or the returned count is less than that requested the recognition engine thinks the input has finished. This sets up a circular buffer that never finishes.
Have you tried wrapping the network stream in a System.IO.BufferedStream?
NetworkStream netStream = new NetworkStream(socket,true);
BufferedStream buffStream = new BufferedStream(netStream, 8000*16*1); // buffers 1 second worth of data
appRecognizer.SetInputToAudioStream(buffStream, formatInfo);
Apparently it can't be done ("By design"!). See http://social.msdn.microsoft.com/Forums/en/netfxbcl/thread/fcf62d6d-19df-4ca9-9f1f-17724441f84e
This is my solution.
class FakeStreamer : Stream
{
public bool bExit = false;
Stream stream;
TcpClient client;
public FakeStreamer(TcpClient client)
{
this.client = client;
this.stream = client.GetStream();
this.stream.ReadTimeout = 100; //100ms
}
public override bool CanRead
{
get { return stream.CanRead; }
}
public override bool CanSeek
{
get { return false; }
}
public override bool CanWrite
{
get { return stream.CanWrite; }
}
public override long Length
{
get { return -1L; }
}
public override long Position
{
get { return 0L; }
set { }
}
public override long Seek(long offset, SeekOrigin origin)
{
return 0L;
}
public override void SetLength(long value)
{
stream.SetLength(value);
}
public override int Read(byte[] buffer, int offset, int count)
{
int len = 0, c = count;
while (c > 0 && !bExit)
{
try
{
len = stream.Read(buffer, offset, c);
}
catch (Exception e)
{
if (e.HResult == -2146232800) // Timeout
{
continue;
}
else
{
//Exit read loop
break;
}
}
if (!client.Connected || len == 0)
{
//Exit read loop
return 0;
}
offset += len;
c -= len;
}
return count;
}
public override void Write(byte[] buffer, int offset, int count)
{
stream.Write(buffer,offset,count);
}
public override void Close()
{
stream.Close();
base.Close();
}
public override void Flush()
{
stream.Flush();
}
}
How to Use:
//client connect in
TcpClient clientSocket = ServerSocket.AcceptTcpClient();
FakeStreamer buffStream = new FakeStreamer(clientSocket);
...
//recognizer init
m_recognizer.SetInputToAudioStream(buffStream , audioFormat);
...
//recognizer end
if (buffStream != null)
buffStream.bExit = true;
I ended up buffering the input and then sending it to the speech recognition engine in successively larger chunks. For instance, I might send at first the first 0.25 seconds, then the first 0.5 seconds, then the first 0.75 seconds, and so on until I get a result. I am not sure if this is the most efficient way of going about this, but it yields satisfactory results for me.
Best of luck, Sean

Bandwidth throttling in C#

I am developing a program that continually sends a stream of data in the background and I want to allow the user to set a cap for both upload and download limit.
I have read up on the token bucket and leaky bucket alghorhithms, and seemingly the latter seems to fit the description since this is not a matter of maximizing the network bandwidth but rather being as unobtrusive as possible.
I am however a bit unsure on how I would implement this. A natural approach is to extend the abstract Stream class to make it simple to extend existing traffic, but would this not require the involvement of extra threads to send the data while simultaneously receiving (leaky bucket)? Any hints on other implementations that do the same would be appreciated.
Also, although I can modify how much data the program receives, how well does bandwidth throttling work at the C# level? Will the computer still receive the data and simply save it, effectively canceling the throttling effect or will it wait until I ask to receive more?
EDIT: I am interested in throttling both incoming and outgoing data, where I have no control over the opposite end of the stream.
Based on #0xDEADBEEF's solution I created the following (testable) solution based on Rx schedulers:
public class ThrottledStream : Stream
{
private readonly Stream parent;
private readonly int maxBytesPerSecond;
private readonly IScheduler scheduler;
private readonly IStopwatch stopwatch;
private long processed;
public ThrottledStream(Stream parent, int maxBytesPerSecond, IScheduler scheduler)
{
this.maxBytesPerSecond = maxBytesPerSecond;
this.parent = parent;
this.scheduler = scheduler;
stopwatch = scheduler.StartStopwatch();
processed = 0;
}
public ThrottledStream(Stream parent, int maxBytesPerSecond)
: this (parent, maxBytesPerSecond, Scheduler.Immediate)
{
}
protected void Throttle(int bytes)
{
processed += bytes;
var targetTime = TimeSpan.FromSeconds((double)processed / maxBytesPerSecond);
var actualTime = stopwatch.Elapsed;
var sleep = targetTime - actualTime;
if (sleep > TimeSpan.Zero)
{
using (var waitHandle = new AutoResetEvent(initialState: false))
{
scheduler.Sleep(sleep).GetAwaiter().OnCompleted(() => waitHandle.Set());
waitHandle.WaitOne();
}
}
}
public override bool CanRead
{
get { return parent.CanRead; }
}
public override bool CanSeek
{
get { return parent.CanSeek; }
}
public override bool CanWrite
{
get { return parent.CanWrite; }
}
public override void Flush()
{
parent.Flush();
}
public override long Length
{
get { return parent.Length; }
}
public override long Position
{
get
{
return parent.Position;
}
set
{
parent.Position = value;
}
}
public override int Read(byte[] buffer, int offset, int count)
{
var read = parent.Read(buffer, offset, count);
Throttle(read);
return read;
}
public override long Seek(long offset, SeekOrigin origin)
{
return parent.Seek(offset, origin);
}
public override void SetLength(long value)
{
parent.SetLength(value);
}
public override void Write(byte[] buffer, int offset, int count)
{
Throttle(count);
parent.Write(buffer, offset, count);
}
}
and some tests that just take some milliseconds:
[TestMethod]
public void ShouldThrottleReading()
{
var content = Enumerable
.Range(0, 1024 * 1024)
.Select(_ => (byte)'a')
.ToArray();
var scheduler = new TestScheduler();
var source = new ThrottledStream(new MemoryStream(content), content.Length / 8, scheduler);
var target = new MemoryStream();
var t = source.CopyToAsync(target);
t.Wait(10).Should().BeFalse();
scheduler.AdvanceTo(TimeSpan.FromSeconds(4).Ticks);
t.Wait(10).Should().BeFalse();
scheduler.AdvanceTo(TimeSpan.FromSeconds(8).Ticks - 1);
t.Wait(10).Should().BeFalse();
scheduler.AdvanceTo(TimeSpan.FromSeconds(8).Ticks);
t.Wait(10).Should().BeTrue();
}
[TestMethod]
public void ShouldThrottleWriting()
{
var content = Enumerable
.Range(0, 1024 * 1024)
.Select(_ => (byte)'a')
.ToArray();
var scheduler = new TestScheduler();
var source = new MemoryStream(content);
var target = new ThrottledStream(new MemoryStream(), content.Length / 8, scheduler);
var t = source.CopyToAsync(target);
t.Wait(10).Should().BeFalse();
scheduler.AdvanceTo(TimeSpan.FromSeconds(4).Ticks);
t.Wait(10).Should().BeFalse();
scheduler.AdvanceTo(TimeSpan.FromSeconds(8).Ticks - 1);
t.Wait(10).Should().BeFalse();
scheduler.AdvanceTo(TimeSpan.FromSeconds(8).Ticks);
t.Wait(10).Should().BeTrue();
}
I came up with a different implementation of the ThrottledStream-Class mentioned by arul. My version uses a WaitHandle and a Timer with a 1s Interval:
public ThrottledStream(Stream parentStream, int maxBytesPerSecond=int.MaxValue)
{
MaxBytesPerSecond = maxBytesPerSecond;
parent = parentStream;
processed = 0;
resettimer = new System.Timers.Timer();
resettimer.Interval = 1000;
resettimer.Elapsed += resettimer_Elapsed;
resettimer.Start();
}
protected void Throttle(int bytes)
{
try
{
processed += bytes;
if (processed >= maxBytesPerSecond)
wh.WaitOne();
}
catch
{
}
}
private void resettimer_Elapsed(object sender, ElapsedEventArgs e)
{
processed = 0;
wh.Set();
}
Whenever the bandwidth-limit exceeds the Thread will sleep until the next second begins. No need to calculate the optimal sleep duration.
Full Implementation:
public class ThrottledStream : Stream
{
#region Properties
private int maxBytesPerSecond;
/// <summary>
/// Number of Bytes that are allowed per second
/// </summary>
public int MaxBytesPerSecond
{
get { return maxBytesPerSecond; }
set
{
if (value < 1)
throw new ArgumentException("MaxBytesPerSecond has to be >0");
maxBytesPerSecond = value;
}
}
#endregion
#region Private Members
private int processed;
System.Timers.Timer resettimer;
AutoResetEvent wh = new AutoResetEvent(true);
private Stream parent;
#endregion
/// <summary>
/// Creates a new Stream with Databandwith cap
/// </summary>
/// <param name="parentStream"></param>
/// <param name="maxBytesPerSecond"></param>
public ThrottledStream(Stream parentStream, int maxBytesPerSecond=int.MaxValue)
{
MaxBytesPerSecond = maxBytesPerSecond;
parent = parentStream;
processed = 0;
resettimer = new System.Timers.Timer();
resettimer.Interval = 1000;
resettimer.Elapsed += resettimer_Elapsed;
resettimer.Start();
}
protected void Throttle(int bytes)
{
try
{
processed += bytes;
if (processed >= maxBytesPerSecond)
wh.WaitOne();
}
catch
{
}
}
private void resettimer_Elapsed(object sender, ElapsedEventArgs e)
{
processed = 0;
wh.Set();
}
#region Stream-Overrides
public override void Close()
{
resettimer.Stop();
resettimer.Close();
base.Close();
}
protected override void Dispose(bool disposing)
{
resettimer.Dispose();
base.Dispose(disposing);
}
public override bool CanRead
{
get { return parent.CanRead; }
}
public override bool CanSeek
{
get { return parent.CanSeek; }
}
public override bool CanWrite
{
get { return parent.CanWrite; }
}
public override void Flush()
{
parent.Flush();
}
public override long Length
{
get { return parent.Length; }
}
public override long Position
{
get
{
return parent.Position;
}
set
{
parent.Position = value;
}
}
public override int Read(byte[] buffer, int offset, int count)
{
Throttle(count);
return parent.Read(buffer, offset, count);
}
public override long Seek(long offset, SeekOrigin origin)
{
return parent.Seek(offset, origin);
}
public override void SetLength(long value)
{
parent.SetLength(value);
}
public override void Write(byte[] buffer, int offset, int count)
{
Throttle(count);
parent.Write(buffer, offset, count);
}
#endregion
}

Categories