2 // System.Net.WebConnectionGroup
5 // Gonzalo Paniagua Javier (gonzalo@ximian.com)
7 // (C) 2003 Ximian, Inc (http://www.ximian.com)
11 using System.Collections;
12 using System.Configuration;
13 using System.Net.Configuration;
14 using System.Net.Sockets;
18 class WebConnectionGroup
22 ArrayList connections;
23 static ConnectionManagementData manager;
24 const string configKey = "system.net/connectionManagement";
28 static WebConnectionGroup ()
30 manager = (ConnectionManagementData) ConfigurationSettings.GetConfig (configKey);
33 public WebConnectionGroup (ServicePoint sPoint, string name)
37 connections = new ArrayList (1);
38 maxConnections = (int) manager.GetMaxConnections (sPoint.Address.Host);
41 public WebConnection GetConnection (string name)
43 WebConnection cnc = null;
45 WeakReference cncRef = null;
47 // Remove disposed connections
48 int end = connections.Count;
49 ArrayList removed = null;
50 for (int i = 0; i < end; i++) {
51 cncRef = (WeakReference) connections [i];
52 cnc = cncRef.Target as WebConnection;
55 removed = new ArrayList (1);
61 if (removed != null) {
62 for (int i = removed.Count - 1; i >= 0; i--)
63 connections.RemoveAt ((int) removed [i]);
66 cnc = CreateOrReuseConnection ();
72 WebConnection CreateOrReuseConnection ()
78 int count = connections.Count;
79 if (maxConnections > count) {
80 cnc = new WebConnection (this, sPoint);
81 connections.Add (new WeakReference (cnc));
88 foreach (WeakReference wr in connections) {
89 cnc = wr.Target as WebConnection;
96 int idx = (count > 1) ? rnd.Next (0, count - 1) : 0;
97 cncRef = (WeakReference) connections [idx];
98 cnc = cncRef.Target as WebConnection;
100 cnc = new WebConnection (this, sPoint);
101 connections.RemoveAt (idx);
102 connections.Add (new WeakReference (cnc));