I have 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 the classic problem of consumer producers: 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 be blocked if data is not available (until a timeout occurs). The implementation must use a fixed-size internal buffer, so the callback method should block when the buffer is currently full.
Since thinking about multithreading usually leads to a serious headache, my question is: Is there already an implementation of such a data structure?
(I think the implementation of the Read method should be fairly simple, but I would like to avoid implementing the Begin / EndRead with Read. Begin / EndInvoke .)
dtb
source share