ServicePoint.cs 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  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. bool sendContinue = true;
  32. // Constructors
  33. internal ServicePoint (Uri uri, int connectionLimit, int maxIdleTime)
  34. {
  35. this.uri = uri;
  36. this.connectionLimit = connectionLimit;
  37. this.maxIdleTime = maxIdleTime;
  38. this.currentConnections = 0;
  39. this.idleSince = DateTime.Now;
  40. }
  41. // Properties
  42. public Uri Address {
  43. get { return uri; }
  44. }
  45. public X509Certificate Certificate {
  46. get { return certificate; }
  47. }
  48. public X509Certificate ClientCertificate {
  49. get { return clientCertificate; }
  50. }
  51. public int ConnectionLimit {
  52. get { return connectionLimit; }
  53. set {
  54. if (value <= 0)
  55. throw new ArgumentOutOfRangeException ();
  56. connectionLimit = value;
  57. }
  58. }
  59. public string ConnectionName {
  60. get { return uri.Scheme; }
  61. }
  62. public int CurrentConnections {
  63. get { return currentConnections; }
  64. }
  65. public DateTime IdleSince {
  66. get { return idleSince; }
  67. }
  68. public int MaxIdleTime {
  69. get { return maxIdleTime; }
  70. set {
  71. if (value < Timeout.Infinite || value > Int32.MaxValue)
  72. throw new ArgumentOutOfRangeException ();
  73. this.maxIdleTime = value;
  74. }
  75. }
  76. public virtual Version ProtocolVersion {
  77. get { return protocolVersion; }
  78. }
  79. public bool SupportsPipelining {
  80. get { return HttpVersion.Version11.Equals (protocolVersion); }
  81. }
  82. internal bool SendContinue {
  83. get { return sendContinue; }
  84. set { sendContinue = value; }
  85. }
  86. // Methods
  87. public override int GetHashCode()
  88. {
  89. return base.GetHashCode ();
  90. }
  91. // Internal Methods
  92. internal bool UsesProxy {
  93. get { return usesProxy; }
  94. set { usesProxy = value; }
  95. }
  96. internal bool AvailableForRecycling {
  97. get {
  98. return CurrentConnections == 0
  99. && maxIdleTime != Timeout.Infinite
  100. && DateTime.Now >= IdleSince.AddMilliseconds (maxIdleTime);
  101. }
  102. }
  103. internal Hashtable Groups {
  104. get {
  105. if (groups == null)
  106. groups = new Hashtable ();
  107. return groups;
  108. }
  109. }
  110. internal IPEndPoint GetEndPoint ()
  111. {
  112. if (host == null)
  113. host = Dns.GetHostByName (uri.Host);
  114. return new IPEndPoint (host.AddressList [0], uri.Port);
  115. }
  116. internal IPAddress GetIPAddress ()
  117. {
  118. if (host == null) {
  119. try {
  120. host = Dns.GetHostByName (uri.Host);
  121. } catch {
  122. return null;
  123. }
  124. }
  125. return host.AddressList [0];
  126. }
  127. internal WebExceptionStatus Connect (Socket sock)
  128. {
  129. IPEndPoint ep = null;
  130. try {
  131. ep = GetEndPoint ();
  132. } catch (SocketException e) {
  133. return (usesProxy) ? WebExceptionStatus.ProxyNameResolutionFailure :
  134. WebExceptionStatus.NameResolutionFailure;
  135. }
  136. try {
  137. sock.Connect (ep);
  138. } catch (SocketException e2) {
  139. return WebExceptionStatus.ConnectFailure;
  140. }
  141. return WebExceptionStatus.Success;
  142. }
  143. internal WebConnectionGroup GetConnectionGroup (string name)
  144. {
  145. if (name == null)
  146. name = "";
  147. WebConnectionGroup group = Groups [name] as WebConnectionGroup;
  148. if (group != null)
  149. return group;
  150. group = new WebConnectionGroup (this, name, GetIPAddress ());
  151. Groups [name] = group;
  152. return group;
  153. }
  154. internal EventHandler SendRequest (HttpWebRequest request, string groupName)
  155. {
  156. WebConnection cnc;
  157. lock (this) {
  158. WebConnectionGroup cncGroup = GetConnectionGroup (groupName);
  159. cnc = cncGroup.GetConnection (groupName);
  160. }
  161. return cnc.SendRequest (request);
  162. }
  163. }
  164. }