Example #1
0
        private void AsyncEndWrite(IAsyncResult asyncResult)
        {
            AsyncStateBag asyncState = asyncResult.AsyncState as AsyncStateBag;
            PerRequest    request    = (asyncState == null) ? null : asyncState.PerRequest;

            try
            {
                this.CompleteCheck(request, InternalError.InvalidEndWriteCompleted);
                request.SetRequestCompletedSynchronously(asyncResult.CompletedSynchronously);
                EqualRefCheck(this.perRequest, request, InternalError.InvalidEndWrite);
                ContentStream requestContentStream = request.RequestContentStream;
                Util.NullCheck <ContentStream>(requestContentStream, InternalError.InvalidEndWriteStream);
                Util.NullCheck <Stream>(requestContentStream.Stream, InternalError.InvalidEndWriteStream);
                Util.NullCheck <Stream>(request.RequestStream, InternalError.InvalidEndWriteStream).EndWrite(asyncResult);
                if (!asyncResult.CompletedSynchronously)
                {
                    Stream stream = requestContentStream.Stream;
                    asyncResult = InvokeAsync(new AsyncAction(stream.BeginRead), request.RequestContentBuffer, 0, request.RequestContentBuffer.Length, new AsyncCallback(this.AsyncRequestContentEndRead), asyncState);
                    request.SetRequestCompletedSynchronously(asyncResult.CompletedSynchronously);
                }
            }
            catch (Exception exception)
            {
                if (this.HandleFailure(request, exception))
                {
                    throw;
                }
            }
            finally
            {
                this.HandleCompleted(request);
            }
        }
Example #2
0
        private void AsyncRequestContentEndRead(IAsyncResult asyncResult)
        {
            AsyncStateBag asyncState = asyncResult.AsyncState as AsyncStateBag;
            PerRequest    request    = (asyncState == null) ? null : asyncState.PerRequest;

            try
            {
                this.CompleteCheck(request, InternalError.InvalidEndReadCompleted);
                request.SetRequestCompletedSynchronously(asyncResult.CompletedSynchronously);
                EqualRefCheck(this.perRequest, request, InternalError.InvalidEndRead);
                ContentStream requestContentStream = request.RequestContentStream;
                Util.NullCheck <ContentStream>(requestContentStream, InternalError.InvalidEndReadStream);
                Util.NullCheck <Stream>(requestContentStream.Stream, InternalError.InvalidEndReadStream);
                Stream stream2 = Util.NullCheck <Stream>(request.RequestStream, InternalError.InvalidEndReadStream);
                int    num     = requestContentStream.Stream.EndRead(asyncResult);
                if (0 < num)
                {
                    bool flag = request.RequestContentBufferValidLength == -1;
                    request.RequestContentBufferValidLength = num;
                    if (!asyncResult.CompletedSynchronously || flag)
                    {
                        do
                        {
                            asyncResult = InvokeAsync(new AsyncAction(stream2.BeginWrite), request.RequestContentBuffer, 0, request.RequestContentBufferValidLength, new AsyncCallback(this.AsyncEndWrite), asyncState);
                            request.SetRequestCompletedSynchronously(asyncResult.CompletedSynchronously);
                            if ((asyncResult.CompletedSynchronously && !request.RequestCompleted) && !this.IsCompletedInternally)
                            {
                                Stream stream = requestContentStream.Stream;
                                asyncResult = InvokeAsync(new AsyncAction(stream.BeginRead), request.RequestContentBuffer, 0, request.RequestContentBuffer.Length, new AsyncCallback(this.AsyncRequestContentEndRead), asyncState);
                                request.SetRequestCompletedSynchronously(asyncResult.CompletedSynchronously);
                            }
                        }while (((asyncResult.CompletedSynchronously && !request.RequestCompleted) && !this.IsCompletedInternally) && (request.RequestContentBufferValidLength > 0));
                    }
                }
                else
                {
                    request.RequestContentBufferValidLength = 0;
                    request.RequestStream = null;
                    stream2.Close();
                    ODataRequestMessageWrapper wrapper = Util.NullCheck <ODataRequestMessageWrapper>(request.Request, InternalError.InvalidEndWriteRequest);
                    asyncResult = InvokeAsync(new Func <ODataRequestMessageWrapper, AsyncCallback, object, IAsyncResult>(WebUtil.BeginGetResponse), wrapper, new AsyncCallback(this.AsyncEndGetResponse), asyncState);
                    request.SetRequestCompletedSynchronously(asyncResult.CompletedSynchronously);
                }
            }
            catch (Exception exception)
            {
                if (this.HandleFailure(request, exception))
                {
                    throw;
                }
            }
            finally
            {
                this.HandleCompleted(request);
            }
        }
Example #3
0
        protected override void AsyncEndGetResponse(IAsyncResult asyncResult)
        {
            BaseAsyncResult.AsyncStateBag asyncState = asyncResult.AsyncState as BaseAsyncResult.AsyncStateBag;
            BaseAsyncResult.PerRequest    request    = (asyncState == null) ? null : asyncState.PerRequest;
            DataServiceContext            context    = (asyncState == null) ? null : asyncState.Context;

            try
            {
                this.CompleteCheck(request, InternalError.InvalidEndGetResponseCompleted);
                request.SetRequestCompletedSynchronously(asyncResult.CompletedSynchronously);
                BaseAsyncResult.EqualRefCheck(base.perRequest, request, InternalError.InvalidEndGetResponse);
                ODataRequestMessageWrapper wrapper  = Util.NullCheck <ODataRequestMessageWrapper>(request.Request, InternalError.InvalidEndGetResponseRequest);
                HttpWebResponse            response = null;
                response = WebUtil.EndGetResponse(wrapper, asyncResult, context);
                request.HttpWebResponse = Util.NullCheck <HttpWebResponse>(response, InternalError.InvalidEndGetResponseResponse);
                if (!this.IsBatch)
                {
                    this.HandleOperationResponse(response);
                    this.HandleOperationResponseHeaders(response.StatusCode, WebUtil.WrapResponseHeaders(response));
                }
                Stream responseStream = WebUtil.GetResponseStream(response, context);
                request.ResponseStream = responseStream;
                if ((responseStream != null) && responseStream.CanRead)
                {
                    if (this.buildBatchBuffer == null)
                    {
                        this.buildBatchBuffer = new byte[0x1f40];
                    }
                    do
                    {
                        asyncResult = BaseAsyncResult.InvokeAsync(new BaseAsyncResult.AsyncAction(responseStream.BeginRead), this.buildBatchBuffer, 0, this.buildBatchBuffer.Length, new AsyncCallback(this.AsyncEndRead), new AsyncReadState(request));
                        request.SetRequestCompletedSynchronously(asyncResult.CompletedSynchronously);
                    }while (((asyncResult.CompletedSynchronously && !request.RequestCompleted) && !base.IsCompletedInternally) && responseStream.CanRead);
                }
                else
                {
                    request.SetComplete();
                    if (!base.IsCompletedInternally && !request.RequestCompletedSynchronously)
                    {
                        this.FinishCurrentChange(request);
                    }
                }
            }
            catch (Exception exception)
            {
                if (base.HandleFailure(request, exception))
                {
                    throw;
                }
            }
            finally
            {
                this.HandleCompleted(request);
            }
        }
Example #4
0
        /// <summary>Synchronous web request</summary>
        /// <returns>The IODataResponseMessage for the Request.</returns>
        internal IODataResponseMessage ExecuteQuery()
        {
            try
            {
                if (this.requestContentStream != null && this.requestContentStream.Stream != null)
                {
                    this.Request.SetRequestStream(this.requestContentStream);
                }
#if false
                if ((null != requestContent) && (0 < requestContent.Length))
                {
                    using (System.IO.Stream stream = Util.NullCheck(this.Request.GetRequestStream(), InternalError.InvalidGetRequestStream))
                    {
                        byte[] buffer = requestContent.GetBuffer();
                        int bufferOffset = checked((int)requestContent.Position);
                        int bufferLength = checked((int)requestContent.Length) - bufferOffset;

                        // the following is useful in the debugging Immediate Window
                        // string x = System.Text.Encoding.UTF8.GetString(buffer, bufferOffset, bufferLength);
                        stream.Write(buffer, bufferOffset, bufferLength);
                    }
                }
#endif
                IODataResponseMessage response = null;
                response = this.RequestInfo.GetSyncronousResponse(this.Request, true);
                this.SetHttpWebResponse(Util.NullCheck(response, InternalError.InvalidGetResponse));

                if (this.AllowDirectNetworkStreamReading)
                {
                    this.GetStreamFromResponseMessage();
                }
                else
                {
                    this.CopyStreamFromResponseMessage();
                }
            }
            catch (Exception e)
            {
                this.HandleFailure(e);
                throw;
            }
            finally
            {
                this.SetCompleted();
                this.CompletedRequest();
            }

            if (null != this.Failure)
            {
                throw this.Failure;
            }

            return this.AllowDirectNetworkStreamReading ? this.responseMessage : null;
        }
Example #5
0
        private void AsyncEndRead(IAsyncResult asyncResult)
        {
            AsyncReadState asyncState = (AsyncReadState)asyncResult.AsyncState;

            BaseAsyncResult.PerRequest pereq = asyncState.Pereq;
            int count = 0;

            try
            {
                this.CompleteCheck(pereq, InternalError.InvalidEndReadCompleted);
                pereq.SetRequestCompletedSynchronously(asyncResult.CompletedSynchronously);
                BaseAsyncResult.EqualRefCheck(base.perRequest, pereq, InternalError.InvalidEndRead);
                Stream stream = Util.NullCheck <Stream>(pereq.ResponseStream, InternalError.InvalidEndReadStream);
                count = stream.EndRead(asyncResult);
                if (0 < count)
                {
                    Util.NullCheck <Stream>(this.ResponseStream, InternalError.InvalidEndReadCopy).Write(this.buildBatchBuffer, 0, count);
                    asyncState.TotalByteCopied += count;
                    if (!asyncResult.CompletedSynchronously && stream.CanRead)
                    {
                        do
                        {
                            asyncResult = BaseAsyncResult.InvokeAsync(new BaseAsyncResult.AsyncAction(stream.BeginRead), this.buildBatchBuffer, 0, this.buildBatchBuffer.Length, new AsyncCallback(this.AsyncEndRead), asyncState);
                            pereq.SetRequestCompletedSynchronously(asyncResult.CompletedSynchronously);
                            if ((!asyncResult.CompletedSynchronously || pereq.RequestCompleted) || base.IsCompletedInternally)
                            {
                                return;
                            }
                        }while (stream.CanRead);
                    }
                }
                else
                {
                    pereq.SetComplete();
                    if (!base.IsCompletedInternally && !pereq.RequestCompletedSynchronously)
                    {
                        this.FinishCurrentChange(pereq);
                    }
                }
            }
            catch (Exception exception)
            {
                if (base.HandleFailure(pereq, exception))
                {
                    throw;
                }
            }
            finally
            {
                this.HandleCompleted(pereq);
            }
        }
Example #6
0
        private void AsyncEndRead(IAsyncResult asyncResult)
        {
            BaseAsyncResult.AsyncStateBag asyncState = asyncResult.AsyncState as BaseAsyncResult.AsyncStateBag;
            BaseAsyncResult.PerRequest    request    = (asyncState == null) ? null : asyncState.PerRequest;
            int count = 0;

            try
            {
                this.CompleteCheck(request, InternalError.InvalidEndReadCompleted);
                request.SetRequestCompletedSynchronously(asyncResult.CompletedSynchronously);
                base.SetCompletedSynchronously(asyncResult.CompletedSynchronously);
                Stream stream  = Util.NullCheck <Stream>(request.ResponseStream, InternalError.InvalidEndReadStream);
                Stream stream2 = Util.NullCheck <Stream>(this.outputResponseStream, InternalError.InvalidEndReadCopy);
                byte[] buffer  = Util.NullCheck <byte[]>(this.asyncStreamCopyBuffer, InternalError.InvalidEndReadBuffer);
                count            = stream.EndRead(asyncResult);
                this.usingBuffer = false;
                if (0 < count)
                {
                    stream2.Write(buffer, 0, count);
                }
                if (((0 < count) && (0 < buffer.Length)) && stream.CanRead)
                {
                    if (!asyncResult.CompletedSynchronously)
                    {
                        this.ReadResponseStream(asyncState);
                    }
                }
                else
                {
                    if (stream2.Position < stream2.Length)
                    {
                        ((MemoryStream)stream2).SetLength(stream2.Position);
                    }
                    request.SetComplete();
                    base.SetCompleted();
                }
            }
            catch (Exception exception)
            {
                if (base.HandleFailure(exception))
                {
                    throw;
                }
            }
            finally
            {
                this.HandleCompleted(request);
            }
        }
Example #7
0
        protected void AsyncEndGetRequestStream(IAsyncResult asyncResult)
        {
            AsyncStateBag      asyncState = asyncResult.AsyncState as AsyncStateBag;
            PerRequest         request    = (asyncState == null) ? null : asyncState.PerRequest;
            DataServiceContext context    = (asyncState == null) ? null : asyncState.Context;

            try
            {
                this.CompleteCheck(request, InternalError.InvalidEndGetRequestCompleted);
                request.SetRequestCompletedSynchronously(asyncResult.CompletedSynchronously);
                EqualRefCheck(this.perRequest, request, InternalError.InvalidEndGetRequestStream);
                Stream stream = Util.NullCheck <Stream>(WebUtil.EndGetRequestStream(Util.NullCheck <ODataRequestMessageWrapper>(request.Request, InternalError.InvalidEndGetRequestStreamRequest), asyncResult, context), InternalError.InvalidEndGetRequestStreamStream);
                request.RequestStream = stream;
                ContentStream requestContentStream = request.RequestContentStream;
                Util.NullCheck <ContentStream>(requestContentStream, InternalError.InvalidEndGetRequestStreamContent);
                Util.NullCheck <Stream>(requestContentStream.Stream, InternalError.InvalidEndGetRequestStreamContent);
                if (requestContentStream.IsKnownMemoryStream)
                {
                    MemoryStream stream3  = requestContentStream.Stream as MemoryStream;
                    byte[]       buffer   = stream3.GetBuffer();
                    int          position = (int)stream3.Position;
                    int          num2     = ((int)stream3.Length) - position;
                    if ((buffer == null) || (num2 == 0))
                    {
                        Error.ThrowInternalError(InternalError.InvalidEndGetRequestStreamContentLength);
                    }
                }
                request.RequestContentBufferValidLength = -1;
                Stream stream1 = requestContentStream.Stream;
                asyncResult = InvokeAsync(new AsyncAction(stream1.BeginRead), request.RequestContentBuffer, 0, request.RequestContentBuffer.Length, new AsyncCallback(this.AsyncRequestContentEndRead), asyncState);
                request.SetRequestCompletedSynchronously(asyncResult.CompletedSynchronously);
            }
            catch (Exception exception)
            {
                if (this.HandleFailure(request, exception))
                {
                    throw;
                }
            }
            finally
            {
                this.HandleCompleted(request);
            }
        }
        private static void AsyncEndGetRequestStream(IAsyncResult asyncResult)
        {
            QueryAsyncResult state = asyncResult.AsyncState as QueryAsyncResult;

            try
            {
                int step = CompleteCheck(state, InternalError.InvalidEndGetRequestCompleted);
                state.CompletedSynchronously &= asyncResult.CompletedSynchronously; // BeginGetRequestStream

                HttpWebRequest httpWebRequest = Util.NullCheck(state.Request, InternalError.InvalidEndGetRequestStreamRequest);

                Stream stream = Util.NullCheck(httpWebRequest.EndGetRequestStream(asyncResult), InternalError.InvalidEndGetRequestStreamStream);
                state.requestStream = stream;

                MemoryStream memoryStream = Util.NullCheck(state.requestStreamContent, InternalError.InvalidEndGetRequestStreamContent);
                byte[]       buffer       = memoryStream.GetBuffer();
                int          bufferOffset = checked ((int)memoryStream.Position);
                int          bufferLength = checked ((int)memoryStream.Length) - bufferOffset;
                if ((null == buffer) || (0 == bufferLength))
                {
                    Error.ThrowInternalError(InternalError.InvalidEndGetRequestStreamContentLength);
                }

                // the following is useful in the debugging Immediate Window
                // string x = System.Text.Encoding.UTF8.GetString(buffer, bufferOffset, bufferLength);
                asyncResult = stream.BeginWrite(buffer, bufferOffset, bufferLength, QueryAsyncResult.AsyncEndWrite, state);

                bool reallyCompletedSynchronously = asyncResult.CompletedSynchronously && (step < state.asyncCompleteStep);
                state.CompletedSynchronously &= reallyCompletedSynchronously; // BeginWrite
            }
            catch (Exception e)
            {
                if (state.HandleFailure(e))
                {
                    throw;
                }
            }
            finally
            {
                state.HandleCompleted();
            }
        }
Example #9
0
        private static void AsyncEndWrite(IAsyncResult asyncResult)
        {
            QueryAsyncResult state = asyncResult.AsyncState as QueryAsyncResult;

            try
            {
                int step = CompleteCheck(state, InternalError.InvalidEndWriteCompleted);
                state.CompletedSynchronously &= asyncResult.CompletedSynchronously;

                HttpWebRequest httpWebRequest = Util.NullCheck(state.Request, InternalError.InvalidEndWriteRequest);

                Stream stream = Util.NullCheck(state.requestStream, InternalError.InvalidEndWriteStream);
                stream.EndWrite(asyncResult);

                state.requestStream = null;
                stream.Dispose();

                stream = state.requestStreamContent;
                if (null != stream)
                {
                    state.requestStreamContent = null;
                    stream.Dispose();
                }

                asyncResult = httpWebRequest.BeginGetResponse(QueryAsyncResult.AsyncEndGetResponse, state);

                bool reallyCompletedSynchronously = asyncResult.CompletedSynchronously && (step < state.asyncCompleteStep);
                state.CompletedSynchronously &= reallyCompletedSynchronously;
            }
            catch (Exception e)
            {
                if (state.HandleFailure(e))
                {
                    throw;
                }
            }
            finally
            {
                state.HandleCompleted();
            }
        }
Example #10
0
        private static void AsyncEndGetResponse(IAsyncResult asyncResult)
        {
            GetReadStreamResult state = asyncResult.AsyncState as GetReadStreamResult;

            Debug.Assert(state != null, "Async callback got called for different request.");

            try
            {
                state.CompletedSynchronously &= asyncResult.CompletedSynchronously;
                HttpWebRequest request = Util.NullCheck(state.request, InternalError.InvalidEndGetResponseRequest);

                HttpWebResponse webresponse = null;
                try
                {
                    webresponse = (HttpWebResponse)request.EndGetResponse(asyncResult);
                }
                catch (WebException e)
                {
                    webresponse = (HttpWebResponse)e.Response;
                    if (null == webresponse)
                    {
                        throw;
                    }
                }

                state.SetHttpWebResponse(webresponse);
                state.SetCompleted();
            }
            catch (Exception e)
            {
                if (state.HandleFailure(e))
                {
                    throw;
                }
            }
            finally
            {
                state.HandleCompleted();
            }
        }
Example #11
0
 protected override void AsyncEndGetResponse(IAsyncResult asyncResult)
 {
     try
     {
         base.SetCompletedSynchronously(asyncResult.CompletedSynchronously);
         ODataRequestMessageWrapper request     = Util.NullCheck <ODataRequestMessageWrapper>(this.requestMessage, InternalError.InvalidEndGetResponseRequest);
         HttpWebResponse            webResponse = null;
         webResponse = WebUtil.EndGetResponse(request, asyncResult, (DataServiceContext)base.Source);
         this.SetHttpWebResponse(webResponse);
         base.SetCompleted();
     }
     catch (Exception exception)
     {
         if (base.HandleFailure(exception))
         {
             throw;
         }
     }
     finally
     {
         base.HandleCompleted();
     }
 }
Example #12
0
        /// <summary>
        /// Async callback registered with the underlying HttpWebRequest object.
        /// </summary>
        /// <param name="asyncResult">The async result associated with the HttpWebRequest operation.</param>
        protected override void AsyncEndGetResponse(IAsyncResult asyncResult)
        {
            try
            {
                this.SetCompletedSynchronously(asyncResult.CompletedSynchronously); // BeginGetResponse
                ODataRequestMessageWrapper request = Util.NullCheck(this.requestMessage, InternalError.InvalidEndGetResponseRequest);

                this.responseMessage = this.requestInfo.EndGetResponse(request, asyncResult);
                Debug.Assert(this.responseMessage != null, "Can't set a null response.");

                this.SetCompleted();
            }
            catch (Exception e)
            {
                if (this.HandleFailure(e))
                {
                    throw;
                }
            }
            finally
            {
                this.HandleCompleted();
            }
        }
Example #13
0
        internal void Execute()
        {
            try
            {
#if false
                if ((null != requestContent) && (0 < requestContent.Length))
                {
                    using (System.IO.Stream stream = Util.NullCheck(this.Request.GetRequestStream(), InternalError.InvalidGetRequestStream))
                    {
                        byte[] buffer       = requestContent.GetBuffer();
                        int    bufferOffset = checked ((int)requestContent.Position);
                        int    bufferLength = checked ((int)requestContent.Length) - bufferOffset;

                        stream.Write(buffer, bufferOffset, bufferLength);
                    }
                }
#endif

                HttpWebResponse response = null;
                try
                {
                    response = (HttpWebResponse)this.Request.GetResponse();
                }
                catch (WebException ex)
                {
                    response = (HttpWebResponse)ex.Response;
                    if (null == response)
                    {
                        throw;
                    }
                }

                this.SetHttpWebResponse(Util.NullCheck(response, InternalError.InvalidGetResponse));

                if (HttpStatusCode.NoContent != this.StatusCode)
                {
                    using (Stream stream = this.httpWebResponse.GetResponseStream())
                    {
                        if (null != stream)
                        {
                            Stream copy = this.GetAsyncResponseStreamCopy();
                            this.responseStream = copy;

                            Byte[] buffer = this.GetAsyncResponseStreamCopyBuffer();

                            long copied = WebUtil.CopyStream(stream, copy, ref buffer);
                            if (this.responseStreamOwner)
                            {
                                if (0 == copied)
                                {
                                    this.responseStream = null;
                                }
                                else if (copy.Position < copy.Length)
                                {
                                    ((MemoryStream)copy).SetLength(copy.Position);
                                }
                            }

                            this.PutAsyncResponseStreamCopyBuffer(buffer);
                        }
                    }
                }
            }
            catch (Exception e)
            {
                this.HandleFailure(e);
                throw;
            }
            finally
            {
                this.SetCompleted();
                this.CompletedRequest();
            }

            if (null != this.Failure)
            {
                throw this.Failure;
            }
        }
Example #14
0
 internal void ExecuteQuery(DataServiceContext context)
 {
     try
     {
         if ((this.requestContentStream != null) && (this.requestContentStream.Stream != null))
         {
             if (this.requestContentStream.IsKnownMemoryStream)
             {
                 this.Request.SetContentLengthHeader();
             }
             using (Stream stream = WebUtil.GetRequestStream(this.Request, context))
             {
                 int    num;
                 byte[] buffer = new byte[0x10000];
                 do
                 {
                     num = this.requestContentStream.Stream.Read(buffer, 0, buffer.Length);
                     if (num > 0)
                     {
                         stream.Write(buffer, 0, num);
                     }
                 }while (num > 0);
             }
         }
         HttpWebResponse response = null;
         response = WebUtil.GetResponse(this.Request, context, true);
         this.SetHttpWebResponse(Util.NullCheck <HttpWebResponse>(response, InternalError.InvalidGetResponse));
         if (HttpStatusCode.NoContent != this.StatusCode)
         {
             using (Stream stream2 = WebUtil.GetResponseStream(this.httpWebResponse, context))
             {
                 if (stream2 != null)
                 {
                     Stream asyncResponseStreamCopy = this.GetAsyncResponseStreamCopy();
                     this.outputResponseStream = asyncResponseStreamCopy;
                     byte[] asyncResponseStreamCopyBuffer = this.GetAsyncResponseStreamCopyBuffer();
                     long   num2 = WebUtil.CopyStream(stream2, asyncResponseStreamCopy, ref asyncResponseStreamCopyBuffer);
                     if (this.responseStreamOwner)
                     {
                         if (0L == num2)
                         {
                             this.outputResponseStream = null;
                         }
                         else if (asyncResponseStreamCopy.Position < asyncResponseStreamCopy.Length)
                         {
                             ((MemoryStream)asyncResponseStreamCopy).SetLength(asyncResponseStreamCopy.Position);
                         }
                     }
                     this.PutAsyncResponseStreamCopyBuffer(asyncResponseStreamCopyBuffer);
                 }
             }
         }
     }
     catch (Exception exception)
     {
         base.HandleFailure(exception);
         throw;
     }
     finally
     {
         base.SetCompleted();
         this.CompletedRequest();
     }
     if (base.Failure != null)
     {
         throw base.Failure;
     }
 }
Example #15
0
        private void AsyncEndRead(IAsyncResult asyncResult)
#endif
        {
#if WINRT
            IAsyncResult asyncResult = (IAsyncResult)task;
#endif
            Debug.Assert(asyncResult != null && asyncResult.IsCompleted, "asyncResult.IsCompleted");
#if WINRT
            AsyncStateBag asyncStateBag = asyncState as AsyncStateBag;
#else
            AsyncStateBag asyncStateBag = asyncResult.AsyncState as AsyncStateBag;
#endif
            PerRequest pereq = asyncStateBag == null ? null : asyncStateBag.PerRequest;

            int count = 0;
            try
            {
                this.CompleteCheck(pereq, InternalError.InvalidEndReadCompleted);
                pereq.SetRequestCompletedSynchronously(asyncResult.CompletedSynchronously); // BeginRead
                this.SetCompletedSynchronously(asyncResult.CompletedSynchronously);

                Stream httpResponseStream = Util.NullCheck(pereq.ResponseStream, InternalError.InvalidEndReadStream); // get the http response stream.

                Stream outResponseStream = Util.NullCheck(this.outputResponseStream, InternalError.InvalidEndReadCopy);

                byte[] buffer = Util.NullCheck(this.asyncStreamCopyBuffer, InternalError.InvalidEndReadBuffer);
#if WINRT
                count = ((Task<int>)task).Result;
#else
                count = httpResponseStream.EndRead(asyncResult);
#endif
                this.usingBuffer = false;

                if (0 < count)
                {
                    outResponseStream.Write(buffer, 0, count);
                }

                if (0 < count && 0 < buffer.Length && httpResponseStream.CanRead)
                {
                    if (!asyncResult.CompletedSynchronously)
                    {
                        // if CompletedSynchronously then caller will call and we reduce risk of stack overflow
                        this.ReadResponseStream(asyncStateBag);
                    }
                }
                else
                {
                    // Debug.Assert(this.ContentLength < 0 || outResponseStream.Length == this.ContentLength, "didn't read expected ContentLength");
                    if (outResponseStream.Position < outResponseStream.Length)
                    {
                        // In Silverlight, generally 3 bytes less than advertised by ContentLength are read
                        ((MemoryStream)outResponseStream).SetLength(outResponseStream.Position);
                    }

                    pereq.SetComplete();
                    this.SetCompleted();
                }
            }
            catch (Exception e)
            {
                if (this.HandleFailure(e))
                {
                    throw;
                }
            }
            finally
            {
                this.HandleCompleted(pereq);
            }
        }
        private static void AsyncEndRead(IAsyncResult asyncResult)
        {
            Debug.Assert(asyncResult != null && asyncResult.IsCompleted, "asyncResult.IsCompleted");
            QueryResult state = asyncResult.AsyncState as QueryResult;
            int         count = 0;

            try
            {
                CompleteCheck(state, InternalError.InvalidEndReadCompleted);
                state.CompletedSynchronously &= asyncResult.CompletedSynchronously; // BeginRead

                Stream stream         = Util.NullCheck(state.asyncResponseStream, InternalError.InvalidEndReadStream);
                Stream outputResponse = Util.NullCheck(state.responseStream, InternalError.InvalidEndReadCopy);
                byte[] buffer         = Util.NullCheck(state.asyncStreamCopyBuffer, InternalError.InvalidEndReadBuffer);

                count             = stream.EndRead(asyncResult);
                state.usingBuffer = false;
                if (0 < count)
                {
#if StreamContainsBuffer
                    if (state.responseStreamIsCopyBuffer)
                    {   // we may have asked for, but not received the entire stream
                        outputResponse.Position = outputResponse.Position + count;
                    }
                    else
#endif
                    {
                        outputResponse.Write(buffer, 0, count);
                    }
                }

                if (0 < count && 0 < buffer.Length && stream.CanRead)
                {
                    if (!asyncResult.CompletedSynchronously)
                    {
                        // if CompletedSynchronously then caller will call and we reduce risk of stack overflow
                        QueryResult.ReadResponseStream(state);
                    }
                }
                else
                {
#if StreamContainsBuffer
                    Debug.Assert(!state.responseStreamIsCopyBuffer || (outputResponse.Position == outputResponse.Length), "didn't read expected count");
#endif
                    // Debug.Assert(state.ContentLength < 0 || outputResponse.Length == state.ContentLength, "didn't read expected ContentLength");
                    if (outputResponse.Position < outputResponse.Length)
                    {
                        // In Silverlight, generally 3 bytes less than advertised by ContentLength are read
                        ((MemoryStream)outputResponse).SetLength(outputResponse.Position);
                    }

                    state.SetCompleted();
                }
            }
            catch (Exception e)
            {
                if (state.HandleFailure(e))
                {
                    throw;
                }
            }
            finally
            {
                state.HandleCompleted();
            }
        }
Example #17
0
        protected override void AsyncEndGetResponse(IAsyncResult asyncResult)
        {
            Debug.Assert(asyncResult != null && asyncResult.IsCompleted, "asyncResult.IsCompleted");
            AsyncStateBag asyncStateBag = asyncResult.AsyncState as AsyncStateBag;

            PerRequest pereq = asyncStateBag == null ? null : asyncStateBag.PerRequest;

            try
            {
                if (this.IsAborted)
                {
                    if (pereq != null)
                    {
                        pereq.SetComplete();
                    }

                    this.SetCompleted();
                }
                else
                {
                    this.CompleteCheck(pereq, InternalError.InvalidEndGetResponseCompleted);
                    pereq.SetRequestCompletedSynchronously(asyncResult.CompletedSynchronously);
                    this.SetCompletedSynchronously(asyncResult.CompletedSynchronously);

                    ODataRequestMessageWrapper requestMessage = Util.NullCheck(pereq.Request, InternalError.InvalidEndGetResponseRequest);

                    // the httpWebResponse is kept for batching, discarded by non-batch
                    IODataResponseMessage response = this.RequestInfo.EndGetResponse(requestMessage, asyncResult);
                    pereq.ResponseMessage = Util.NullCheck(response, InternalError.InvalidEndGetResponseResponse);

                    this.SetHttpWebResponse(pereq.ResponseMessage);

                    Debug.Assert(null == pereq.ResponseStream, "non-null async ResponseStream");
                    Stream httpResponseStream = null;

                    if (HttpStatusCode.NoContent != (HttpStatusCode)response.StatusCode)
                    {
                        httpResponseStream = response.GetStream();
                        pereq.ResponseStream = httpResponseStream;
                    }

                    if ((null != httpResponseStream) && httpResponseStream.CanRead)
                    {
                        if (null == this.outputResponseStream)
                        {
                            // this is the stream we copy the reponse to
                            this.outputResponseStream = Util.NullCheck(this.GetAsyncResponseStreamCopy(), InternalError.InvalidAsyncResponseStreamCopy);
                        }

                        if (null == this.asyncStreamCopyBuffer)
                        {
                            // this is the buffer we read into and copy out of
                            this.asyncStreamCopyBuffer = Util.NullCheck(this.GetAsyncResponseStreamCopyBuffer(), InternalError.InvalidAsyncResponseStreamCopyBuffer);
                        }

                        // Make async calls to read the response stream
                        this.ReadResponseStream(asyncStateBag);
                    }
                    else
                    {
                        pereq.SetComplete();
                        this.SetCompleted();
                    }
                }
            }
            catch (Exception e)
            {
                if (this.HandleFailure(e))
                {
                    throw;
                }
            }
            finally
            {
                this.HandleCompleted(pereq);
            }
        }
Example #18
0
        private static void AsyncEndGetResponse(IAsyncResult asyncResult)
        {
            Debug.Assert(asyncResult != null && asyncResult.IsCompleted, "asyncResult.IsCompleted");
            QueryResult state = asyncResult.AsyncState as QueryResult;

            try
            {
                CompleteCheck(state, InternalError.InvalidEndGetResponseCompleted);
                state.CompletedSynchronously &= asyncResult.CompletedSynchronously;

                HttpWebRequest httpWebRequest = Util.NullCheck(state.Request, InternalError.InvalidEndGetResponseRequest);

                HttpWebResponse response = null;
                try
                {
                    response = (HttpWebResponse)httpWebRequest.EndGetResponse(asyncResult);
                }
                catch (WebException e)
                {
                    response = (HttpWebResponse)e.Response;
                    if (null == response)
                    {
                        throw;
                    }
                }

                state.SetHttpWebResponse(Util.NullCheck(response, InternalError.InvalidEndGetResponseResponse));
                Debug.Assert(null == state.asyncResponseStream, "non-null asyncResponseStream");

                Stream stream = null;
                if (HttpStatusCode.NoContent != response.StatusCode)
                {
                    stream = response.GetResponseStream();
                    state.asyncResponseStream = stream;
                }

                if ((null != stream) && stream.CanRead)
                {
                    if (null == state.responseStream)
                    {
                        state.responseStream = Util.NullCheck(state.GetAsyncResponseStreamCopy(), InternalError.InvalidAsyncResponseStreamCopy);
                    }

                    if (null == state.asyncStreamCopyBuffer)
                    {
                        state.asyncStreamCopyBuffer = Util.NullCheck(state.GetAsyncResponseStreamCopyBuffer(), InternalError.InvalidAsyncResponseStreamCopyBuffer);
                    }

                    QueryResult.ReadResponseStream(state);
                }
                else
                {
                    state.SetCompleted();
                }
            }
            catch (Exception e)
            {
                if (state.HandleFailure(e))
                {
                    throw;
                }
            }
            finally
            {
                state.HandleCompleted();
            }
        }
        /// <summary>Synchronous web request</summary>
        internal void Execute()
        {
            try
            {
#if false
                if ((null != requestContent) && (0 < requestContent.Length))
                {
                    using (System.IO.Stream stream = Util.NullCheck(this.Request.GetRequestStream(), InternalError.InvalidGetRequestStream))
                    {
                        byte[] buffer       = requestContent.GetBuffer();
                        int    bufferOffset = checked ((int)requestContent.Position);
                        int    bufferLength = checked ((int)requestContent.Length) - bufferOffset;

                        // the following is useful in the debugging Immediate Window
                        // string x = System.Text.Encoding.UTF8.GetString(buffer, bufferOffset, bufferLength);
                        stream.Write(buffer, bufferOffset, bufferLength);
                    }
                }
#endif

                HttpWebResponse response = null;
                try
                {
                    response = (HttpWebResponse)this.Request.GetResponse();
                }
                catch (WebException ex)
                {
                    response = (HttpWebResponse)ex.Response;
                    if (null == response)
                    {
                        throw;
                    }
                }

                this.SetHttpWebResponse(Util.NullCheck(response, InternalError.InvalidGetResponse));

                if (HttpStatusCode.NoContent != this.StatusCode)
                {
                    using (Stream stream = this.httpWebResponse.GetResponseStream())
                    {
                        if (null != stream)
                        {
                            Stream copy = this.GetAsyncResponseStreamCopy();
                            this.responseStream = copy;

                            Byte[] buffer = this.GetAsyncResponseStreamCopyBuffer();

                            long copied = WebUtil.CopyStream(stream, copy, ref buffer);
                            if (this.responseStreamOwner)
                            {
                                if (0 == copied)
                                {
                                    this.responseStream = null;
                                }
                                else if (copy.Position < copy.Length)
                                {   // In Silverlight, generally 3 bytes less than advertised by ContentLength are read
                                    ((MemoryStream)copy).SetLength(copy.Position);
                                }
                            }

                            this.PutAsyncResponseStreamCopyBuffer(buffer);
                        }
                    }
                }
            }
            catch (Exception e)
            {
                this.HandleFailure(e);
                throw;
            }
            finally
            {
                this.SetCompleted();
                this.CompletedRequest();
            }

            if (null != this.Failure)
            {
                throw this.Failure;
            }
        }
Example #20
0
        protected override void AsyncEndGetResponse(IAsyncResult asyncResult)
        {
            BaseAsyncResult.AsyncStateBag asyncState = asyncResult.AsyncState as BaseAsyncResult.AsyncStateBag;
            BaseAsyncResult.PerRequest    request    = (asyncState == null) ? null : asyncState.PerRequest;
            DataServiceContext            context    = (asyncState == null) ? null : asyncState.Context;

            try
            {
                if (base.IsAborted)
                {
                    if (request != null)
                    {
                        request.SetComplete();
                    }
                    base.SetCompleted();
                }
                else
                {
                    this.CompleteCheck(request, InternalError.InvalidEndGetResponseCompleted);
                    request.SetRequestCompletedSynchronously(asyncResult.CompletedSynchronously);
                    base.SetCompletedSynchronously(asyncResult.CompletedSynchronously);
                    HttpWebResponse response = WebUtil.EndGetResponse(Util.NullCheck <ODataRequestMessageWrapper>(request.Request, InternalError.InvalidEndGetResponseRequest), asyncResult, context);
                    request.HttpWebResponse = Util.NullCheck <HttpWebResponse>(response, InternalError.InvalidEndGetResponseResponse);
                    this.SetHttpWebResponse(request.HttpWebResponse);
                    Stream responseStream = null;
                    if (HttpStatusCode.NoContent != response.StatusCode)
                    {
                        responseStream         = WebUtil.GetResponseStream(response, context);
                        request.ResponseStream = responseStream;
                    }
                    if ((responseStream != null) && responseStream.CanRead)
                    {
                        if (this.outputResponseStream == null)
                        {
                            this.outputResponseStream = Util.NullCheck <Stream>(this.GetAsyncResponseStreamCopy(), InternalError.InvalidAsyncResponseStreamCopy);
                        }
                        if (this.asyncStreamCopyBuffer == null)
                        {
                            this.asyncStreamCopyBuffer = Util.NullCheck <byte[]>(this.GetAsyncResponseStreamCopyBuffer(), InternalError.InvalidAsyncResponseStreamCopyBuffer);
                        }
                        this.ReadResponseStream(asyncState);
                    }
                    else
                    {
                        request.SetComplete();
                        base.SetCompleted();
                    }
                }
            }
            catch (Exception exception)
            {
                if (base.HandleFailure(exception))
                {
                    throw;
                }
            }
            finally
            {
                this.HandleCompleted(request);
            }
        }
Example #21
0
        private static void AsyncEndRead(IAsyncResult asyncResult)
        {
            Debug.Assert(asyncResult != null && asyncResult.IsCompleted, "asyncResult.IsCompleted");
            QueryResult state = asyncResult.AsyncState as QueryResult;
            int         count = 0;

            try
            {
                CompleteCheck(state, InternalError.InvalidEndReadCompleted);
                state.CompletedSynchronously &= asyncResult.CompletedSynchronously;

                Stream stream         = Util.NullCheck(state.asyncResponseStream, InternalError.InvalidEndReadStream);
                Stream outputResponse = Util.NullCheck(state.responseStream, InternalError.InvalidEndReadCopy);
                byte[] buffer         = Util.NullCheck(state.asyncStreamCopyBuffer, InternalError.InvalidEndReadBuffer);

                count             = stream.EndRead(asyncResult);
                state.usingBuffer = false;
                if (0 < count)
                {
#if StreamContainsBuffer
                    if (state.responseStreamIsCopyBuffer)
                    {
                        outputResponse.Position = outputResponse.Position + count;
                    }
                    else
#endif
                    {
                        outputResponse.Write(buffer, 0, count);
                    }
                }

                if (0 < count && 0 < buffer.Length && stream.CanRead)
                {
                    if (!asyncResult.CompletedSynchronously)
                    {
                        QueryResult.ReadResponseStream(state);
                    }
                }
                else
                {
#if StreamContainsBuffer
                    Debug.Assert(!state.responseStreamIsCopyBuffer || (outputResponse.Position == outputResponse.Length), "didn't read expected count");
#endif
                    if (outputResponse.Position < outputResponse.Length)
                    {
                        ((MemoryStream)outputResponse).SetLength(outputResponse.Position);
                    }

                    state.SetCompleted();
                }
            }
            catch (Exception e)
            {
                if (state.HandleFailure(e))
                {
                    throw;
                }
            }
            finally
            {
                state.HandleCompleted();
            }
        }
        private static void AsyncEndGetResponse(IAsyncResult asyncResult)
        {
            Debug.Assert(asyncResult != null && asyncResult.IsCompleted, "asyncResult.IsCompleted");
            QueryResult state = asyncResult.AsyncState as QueryResult;

            try
            {
                CompleteCheck(state, InternalError.InvalidEndGetResponseCompleted);
                state.CompletedSynchronously &= asyncResult.CompletedSynchronously; // BeginGetResponse

                HttpWebRequest httpWebRequest = Util.NullCheck(state.Request, InternalError.InvalidEndGetResponseRequest);

                // the httpWebResponse is kept for batching, discarded by non-batch
                HttpWebResponse response = null;
                try
                {
                    response = (HttpWebResponse)httpWebRequest.EndGetResponse(asyncResult);
                }
                catch (WebException e)
                {
                    response = (HttpWebResponse)e.Response;
                    if (null == response)
                    {
                        throw;
                    }
                }

                state.SetHttpWebResponse(Util.NullCheck(response, InternalError.InvalidEndGetResponseResponse));
                Debug.Assert(null == state.asyncResponseStream, "non-null asyncResponseStream");

                Stream stream = null;
                if (HttpStatusCode.NoContent != response.StatusCode)
                {
                    stream = response.GetResponseStream();
                    state.asyncResponseStream = stream;
                }

                if ((null != stream) && stream.CanRead)
                {
                    if (null == state.responseStream)
                    {   // this is the stream we copy the reponse to
                        state.responseStream = Util.NullCheck(state.GetAsyncResponseStreamCopy(), InternalError.InvalidAsyncResponseStreamCopy);
                    }

                    if (null == state.asyncStreamCopyBuffer)
                    {   // this is the buffer we read into and copy out of
                        state.asyncStreamCopyBuffer = Util.NullCheck(state.GetAsyncResponseStreamCopyBuffer(), InternalError.InvalidAsyncResponseStreamCopyBuffer);
                    }

                    // Make async calls to read the response stream
                    QueryResult.ReadResponseStream(state);
                }
                else
                {
                    state.SetCompleted();
                }
            }
            catch (Exception e)
            {
                if (state.HandleFailure(e))
                {
                    throw;
                }
            }
            finally
            {
                state.HandleCompleted();
            }
        }