I am trying to find out when the TCP connection has been established while using HttpWebRequest, how these connections have been pooled and reused using ServicePoint.
I have looked at the system.dll, and tried to browse through the code using ILSpy and Reflector, somehow didn't see any references to sockets, establishing tcp connection etc.
Below I have pasted the decompiled code - can any please give me tips or redirect me so that I can understand:
- When the TCP connection has been created?
- How these connections are kept alive, pooled and reused using ServicePoint?
Code snippet from HttpWebRequest of System.dll:
public override Stream GetRequestStream()
{
TransportContext context;
return this.GetRequestStream(out context);
}
public Stream GetRequestStream(out TransportContext context)
{
if (Logging.On)
{
Logging.Enter(Logging.Web, this, "GetRequestStream", "");
}
context = null;
this.CheckProtocol(true);
if ((this._WriteAResult == null) || !this._WriteAResult.InternalPeekCompleted)
{
lock (this)
{
if (this._WriteAResult != null)
{
throw new InvalidOperationException(SR.GetString("net_repcall"));
}
if (this.SetRequestSubmitted())
{
throw new InvalidOperationException(SR.GetString("net_reqsubmitted"));
}
if (this._ReadAResult != null)
{
throw ((Exception) this._ReadAResult.Result);
}
this._WriteAResult = new LazyAsyncResult(this, null, null);
this.Async = false;
}
this.CurrentMethod = this._OriginVerb;
while (this.m_Retry && !this._WriteAResult.InternalPeekCompleted)
{
this._OldSubmitWriteStream = null;
this._SubmitWriteStream = null;
this.BeginSubmitRequest();
}
while (this.Aborted && !this._WriteAResult.InternalPeekCompleted)
{
if (!(this._CoreResponse is Exception))
{
Thread.SpinWait(1);
}
else
{
this.CheckWriteSideResponseProcessing();
}
}
}
ConnectStream connectStream = this._WriteAResult.InternalWaitForCompletion() as ConnectStream;
this._WriteAResult.EndCalled = true;
if (connectStream == null)
{
if (Logging.On)
{
Logging.Exception(Logging.Web, this, "EndGetRequestStream", this._WriteAResult.Result as Exception);
}
throw ((Exception) this._WriteAResult.Result);
}
context = new ConnectStreamContext(connectStream);
if (Logging.On)
{
Logging.Exit(Logging.Web, this, "GetRequestStream", connectStream);
}
return connectStream;
}
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…