Beispiel #1
0
        HttpStageProcessingAsyncResult BeginSendCore(HttpRequestMessage request, AsyncCallback callback, object state)
        {
            var async = new HttpStageProcessingAsyncState(GetPipeline(), request);
            var stage = new HttpStageProcessingAsyncResult(async, callback, state);

            return(stage);
        }
Beispiel #2
0
        public void SendAsyncCancel(object userState)
        {
            if (userState == null)
            {
                throw new ArgumentNullException("userState");
            }
            HttpStageProcessingAsyncResult result;

            lock (pendingAsync)
            {
                if (!pendingAsync.TryGetValue(userState, out result))
                {
                    Debug.WriteLine(userState + " not found");
                    return;
                }
                if (result == null)
                {
                    Debug.WriteLine(userState + " was null (pending cancel)");
                    return;
                }
                // disallow additional cancellations
                pendingAsync[userState] = null;
            }

            HttpStageProcessingAsyncResult stage = (HttpStageProcessingAsyncResult)result;

            stage.MarkCancelled();
        }
        static void NextResponse(HttpStageProcessingAsyncResult self)
        {
            var       state    = self.state;
            HttpStage previous = null;

            while (state.states.Count != 0)
            {
                var current = (HttpStage)state.stages[state.states.Count - 1];
                try
                {
                    if (state.Cancelled)
                    {
                        Trace(state, "response cancel current", current);
                        Trace(state, "response cancel previous", previous);
                        self.Complete(previous, new OperationCanceledException());
                        return;
                    }

                    var stageState = state.states.Pop();
                    var async      = current as HttpAsyncStage;
                    if (async != null && state.AllowAsync)
                    {
                        Trace(state, "response async", current);

                        var r = async.BeginProcessResponse(state.Response, stageState, FinishResponseCallback, self);
                        if (r.CompletedSynchronously)
                        {
                            Trace(state, "response async", current, "completed sync");
                            async.EndProcessResponse(r);
                        }
                        else
                        {
                            return;
                        }
                    }
                    else
                    {
                        Trace(state, "response sync", current);
                        current.ProcessResponse(state.Response, stageState);
                    }
                }
                catch (Exception e)
                {
                    if (IsFatal(e))
                    {
                        throw;
                    }
                    Trace(state, "NextResponse", current, "exception in processing: " + e.GetType() + " " + e.Message);

                    self.Complete(current, e);
                    return;
                }
                previous = current;
            }
            self.Complete();
        }
Beispiel #4
0
        public HttpResponseMessage Send(HttpRequestMessage request)
        {
            PrepareRequest(ref request);

            var async = new HttpStageProcessingAsyncState(GetPipeline(), request)
            {
                ForceSynchronous = true
            };
            var result = new HttpStageProcessingAsyncResult(async, null, null);
            var xyz    = HttpStageProcessingAsyncResult.End(result, true);

            if (!result.CompletedSynchronously)
            {
                throw new InvalidOperationException("didn't complete synchronously: " + result + " " + xyz.Response);
            }
            return(xyz.Response);
        }
Beispiel #5
0
        static void SendCompletedCore(IAsyncResult a)
        {
            var result = (HttpStageProcessingAsyncResult)a;
            var state  = (SendAsyncState)result.AsyncState;
            var pend   = state.Client.pendingAsync;

            if (state.Operation.UserSuppliedState != null)
            {
                lock (pend)
                {
#if DEBUG
                    bool removed = pend.Remove(state.Operation.UserSuppliedState);
                    Debug.WriteLine(state.Operation.UserSuppliedState + " removed " + removed);
#else
                    pend.Remove(state.Operation.UserSuppliedState);
#endif
                }
            }
            // false == don't throw exception
            state.AsyncState = HttpStageProcessingAsyncResult.End(result, false);

            state.Operation.PostOperationCompleted(OperationCompleted, state);
        }
Beispiel #6
0
        public HttpResponseMessage EndSend(IAsyncResult result)
        {
            var state = HttpStageProcessingAsyncResult.End(result, true);

            return(state.Response);
        }
        static void NextRequest(HttpStageProcessingAsyncResult self)
        {
            var                 state    = self.state;
            HttpStage           previous = null;
            HttpResponseMessage response = null;

            while (state.states.Count < state.stages.Count)
            {
                var current = state.stages[state.states.Count];
                try
                {
                    var    async = current as HttpAsyncStage;
                    object stageState;

                    if (state.Cancelled)
                    {
                        response = null;
                        Trace(state, "request cancel previous: ", previous);
                        Trace(state, "request cancel current: ", current);
                        self.Complete(previous, new OperationCanceledException());
                        return;
                    }

                    if (async != null && state.AllowAsync)
                    {
                        Trace(state, "request async", current);

                        var r = async.BeginProcessRequestAndTryGetResponse(state.request, FinishRequestCallback, self);
                        if (r.CompletedSynchronously)
                        {
                            Trace(state, "request async", current, "completed sync");
                            async.EndProcessRequestAndTryGetResponse(r, out response, out stageState);
                        }
                        else
                        {
                            Trace(state, "request async", current, " is running");
                            return;
                        }
                    }
                    else
                    {
                        Trace(state, "request sync", current);
                        current.ProcessRequestAndTryGetResponse(state.request, out response, out stageState);
                    }
                    state.states.Push(stageState);
                    if (response != null)
                    {
                        state.SetResponse(response);
                        break;
                    }
                }
                catch (Exception e)
                {
                    if (IsFatal(e))
                    {
                        throw;
                    }
                    Trace(state, "NextRequest", current, "exception in processing: " + e.GetType() + " " + e.Message);

                    self.Complete(current, e);
                    return;
                }
                previous = current;
            }

            if (response != null)
            {
                NextResponse(self);
            }
            else
            {
                self.Complete(null, new InvalidOperationException("HttpResponseMessage not available"));
            }
        }