TcpConnectionPool.cs 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288
  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. if (_poolThread != null)
  41. _poolThread.Abort();
  42. }
  43. public static int MaxOpenConnections
  44. {
  45. get { return _maxOpenConnections; }
  46. set
  47. {
  48. if (value < 1) throw new RemotingException ("MaxOpenConnections must be greater than zero");
  49. _maxOpenConnections = value;
  50. }
  51. }
  52. public static int KeepAliveSeconds
  53. {
  54. get { return _keepAliveSeconds; }
  55. set { _keepAliveSeconds = value; }
  56. }
  57. public static TcpConnection GetConnection (string host, int port)
  58. {
  59. HostConnectionPool hostPool;
  60. lock (_pools)
  61. {
  62. string key = host + ":" + port;
  63. hostPool = (HostConnectionPool) _pools[key];
  64. if (hostPool == null)
  65. {
  66. hostPool = new HostConnectionPool(host, port);
  67. _pools[key] = hostPool;
  68. }
  69. }
  70. return hostPool.GetConnection();
  71. }
  72. private static void ConnectionCollector ()
  73. {
  74. while (true)
  75. {
  76. Thread.Sleep(3000);
  77. lock (_pools)
  78. {
  79. ICollection values = _pools.Values;
  80. foreach (HostConnectionPool pool in values)
  81. pool.PurgeConnections();
  82. }
  83. }
  84. }
  85. }
  86. internal class ReusableTcpClient : TcpClient
  87. {
  88. public ReusableTcpClient (string host, int port): base (host, port)
  89. {
  90. }
  91. public bool IsAlive
  92. {
  93. get
  94. {
  95. // This Poll will return true if there is data pending to
  96. // be read. It prob. means that a client object using this
  97. // connection got an exception and did not finish to read
  98. // the data. It can also mean that the connection has been
  99. // closed in the server. In both cases, the connection cannot
  100. // be reused.
  101. return !Client.Poll (0, SelectMode.SelectRead);
  102. }
  103. }
  104. }
  105. internal class TcpConnection
  106. {
  107. DateTime _controlTime;
  108. Stream _stream;
  109. ReusableTcpClient _client;
  110. HostConnectionPool _pool;
  111. byte[] _buffer;
  112. public TcpConnection (HostConnectionPool pool, ReusableTcpClient client)
  113. {
  114. _pool = pool;
  115. _client = client;
  116. _stream = client.GetStream();
  117. _controlTime = DateTime.Now;
  118. _buffer = new byte[TcpMessageIO.DefaultStreamBufferSize];
  119. }
  120. public Stream Stream
  121. {
  122. get { return _stream; }
  123. }
  124. public DateTime ControlTime
  125. {
  126. get { return _controlTime; }
  127. set { _controlTime = value; }
  128. }
  129. public bool IsAlive
  130. {
  131. get { return _client.IsAlive; }
  132. }
  133. // This is a "thread safe" buffer that can be used by
  134. // TcpClientTransportSink to read or send data to the stream.
  135. // The buffer is "thread safe" since only one thread can
  136. // use a connection at a given time.
  137. public byte[] Buffer
  138. {
  139. get { return _buffer; }
  140. }
  141. // Returns the connection to the pool
  142. public void Release()
  143. {
  144. _pool.ReleaseConnection (this);
  145. }
  146. public void Close()
  147. {
  148. _client.Close();
  149. }
  150. }
  151. internal class HostConnectionPool
  152. {
  153. ArrayList _pool = new ArrayList();
  154. int _activeConnections = 0;
  155. string _host;
  156. int _port;
  157. public HostConnectionPool (string host, int port)
  158. {
  159. _host = host;
  160. _port = port;
  161. }
  162. public TcpConnection GetConnection ()
  163. {
  164. lock (_pool)
  165. {
  166. TcpConnection connection = null;
  167. do
  168. {
  169. if (_pool.Count > 0)
  170. {
  171. // There are available connections
  172. connection = (TcpConnection)_pool[_pool.Count - 1];
  173. _pool.RemoveAt(_pool.Count - 1);
  174. if (!connection.IsAlive) {
  175. CancelConnection (connection);
  176. connection = null;
  177. continue;
  178. }
  179. }
  180. if (connection == null && _activeConnections < TcpConnectionPool.MaxOpenConnections)
  181. {
  182. // No connections available, but the max connections
  183. // has not been reached yet, so a new one can be created
  184. connection = CreateConnection();
  185. }
  186. // No available connections in the pool
  187. // Wait for somewone to release one.
  188. if (connection == null)
  189. {
  190. Monitor.Wait(_pool);
  191. }
  192. }
  193. while (connection == null);
  194. return connection;
  195. }
  196. }
  197. private TcpConnection CreateConnection()
  198. {
  199. try
  200. {
  201. ReusableTcpClient client = new ReusableTcpClient(_host, _port);
  202. TcpConnection entry = new TcpConnection(this, client);
  203. _activeConnections++;
  204. return entry;
  205. }
  206. catch (Exception ex)
  207. {
  208. throw new RemotingException (ex.Message);
  209. }
  210. }
  211. public void ReleaseConnection (TcpConnection entry)
  212. {
  213. lock (_pool)
  214. {
  215. entry.ControlTime = DateTime.Now; // Initialize timeout
  216. _pool.Add (entry);
  217. Monitor.Pulse (_pool);
  218. }
  219. }
  220. private void CancelConnection(TcpConnection entry)
  221. {
  222. try
  223. {
  224. entry.Stream.Close();
  225. _activeConnections--;
  226. }
  227. catch
  228. {
  229. }
  230. }
  231. public void PurgeConnections()
  232. {
  233. lock (_pool)
  234. {
  235. for (int n=0; n < _pool.Count; n++)
  236. {
  237. TcpConnection entry = (TcpConnection)_pool[n];
  238. if ( (DateTime.Now - entry.ControlTime).TotalSeconds > TcpConnectionPool.KeepAliveSeconds)
  239. {
  240. CancelConnection (entry);
  241. _pool.RemoveAt(n);
  242. n--;
  243. }
  244. }
  245. }
  246. }
  247. }
  248. }