TcpServerChannel.cs 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  1. //
  2. // System.Runtime.Remoting.Channels.Tcp.TcpServerChannel.cs
  3. //
  4. // Author: Rodrigo Moya ([email protected])
  5. // Lluis Sanchez Gual ([email protected])
  6. //
  7. // 2002 (C) Copyright, Ximian, Inc.
  8. //
  9. using System.Collections;
  10. using System.Runtime.Remoting.Messaging;
  11. using System.Text.RegularExpressions;
  12. using System.Net.Sockets;
  13. using System.Net;
  14. using System.Threading;
  15. using System.IO;
  16. namespace System.Runtime.Remoting.Channels.Tcp
  17. {
  18. public class TcpServerChannel : IChannelReceiver, IChannel
  19. {
  20. int port = 0;
  21. string name = "tcp";
  22. string host;
  23. int priority = 1;
  24. Thread server_thread = null;
  25. TcpListener listener;
  26. TcpServerTransportSink sink;
  27. ChannelDataStore channel_data;
  28. int _maxConcurrentConnections = 100;
  29. ArrayList _activeConnections = new ArrayList();
  30. void Init (IServerChannelSinkProvider serverSinkProvider)
  31. {
  32. if (serverSinkProvider == null) {
  33. serverSinkProvider = new BinaryServerFormatterSinkProvider ();
  34. }
  35. host = Dns.GetHostByName(Dns.GetHostName()).HostName;
  36. string [] uris = null;
  37. if (port != 0) {
  38. uris = new String [1];
  39. uris [0] = GetChannelUri ();
  40. }
  41. // Gets channel data from the chain of channel providers
  42. channel_data = new ChannelDataStore (uris);
  43. IServerChannelSinkProvider provider = serverSinkProvider;
  44. while (provider != null)
  45. {
  46. provider.GetChannelData(channel_data);
  47. provider = provider.Next;
  48. }
  49. // Creates the sink chain that will process all incoming messages
  50. IServerChannelSink next_sink = ChannelServices.CreateServerChannelSinkChain (serverSinkProvider, this);
  51. sink = new TcpServerTransportSink (next_sink);
  52. listener = new TcpListener (port);
  53. StartListening (null);
  54. }
  55. public TcpServerChannel (int port)
  56. {
  57. this.port = port;
  58. Init (null);
  59. }
  60. public TcpServerChannel (IDictionary properties,
  61. IServerChannelSinkProvider serverSinkProvider)
  62. {
  63. port = Int32.Parse ((string)properties ["port"]);
  64. Init (serverSinkProvider);
  65. }
  66. public TcpServerChannel (string name, int port,
  67. IServerChannelSinkProvider serverSinkProvider)
  68. {
  69. this.name = name;
  70. this.port = port;
  71. Init (serverSinkProvider);
  72. }
  73. public TcpServerChannel (string name, int port)
  74. {
  75. this.name = name;
  76. this.port = port;
  77. Init (null);
  78. }
  79. public object ChannelData
  80. {
  81. get {
  82. return channel_data;
  83. }
  84. }
  85. public string ChannelName
  86. {
  87. get {
  88. return name;
  89. }
  90. }
  91. public int ChannelPriority
  92. {
  93. get {
  94. return priority;
  95. }
  96. }
  97. public string GetChannelUri ()
  98. {
  99. return "tcp://" + host + ":" + port;
  100. }
  101. public string[] GetUrlsForUri (string uri)
  102. {
  103. if (!uri.StartsWith ("/")) uri = "/" + uri;
  104. string [] chnl_uris = channel_data.ChannelUris;
  105. string [] result = new String [chnl_uris.Length];
  106. for (int i = 0; i < chnl_uris.Length; i++)
  107. result [i] = chnl_uris [i] + uri;
  108. return result;
  109. }
  110. public string Parse (string url, out string objectURI)
  111. {
  112. return TcpChannel.ParseChannelUrl (url, out objectURI);
  113. }
  114. void WaitForConnections ()
  115. {
  116. try
  117. {
  118. while (true)
  119. {
  120. TcpClient client = listener.AcceptTcpClient ();
  121. CreateListenerConnection (client);
  122. }
  123. }
  124. catch
  125. {}
  126. }
  127. internal void CreateListenerConnection (TcpClient client)
  128. {
  129. lock (_activeConnections)
  130. {
  131. if (_activeConnections.Count >= _maxConcurrentConnections)
  132. Monitor.Wait (_activeConnections);
  133. if (server_thread == null) return; // Server was stopped while waiting
  134. ClientConnection reader = new ClientConnection (this, client, sink);
  135. Thread thread = new Thread (new ThreadStart (reader.ProcessMessages));
  136. thread.Start();
  137. thread.IsBackground = true;
  138. _activeConnections.Add (thread);
  139. }
  140. }
  141. internal void ReleaseConnection (Thread thread)
  142. {
  143. lock (_activeConnections)
  144. {
  145. _activeConnections.Remove (thread);
  146. Monitor.Pulse (_activeConnections);
  147. }
  148. }
  149. public void StartListening (object data)
  150. {
  151. if (server_thread == null) {
  152. listener.Start ();
  153. if (port == 0) {
  154. port = ((IPEndPoint)listener.LocalEndpoint).Port;
  155. channel_data.ChannelUris = new String [1];
  156. channel_data.ChannelUris [0] = GetChannelUri ();
  157. }
  158. server_thread = new Thread (new ThreadStart (WaitForConnections));
  159. server_thread.IsBackground = true;
  160. server_thread.Start ();
  161. }
  162. }
  163. public void StopListening (object data)
  164. {
  165. if (server_thread == null) return;
  166. lock (_activeConnections)
  167. {
  168. server_thread.Abort ();
  169. server_thread = null;
  170. listener.Stop ();
  171. foreach (Thread thread in _activeConnections)
  172. thread.Abort();
  173. _activeConnections.Clear();
  174. Monitor.PulseAll (_activeConnections);
  175. }
  176. }
  177. }
  178. class ClientConnection
  179. {
  180. TcpClient _client;
  181. TcpServerTransportSink _sink;
  182. Stream _stream;
  183. TcpServerChannel _serverChannel;
  184. byte[] _buffer = new byte[TcpMessageIO.DefaultStreamBufferSize];
  185. public ClientConnection (TcpServerChannel serverChannel, TcpClient client, TcpServerTransportSink sink)
  186. {
  187. _serverChannel = serverChannel;
  188. _client = client;
  189. _sink = sink;
  190. }
  191. public Stream Stream
  192. {
  193. get { return _stream; }
  194. }
  195. public byte[] Buffer
  196. {
  197. get { return _buffer; }
  198. }
  199. public void ProcessMessages()
  200. {
  201. _stream = _client.GetStream();
  202. try
  203. {
  204. bool end = false;
  205. while (!end)
  206. {
  207. MessageStatus type = TcpMessageIO.ReceiveMessageStatus (_stream);
  208. switch (type)
  209. {
  210. case MessageStatus.MethodMessage:
  211. _sink.InternalProcessMessage (this);
  212. break;
  213. case MessageStatus.CancelSignal:
  214. end = true;
  215. break;
  216. }
  217. }
  218. }
  219. catch
  220. {}
  221. _stream.Close();
  222. _serverChannel.ReleaseConnection (Thread.CurrentThread);
  223. }
  224. }
  225. }