简体   繁体   English

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

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

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. 我试图找出使用HttpWebRequest时何时建立TCP连接,如何使用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. 我查看了system.dll,并尝试使用ILSpy和Reflector浏览代码,以某种方式看不到套接字的任何引用,建立tcp连接等。

Below I have pasted the decompiled code - can any please give me tips or redirect me so that I can understand: 下面我粘贴了反编译的代码-可以请给我提示或重定向我,以便我可以理解:

  1. When the TCP connection has been created? TCP连接何时创建?
  2. How these connections are kept alive, pooled and reused using ServicePoint? 如何使用ServicePoint使这些连接保持活动状态,池化和重用?

Code snippet from HttpWebRequest of System.dll: 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, after browsing through code some time I think I kind of understood the abstractions. K,经过一段时间浏览代码后,我认为我有点理解抽象了。 Basically servicepoint, servicepoint manager, how the tcp connection has been created, connections have been pooled, queued etc. always confused me. 基本上,服务点,服务点管理器,如何创建tcp连接,如何将连接池化,排队等总是让我感到困惑。 Below information kind of helped me - hopefully this is useful for others who are curious or tried to understand these details: 下面的信息对我有所帮助-希望这对其他好奇或试图了解这些细节的人有用:

ServicePoint : High level abstraction of 'connection' to a particular host (destination Host Ip:port) (that's why for ex, function static ServicePoint FindServicePoint (string host, int port) is defined in servicePointManger. ServicePoint :到特定主机(目标主机Ip:port)的“连接”的高级抽象(这就是为什么在servicePointManger中定义函数静态ServicePoint FindServicePoint (字符串主机,int端口)的原因。

ServicePointManager : as the name indicates its the global (static) class which manages service points. ServicePointManager :顾名思义,其指示用于管理服务点的全局(静态)类。

Connection (internal class) : Basically this is the one I think that represents TCP connection. 连接(内部类) :基本上,我认为这代表TCP连接。 it basically derives from System.Net.PoolStream (internal class - it has the definitions of the sockets its using) which derives from stream. 它基本上是从System.Net.PoolStream派生的(内部类-它具有使用的套接字的定义),它是从流派生的。

ConnectionGroup (internal class) : Each HttpWebRequest is associated with a connection group. ConnectionGroup(内部类) :每个HttpWebRequest都与一个连接组关联。 (basically based on connectionLimit it creates at most connectionLimit (can be configured globally through ServicePointManager, and also per httpwebrequest using its servicePoint property) number of connection objects per httpwebrequest) (基本上基于connectionLimit,它最多创建connectionLimit (可以通过ServicePointManager进行全局配置,也可以使用其servicePoint属性通过每个httpwebrequest进行配置)每个httpwebrequest的连接对象数)

If the connection limit is reached, its simply queued and passed to the wire (most likely - but still didn't get the code which does this). 如果达到连接限制,则只需将其排队并传递到电线(最有可能-但仍未获得执行此操作的代码)。

And if you are connecting to service on the local machine, the servicepoint.connectionlimit no longer equal to servicepointmanager.defaultconnectionlimit. 而且,如果您要连接到本地计算机上的服务,则servicepoint.connectionlimit不再等于servicepointmanager.defaultconnectionlimit。 it defaults to; 默认为 int.Maxvalue ( 2147483647 or 7FFFFFFF ) ( you may refer to: http://blogs.microsoft.co.il/idof/2011/06/20/servicepointmanagerdefaultconnectionlimit-2-depends/ ) int.Maxvalue( 21474836477FFFFFFF )(您可以参考: http : //blogs.microsoft.co.il/idof/2011/06/20/servicepointmanagerdefaultconnectionlimit-2-depends/

Update: 更新:

Looks like following two links are useful too: 看起来以下两个链接也很有用:

System.Net.ServicePointManager.DefaultConnectionLimit and .MaxServicePointIdleTime System.Net.ServicePointManager.DefaultConnectionLimit和.MaxServicePointIdleTime

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

Best Regards! 最好的祝福!

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

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