如何以及在何处TCP连接已经在HttpWebRequest的创建,以及它是如何涉及到服务点? [英] How and where the TCP connection has been created in httpwebrequest, and how is it related to servicepoint?

查看:244
本文介绍了如何以及在何处TCP连接已经在HttpWebRequest的创建,以及它是如何涉及到服务点?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图找出当TCP连接已经建立,而使用HttpWebRequest的,如何将这些连接都被汇集起来,再利用使用的ServicePoint。

我已经看过了system.dll中,并试图浏览使用ILSpy和反射的code,不知何故没看插座的任何引用,建立TCP连接等。

下面我贴上了反编译code - 可以在任何请给我提示或重定向我,让我能理解:

  1. 在当前已经建立的TCP连接?
  2. 如何这些连接正在使用的ServicePoint保持活力,汇集和重用?

code从HttpWebRequest的System.dll中的片段:

 公共覆盖流GetRequestStream()
    {
        TransportContext环境;
        返回this.GetRequestStream(断章取义);
    }

    公共流GetRequestStream(出TransportContext上下文)
    {
        如果(Logging.On)
        {
            Logging.Enter(Logging.Web,这一点,GetRequestStream,);
        }
        上下文= NULL;
        this.CheckProtocol(真正的);
        如果((this._WriteAResult == NULL)||!this._WriteAResult.InternalPeekCompleted)
        {
            锁(本)
            {
                如果(this._WriteAResult!= NULL)
                {
                    抛出新的InvalidOperationException异常(SR.GetString(net_repcall));
                }
                如果(this.SetRequestSubmitted())
                {
                    抛出新的InvalidOperationException异常(SR.GetString(net_reqsubmitted));
                }
                如果(this._ReadAResult!= NULL)
                {
                    扔((异常)this._ReadAResult.Result);
                }
                this._WriteAResult =新LazyAsyncResult(这一点,NULL,NULL);
                this.Async = FALSE;
            }
            this.CurrentMethod = this._OriginVerb;
            而(this.m_Retry&安培;&安培;!this._WriteAResult.InternalPeekCompleted)
            {
                this._OldSubmitWriteStream = NULL;
                this._SubmitWriteStream = NULL;
                this.BeginSubmitRequest();
            }
            而(this.Aborted&安培;&安培;!this._WriteAResult.InternalPeekCompleted)
            {
                如果(!(this._CoreResponse是例外))
                {
                    Thread.SpinWait(1);
                }
                其他
                {
                    this.CheckWriteSideResponseProcessing();
                }
            }
        }
        ConnectStream connectStream = this._WriteAResult.InternalWaitForCompletion()作为ConnectStream;
        this._WriteAResult.EndCalled = TRUE;
        如果(connectStream == NULL)
        {
            如果(Logging.On)
            {
                Logging.Exception(Logging.Web,这一点,EndGetRequestStream,this._WriteAResult.Result为例外);
            }
            扔((异常)this._WriteAResult.Result);
        }
        上下文=新ConnectStreamContext(connectStream);
        如果(Logging.On)
        {
            Logging.Exit(Logging.Web,这一点,GetRequestStream,connectStream);
        }
        返回connectStream;
    }
 

解决方案

K,有一段时间我觉得我有点明白了抽象翻阅code之后。基本服务点,服务点经理,如何在TCP连接已经建立,连接已汇集,排队等总是搞糊涂了。以下信息样的帮助我 - 希望这是给别人谁是好奇,或试图了解这些细节有益的:

的ServicePoint :连接到特定主​​机的高层次的抽象(目标主机IP:端口) (这就是为什么为前,函数静态的ServicePoint FindServicePoint (字符串主机,INT端口)在servicePointManger定义。

ServicePointManager :顾名思义,它的全局(静态)类,它负责管理维修点

连接(内部类):基本上这是一个我认为再presents TCP连接。它主要源于System.Net.PoolStream(内部类 - 它有它使用套接字的定义),它源于流。

ConnectionGroup(内部类):每HttpWebRequest的是与连接组相关联。 (基本上基于connectionLimit它创造最多的 connectionLimit 使用其服务点属性(可在全球范围内配置为通过ServicePointManager,以及每个HttpWebRequest的)每个HttpWebRequest的连接对象的数量)

如果达到连接限制,它只是排队,并传递到线(最有可能的 - 但仍然没有得到code的做到这一点)。

如果要连接到本地计算机上的服务,servicepoint.connectionlimit不再等于servicepointmanager.defaultconnectionlimit。它默认为; int.Maxvalue( 2147483647 7FFFFFFF )(可参考:<一href="http://blogs.microsoft.co.il/idof/2011/06/20/servicepointmanagerdefaultconnectionlimit-2-depends/" rel="nofollow">http://blogs.microsoft.co.il/idof/2011/06/20/servicepointmanagerdefaultconnectionlimit-2-depends/ )

更新:

看起来像以下两个链接都是有用的太:

<一个href="http://stackoverflow.com/questions/3363183/system-net-servicepointmanager-defaultconnectionlimit-and-maxservicepointidleti">System.Net.ServicePointManager.DefaultConnectionLimit和.MaxServicePointIdleTime

<一个href="http://blogs.msdn.com/b/jpsanders/archive/2009/05/20/understanding-maxservicepointidletime-and-defaultconnectionlimit.aspx" rel="nofollow">http://blogs.msdn.com/b/jpsanders/archive/2009/05/20/understanding-maxservicepointidletime-and-defaultconnectionlimit.aspx

祝商祺!

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:

  1. When the TCP connection has been created?
  2. 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;
    }

解决方案

K, after browsing through code some time I think I kind of understood the abstractions. Basically servicepoint, servicepoint manager, how the tcp connection has been created, connections have been pooled, queued etc. always confused me. 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.

ServicePointManager: as the name indicates its the global (static) class which manages service points.

Connection (internal class): Basically this is the one I think that represents TCP connection. it basically derives from System.Net.PoolStream (internal class - it has the definitions of the sockets its using) which derives from stream.

ConnectionGroup (internal class): Each HttpWebRequest is associated with a connection group. (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)

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. it defaults to; int.Maxvalue (2147483647 or 7FFFFFFF) ( you may refer to: 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

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

Best Regards!

这篇关于如何以及在何处TCP连接已经在HttpWebRequest的创建,以及它是如何涉及到服务点?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆