簡體   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