// The requirements are: // * We must read at least one byte from the stream every time // we get a HasBytesAvailable event. // * SerializeToStreamAsync is executed on a separate thread, // so reads must somehow be synchronized with that thread. // // Current implementation: // * We read data in ReadStreamData (on the same thread // we got the HasBytesAvailable event, i.e. inside the // HasBytesAvailable event handler). // * Data is stored in a class-level buffer. // * SerializeToStreamAsync blocks while waiting for // data from ReadStreamData. // * ReadStreamData will only read more data once SerializeToStreamAsync // has consumed any existing data. This means we'll be // blocking in the HasBytesAvailable event handler until // any previously read data has been processed (this prevents // any unbound memory growth). public CFContentStream (CFHTTPStream stream) { this.http_stream = stream; data = new BufferData () { Buffer = new byte [4096], }; data_event = new AutoResetEvent (false); data_read_event = new AutoResetEvent (true); data_mutex = new Mutex (); }
public void Close () { data_read_event.WaitOne (); // make sure there's no pending data data_mutex.WaitOne (); data = null; data_mutex.ReleaseMutex (); data_event.Set (); }