Example #1
0
        public static Exception AttemptClone(Exception ex, RethrowableWrapperBehavior behavior)
        {
            LateBoundMethod lateBoundMethod;
            Exception       rethrowableClone = null;

            if (ex == null)
            {
                return(null);
            }
            IRethrowableException rethrowableException = ex as IRethrowableException;

            if (rethrowableException != null)
            {
                rethrowableClone = rethrowableException.GetRethrowableClone();
            }
            else if (ExceptionCloner.exceptionTypesWhichCanBeCloned.TryGetValue(ex.GetType(), out lateBoundMethod))
            {
                object[] objArray = new object[] { ex };
                rethrowableClone = (Exception)lateBoundMethod(null, objArray);
            }
            else if (behavior != RethrowableWrapperBehavior.NoWrap)
            {
                rethrowableClone = RethrowableWrapperException.MakeRethrowable(ex);
            }
            else
            {
                ExceptionCloner.remoteStackTraceSetter(ex, ex.StackTrace);
                rethrowableClone = ex;
            }
            return(rethrowableClone);
        }
Example #2
0
 public void End(out Exception exceptionToThrow, RethrowableWrapperBehavior wrapperBehavior)
 {
     if (AsyncResultBase.completeWaitHandleSentinel != this.waitHandle)
     {
         this.AsyncWaitHandle.WaitOne();
         this.waitHandle.Close();
         this.waitHandle = null;
     }
     exceptionToThrow = null;
     try
     {
         exceptionToThrow = ExceptionCloner.AttemptClone(this.exception, wrapperBehavior);
     }
     catch (TypeInitializationException typeInitializationException)
     {
     }
     this.exception = null;
 }
Example #3
0
        private static IEnumerator <IAsyncResult> AsyncStreamCopyImpl(Stream source, Stream destination, long numBytes, int bufferSize, AsyncStreamCopy.ComputeAndLogCrcMethod computeCrcMethodForCrcLogging, TimeSpan timeout, AsyncIteratorContext <long> context)
        {
            Exception     exception;
            BufferWrapper buffer        = null;
            BufferWrapper bufferWrapper = null;

            try
            {
                Duration     startingNow  = Duration.StartingNow;
                IAsyncResult asyncResult  = null;
                IAsyncResult asyncResult1 = null;
                buffer        = BufferPool.GetBuffer(bufferSize);
                bufferWrapper = BufferPool.GetBuffer(bufferSize);
                long num = (long)0;
                context.ResultData = (long)0;
                bool canTimeout = source.CanTimeout;
                bool flag       = destination.CanTimeout;
                while (true)
                {
                    int timeoutInMS = AsyncStreamCopy.TimeSpanToTimeoutInMS(startingNow.Remaining(timeout));
                    if (timeoutInMS == 0)
                    {
                        throw new TimeoutException("The asynchronous stream copy timed out.");
                    }
                    if (canTimeout)
                    {
                        try
                        {
                            source.ReadTimeout = timeoutInMS;
                        }
                        catch (InvalidOperationException invalidOperationException)
                        {
                            canTimeout = false;
                        }
                    }
                    if (flag)
                    {
                        try
                        {
                            destination.WriteTimeout = timeoutInMS;
                        }
                        catch (InvalidOperationException invalidOperationException1)
                        {
                            flag = false;
                        }
                    }
                    if (numBytes > (long)0)
                    {
                        asyncResult = source.BeginRead(buffer.Buffer, 0, (int)Math.Min((long)((int)buffer.Buffer.Length), numBytes), context.GetResumeCallback(), context.GetResumeState("AsyncStreamCopy.source.Read"));
                    }
                    exception = null;
                    if (num > (long)0)
                    {
                        try
                        {
                            asyncResult1 = destination.BeginWrite(bufferWrapper.Buffer, 0, (int)num, context.GetResumeCallback(), context.GetResumeState("AsyncStreamCopy.dest.Write"));
                        }
                        catch (Exception exception1)
                        {
                            exception = exception1;
                        }
                        if (exception == null)
                        {
                            yield return(asyncResult1);

                            try
                            {
                                destination.EndWrite(asyncResult1);
                            }
                            catch (Exception exception2)
                            {
                                exception = exception2;
                            }
                        }
                    }
                    if (numBytes <= (long)0)
                    {
                        num = (long)0;
                    }
                    else
                    {
                        yield return(asyncResult);

                        num = (long)source.EndRead(asyncResult);
                    }
                    if (exception != null)
                    {
                        throw ExceptionCloner.AttemptClone(exception, RethrowableWrapperBehavior.NoWrap);
                    }
                    if (num < (long)0)
                    {
                        break;
                    }
                    numBytes -= num;
                    AsyncIteratorContext <long> resultData = context;
                    resultData.ResultData = resultData.ResultData + num;
                    if (computeCrcMethodForCrcLogging != null)
                    {
                        computeCrcMethodForCrcLogging(buffer.Buffer, 0, (int)num, context.ResultData);
                    }
                    AsyncStreamCopy.Swap <BufferWrapper>(ref buffer, ref bufferWrapper);
                    if (num <= (long)0)
                    {
                        goto Label2;
                    }
                }
                throw new TimeoutException("Reading from the stream resulted in a negative number of bytes read. This typically means an HttpWebRequest was aborted.");
            }
            finally
            {
                if (buffer != null)
                {
                    BufferPool.ReleaseBuffer(buffer);
                }
                if (bufferWrapper != null)
                {
                    BufferPool.ReleaseBuffer(bufferWrapper);
                }
            }
Label2:
            yield break;
            throw new TimeoutException("The asynchronous stream copy timed out.");
            throw ExceptionCloner.AttemptClone(exception, RethrowableWrapperBehavior.NoWrap);
        }