private InvokeCallback ( bool completedSynchronously, object result ) : void | ||
completedSynchronously | bool | |
result | object | |
return | void |
public override IAsyncResult BeginRead(byte[] array, int offset, int count, AsyncCallback asyncCallback, object asyncState) { IAsyncResult result2; this.EnsureDecompressionMode(); if (this.asyncOperations != 0) { throw new InvalidOperationException("Only one asynchronous reader is allowed time at one time."); } Interlocked.Increment(ref this.asyncOperations); try { this.ValidateParameters(array, offset, count); DeflateStreamAsyncResult state = new DeflateStreamAsyncResult(this, asyncState, asyncCallback, array, offset, count); state.isWrite = false; int result = this.inflater.Inflate(array, offset, count); if (result != 0) { state.InvokeCallback(true, result); return state; } if (this.inflater.Finished()) { state.InvokeCallback(true, 0); return state; } this._stream.BeginRead(this.buffer, 0, this.buffer.Length, this.m_CallBack, state); state.m_CompletedSynchronously &= state.IsCompleted; result2 = state; } catch { Interlocked.Decrement(ref this.asyncOperations); throw; } return result2; }
// callback function for asynchrous reading on base stream private void ReadCallback(IAsyncResult baseStreamResult) { DeflateStreamAsyncResult outerResult = (DeflateStreamAsyncResult)baseStreamResult.AsyncState; outerResult.m_CompletedSynchronously &= baseStreamResult.CompletedSynchronously; int bytesRead = 0; try { EnsureNotDisposed(); bytesRead = _stream.EndRead(baseStreamResult); if (bytesRead <= 0) { // This indicates the base stream has received EOF outerResult.InvokeCallback((object)0); return; } // Feed the data from base stream into decompression engine inflater.SetInput(buffer, 0, bytesRead); bytesRead = inflater.Inflate(outerResult.buffer, outerResult.offset, outerResult.count); if (bytesRead == 0 && !inflater.Finished()) { // We could have read in head information and didn't get any data. // Read from the base stream again. // Need to solve recusion. _stream.BeginRead(buffer, 0, buffer.Length, m_CallBack, outerResult); } else { outerResult.InvokeCallback((object)bytesRead); } } catch (Exception exc) { // Defer throwing this until EndRead where we will likely have user code on the stack. outerResult.InvokeCallback(exc); return; } }
public override IAsyncResult BeginRead(byte[] array, int offset, int count, AsyncCallback asyncCallback, object asyncState) { IAsyncResult result2; this.EnsureDecompressionMode(); if (this.asyncOperations != 0) { throw new InvalidOperationException(SR.GetString("InvalidBeginCall")); } Interlocked.Increment(ref this.asyncOperations); try { this.ValidateParameters(array, offset, count); DeflateStreamAsyncResult state = new DeflateStreamAsyncResult(this, asyncState, asyncCallback, array, offset, count) { isWrite = false }; int result = this.inflater.Inflate(array, offset, count); if (result != 0) { state.InvokeCallback(true, result); return(state); } if (this.inflater.Finished()) { state.InvokeCallback(true, 0); return(state); } this._stream.BeginRead(this.buffer, 0, this.buffer.Length, this.m_CallBack, state); state.m_CompletedSynchronously &= state.IsCompleted; result2 = state; } catch { Interlocked.Decrement(ref this.asyncOperations); throw; } return(result2); }
public override IAsyncResult BeginRead(byte[] array, int offset, int count, AsyncCallback asyncCallback, object asyncState) { EnsureDecompressionMode(); // We use this checking order for compat to earlier versions: if (asyncOperations != 0) throw new InvalidOperationException(SR.GetString(SR.InvalidBeginCall)); ValidateParameters(array, offset, count); EnsureNotDisposed(); Interlocked.Increment(ref asyncOperations); try { DeflateStreamAsyncResult userResult = new DeflateStreamAsyncResult( this, asyncState, asyncCallback, array, offset, count); userResult.isWrite = false; // Try to read decompressed data in output buffer int bytesRead = inflater.Inflate(array, offset, count); if( bytesRead != 0) { // If decompression output buffer is not empty, return immediately. // 'true' means we complete synchronously. userResult.InvokeCallback(true, (object) bytesRead); return userResult; } if (inflater.Finished() ) { // end of compression stream userResult.InvokeCallback(true, (object) 0); return userResult; } // If there is no data on the output buffer and we are not at // the end of the stream, we need to get more data from the base stream _stream.BeginRead(buffer, 0, buffer.Length, m_CallBack, userResult); userResult.m_CompletedSynchronously &= userResult.IsCompleted; return userResult; } catch { Interlocked.Decrement( ref asyncOperations); throw; } }