TcpConnectionPool.cs 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  1. //
  2. // System.Runtime.Remoting.Channels.Tcp.TcpConnectionPool.cs
  3. //
  4. // Author: Lluis Sanchez Gual ([email protected])
  5. //
  6. // 2002 (C) Lluis Sanchez Gual
  7. //
  8. using System;
  9. using System.Collections;
  10. using System.Threading;
  11. using System.IO;
  12. using System.Net.Sockets;
  13. namespace System.Runtime.Remoting.Channels.Tcp
  14. {
  15. // This is a pool of Tcp connections. Connections requested
  16. // by the TCP channel are pooled after their use, and can
  17. // be reused later. Connections are automaticaly closed
  18. // if not used after some time, specified in KeepAliveSeconds.
  19. // The number of allowed open connections can also be specified
  20. // in MaxOpenConnections. The limit is per host.
  21. // If a thread requests a connection and the limit has been
  22. // reached, the thread is suspended until one is released.
  23. internal class TcpConnectionPool
  24. {
  25. // Table of pools. There is a HostConnectionPool
  26. // instance for each host
  27. static Hashtable _pools = new Hashtable();
  28. static int _maxOpenConnections = 50;
  29. static int _keepAliveSeconds = 15;
  30. static Thread _poolThread;
  31. static TcpConnectionPool()
  32. {
  33. // This thread will close unused connections
  34. _poolThread = new Thread (new ThreadStart (ConnectionCollector));
  35. _poolThread.Start();
  36. _poolThread.IsBackground = true;
  37. }
  38. public static void Shutdown ()
  39. {
  40. // FIXME: this should not be needed when background threads work.
  41. if (_poolThread != null)
  42. _poolThread.Abort();
  43. }
  44. public static int MaxOpenConnections
  45. {
  46. get { return _maxOpenConnections; }
  47. set
  48. {
  49. if (value < 1) throw new RemotingException ("MaxOpenConnections must be greater than zero");
  50. _maxOpenConnections = value;
  51. }
  52. }
  53. public static int KeepAliveSeconds
  54. {
  55. get { return _keepAliveSeconds; }
  56. set { _keepAliveSeconds = value; }
  57. }
  58. public static TcpConnection GetConnection (string host, int port)
  59. {
  60. HostConnectionPool hostPool;
  61. lock (_pools)
  62. {
  63. string key = host + ":" + port;
  64. hostPool = (HostConnectionPool) _pools[key];
  65. if (hostPool == null)
  66. {
  67. hostPool = new HostConnectionPool(host, port);
  68. _pools[key] = hostPool;
  69. }
  70. }
  71. return hostPool.GetConnection();
  72. }
  73. private static void ConnectionCollector ()
  74. {
  75. while (true)
  76. {
  77. Thread.Sleep(3000);
  78. lock (_pools)
  79. {
  80. ICollection values = _pools.Values;
  81. foreach (HostConnectionPool pool in values)
  82. pool.PurgeConnections();
  83. }
  84. }
  85. }
  86. }
  87. internal class TcpConnection
  88. {
  89. DateTime _controlTime;
  90. Stream _stream;
  91. TcpClient _client;
  92. HostConnectionPool _pool;
  93. byte[] _buffer;
  94. public TcpConnection (HostConnectionPool pool, TcpClient client)
  95. {
  96. _pool = pool;
  97. _client = client;
  98. _stream = client.GetStream();
  99. _controlTime = DateTime.Now;
  100. _buffer = new byte[TcpMessageIO.DefaultStreamBufferSize];
  101. }
  102. public Stream Stream
  103. {
  104. get { return _stream; }
  105. }
  106. public DateTime ControlTime
  107. {
  108. get { return _controlTime; }
  109. set { _controlTime = value; }
  110. }
  111. // This is a "thread safe" buffer that can be used by
  112. // TcpClientTransportSink to read or send data to the stream.
  113. // The buffer is "thread safe" since only one thread can
  114. // use a connection at a given time.
  115. public byte[] Buffer
  116. {
  117. get { return _buffer; }
  118. }
  119. // Returns the connection to the pool
  120. public void Release()
  121. {
  122. _pool.ReleaseConnection (this);
  123. }
  124. public void Close()
  125. {
  126. _client.Close();
  127. }
  128. }
  129. internal class HostConnectionPool
  130. {
  131. ArrayList _pool = new ArrayList();
  132. int _activeConnections = 0;
  133. string _host;
  134. int _port;
  135. public HostConnectionPool (string host, int port)
  136. {
  137. _host = host;
  138. _port = port;
  139. }
  140. public TcpConnection GetConnection ()
  141. {
  142. lock (_pool)
  143. {
  144. TcpConnection connection = null;
  145. do
  146. {
  147. if (_pool.Count > 0)
  148. {
  149. // There are available connections
  150. connection = (TcpConnection)_pool[_pool.Count - 1];
  151. _pool.RemoveAt(_pool.Count - 1);
  152. }
  153. if (connection == null && _activeConnections < TcpConnectionPool.MaxOpenConnections)
  154. {
  155. // No connections available, but the max connections
  156. // has not been reached yet, so a new one can be created
  157. connection = CreateConnection();
  158. }
  159. // No available connections in the pool
  160. // Wait for somewone to release one.
  161. if (connection == null)
  162. {
  163. Monitor.Wait(_pool);
  164. }
  165. }
  166. while (connection == null);
  167. return connection;
  168. }
  169. }
  170. private TcpConnection CreateConnection()
  171. {
  172. try
  173. {
  174. TcpClient client = new TcpClient(_host, _port);
  175. TcpConnection entry = new TcpConnection(this, client);
  176. _activeConnections++;
  177. return entry;
  178. }
  179. catch (Exception ex)
  180. {
  181. throw new RemotingException (ex.Message);
  182. }
  183. }
  184. public void ReleaseConnection (TcpConnection entry)
  185. {
  186. lock (_pool)
  187. {
  188. entry.ControlTime = DateTime.Now; // Initialize timeout
  189. _pool.Add (entry);
  190. Monitor.Pulse (_pool);
  191. }
  192. }
  193. private void CancelConnection(TcpConnection entry)
  194. {
  195. try
  196. {
  197. entry.Stream.Close();
  198. _activeConnections--;
  199. }
  200. catch
  201. {
  202. }
  203. }
  204. public void PurgeConnections()
  205. {
  206. lock (_pool)
  207. {
  208. for (int n=0; n < _pool.Count; n++)
  209. {
  210. TcpConnection entry = (TcpConnection)_pool[n];
  211. if ( (DateTime.Now - entry.ControlTime).TotalSeconds > TcpConnectionPool.KeepAliveSeconds)
  212. {
  213. CancelConnection (entry);
  214. _pool.RemoveAt(n);
  215. n--;
  216. }
  217. }
  218. }
  219. }
  220. }
  221. }