我试图找出在使用HttpWebRequest时何时建立了TCP连接,以及这些连接是如何使用ServicePoint池和重用的。
我看过system.dll,尝试使用ILSpy和Reflector浏览代码,不知怎么没有看到任何对套接字的引用,没有建立tcp连接等等。
下面我已经粘贴了解压缩代码-请给我一些提示或者重定向,这样我才能理解:
代码片段来自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;
}发布于 2014-04-02 10:19:03
在浏览了一段时间的代码之后,我想我有点理解抽象了。基本上,servicepoint、servicepoint管理器、tcp连接是如何创建的、连接已被池化、排队等总是让我感到困惑。下面的信息帮助了我--希望这对好奇或试图理解这些细节的人有用:
FindServicePoint(string ServicePoint :对特定主机(目的主机Ip:端口)的“连接”的高级抽象(这就是为什么在servicePointManger中定义了ex函数静态ServicePoint,int端口)。
ServicePointManager:作为名称表示它的全局(静态)类管理服务点。
连接(内部类):基本上,我认为这是一个代表TCP连接的连接。它基本上来自于System.Net.PoolStream (内部类-它有其使用的套接字的定义),它是从流派生的。
ConnectionGroup (内部类):每个HttpWebRequest与一个连接组相关联。(基本上它创建的connectionLimit最多是基于connectionLimit (可以通过ServicePointManager进行全局配置,也可以使用其servicePoint属性配置每个creates请求)每个httpwebrequest的连接对象数量)
如果达到连接限制,它就会简单地排队并传递到线路(很有可能--但仍然没有得到这样做的代码)。
如果要连接到本地机器上的服务,则servicepoint.connectionlimit不再等于servicepointmanager.defaultconnectionlimit。它默认为;int.Maxvalue (2147483647或7FFFFFFFF)(您可以参考:http://blogs.microsoft.co.il/idof/2011/06/20/servicepointmanagerdefaultconnectionlimit-2-depends/ )。
更新:
以下两个链接看起来也很有用:
System.Net.ServicePointManager.DefaultConnectionLimit and .MaxServicePointIdleTime
http://blogs.msdn.com/b/jpsanders/archive/2009/05/20/understanding-maxservicepointidletime-and-defaultconnectionlimit.aspx
诚挚的问候!
https://stackoverflow.com/questions/22800216
复制相似问题