ServicePoint.cs 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. //
  2. // System.Net.ServicePoint
  3. //
  4. // Authors:
  5. // Lawrence Pit ([email protected])
  6. // Gonzalo Paniagua Javier ([email protected])
  7. //
  8. // (c) 2002 Lawrence Pit
  9. // (c) 2003 Ximian, Inc. (http://www.ximian.com)
  10. //
  11. using System;
  12. using System.Collections;
  13. using System.Net.Sockets;
  14. using System.Security.Cryptography.X509Certificates;
  15. using System.Threading;
  16. namespace System.Net
  17. {
  18. public class ServicePoint
  19. {
  20. Uri uri;
  21. int connectionLimit;
  22. int maxIdleTime;
  23. int currentConnections;
  24. DateTime idleSince;
  25. Version protocolVersion;
  26. X509Certificate certificate;
  27. X509Certificate clientCertificate;
  28. IPHostEntry host;
  29. bool usesProxy;
  30. Hashtable groups;
  31. // Constructors
  32. internal ServicePoint (Uri uri, int connectionLimit, int maxIdleTime)
  33. {
  34. this.uri = uri;
  35. this.connectionLimit = connectionLimit;
  36. this.maxIdleTime = maxIdleTime;
  37. this.currentConnections = 0;
  38. this.idleSince = DateTime.Now;
  39. }
  40. // Properties
  41. public Uri Address {
  42. get { return uri; }
  43. }
  44. public X509Certificate Certificate {
  45. get { return certificate; }
  46. }
  47. public X509Certificate ClientCertificate {
  48. get { return clientCertificate; }
  49. }
  50. public int ConnectionLimit {
  51. get { return connectionLimit; }
  52. set {
  53. if (value <= 0)
  54. throw new ArgumentOutOfRangeException ();
  55. connectionLimit = value;
  56. }
  57. }
  58. public string ConnectionName {
  59. get { return uri.Scheme; }
  60. }
  61. public int CurrentConnections {
  62. get { return currentConnections; }
  63. }
  64. public DateTime IdleSince {
  65. get { return idleSince; }
  66. }
  67. public int MaxIdleTime {
  68. get { return maxIdleTime; }
  69. set {
  70. if (value < Timeout.Infinite || value > Int32.MaxValue)
  71. throw new ArgumentOutOfRangeException ();
  72. this.maxIdleTime = value;
  73. }
  74. }
  75. public virtual Version ProtocolVersion {
  76. get { return protocolVersion; }
  77. }
  78. public bool SupportsPipelining {
  79. get { return HttpVersion.Version11.Equals (protocolVersion); }
  80. }
  81. // Methods
  82. public override int GetHashCode()
  83. {
  84. return base.GetHashCode ();
  85. }
  86. // Internal Methods
  87. internal bool UsesProxy {
  88. get { return usesProxy; }
  89. set { usesProxy = value; }
  90. }
  91. internal bool AvailableForRecycling {
  92. get {
  93. return CurrentConnections == 0
  94. && maxIdleTime != Timeout.Infinite
  95. && DateTime.Now >= IdleSince.AddMilliseconds (maxIdleTime);
  96. }
  97. }
  98. internal Hashtable Groups {
  99. get {
  100. if (groups == null)
  101. groups = new Hashtable ();
  102. return groups;
  103. }
  104. }
  105. internal IPEndPoint GetEndPoint ()
  106. {
  107. if (host == null)
  108. host = Dns.GetHostByName (uri.Host);
  109. return new IPEndPoint (host.AddressList [0], uri.Port);
  110. }
  111. internal IPAddress GetIPAddress ()
  112. {
  113. if (host == null) {
  114. try {
  115. host = Dns.GetHostByName (uri.Host);
  116. } catch {
  117. return null;
  118. }
  119. }
  120. return host.AddressList [0];
  121. }
  122. internal WebExceptionStatus Connect (Socket sock)
  123. {
  124. IPEndPoint ep = null;
  125. try {
  126. ep = GetEndPoint ();
  127. } catch (SocketException e) {
  128. return (usesProxy) ? WebExceptionStatus.ProxyNameResolutionFailure :
  129. WebExceptionStatus.NameResolutionFailure;
  130. }
  131. try {
  132. sock.Connect (ep);
  133. } catch (SocketException e2) {
  134. return WebExceptionStatus.ConnectFailure;
  135. }
  136. return WebExceptionStatus.Success;
  137. }
  138. internal WebConnectionGroup GetConnectionGroup (string name)
  139. {
  140. if (name == null)
  141. name = "";
  142. WebConnectionGroup group = Groups [name] as WebConnectionGroup;
  143. if (group != null)
  144. return group;
  145. group = new WebConnectionGroup (this, name, GetIPAddress ());
  146. Groups [name] = group;
  147. return group;
  148. }
  149. internal EventHandler SendRequest (HttpWebRequest request, string groupName)
  150. {
  151. WebConnection cnc;
  152. lock (this) {
  153. WebConnectionGroup cncGroup = GetConnectionGroup (groupName);
  154. cnc = cncGroup.GetConnection (groupName);
  155. }
  156. return cnc.SendRequest (request);
  157. }
  158. }
  159. }