internal void BeginExecute() { try { IAsyncResult asyncResult; #if false if ((null != requestContent) && (0 < requestContent.Length)) { requestContent.Position = 0; this.requestStreamContent = requestContent; this.Request.ContentLength = requestContent.Length; asyncResult = this.Request.BeginGetRequestStream(QueryAsyncResult.AsyncEndGetRequestStream, this); } else #endif { asyncResult = BaseAsyncResult.InvokeAsync(this.Request.BeginGetResponse, QueryResult.AsyncEndGetResponse, this); } this.CompletedSynchronously &= asyncResult.CompletedSynchronously; } catch (Exception e) { this.HandleFailure(e); throw; } finally { this.HandleCompleted(); } Debug.Assert(!this.CompletedSynchronously || this.IsCompleted, "if CompletedSynchronously then MUST IsCompleted"); }
internal void BatchBeginRequest() { BaseAsyncResult.PerRequest pereq = null; try { ODataRequestMessageWrapper request = this.GenerateBatchRequest(); if (request != null) { request.SetContentLengthHeader(); base.perRequest = pereq = new BaseAsyncResult.PerRequest(); pereq.Request = request; pereq.RequestContentStream = new BaseAsyncResult.ContentStream(request.CachedRequestStream, true); BaseAsyncResult.AsyncStateBag state = new BaseAsyncResult.AsyncStateBag(pereq, (DataServiceContext)base.Source); this.responseStream = new MemoryStream(); IAsyncResult result = BaseAsyncResult.InvokeAsync(new Func <ODataRequestMessageWrapper, AsyncCallback, object, IAsyncResult>(WebUtil.BeginGetRequestStream), request, new AsyncCallback(this.AsyncEndGetRequestStream), state); pereq.SetRequestCompletedSynchronously(result.CompletedSynchronously); } } catch (Exception exception) { base.HandleFailure(pereq, exception); throw; } finally { this.HandleCompleted(pereq); } }
/// <summary> /// Make async calls to read the response stream. /// </summary> /// <param name="asyncStateBag">the state containing the information about the asynchronous operation.</param> private void ReadResponseStream(AsyncStateBag asyncStateBag) { Debug.Assert(asyncStateBag != null, "asyncStateBag != null"); PerRequest pereq = asyncStateBag.PerRequest; IAsyncResult asyncResult = null; byte[] buffer = this.asyncStreamCopyBuffer; Stream httpResponseStream = pereq.ResponseStream; do { int bufferOffset = 0; int bufferLength = buffer.Length; this.usingBuffer = true; #if WINRT asyncResult = BaseAsyncResult.InvokeTask(httpResponseStream.ReadAsync, buffer, bufferOffset, bufferLength, this.AsyncEndRead, asyncStateBag); #else asyncResult = BaseAsyncResult.InvokeAsync(httpResponseStream.BeginRead, buffer, bufferOffset, bufferLength, this.AsyncEndRead, asyncStateBag); #endif pereq.SetRequestCompletedSynchronously(asyncResult.CompletedSynchronously); this.SetCompletedSynchronously(asyncResult.CompletedSynchronously); // BeginRead } while (asyncResult.CompletedSynchronously && !pereq.RequestCompleted && !this.IsCompletedInternally && httpResponseStream.CanRead); Debug.Assert((!this.CompletedSynchronously && !pereq.RequestCompletedSynchronously) || this.IsCompletedInternally || pereq.RequestCompleted, "AsyncEndGetResponse !IsCompleted"); }
private static void ReadResponseStream(QueryResult queryResult) { IAsyncResult asyncResult; byte[] buffer = queryResult.asyncStreamCopyBuffer; Stream stream = queryResult.asyncResponseStream; do { int bufferOffset, bufferLength; #if StreamContainsBuffer if (state.responseStreamIsCopyBuffer) { bufferOffset = checked ((int)state.responseStream.Position); bufferLength = buffer.Length - bufferOffset; } else #endif { bufferOffset = 0; bufferLength = buffer.Length; } queryResult.usingBuffer = true; asyncResult = BaseAsyncResult.InvokeAsync(stream.BeginRead, buffer, bufferOffset, bufferLength, QueryResult.AsyncEndRead, queryResult); queryResult.CompletedSynchronously &= asyncResult.CompletedSynchronously; }while (asyncResult.CompletedSynchronously && !queryResult.IsCompletedInternally && stream.CanRead); Debug.Assert(!queryResult.CompletedSynchronously || queryResult.IsCompletedInternally, "AsyncEndGetResponse !IsCompleted"); }
internal static QueryResult EndExecute <TElement>(object source, IAsyncResult asyncResult) { QueryResult response = null; try { response = BaseAsyncResult.EndExecute <QueryResult>(source, "Execute", asyncResult); } catch (InvalidOperationException ex) { response = asyncResult as QueryResult; Debug.Assert(response != null, "response != null, BaseAsyncResult.EndExecute() would have thrown a different exception otherwise."); QueryOperationResponse operationResponse = response.GetResponse <TElement>(MaterializeAtom.EmptyResults); if (operationResponse != null) { operationResponse.Error = ex; throw new DataServiceQueryException(Strings.DataServiceException_GeneralError, ex, operationResponse); } throw; } return(response); }
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); } }
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); } }
internal void Begin() { try { IAsyncResult result = BaseAsyncResult.InvokeAsync(new Func <ODataRequestMessageWrapper, AsyncCallback, object, IAsyncResult>(WebUtil.BeginGetResponse), this.requestMessage, new AsyncCallback(this.AsyncEndGetResponse), null); base.SetCompletedSynchronously(result.CompletedSynchronously); } catch (Exception exception) { base.HandleFailure(exception); throw; } finally { base.HandleCompleted(); } }
private void ReadResponseStream(BaseAsyncResult.AsyncStateBag asyncStateBag) { BaseAsyncResult.PerRequest perRequest = asyncStateBag.PerRequest; IAsyncResult result = null; byte[] asyncStreamCopyBuffer = this.asyncStreamCopyBuffer; Stream responseStream = perRequest.ResponseStream; do { int offset = 0; int length = asyncStreamCopyBuffer.Length; this.usingBuffer = true; result = BaseAsyncResult.InvokeAsync(new BaseAsyncResult.AsyncAction(responseStream.BeginRead), asyncStreamCopyBuffer, offset, length, new AsyncCallback(this.AsyncEndRead), asyncStateBag); perRequest.SetRequestCompletedSynchronously(result.CompletedSynchronously); base.SetCompletedSynchronously(result.CompletedSynchronously); }while ((result.CompletedSynchronously && !perRequest.RequestCompleted) && (!base.IsCompletedInternally && responseStream.CanRead)); }
/// <summary> /// Begins the async request /// </summary> internal void Begin() { try { IAsyncResult asyncResult = BaseAsyncResult.InvokeAsync(this.requestMessage.BeginGetResponse, this.AsyncEndGetResponse, null); this.SetCompletedSynchronously(asyncResult.CompletedSynchronously); } catch (Exception e) { this.HandleFailure(e); throw; } finally { this.HandleCompleted(); } Debug.Assert(!this.CompletedSynchronously || this.IsCompleted, "if CompletedSynchronously then MUST IsCompleted"); }
/// <summary>start the asynchronous request</summary> internal void BeginExecuteQuery() { IAsyncResult asyncResult = null; PerRequest pereq = new PerRequest(); AsyncStateBag asyncStateBag = new AsyncStateBag(pereq); pereq.Request = this.Request; this.perRequest = pereq; try { if (this.requestContentStream != null && this.requestContentStream.Stream != null) { if (this.requestContentStream.IsKnownMemoryStream) { this.Request.SetContentLengthHeader(); } this.perRequest.RequestContentStream = this.requestContentStream; asyncResult = BaseAsyncResult.InvokeAsync(this.Request.BeginGetRequestStream, this.AsyncEndGetRequestStream, asyncStateBag); } else { asyncResult = BaseAsyncResult.InvokeAsync(this.Request.BeginGetResponse, this.AsyncEndGetResponse, asyncStateBag); } // TODO:: Bug: 298920: Async execute methods for query (QueryResult.cs), should not need to maintain "CompletedSynchronously" information in two state variables. pereq.SetRequestCompletedSynchronously(asyncResult.CompletedSynchronously); this.SetCompletedSynchronously(asyncResult.CompletedSynchronously); } catch (Exception e) { this.HandleFailure(e); throw; } finally { this.HandleCompleted(pereq); } Debug.Assert((!this.CompletedSynchronously && !pereq.RequestCompletedSynchronously) || this.IsCompleted, "if CompletedSynchronously then MUST IsCompleted"); }
internal static QueryResult EndExecuteQuery <TElement>(object source, string method, IAsyncResult asyncResult) { QueryResult result = null; try { result = BaseAsyncResult.EndExecute <QueryResult>(source, method, asyncResult); } catch (InvalidOperationException exception) { result = asyncResult as QueryResult; QueryOperationResponse response = result.GetResponse <TElement>(MaterializeAtom.EmptyResults); if (response != null) { response.Error = exception; throw new DataServiceQueryException(System.Data.Services.Client.Strings.DataServiceException_GeneralError, exception, response); } throw; } return(result); }
internal void Begin() { try { IAsyncResult asyncResult; asyncResult = BaseAsyncResult.InvokeAsync(this.request.BeginGetResponse, GetReadStreamResult.AsyncEndGetResponse, this); this.CompletedSynchronously &= asyncResult.CompletedSynchronously; } catch (Exception e) { this.HandleFailure(e); throw; } finally { this.HandleCompleted(); } Debug.Assert(!this.CompletedSynchronously || this.IsCompleted, "if CompletedSynchronously then MUST IsCompleted"); }
internal void BeginExecuteQuery(DataServiceContext context) { IAsyncResult result = null; BaseAsyncResult.PerRequest pereq = new BaseAsyncResult.PerRequest(); BaseAsyncResult.AsyncStateBag state = new BaseAsyncResult.AsyncStateBag(pereq, context); pereq.Request = this.Request; base.perRequest = pereq; try { if ((this.requestContentStream != null) && (this.requestContentStream.Stream != null)) { if (this.requestContentStream.IsKnownMemoryStream) { this.Request.SetContentLengthHeader(); } base.perRequest.RequestContentStream = this.requestContentStream; result = BaseAsyncResult.InvokeAsync(new Func <ODataRequestMessageWrapper, AsyncCallback, object, IAsyncResult>(WebUtil.BeginGetRequestStream), this.Request, new AsyncCallback(this.AsyncEndGetRequestStream), state); } else { result = BaseAsyncResult.InvokeAsync(new Func <ODataRequestMessageWrapper, AsyncCallback, object, IAsyncResult>(WebUtil.BeginGetResponse), this.Request, new AsyncCallback(this.AsyncEndGetResponse), state); } pereq.SetRequestCompletedSynchronously(result.CompletedSynchronously); base.SetCompletedSynchronously(result.CompletedSynchronously); } catch (Exception exception) { base.HandleFailure(exception); throw; } finally { this.HandleCompleted(pereq); } }
internal static IAsyncResult InvokeAsync(AsyncAction asyncAction, byte[] buffer, int offset, int length, AsyncCallback callback, object state) { IAsyncResult asyncResult = asyncAction(buffer, offset, length, BaseAsyncResult.GetDataServiceAsyncCallback(callback), state); return(PostInvokeAsync(asyncResult, callback)); }
internal static IAsyncResult InvokeAsync(Func <AsyncCallback, object, IAsyncResult> asyncAction, AsyncCallback callback, object state) { IAsyncResult asyncResult = asyncAction(BaseAsyncResult.GetDataServiceAsyncCallback(callback), state); return(PostInvokeAsync(asyncResult, callback)); }
internal void BeginCreateNextChange() { HttpWebResponse response; this.inMemoryResponseStream = new MemoryStream(); BaseAsyncResult.PerRequest pereq = null; IAsyncResult result = null; Label_000F: response = null; ODataRequestMessageWrapper requestMessage = null; try { if (base.perRequest != null) { base.SetCompleted(); System.Data.Services.Client.Error.ThrowInternalError(InternalError.InvalidBeginNextChange); } requestMessage = this.CreateNextRequest(); if (requestMessage == null) { base.Abortable = null; } if ((requestMessage != null) || (base.entryIndex < base.ChangedEntries.Count)) { if (base.ChangedEntries[base.entryIndex].ContentGeneratedForSave) { goto Label_0191; } base.Abortable = requestMessage; BaseAsyncResult.ContentStream stream = this.CreateNonBatchChangeData(base.entryIndex, requestMessage); base.perRequest = pereq = new BaseAsyncResult.PerRequest(); pereq.Request = requestMessage; BaseAsyncResult.AsyncStateBag state = new BaseAsyncResult.AsyncStateBag(pereq, (DataServiceContext)base.Source); if ((stream == null) || (stream.Stream == null)) { result = BaseAsyncResult.InvokeAsync(new Func <ODataRequestMessageWrapper, AsyncCallback, object, IAsyncResult>(WebUtil.BeginGetResponse), requestMessage, new AsyncCallback(this.AsyncEndGetResponse), state); } else { if (stream.IsKnownMemoryStream) { requestMessage.SetContentLengthHeader(); } pereq.RequestContentStream = stream; result = BaseAsyncResult.InvokeAsync(new Func <ODataRequestMessageWrapper, AsyncCallback, object, IAsyncResult>(WebUtil.BeginGetRequestStream), requestMessage, new AsyncCallback(this.AsyncEndGetRequestStream), state); } pereq.SetRequestCompletedSynchronously(result.CompletedSynchronously); base.SetCompletedSynchronously(pereq.RequestCompletedSynchronously); } else { base.SetCompleted(); if (base.CompletedSynchronously) { this.HandleCompleted(pereq); } } } catch (InvalidOperationException exception) { WebUtil.GetHttpWebResponse(exception, ref response); this.HandleOperationException(exception, response); this.HandleCompleted(pereq); } finally { if (response != null) { response.Close(); } } if (((pereq != null) && pereq.RequestCompleted) && (pereq.RequestCompletedSynchronously && !base.IsCompletedInternally)) { this.FinishCurrentChange(pereq); } Label_0191: if (((pereq == null) || (pereq.RequestCompleted && pereq.RequestCompletedSynchronously)) && !base.IsCompletedInternally) { goto Label_000F; } }