繁体   English   中英

在httpwebrequest中如何以及在何处创建了TCP连接,它与servicepoint有何关系?

[英]How and where the TCP connection has been created in httpwebrequest, and how is it related to servicepoint?

我试图找出使用HttpWebRequest时何时建立TCP连接,如何使用ServicePoint来池化和重用这些连接。

我查看了system.dll,并尝试使用ILSpy和Reflector浏览代码,以某种方式看不到套接字的任何引用,建立tcp连接等。

下面我粘贴了反编译的代码-可以请给我提示或重定向我,以便我可以理解:

  1. TCP连接何时创建?
  2. 如何使用ServicePoint使这些连接保持活动状态,池化和重用?

System.dll的HttpWebRequest的代码片段:

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;
    }

K,经过一段时间浏览代码后,我认为我有点理解抽象了。 基本上,服务点,服务点管理器,如何创建tcp连接,如何将连接池化,排队等总是让我感到困惑。 下面的信息对我有所帮助-希望这对其他好奇或试图了解这些细节的人有用:

ServicePoint :到特定主机(目标主机Ip:port)的“连接”的高级抽象(这就是为什么在servicePointManger中定义函数静态ServicePoint FindServicePoint (字符串主机,int端口)的原因。

ServicePointManager :顾名思义,其指示用于管理服务点的全局(静态)类。

连接(内部类) :基本上,我认为这代表TCP连接。 它基本上是从System.Net.PoolStream派生的(内部类-它具有使用的套接字的定义),它是从流派生的。

ConnectionGroup(内部类) :每个HttpWebRequest都与一个连接组关联。 (基本上基于connectionLimit,它最多创建connectionLimit (可以通过ServicePointManager进行全局配置,也可以使用其servicePoint属性通过每个httpwebrequest进行配置)每个httpwebrequest的连接对象数)

如果达到连接限制,则只需将其排队并传递到电线(最有可能-但仍未获得执行此操作的代码)。

而且,如果您要连接到本地计算机上的服务,则servicepoint.connectionlimit不再等于servicepointmanager.defaultconnectionlimit。 默认为 int.Maxvalue( 21474836477FFFFFFF )(您可以参考: http : //blogs.microsoft.co.il/idof/2011/06/20/servicepointmanagerdefaultconnectionlimit-2-depends/

更新:

看起来以下两个链接也很有用:

System.Net.ServicePointManager.DefaultConnectionLimit和.MaxServicePointIdleTime

http://blogs.msdn.com/b/jpsanders/archive/2009/05/20/understanding-maxservicepointidletime-and-defaultconnectionlimit.aspx

最好的祝福!

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM