I've got a callback method that is called whenever new data is available:
public delegate void DataCallback( byte[] buffer, int offset, int count); I want to wrap this in a class that implements an interface similar to this:
public interface IDataSource { IAsyncResult BeginRead( byte[] buffer, int offset, int size, TimeSpan timeout, AsyncCallback callback, object state); int EndRead( IAsyncResult asyncResult); int Read( byte[] buffer, int offset, int size, TimeSpan timeout); } This is obviously a classical producer-consumer problem: the bytes are produced by calls to the callback method, and consumed by the Begin/EndRead and Read methods. The Begin/EndRead and Read methods should block if no data is available (until a timeout occurs). The implementation should use a fixed-size internal buffer, so the callback method needs to block when the buffer is currently full.
Since thinking about multithreading usually results in a severe headache, my question is: Is there already an implementation of such a data structure?
(I think implementing the Read method should be quite simple, but I'd like to avoid implementing Begin/EndRead with Read.Begin/EndInvoke.)