TcpConnectionPool.cs 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314
  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. //
  9. // Permission is hereby granted, free of charge, to any person obtaining
  10. // a copy of this software and associated documentation files (the
  11. // "Software"), to deal in the Software without restriction, including
  12. // without limitation the rights to use, copy, modify, merge, publish,
  13. // distribute, sublicense, and/or sell copies of the Software, and to
  14. // permit persons to whom the Software is furnished to do so, subject to
  15. // the following conditions:
  16. //
  17. // The above copyright notice and this permission notice shall be
  18. // included in all copies or substantial portions of the Software.
  19. //
  20. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  21. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  22. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  23. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  24. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  25. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  26. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  27. //
  28. using System;
  29. using System.Collections;
  30. using System.Threading;
  31. using System.IO;
  32. using System.Net.Sockets;
  33. namespace System.Runtime.Remoting.Channels.Tcp
  34. {
  35. // This is a pool of Tcp connections. Connections requested
  36. // by the TCP channel are pooled after their use, and can
  37. // be reused later. Connections are automaticaly closed
  38. // if not used after some time, specified in KeepAliveSeconds.
  39. // The number of allowed open connections can also be specified
  40. // in MaxOpenConnections. The limit is per host.
  41. // If a thread requests a connection and the limit has been
  42. // reached, the thread is suspended until one is released.
  43. internal class TcpConnectionPool
  44. {
  45. // Table of pools. There is a HostConnectionPool
  46. // instance for each host
  47. static Hashtable _pools = new Hashtable();
  48. static int _maxOpenConnections = int.MaxValue;
  49. static int _keepAliveSeconds = 15;
  50. static Thread _poolThread;
  51. static TcpConnectionPool()
  52. {
  53. // This thread will close unused connections
  54. _poolThread = new Thread (new ThreadStart (ConnectionCollector));
  55. _poolThread.IsBackground = true;
  56. _poolThread.Start();
  57. }
  58. public static void Shutdown ()
  59. {
  60. #if !TARGET_JVM
  61. if (_poolThread != null)
  62. _poolThread.Abort();
  63. #endif
  64. }
  65. public static int MaxOpenConnections
  66. {
  67. get { return _maxOpenConnections; }
  68. set
  69. {
  70. if (value < 1) throw new RemotingException ("MaxOpenConnections must be greater than zero");
  71. _maxOpenConnections = value;
  72. }
  73. }
  74. public static int KeepAliveSeconds
  75. {
  76. get { return _keepAliveSeconds; }
  77. set { _keepAliveSeconds = value; }
  78. }
  79. public static TcpConnection GetConnection (string host, int port)
  80. {
  81. HostConnectionPool hostPool;
  82. lock (_pools)
  83. {
  84. string key = host + ":" + port;
  85. hostPool = (HostConnectionPool) _pools[key];
  86. if (hostPool == null)
  87. {
  88. hostPool = new HostConnectionPool(host, port);
  89. _pools[key] = hostPool;
  90. }
  91. }
  92. return hostPool.GetConnection();
  93. }
  94. private static void ConnectionCollector ()
  95. {
  96. while (true)
  97. {
  98. Thread.Sleep(3000);
  99. lock (_pools)
  100. {
  101. ICollection values = _pools.Values;
  102. foreach (HostConnectionPool pool in values)
  103. pool.PurgeConnections();
  104. }
  105. }
  106. }
  107. }
  108. internal class ReusableTcpClient : TcpClient
  109. {
  110. public ReusableTcpClient (string host, int port): base (host, port)
  111. {
  112. }
  113. public bool IsAlive
  114. {
  115. get
  116. {
  117. // This Poll will return true if there is data pending to
  118. // be read. It prob. means that a client object using this
  119. // connection got an exception and did not finish to read
  120. // the data. It can also mean that the connection has been
  121. // closed in the server. In both cases, the connection cannot
  122. // be reused.
  123. return !Client.Poll (0, SelectMode.SelectRead);
  124. }
  125. }
  126. }
  127. internal class TcpConnection
  128. {
  129. DateTime _controlTime;
  130. Stream _stream;
  131. ReusableTcpClient _client;
  132. HostConnectionPool _pool;
  133. byte[] _buffer;
  134. public TcpConnection (HostConnectionPool pool, ReusableTcpClient client)
  135. {
  136. _pool = pool;
  137. _client = client;
  138. _stream = new BufferedStream (client.GetStream());
  139. _controlTime = DateTime.Now;
  140. _buffer = new byte[TcpMessageIO.DefaultStreamBufferSize];
  141. }
  142. public Stream Stream
  143. {
  144. get { return _stream; }
  145. }
  146. public DateTime ControlTime
  147. {
  148. get { return _controlTime; }
  149. set { _controlTime = value; }
  150. }
  151. public bool IsAlive
  152. {
  153. get { return _client.IsAlive; }
  154. }
  155. // This is a "thread safe" buffer that can be used by
  156. // TcpClientTransportSink to read or send data to the stream.
  157. // The buffer is "thread safe" since only one thread can
  158. // use a connection at a given time.
  159. public byte[] Buffer
  160. {
  161. get { return _buffer; }
  162. }
  163. // Returns the connection to the pool
  164. public void Release()
  165. {
  166. _pool.ReleaseConnection (this);
  167. }
  168. public void Close()
  169. {
  170. _client.Close();
  171. }
  172. }
  173. internal class HostConnectionPool
  174. {
  175. ArrayList _pool = new ArrayList();
  176. int _activeConnections = 0;
  177. string _host;
  178. int _port;
  179. public HostConnectionPool (string host, int port)
  180. {
  181. _host = host;
  182. _port = port;
  183. }
  184. public TcpConnection GetConnection ()
  185. {
  186. TcpConnection connection = null;
  187. lock (_pool)
  188. {
  189. do
  190. {
  191. if (_pool.Count > 0)
  192. {
  193. // There are available connections
  194. connection = (TcpConnection)_pool[_pool.Count - 1];
  195. _pool.RemoveAt(_pool.Count - 1);
  196. if (!connection.IsAlive) {
  197. CancelConnection (connection);
  198. connection = null;
  199. continue;
  200. }
  201. }
  202. if (connection == null && _activeConnections < TcpConnectionPool.MaxOpenConnections)
  203. {
  204. // No connections available, but the max connections
  205. // has not been reached yet, so a new one can be created
  206. // Create the connection outside the lock
  207. break;
  208. }
  209. // No available connections in the pool
  210. // Wait for somewone to release one.
  211. if (connection == null)
  212. {
  213. Monitor.Wait(_pool);
  214. }
  215. }
  216. while (connection == null);
  217. }
  218. if (connection == null)
  219. return CreateConnection ();
  220. else
  221. return connection;
  222. }
  223. private TcpConnection CreateConnection()
  224. {
  225. try
  226. {
  227. ReusableTcpClient client = new ReusableTcpClient(_host, _port);
  228. TcpConnection entry = new TcpConnection(this, client);
  229. _activeConnections++;
  230. return entry;
  231. }
  232. catch (Exception ex)
  233. {
  234. throw new RemotingException (ex.Message);
  235. }
  236. }
  237. public void ReleaseConnection (TcpConnection entry)
  238. {
  239. lock (_pool)
  240. {
  241. entry.ControlTime = DateTime.Now; // Initialize timeout
  242. _pool.Add (entry);
  243. Monitor.Pulse (_pool);
  244. }
  245. }
  246. private void CancelConnection(TcpConnection entry)
  247. {
  248. try
  249. {
  250. entry.Stream.Close();
  251. _activeConnections--;
  252. }
  253. catch
  254. {
  255. }
  256. }
  257. public void PurgeConnections()
  258. {
  259. lock (_pool)
  260. {
  261. for (int n=0; n < _pool.Count; n++)
  262. {
  263. TcpConnection entry = (TcpConnection)_pool[n];
  264. if ( (DateTime.Now - entry.ControlTime).TotalSeconds > TcpConnectionPool.KeepAliveSeconds)
  265. {
  266. CancelConnection (entry);
  267. _pool.RemoveAt(n);
  268. n--;
  269. }
  270. }
  271. }
  272. }
  273. }
  274. }