TcpConnectionPool.cs 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  1. //
  2. // System.Runtime.Remoting.Channels.Tcp.TcpConnectionPool.cs
  3. //
  4. // Author: Lluis Sanchez ([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 = 2;
  29. static int _keepAliveSeconds = 15;
  30. static TcpConnectionPool()
  31. {
  32. // This thread will close unused connections
  33. Thread t = new Thread (new ThreadStart (ConnectionCollector));
  34. t.Start();
  35. t.IsBackground = true;
  36. }
  37. public static int MaxOpenConnections
  38. {
  39. get { return _maxOpenConnections; }
  40. set
  41. {
  42. if (value < 1) throw new RemotingException ("MaxOpenConnections must be greater than zero");
  43. _maxOpenConnections = value;
  44. }
  45. }
  46. public static int KeepAliveSeconds
  47. {
  48. get { return _keepAliveSeconds; }
  49. set { _keepAliveSeconds = value; }
  50. }
  51. public static TcpConnection GetConnection (string host, int port)
  52. {
  53. HostConnectionPool hostPool;
  54. lock (_pools)
  55. {
  56. string key = host + ":" + port;
  57. hostPool = (HostConnectionPool) _pools[key];
  58. if (hostPool == null)
  59. {
  60. hostPool = new HostConnectionPool(host, port);
  61. _pools[key] = hostPool;
  62. }
  63. }
  64. return hostPool.GetConnection();
  65. }
  66. private static void ConnectionCollector ()
  67. {
  68. while (true)
  69. {
  70. Thread.Sleep(3000);
  71. lock (_pools)
  72. {
  73. ICollection values = _pools.Values;
  74. foreach (HostConnectionPool pool in values)
  75. pool.PurgeConnections();
  76. }
  77. }
  78. }
  79. }
  80. internal class TcpConnection
  81. {
  82. DateTime _controlTime;
  83. Stream _stream;
  84. TcpClient _client;
  85. HostConnectionPool _pool;
  86. byte[] _buffer;
  87. public TcpConnection (HostConnectionPool pool, TcpClient client)
  88. {
  89. _pool = pool;
  90. _client = client;
  91. _stream = client.GetStream();
  92. _controlTime = DateTime.Now;
  93. _buffer = new byte[TcpMessageIO.DefaultStreamBufferSize];
  94. }
  95. public Stream Stream
  96. {
  97. get { return _stream; }
  98. }
  99. public DateTime ControlTime
  100. {
  101. get { return _controlTime; }
  102. set { _controlTime = value; }
  103. }
  104. // This is a "thread safe" buffer that can be used by
  105. // TcpClientTransportSink to read or send data to the stream.
  106. // The buffer is "thread safe" since only one thread can
  107. // use a connection at a given time.
  108. public byte[] Buffer
  109. {
  110. get { return _buffer; }
  111. }
  112. // Returns the connection to the pool
  113. public void Release()
  114. {
  115. _pool.ReleaseConnection (this);
  116. }
  117. public void Close()
  118. {
  119. _client.Close();
  120. }
  121. }
  122. internal class HostConnectionPool
  123. {
  124. ArrayList _pool = new ArrayList();
  125. int _activeConnections = 0;
  126. string _host;
  127. int _port;
  128. public HostConnectionPool (string host, int port)
  129. {
  130. _host = host;
  131. _port = port;
  132. }
  133. public TcpConnection GetConnection ()
  134. {
  135. lock (_pool)
  136. {
  137. TcpConnection connection = null;
  138. do
  139. {
  140. if (_pool.Count > 0)
  141. {
  142. // There are available connections
  143. connection = (TcpConnection)_pool[_pool.Count - 1];
  144. _pool.RemoveAt(_pool.Count - 1);
  145. }
  146. if (connection == null && _activeConnections < TcpConnectionPool.MaxOpenConnections)
  147. {
  148. // No connections available, but the max connections
  149. // has not been reached yet, so a new one can be created
  150. connection = CreateConnection();
  151. }
  152. // No available connections in the pool
  153. // Wait for somewone to release one.
  154. if (connection == null)
  155. Monitor.Wait(_pool);
  156. }
  157. while (connection == null);
  158. return connection;
  159. }
  160. }
  161. private TcpConnection CreateConnection()
  162. {
  163. try
  164. {
  165. TcpClient client = new TcpClient(_host, _port);
  166. TcpConnection entry = new TcpConnection(this, client);
  167. _activeConnections++;
  168. return entry;
  169. }
  170. catch (Exception ex)
  171. {
  172. throw new RemotingException (ex.Message);
  173. }
  174. }
  175. public void ReleaseConnection (TcpConnection entry)
  176. {
  177. lock (_pool)
  178. {
  179. entry.ControlTime = DateTime.Now; // Initialize timeout
  180. _pool.Add (entry);
  181. Monitor.Pulse (_pool);
  182. }
  183. }
  184. private void CancelConnection(TcpConnection entry)
  185. {
  186. try
  187. {
  188. entry.Stream.Close();
  189. _activeConnections--;
  190. }
  191. catch
  192. {
  193. }
  194. }
  195. public void PurgeConnections()
  196. {
  197. lock (_pool)
  198. {
  199. for (int n=0; n < _pool.Count; n++)
  200. {
  201. TcpConnection entry = (TcpConnection)_pool[n];
  202. if ( (DateTime.Now - entry.ControlTime).TotalSeconds > TcpConnectionPool.KeepAliveSeconds)
  203. {
  204. CancelConnection (entry);
  205. _pool.RemoveAt(n);
  206. n--;
  207. }
  208. }
  209. }
  210. }
  211. }
  212. }