Example #1
0
        private static void ForceAbort(AsyncStreamCopier <T> copier, bool timedOut)
        {
            ExecutionState <T> state = copier.state;

            if (state != null)
            {
                if (state.Req != null)
                {
                    try
                    {
                        state.ReqTimedOut = timedOut;
#if WINDOWS_DESKTOP && !WINDOWS_PHONE
                        state.Req.Abort();
#endif
                    }
                    catch (Exception)
                    {
                        // no op
                    }
                }

                copier.exceptionRef = timedOut ?
                                      Exceptions.GenerateTimeoutException(state.Cmd != null ? state.Cmd.CurrentResult : null, null) :
                                      Exceptions.GenerateCancellationException(state.Cmd != null ? state.Cmd.CurrentResult : null, null);
            }
        }
Example #2
0
        private void EndOpWithCatch(IAsyncResult res)
        {
            // If the last op complete synchronously then ignore this callback as its caller will process next op
            if (res != null && res.CompletedSynchronously)
            {
                return;
            }

            // Begins the next operation and stores any exceptions which occur
            lock (this.lockerObj)
            {
                try
                {
                    this.EndOperation(res);
                }
                catch (Exception ex)
                {
                    if (this.state == null)
                    {
                        // This should not be possible unless there is a bug in one of the streams being copied to/from (which
                        // has been known to happen.)
                        // If the state is null, the below line will throw a NullPointerException.  Because it's unhandled and in
                        // a threadpool thread, it can crash the AppDomain.
                        return;
                    }

                    if (this.state.ReqTimedOut)
                    {
                        this.exceptionRef = Exceptions.GenerateTimeoutException(this.state.Cmd != null ? this.state.Cmd.CurrentResult : null, ex);
                    }
                    else if (this.cancelRequested)
                    {
                        this.exceptionRef = Exceptions.GenerateCancellationException(this.state.Cmd != null ? this.state.Cmd.CurrentResult : null, ex);
                    }
                    else
                    {
                        this.exceptionRef = ex;
                    }

                    // if there is an outstanding read/write let it signal completion since we populated the exception.
                    if (this.readRes == null && this.writeRes == null)
                    {
                        this.SignalCompletion();
                    }
                }
            }
        }
Example #3
0
        /// <summary>
        /// Determines whether the next operation should begin or halt due to an exception or cancellation.
        /// </summary>
        /// <returns>True to continue, false to halt.</returns>
        private bool ShouldDispatchNextOperation()
        {
            if (this.maximumLen.HasValue && Interlocked.Read(ref this.currentBytesReadFromSource) > this.maximumLen)
            {
                this.exceptionRef = new InvalidOperationException(SR.StreamLengthError);
            }
            else if (this.state.OperationExpiryTime.HasValue && DateTime.Now >= this.state.OperationExpiryTime.Value)
            {
                this.exceptionRef = Exceptions.GenerateTimeoutException(this.state.Cmd != null ? this.state.Cmd.CurrentResult : null, null);
            }
            else if (this.state.CancelRequested)
            {
                this.exceptionRef = Exceptions.GenerateCancellationException(this.state.Cmd != null ? this.state.Cmd.CurrentResult : null, null);
            }

            // note cancellation will new up a exception and store it, so this will be not null;
            // continue if no exceptions so far
            return(!this.cancelRequested && this.exceptionRef == null);
        }
Example #4
0
        private bool ShouldDispatchNextOperation()
        {
            if (this.maxLength.HasValue && this.streamCopyState != null && this.streamCopyState.Length > this.maxLength.Value)
            {
                this.state.ExceptionRef = new ArgumentOutOfRangeException("stream");
            }
            else if (this.maxCopyTime.HasValue && DateTime.Now >= this.maxCopyTime.Value)
            {
                this.state.ExceptionRef = Exceptions.GenerateTimeoutException(this.state.Cmd != null ? this.state.Cmd.CurrentResult : null, null);
            }
            else if (this.state.CancelRequested)
            {
                this.state.ExceptionRef = Exceptions.GenerateCancellationException(this.state.Cmd != null ? this.state.Cmd.CurrentResult : null, null);
            }

            // note cancellation will new up a exception and store it, so this will be not null;
            // continue if no exceptions so far
            return(!this.cancelRequested && this.state.ExceptionRef == null);
        }
        private static void ForceAbort(ExecutionState <T> executionState, bool timedOut)
        {
            if (executionState.Req != null)
            {
                try
                {
                    executionState.ReqTimedOut = timedOut;
                    executionState.Req.Abort();
                }
                catch (Exception)
                {
                    // no op
                }
            }

            executionState.ExceptionRef = timedOut ?
                                          Exceptions.GenerateTimeoutException(executionState.Cmd != null ? executionState.Cmd.CurrentResult : null, null) :
                                          Exceptions.GenerateCancellationException(executionState.Cmd != null ? executionState.Cmd.CurrentResult : null, null);
        }
Example #6
0
        private static void ForceAbort(AsyncStreamCopier <T> copier, bool timedOut)
        {
            if (copier.state.Req != null)
            {
                try
                {
                    copier.state.ReqTimedOut = timedOut;
#if !WINDOWS_PHONE
                    copier.state.Req.Abort();
#endif
                }
                catch (Exception)
                {
                    // no op
                }
            }

            copier.exceptionRef = timedOut ?
                                  Exceptions.GenerateTimeoutException(copier.state.Cmd != null ? copier.state.Cmd.CurrentResult : null, null) :
                                  Exceptions.GenerateCancellationException(copier.state.Cmd != null ? copier.state.Cmd.CurrentResult : null, null);
        }
Example #7
0
        private void ForceAbort(bool timeout)
        {
            this.cancelRequested = true;

            if (this.state.Req != null)
            {
                try
                {
                    this.state.ReqTimedOut = timeout;
                    this.state.Req.Abort();
                }
                catch (Exception)
                {
                    // no op
                }
            }

            this.state.ExceptionRef = timeout ?
                                      Exceptions.GenerateTimeoutException(this.state.Cmd != null ? this.state.Cmd.CurrentResult : null, null) :
                                      Exceptions.GenerateCancellationException(this.state.Cmd != null ? this.state.Cmd.CurrentResult : null, null);
        }
Example #8
0
        private void EndOpWithCatch(IAsyncResult res)
        {
            // If the last op complete synchronously then ignore this callback as its caller will process next op
            if (res != null && res.CompletedSynchronously)
            {
                return;
            }

            // Begins the next operation and stores any exceptions which occur
            lock (this.lockerObj)
            {
                try
                {
                    this.EndOperation(res);
                }
                catch (Exception ex)
                {
                    if (this.state.ReqTimedOut)
                    {
                        this.exceptionRef = Exceptions.GenerateTimeoutException(this.state.Cmd != null ? this.state.Cmd.CurrentResult : null, ex);
                    }
                    else if (this.cancelRequested)
                    {
                        this.exceptionRef = Exceptions.GenerateCancellationException(this.state.Cmd != null ? this.state.Cmd.CurrentResult : null, ex);
                    }
                    else
                    {
                        this.exceptionRef = ex;
                    }

                    // if there is an outstanding read/write let it signal completion since we populated the exception.
                    if (this.readRes == null && this.writeRes == null)
                    {
                        this.SignalCompletion();
                    }
                }
            }
        }
Example #9
0
        /// <summary>
        /// Begins a stream copy operation.
        ///
        /// This method wraps the StartCopyStreamAsync method, presenting a different API for it.
        /// As we update the library to be more task-based, callers should gradually move to StartCopyStreamAsync.
        /// </summary>
        /// <param name="completedDelegate">Callback delegate</param>
        /// <param name="copyLength">Number of bytes to copy from source stream to destination stream. Cannot pass in both copyLength and maxLength.</param>
        /// <param name="maxLength">Maximum length of the source stream. Cannot pass in both copyLength and maxLength.</param>
        public void StartCopyStream(Action <ExecutionState <T> > completedDelegate, long?copyLength, long?maxLength)
        {
            Task streamCopyTask = this.StartCopyStreamAsync(copyLength, maxLength);

            streamCopyTask.ContinueWith(completedStreamCopyTask =>
            {
                this.state.CancelDelegate = this.previousCancellationDelegate;
                if (completedStreamCopyTask.IsFaulted)
                {
                    this.state.ExceptionRef = completedStreamCopyTask.Exception.InnerException;
                }
                else if (completedStreamCopyTask.IsCanceled)
                {
                    bool timedOut = false;
                    try
                    {
                        timedOut = !this.cancellationTokenSourceAbort.IsCancellationRequested;
                        if (!timedOut && this.cancellationTokenSourceTimeout != null)
                        {
                            // Free up the OS timer as soon as possible.
                            this.cancellationTokenSourceTimeout.Dispose();
                        }
                    }
                    catch (Exception)
                    {
                        // No-op, we want to propagate the cancellation/timeout exception, not whatever may have happened here.
                    }
                    try
                    {
                        if (state.Req != null)
                        {
                            try
                            {
                                state.ReqTimedOut = timedOut;

#if WINDOWS_DESKTOP || WINDOWS_PHONE
                                state.Req.Abort();
#endif
                            }
                            catch (Exception ex)
                            {
                                Logger.LogWarning(state.OperationContext, "Aborting the request failed with exception: {0}", ex);
                            }
                        }
                    }
                    catch (Exception)
                    {
                        // No-op, we want to propagate the cancellation/timeout exception, not whatever may have happened here.
                    }

                    this.state.ExceptionRef = timedOut ?
                                              Exceptions.GenerateTimeoutException(state.Cmd != null ? state.Cmd.CurrentResult : null, null) :
                                              Exceptions.GenerateCancellationException(state.Cmd != null ? state.Cmd.CurrentResult : null, null);
                }

                try
                {
                    completedDelegate(this.state);
                }
                catch (Exception ex)
                {
                    this.state.ExceptionRef = ex;
                }

                this.Dispose();
            });

            return;
        }