WebConnectionGroup.cs 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. //
  2. // System.Net.WebConnectionGroup
  3. //
  4. // Authors:
  5. // Gonzalo Paniagua Javier ([email protected])
  6. //
  7. // (C) 2003 Ximian, Inc (http://www.ximian.com)
  8. //
  9. //
  10. // Permission is hereby granted, free of charge, to any person obtaining
  11. // a copy of this software and associated documentation files (the
  12. // "Software"), to deal in the Software without restriction, including
  13. // without limitation the rights to use, copy, modify, merge, publish,
  14. // distribute, sublicense, and/or sell copies of the Software, and to
  15. // permit persons to whom the Software is furnished to do so, subject to
  16. // the following conditions:
  17. //
  18. // The above copyright notice and this permission notice shall be
  19. // included in all copies or substantial portions of the Software.
  20. //
  21. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  22. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  23. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  24. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  25. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  26. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  27. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  28. //
  29. using System;
  30. using System.Collections;
  31. using System.Net.Configuration;
  32. using System.Net.Sockets;
  33. namespace System.Net
  34. {
  35. class WebConnectionGroup
  36. {
  37. ServicePoint sPoint;
  38. string name;
  39. ArrayList connections;
  40. Random rnd;
  41. Queue queue;
  42. public WebConnectionGroup (ServicePoint sPoint, string name)
  43. {
  44. this.sPoint = sPoint;
  45. this.name = name;
  46. connections = new ArrayList (1);
  47. queue = new Queue ();
  48. }
  49. public WebConnection GetConnection ()
  50. {
  51. WebConnection cnc = null;
  52. lock (connections) {
  53. WeakReference cncRef = null;
  54. // Remove disposed connections
  55. int end = connections.Count;
  56. ArrayList removed = null;
  57. for (int i = 0; i < end; i++) {
  58. cncRef = (WeakReference) connections [i];
  59. cnc = cncRef.Target as WebConnection;
  60. if (cnc == null) {
  61. if (removed == null)
  62. removed = new ArrayList (1);
  63. removed.Add (i);
  64. }
  65. }
  66. if (removed != null) {
  67. for (int i = removed.Count - 1; i >= 0; i--)
  68. connections.RemoveAt ((int) removed [i]);
  69. }
  70. cnc = CreateOrReuseConnection ();
  71. }
  72. return cnc;
  73. }
  74. WebConnection CreateOrReuseConnection ()
  75. {
  76. // lock is up there.
  77. WebConnection cnc;
  78. WeakReference cncRef;
  79. int count = connections.Count;
  80. for (int i = 0; i < count; i++) {
  81. WeakReference wr = connections [i] as WeakReference;
  82. cnc = wr.Target as WebConnection;
  83. if (cnc == null) {
  84. connections.RemoveAt (i);
  85. count--;
  86. i--;
  87. continue;
  88. }
  89. if (cnc.Busy)
  90. continue;
  91. return cnc;
  92. }
  93. if (sPoint.ConnectionLimit > count) {
  94. cnc = new WebConnection (this, sPoint);
  95. connections.Add (new WeakReference (cnc));
  96. return cnc;
  97. }
  98. if (rnd == null)
  99. rnd = new Random ();
  100. int idx = (count > 1) ? rnd.Next (0, count - 1) : 0;
  101. cncRef = (WeakReference) connections [idx];
  102. cnc = cncRef.Target as WebConnection;
  103. if (cnc == null) {
  104. cnc = new WebConnection (this, sPoint);
  105. connections.RemoveAt (idx);
  106. connections.Add (new WeakReference (cnc));
  107. }
  108. return cnc;
  109. }
  110. public string Name {
  111. get { return name; }
  112. }
  113. internal Queue Queue {
  114. get { return queue; }
  115. }
  116. }
  117. }