2 // System.Net.ServicePoint
5 // Lawrence Pit (loz@cable.a2000.nl)
6 // Gonzalo Paniagua Javier (gonzalo@ximian.com)
8 // (c) 2002 Lawrence Pit
9 // (c) 2003 Ximian, Inc. (http://www.ximian.com)
13 // Permission is hereby granted, free of charge, to any person obtaining
14 // a copy of this software and associated documentation files (the
15 // "Software"), to deal in the Software without restriction, including
16 // without limitation the rights to use, copy, modify, merge, publish,
17 // distribute, sublicense, and/or sell copies of the Software, and to
18 // permit persons to whom the Software is furnished to do so, subject to
19 // the following conditions:
21 // The above copyright notice and this permission notice shall be
22 // included in all copies or substantial portions of the Software.
24 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
25 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
26 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
27 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
28 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
29 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
30 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
34 using System.Collections;
35 using System.Net.Sockets;
36 using System.Security.Cryptography.X509Certificates;
37 using System.Threading;
41 public class ServicePoint
46 int currentConnections;
48 Version protocolVersion;
49 X509Certificate certificate;
50 X509Certificate clientCertificate;
54 bool sendContinue = true;
56 object locker = new object ();
57 object hostE = new object ();
59 BindIPEndPoint endPointCallback = null;
61 int tcp_keepalive_time;
62 int tcp_keepalive_interval;
66 internal ServicePoint (Uri uri, int connectionLimit, int maxIdleTime)
69 this.connectionLimit = connectionLimit;
70 this.maxIdleTime = maxIdleTime;
71 this.currentConnections = 0;
72 this.idleSince = DateTime.Now;
82 static Exception GetMustImplement ()
84 return new NotImplementedException ();
87 public BindIPEndPoint BindIPEndPointDelegate
89 get { return endPointCallback; }
90 set { endPointCallback = value; }
94 public X509Certificate Certificate {
95 get { return certificate; }
98 public X509Certificate ClientCertificate {
99 get { return clientCertificate; }
104 public int ConnectionLeaseTimeout
107 throw GetMustImplement ();
110 throw GetMustImplement ();
115 public int ConnectionLimit {
116 get { return connectionLimit; }
119 throw new ArgumentOutOfRangeException ();
121 connectionLimit = value;
125 public string ConnectionName {
126 get { return uri.Scheme; }
129 public int CurrentConnections {
131 return currentConnections;
135 public DateTime IdleSince {
145 public int MaxIdleTime {
146 get { return maxIdleTime; }
148 if (value < Timeout.Infinite || value > Int32.MaxValue)
149 throw new ArgumentOutOfRangeException ();
150 this.maxIdleTime = value;
154 public virtual Version ProtocolVersion {
155 get { return protocolVersion; }
160 public int ReceiveBufferSize
163 throw GetMustImplement ();
166 throw GetMustImplement ();
171 public bool SupportsPipelining {
172 get { return HttpVersion.Version11.Equals (protocolVersion); }
176 public bool Expect100Continue {
177 get { return SendContinue; }
178 set { SendContinue = value; }
181 public bool UseNagleAlgorithm {
182 get { return useNagle; }
183 set { useNagle = value; }
187 internal bool SendContinue {
188 get { return sendContinue &&
189 (protocolVersion == null || protocolVersion == HttpVersion.Version11); }
190 set { sendContinue = value; }
194 public void SetTcpKeepAlive (bool enabled, int keepAliveTime, int keepAliveInterval)
197 if (keepAliveTime <= 0)
198 throw new ArgumentOutOfRangeException ("keepAliveTime", "Must be greater than 0");
199 if (keepAliveInterval <= 0)
200 throw new ArgumentOutOfRangeException ("keepAliveInterval", "Must be greater than 0");
203 tcp_keepalive = enabled;
204 tcp_keepalive_time = keepAliveTime;
205 tcp_keepalive_interval = keepAliveInterval;
208 internal void KeepAliveSetup (Socket socket)
213 byte [] bytes = new byte [12];
214 PutBytes (bytes, (uint) (tcp_keepalive ? 1 : 0), 0);
215 PutBytes (bytes, (uint) tcp_keepalive_time, 4);
216 PutBytes (bytes, (uint) tcp_keepalive_interval, 8);
217 socket.IOControl (IOControlCode.KeepAliveValues, bytes, null);
220 static void PutBytes (byte [] bytes, uint v, int offset)
222 if (BitConverter.IsLittleEndian) {
223 bytes [offset] = (byte) (v & 0x000000ff);
224 bytes [offset + 1] = (byte) ((v & 0x0000ff00) >> 8);
225 bytes [offset + 2] = (byte) ((v & 0x00ff0000) >> 16);
226 bytes [offset + 3] = (byte) ((v & 0xff000000) >> 24);
228 bytes [offset + 3] = (byte) (v & 0x000000ff);
229 bytes [offset + 2] = (byte) ((v & 0x0000ff00) >> 8);
230 bytes [offset + 1] = (byte) ((v & 0x00ff0000) >> 16);
231 bytes [offset] = (byte) ((v & 0xff000000) >> 24);
236 public override int GetHashCode()
238 return base.GetHashCode ();
244 internal bool UsesProxy {
245 get { return usesProxy; }
246 set { usesProxy = value; }
249 internal bool UseConnect {
250 get { return useConnect; }
251 set { useConnect = value; }
254 internal bool AvailableForRecycling {
256 return CurrentConnections == 0
257 && maxIdleTime != Timeout.Infinite
258 && DateTime.Now >= IdleSince.AddMilliseconds (maxIdleTime);
262 internal Hashtable Groups {
265 groups = new Hashtable ();
271 internal IPHostEntry HostEntry
278 string uriHost = uri.Host;
280 // There is no need to do DNS resolution on literal IP addresses
281 if (uri.HostNameType == UriHostNameType.IPv6 ||
282 uri.HostNameType == UriHostNameType.IPv4) {
284 if (uri.HostNameType == UriHostNameType.IPv6) {
285 // Remove square brackets
286 uriHost = uriHost.Substring(1,uriHost.Length-2);
289 // Creates IPHostEntry
290 host = new IPHostEntry();
291 host.AddressList = new IPAddress[] { IPAddress.Parse(uriHost) };
296 // Try DNS resolution on host names
298 host = Dns.GetHostByName (uriHost);
309 internal void SetVersion (Version version)
311 protocolVersion = version;
315 WebConnectionGroup GetConnectionGroup (string name)
320 WebConnectionGroup group = Groups [name] as WebConnectionGroup;
324 group = new WebConnectionGroup (this, name);
325 Groups [name] = group;
329 internal EventHandler SendRequest (HttpWebRequest request, string groupName)
334 WebConnectionGroup cncGroup = GetConnectionGroup (groupName);
335 cnc = cncGroup.GetConnection (request);
338 return cnc.SendRequest (request);
342 public bool CloseConnectionGroup (string connectionGroupName)
345 WebConnectionGroup cncGroup = GetConnectionGroup (connectionGroupName);
346 if (cncGroup != null) {
356 internal void IncrementConnection ()
359 currentConnections++;
360 idleSince = DateTime.Now.AddMilliseconds (1000000);
364 internal void DecrementConnection ()
367 currentConnections--;
368 if (currentConnections == 0)
369 idleSince = DateTime.Now;
373 internal void SetCertificates (X509Certificate client, X509Certificate server)
375 certificate = server;
376 clientCertificate = client;
380 internal bool CallEndPointDelegate (Socket sock, IPEndPoint remote)
382 if (endPointCallback == null)
387 IPEndPoint local = null;
389 local = endPointCallback (this,
392 // This is to differentiate from an
393 // OverflowException, which should propagate.
402 } catch (SocketException) {
403 // This is intentional; the docs say
404 // that if the Bind fails, we keep
405 // going until there is an
406 // OverflowException on the retry