ServicePoint.cs 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  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 {
  64. lock (this) {
  65. return currentConnections;
  66. }
  67. }
  68. }
  69. public DateTime IdleSince {
  70. get {
  71. lock (this) {
  72. return idleSince;
  73. }
  74. }
  75. }
  76. public int MaxIdleTime {
  77. get { return maxIdleTime; }
  78. set {
  79. if (value < Timeout.Infinite || value > Int32.MaxValue)
  80. throw new ArgumentOutOfRangeException ();
  81. this.maxIdleTime = value;
  82. }
  83. }
  84. public virtual Version ProtocolVersion {
  85. get { return protocolVersion; }
  86. }
  87. public bool SupportsPipelining {
  88. get { return HttpVersion.Version11.Equals (protocolVersion); }
  89. }
  90. internal bool SendContinue {
  91. get { return sendContinue; }
  92. set { sendContinue = value; }
  93. }
  94. // Methods
  95. public override int GetHashCode()
  96. {
  97. return base.GetHashCode ();
  98. }
  99. // Internal Methods
  100. internal bool UsesProxy {
  101. get { return usesProxy; }
  102. set { usesProxy = value; }
  103. }
  104. internal bool AvailableForRecycling {
  105. get {
  106. return CurrentConnections == 0
  107. && maxIdleTime != Timeout.Infinite
  108. && DateTime.Now >= IdleSince.AddMilliseconds (maxIdleTime);
  109. }
  110. }
  111. internal Hashtable Groups {
  112. get {
  113. if (groups == null)
  114. groups = new Hashtable ();
  115. return groups;
  116. }
  117. }
  118. internal IPHostEntry HostEntry
  119. {
  120. get {
  121. if (host == null) {
  122. string uriHost = uri.Host;
  123. // There is no need to do DNS resolution on literal IP addresses
  124. if (uri.HostNameType == UriHostNameType.IPv6 ||
  125. uri.HostNameType == UriHostNameType.IPv4) {
  126. if (uri.HostNameType == UriHostNameType.IPv6) {
  127. // Remove square brackets
  128. uriHost = uriHost.Substring(1,uriHost.Length-2);
  129. }
  130. // Creates IPHostEntry
  131. host = new IPHostEntry();
  132. host.AddressList = new IPAddress[] { IPAddress.Parse(uriHost) };
  133. return host;
  134. }
  135. // Try DNS resolution on host names
  136. try {
  137. host = Dns.GetHostByName (uriHost);
  138. }
  139. catch {
  140. return null;
  141. }
  142. }
  143. return host;
  144. }
  145. }
  146. internal WebConnectionGroup GetConnectionGroup (string name)
  147. {
  148. if (name == null)
  149. name = "";
  150. WebConnectionGroup group = Groups [name] as WebConnectionGroup;
  151. if (group != null)
  152. return group;
  153. group = new WebConnectionGroup (this, name);
  154. Groups [name] = group;
  155. return group;
  156. }
  157. internal EventHandler SendRequest (HttpWebRequest request, string groupName)
  158. {
  159. WebConnection cnc;
  160. lock (this) {
  161. WebConnectionGroup cncGroup = GetConnectionGroup (groupName);
  162. cnc = cncGroup.GetConnection ();
  163. }
  164. return cnc.SendRequest (request);
  165. }
  166. internal void IncrementConnection ()
  167. {
  168. lock (this) {
  169. currentConnections++;
  170. idleSince = DateTime.Now.AddMilliseconds (1000000);
  171. Console.WriteLine ("+CurerntCnc: {0} {1}", Address, currentConnections);
  172. }
  173. }
  174. internal void DecrementConnection ()
  175. {
  176. lock (this) {
  177. currentConnections--;
  178. if (currentConnections == 0)
  179. idleSince = DateTime.Now;
  180. Console.WriteLine ("-CurerntCnc: {0} {1}", Address, currentConnections);
  181. }
  182. }
  183. }
  184. }