The Labs \ Source Viewer \ SSCLI \ System.Net \ ConnectionPoolManager

  1. //------------------------------------------------------------------------------
  2. // <copyright file="ConnectionPoolManager.cs" company="Microsoft">
  3. //
  4. // Copyright (c) 2006 Microsoft Corporation. All rights reserved.
  5. //
  6. // The use and distribution terms for this software are contained in the file
  7. // named license.txt, which can be found in the root of this distribution.
  8. // By using this software in any fashion, you are agreeing to be bound by the
  9. // terms of this license.
  10. //
  11. // You must not remove this notice, or any other, from this software.
  12. //
  13. // </copyright>
  14. //------------------------------------------------------------------------------
  15. namespace System.Net
  16. {
  17.     using System;
  18.     using System.Collections;
  19.     using System.Diagnostics;
  20.     using System.Security;
  21.     using System.Security.Permissions;
  22.     using System.Threading;
  23.     using System.Globalization;
  24.    
  25.     internal class ConnectionPoolManager
  26.     {
  27.         private static Hashtable m_ConnectionPools = new Hashtable();
  28.         // Hashtable used for connection pools
  29.         private static object s_InternalSyncObject;
  30.        
  31.         private ConnectionPoolManager()
  32.         {
  33.         }
  34.        
  35.         private static object InternalSyncObject {
  36.             get {
  37.                 if (s_InternalSyncObject == null) {
  38.                     object o = new object();
  39.                     Interlocked.CompareExchange(ref s_InternalSyncObject, o, null);
  40.                 }
  41.                 return s_InternalSyncObject;
  42.             }
  43.         }
  44.        
  45. /*internal static ConnectionPool[] ConnectionPools {
  46.             get {
  47.                 lock(InternalSyncObject) {
  48.                     ConnectionPool [] connectionPools = new ConnectionPool[m_ConnectionPools.Count];
  49.                     m_ConnectionPools.CopyTo(connectionPools, 0);
  50.                     return connectionPools;
  51.                 }
  52.             }
  53.         }
  54.         */       
  55.         private static string GenerateKey(string hostName, int port, string groupName)
  56.         {
  57.             return hostName + "\r" + port.ToString(NumberFormatInfo.InvariantInfo) + "\r" + groupName;
  58.         }
  59.        
  60.         static internal ConnectionPool GetConnectionPool(ServicePoint servicePoint, string groupName, CreateConnectionDelegate createConnectionCallback)
  61.         {
  62.             string key = GenerateKey(servicePoint.Host, servicePoint.Port, groupName);
  63.             lock (InternalSyncObject) {
  64.                 ConnectionPool connectionPool = (ConnectionPool)m_ConnectionPools[key];
  65.                 if (connectionPool == null) {
  66.                     connectionPool = new ConnectionPool(servicePoint, servicePoint.ConnectionLimit, 0, servicePoint.MaxIdleTime, createConnectionCallback);
  67.                     m_ConnectionPools[key] = connectionPool;
  68.                 }
  69.                 return connectionPool;
  70.             }
  71.         }
  72.        
  73. /*
  74.         internal static ConnectionPool GetConnectionPool(string hostName, int port, string groupName, CreateConnectionDelegate createConnectionCallback) {
  75.             string key = hostName + "\r" + port.ToString(NumberFormatInfo.InvariantInfo) + "\r" + groupName;
  76.             lock(InternalSyncObject) {
  77.                 ConnectionPool connectionPool = (ConnectionPool) m_ConnectionPools[key];
  78.                 if (connectionPool == null) {                   
  79.                     ServicePoint servicePoint = ServicePointManager.FindServicePoint(new Uri("sockets://" + hostName + ":" + port.ToString(NumberFormatInfo.InvariantInfo)), null);
  80.                     connectionPool = new ConnectionPool(servicePoint, m_DefaultMaxPool, 0,  servicePoint.MaxIdleTime, createConnectionCallback);
  81.                     m_ConnectionPools[key] = connectionPool;
  82.                 }
  83.                 return connectionPool;
  84.             }
  85.         }
  86.         */       
  87.        
  88.         static internal bool RemoveConnectionPool(ServicePoint servicePoint, string groupName)
  89.         {
  90.             string key = GenerateKey(servicePoint.Host, servicePoint.Port, groupName);
  91.             lock (InternalSyncObject) {
  92.                 ConnectionPool connectionPool = (ConnectionPool)(m_ConnectionPools[key]);
  93.                 if (connectionPool != null) {
  94.                     m_ConnectionPools[key] = null;
  95.                     m_ConnectionPools.Remove(key);
  96.                     return true;
  97.                 }
  98.             }
  99.             return false;
  100.         }
  101.     }
  102. }

Developer Fusion