Beispiel #1
0
        /// <summary>
        /// Sets the request stream.
        /// </summary>
        /// <param name="requestStreamContent">The content stream to copy into the request stream.</param>
        internal void SetRequestStream(ContentStream requestStreamContent)
        {
            if (requestStreamContent.IsKnownMemoryStream)
            {
                this.SetContentLengthHeader();
            }

#if DEBUG
            this.ValidateHeaders();
#endif
            using (Stream requestStream = this.requestMessage.GetStream())
            {
                if (requestStreamContent.IsKnownMemoryStream)
                {
                    MemoryStream bufferableStream = (MemoryStream)requestStreamContent.Stream;
                    Debug.Assert(bufferableStream.Position == 0, "Cached/buffered stream position should be 0");

                    byte[] buffer       = bufferableStream.GetBuffer();
                    int    bufferOffset = checked ((int)bufferableStream.Position);
                    int    bufferLength = checked ((int)bufferableStream.Length) - bufferOffset;

                    // the following is useful in the debugging Immediate Window
                    // string x = System.Text.Encoding.UTF8.GetString(buffer, bufferOffset, bufferLength);
                    requestStream.Write(buffer, bufferOffset, bufferLength);
                }
                else
                {
                    byte[] buffer = new byte[WebUtil.DefaultBufferSizeForStreamCopy];
                    WebUtil.CopyStream(requestStreamContent.Stream, requestStream, ref buffer);
                }
            }
        }
Beispiel #2
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);
            }
        }
Beispiel #3
0
            public Stream GetStream()
            {
                if (this.cachedRequestStream == null)
                {
                    this.cachedRequestStream = new ContentStream(new MemoryStream(), true /*isKnownMemoryStream*/);
                }

                return(this.cachedRequestStream.Stream);
            }
Beispiel #4
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);
            }
        }
Beispiel #5
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);
            }
        }
Beispiel #6
0
            /// <summary>
            /// Fires the WritingRequest event
            /// </summary>
            /// <param name="isBatchPart">Boolean flag indicating if this request is part of a batch request..</param>
            /// <param name="requestInfo">RequestInfo instance.</param>
            internal void FireWritingRequest(bool isBatchPart, RequestInfo requestInfo)
            {
                Stream stream = WebUtil.FireWritingRequest(new HeaderCollection(this.Headers), this.cachedRequestStream.Stream, isBatchPart, requestInfo, this, true);

                this.cachedRequestStream = new ContentStream(stream, false /*isKnownMemoryStream*/);
            }
Beispiel #7
0
 /// <summary>constructor</summary>
 /// <param name="source">source object of async request</param>
 /// <param name="method">async method name on source object</param>
 /// <param name="serviceRequest">Originating serviceRequest</param>
 /// <param name="request">Originating WebRequest</param>
 /// <param name="requestInfo">The request info of the originating request.</param>
 /// <param name="callback">user callback</param>
 /// <param name="state">user state</param>
 /// <param name="requestContentStream">the stream containing the request data.</param>
 internal QueryResult(object source, string method, DataServiceRequest serviceRequest, ODataRequestMessageWrapper request, RequestInfo requestInfo, AsyncCallback callback, object state, ContentStream requestContentStream)
     : this(source, method, serviceRequest, request, requestInfo, callback, state)
 {
     Debug.Assert(null != requestContentStream, "null requestContentStream");
     this.requestContentStream = requestContentStream;
 }