public override WebResponse EndGetResponse(IAsyncResult asyncResult) { GlobalLog.Enter("FileWebRequest::EndGetResponse"); WebResponse response; bool success = false; try { LazyAsyncResult ar = asyncResult as LazyAsyncResult; if (asyncResult == null || ar == null) { Exception e = asyncResult == null? new ArgumentNullException("asyncResult"): new ArgumentException(SR.GetString(SR.InvalidAsyncResult), "asyncResult"); GlobalLog.LeaveException("FileWebRequest::EndGetRequestStream", e); throw e; } object result = ar.InternalWaitForCompletion(); if (result is Exception) { throw (Exception)result; } response = (WebResponse)result; m_readPending = false; success = true; } catch (Exception exception) { if (Logging.On) { Logging.Exception(Logging.Web, this, "EndGetResponse", exception); } throw; } finally { GlobalLog.Leave("FileWebRequest::EndGetResponse"); // there is no statusCode in FileWebRequest object, defaulting it to zero. if (FrameworkEventSource.Log.IsEnabled()) { LogEndGetResponse(success, synchronous: false, statusCode: 0); } } return(response); }
public override Stream EndGetRequestStream(IAsyncResult asyncResult) { GlobalLog.Enter("FileWebRequest::EndGetRequestStream"); Stream stream; bool success = false; try { LazyAsyncResult ar = asyncResult as LazyAsyncResult; if (asyncResult == null || ar == null) { Exception e = asyncResult == null? new ArgumentNullException("asyncResult"): new ArgumentException(SR.GetString(SR.InvalidAsyncResult), "asyncResult"); GlobalLog.LeaveException("FileWebRequest::EndGetRequestStream", e); throw e; } object result = ar.InternalWaitForCompletion(); if (result is Exception) { throw (Exception)result; } stream = (Stream)result; m_writePending = false; success = true; } catch (Exception exception) { if (Logging.On) { Logging.Exception(Logging.Web, this, "EndGetRequestStream", exception); } throw; } finally { GlobalLog.Leave("FileWebRequest::EndGetRequestStream"); #if !MONO if (FrameworkEventSource.Log.IsEnabled()) { LogEndGetRequestStream(success, synchronous: false); } #endif } return(stream); }
public override WebResponse EndGetResponse(IAsyncResult asyncResult) { GlobalLog.Enter("FileWebRequest::EndGetResponse"); WebResponse response; try { LazyAsyncResult ar = asyncResult as LazyAsyncResult; if (asyncResult == null || ar == null) { Exception e = asyncResult == null? new ArgumentNullException("asyncResult"): new ArgumentException(SR.GetString(SR.InvalidAsyncResult), "asyncResult"); GlobalLog.LeaveException("FileWebRequest::EndGetRequestStream", e); throw e; } object result = ar.InternalWaitForCompletion(); if (result is Exception) { throw (Exception)result; } response = (WebResponse)result; m_readPending = false; } catch (Exception exception) { if (Logging.On) { Logging.Exception(Logging.Web, this, "EndGetResponse", exception); } throw; } finally { GlobalLog.Leave("FileWebRequest::EndGetResponse"); } FrameworkEventSource.Log.EndGetResponse(this); return(response); }
/// <devdoc> /// <para>Gets a <see cref='System.IO.Stream'/> that the application can use to write request data. /// This property returns a stream that the calling application can write on. /// This property is not settable. Getting this property may cause the /// request to be sent, if it wasn't already. Getting this property after /// a request has been sent that doesn't have an entity body causes an /// exception to be thrown. ///</para> /// </devdoc> public Stream GetRequestStream(out TransportContext context) { #if DEBUG using (GlobalLog.SetThreadKind(ThreadKinds.User | ThreadKinds.Sync)) { #endif GlobalLog.Enter("HttpWebRequest#" + ValidationHelper.HashString(this) + "::GetRequestStream"); if(Logging.On)Logging.Enter(Logging.Web, this, "GetRequestStream", ""); context = null; CheckProtocol(true); // See if we're already submitted a request and have a result cached. if (_WriteAResult == null || !_WriteAResult.InternalPeekCompleted) { lock(this) { if (_WriteAResult != null) { throw new InvalidOperationException(SR.GetString(SR.net_repcall)); } // See if we're already submitted a request (e.g. via GetResponse). if (SetRequestSubmitted()) { // Not completed write stream, this is an application error. throw new InvalidOperationException(SR.GetString(SR.net_reqsubmitted)); } // If there's already been a _ReadAResult completed, it better have been with an exception, like an abort. // We need to check within this lock. Before the lock, _WriteAResult didn't exist so won't have been notified. // BeginSubmitRequest() will fail silently if we go ahead and call it after an abort. Since we know this is the // first call to any of the [Begin]GetRe... methods by the above checks, we know ProcessResponse can't have been // called or any other valid _ReadAResult created yet. if (_ReadAResult != null) { GlobalLog.Assert(_ReadAResult.InternalPeekCompleted, "HttpWebRequest#{0}::GetRequestStream()|Incomplete _ReadAResult present on request.", ValidationHelper.HashString(this)); GlobalLog.Assert(_ReadAResult.Result is Exception, "HttpWebRequest#{0}::GetRequestStream()|_ReadAResult with successful completion already present on request.", ValidationHelper.HashString(this)); throw (Exception) _ReadAResult.Result; } // use the AsyncResult to return our Stream _WriteAResult = new LazyAsyncResult(this, null, null); // Submit the Request, causes us to queue ourselves to a Connection and may block // It has happened that [....] path uses this loop the Retry memeber for handling resubmissions. while (m_Retry && !_WriteAResult.InternalPeekCompleted) { _OldSubmitWriteStream = null; _SubmitWriteStream = null; BeginSubmitRequest(); } // OK, we haven't submitted the request yet, so do so now // save off verb from origin Verb GlobalLog.Print("HttpWebRequest#" + ValidationHelper.HashString(this) + "GetRequestStream() resetting CurrentMethod to " + _OriginVerb); CurrentMethod = _OriginVerb; // Submit the Request, causes us to queue ourselves to a Connection and may block // It has happened that [....] path uses this loop the Retry memeber for handling resubmissions. while (m_Retry && !_WriteAResult.InternalPeekCompleted) { _OldSubmitWriteStream = null; _SubmitWriteStream = null; BeginSubmitRequest(); } while(Aborted && !_WriteAResult.InternalPeekCompleted) { // spin untill the _CoreResponse is set if (!(_CoreResponse is Exception)) Thread.SpinWait(1); else CheckWriteSideResponseProcessing(); } } ConnectStream connectStream = _WriteAResult.InternalWaitForCompletion() as ConnectStream; _WriteAResult.EndCalled = true; if (connectStream == null) { if (Logging.On) Logging.Exception(Logging.Web, this, "EndGetRequestStream", _WriteAResult.Result as Exception); throw (Exception) _WriteAResult.Result; } context = new ConnectStreamContext(connectStream); if(Logging.On)Logging.Exit(Logging.Web, this, "GetRequestStream", connectStream); GlobalLog.Leave("HttpWebRequest#" + ValidationHelper.HashString(this) + "::GetRequestStream", ValidationHelper.HashString(connectStream)); return connectStream; #if DEBUG } #endif }
internal void InternalEndProcessAuthentication(LazyAsyncResult lazyResult) { lazyResult.InternalWaitForCompletion(); Exception result = lazyResult.Result as Exception; if (result != null) { this._Framing = Framing.None; this._HandshakeCompleted = false; throw this.SetException(result); } }
internal bool CheckEnqueueWrite(AsyncProtocolRequest asyncRequest) { this._QueuedWriteStateRequest = null; if (Interlocked.CompareExchange(ref this._LockWriteState, 1, 0) == 2) { LazyAsyncResult result = null; lock (this) { if (this._LockWriteState == 1) { this.CheckThrow(true); return false; } this._LockWriteState = 3; if (asyncRequest != null) { this._QueuedWriteStateRequest = asyncRequest; return true; } result = new LazyAsyncResult(null, null, null); this._QueuedWriteStateRequest = result; } result.InternalWaitForCompletion(); this.CheckThrow(true); } return false; }
internal int CheckEnqueueRead(byte[] buffer, int offset, int count, AsyncProtocolRequest request) { if (Interlocked.CompareExchange(ref this._LockReadState, 4, 0) != 2) { return this.CheckOldKeyDecryptedData(buffer, offset, count); } LazyAsyncResult result = null; lock (this) { int num2 = this.CheckOldKeyDecryptedData(buffer, offset, count); if (num2 != -1) { return num2; } if (this._LockReadState != 2) { this._LockReadState = 4; return -1; } this._LockReadState = 6; if (request != null) { this._QueuedReadStateRequest = request; return 0; } result = new LazyAsyncResult(null, null, null); this._QueuedReadStateRequest = result; } result.InternalWaitForCompletion(); lock (this) { return this.CheckOldKeyDecryptedData(buffer, offset, count); } }
private bool CheckEnqueueHandshakeRead(ref byte[] buffer, AsyncProtocolRequest request) { LazyAsyncResult result = null; lock (this) { if (this._LockReadState == 6) { return false; } if (Interlocked.Exchange(ref this._LockReadState, 2) != 4) { return false; } if (request != null) { this._QueuedReadStateRequest = request; return true; } result = new LazyAsyncResult(null, null, null); this._QueuedReadStateRequest = result; } result.InternalWaitForCompletion(); buffer = (byte[]) result.Result; return false; }
private bool CheckEnqueueHandshake(byte[] buffer, AsyncProtocolRequest asyncRequest) { LazyAsyncResult result = null; lock (this) { if (this._LockWriteState == 3) { return false; } if (Interlocked.Exchange(ref this._LockWriteState, 2) != 1) { return false; } if (asyncRequest != null) { asyncRequest.Buffer = buffer; this._QueuedWriteStateRequest = asyncRequest; return true; } result = new LazyAsyncResult(null, null, null); this._QueuedWriteStateRequest = result; } result.InternalWaitForCompletion(); return false; }
public override int EndRead(IAsyncResult asyncResult) { LazyAsyncResult result = (LazyAsyncResult)asyncResult; return((int)result.InternalWaitForCompletion()); }