//[HostProtection(ExternalThreading = true)] public override IAsyncResult BeginWrite(byte[] array, int offset, int count, AsyncCallback asyncCallback, object asyncState) { EnsureCompressionMode(); if (_asyncOperations != 0) { throw new InvalidOperationException("SR.InvalidBeginCall"); } Interlocked.Increment(ref _asyncOperations); try { ValidateParameters(array, offset, count); DeflateStreamAsyncResult userResult = new DeflateStreamAsyncResult( this, asyncState, asyncCallback, array, offset, count); userResult.IsWrite = true; _mAsyncWriterDelegate.BeginInvoke(array, offset, count, true, _mCallBack, userResult); userResult.MCompletedSynchronously &= userResult.IsCompleted; return(userResult); } catch { Interlocked.Decrement(ref _asyncOperations); throw; } }
//[HostProtection(ExternalThreading = true)] public override IAsyncResult BeginRead(byte[] array, int offset, int count, AsyncCallback asyncCallback, object asyncState) { EnsureDecompressionMode(); if (_asyncOperations != 0) { throw new InvalidOperationException("SR.InvalidBeginCall"); } Interlocked.Increment(ref _asyncOperations); try { ValidateParameters(array, offset, count); DeflateStreamAsyncResult userResult = new DeflateStreamAsyncResult( this, asyncState, asyncCallback, array, offset, count) { 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, _mCallBack, userResult); userResult.MCompletedSynchronously &= userResult.IsCompleted; return(userResult); } catch { Interlocked.Decrement(ref _asyncOperations); throw; } }
public override int EndRead(IAsyncResult asyncResult) { EnsureDecompressionMode(); if (_asyncOperations != 1) { throw new InvalidOperationException("SR.InvalidEndCall"); } if (asyncResult == null) { throw new ArgumentNullException("asyncResult"); } if (_stream == null) { throw new InvalidOperationException("SR.ObjectDisposed_StreamClosed"); } DeflateStreamAsyncResult myResult = asyncResult as DeflateStreamAsyncResult; if (myResult == null) { throw new ArgumentNullException("asyncResult"); } try { if (!myResult.IsCompleted) { myResult.AsyncWaitHandle.WaitOne(); } } finally { Interlocked.Decrement(ref _asyncOperations); // this will just close the wait handle myResult.Close(); } if (myResult.Result is Exception) { throw (Exception)(myResult.Result); } return((int)myResult.Result); }
// callback function for asynchrous reading on base stream private void WriteCallback(IAsyncResult asyncResult) { DeflateStreamAsyncResult outerResult = (DeflateStreamAsyncResult)asyncResult.AsyncState; outerResult.MCompletedSynchronously &= asyncResult.CompletedSynchronously; try { _mAsyncWriterDelegate.EndInvoke(asyncResult); } catch (Exception exc) { // Defer throwing this until EndXxx where we are ensured of user code on the stack. outerResult.InvokeCallback(exc); return; } outerResult.InvokeCallback(null); }
// callback function for asynchrous reading on base stream private void ReadCallback(IAsyncResult baseStreamResult) { DeflateStreamAsyncResult outerResult = (DeflateStreamAsyncResult)baseStreamResult.AsyncState; outerResult.MCompletedSynchronously &= baseStreamResult.CompletedSynchronously; int bytesRead = 0; try { bytesRead = _stream.EndRead(baseStreamResult); } catch (Exception exc) { // Defer throwing this until EndXxx where we are ensured of user code on the stack. outerResult.InvokeCallback(exc); return; } 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, _mCallBack, outerResult); } else { outerResult.InvokeCallback((object)bytesRead); } }
//[HostProtection(ExternalThreading = true)] public override IAsyncResult BeginWrite(byte[] array, int offset, int count, AsyncCallback asyncCallback, object asyncState) { EnsureCompressionMode(); if (_asyncOperations != 0) { throw new InvalidOperationException("SR.InvalidBeginCall"); } Interlocked.Increment(ref _asyncOperations); try { ValidateParameters(array, offset, count); DeflateStreamAsyncResult userResult = new DeflateStreamAsyncResult( this, asyncState, asyncCallback, array, offset, count); userResult.IsWrite = true; _mAsyncWriterDelegate.BeginInvoke(array, offset, count, true, _mCallBack, userResult); userResult.MCompletedSynchronously &= userResult.IsCompleted; return userResult; } catch { Interlocked.Decrement(ref _asyncOperations); throw; } }
//[HostProtection(ExternalThreading = true)] public override IAsyncResult BeginRead(byte[] array, int offset, int count, AsyncCallback asyncCallback, object asyncState) { EnsureDecompressionMode(); if (_asyncOperations != 0) { throw new InvalidOperationException("SR.InvalidBeginCall"); } Interlocked.Increment(ref _asyncOperations); try { ValidateParameters(array, offset, count); DeflateStreamAsyncResult userResult = new DeflateStreamAsyncResult( this, asyncState, asyncCallback, array, offset, count) {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, _mCallBack, userResult); userResult.MCompletedSynchronously &= userResult.IsCompleted; return userResult; } catch { Interlocked.Decrement(ref _asyncOperations); throw; } }