2003-07-13 Andreas Nahr <ClassDevelopment@A-SoftTech.com>
[mono.git] / mcs / class / System / System.Net / WebConnectionGroup.cs
1 //
2 // System.Net.WebConnectionGroup
3 //
4 // Authors:
5 //      Gonzalo Paniagua Javier (gonzalo@ximian.com)
6 //
7 // (C) 2003 Ximian, Inc (http://www.ximian.com)
8 //
9
10 using System.Collections;
11 using System.Net.Sockets;
12
13 namespace System.Net
14 {
15         class WebConnectionGroup
16         {
17                 ServicePoint sPoint;
18                 string name;
19                 IPAddress address;
20                 ArrayList connections;
21
22                 public WebConnectionGroup (ServicePoint sPoint, string name, IPAddress address)
23                 {
24                         this.sPoint = sPoint;
25                         this.name = name;
26                         this.address = address;
27                         connections = new ArrayList (1);
28                 }
29
30                 public WebConnection GetConnection (string name)
31                 {
32                         WebConnection cnc = null;
33                         lock (connections) {
34                                 WeakReference cncRef = null;
35
36                                 // Remove disposed connections
37                                 int end = connections.Count;
38                                 ArrayList removed = null;
39                                 for (int i = 0; i < end; i++) {
40                                         cncRef = (WeakReference) connections [i];
41                                         cnc = cncRef.Target as WebConnection;
42                                         if (cnc == null) {
43                                                 if (removed == null)
44                                                         removed = new ArrayList (1);
45
46                                                 removed.Add (i);
47                                         }
48                                 }
49
50                                 if (removed != null) {
51                                         for (int i = removed.Count - 1; i >= 0; i--)
52                                                 connections.RemoveAt ((int) removed [i]);
53                                 }
54
55                                 //TODO: Should use the limits in the config file.
56                                 if (connections.Count == 0) {
57                                         cnc = new WebConnection (this, sPoint);
58                                         connections.Add (new WeakReference (cnc));
59                                 } else {
60                                         cncRef = (WeakReference) connections [connections.Count - 1];
61                                         cnc = cncRef.Target as WebConnection;
62                                 }
63                         }
64
65                         return cnc;
66                 }
67
68                 public string Name {
69                         get { return name; }
70                 }
71                 
72         }
73 }
74