TcpServerChannel.cs 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291
  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. IPAddress bindAddress = IPAddress.Any;
  25. Thread server_thread = null;
  26. TcpListener listener;
  27. TcpServerTransportSink sink;
  28. ChannelDataStore channel_data;
  29. int _maxConcurrentConnections = 100;
  30. ArrayList _activeConnections = new ArrayList();
  31. void Init (IServerChannelSinkProvider serverSinkProvider)
  32. {
  33. if (serverSinkProvider == null)
  34. {
  35. serverSinkProvider = new BinaryServerFormatterSinkProvider ();
  36. }
  37. host = Dns.GetHostByName(Dns.GetHostName()).HostName;
  38. string [] uris = null;
  39. if (port != 0) {
  40. uris = new String [1];
  41. uris [0] = GetChannelUri ();
  42. }
  43. // Gets channel data from the chain of channel providers
  44. channel_data = new ChannelDataStore (uris);
  45. IServerChannelSinkProvider provider = serverSinkProvider;
  46. while (provider != null)
  47. {
  48. provider.GetChannelData(channel_data);
  49. provider = provider.Next;
  50. }
  51. // Creates the sink chain that will process all incoming messages
  52. IServerChannelSink next_sink = ChannelServices.CreateServerChannelSinkChain (serverSinkProvider, this);
  53. sink = new TcpServerTransportSink (next_sink);
  54. listener = new TcpListener(bindAddress, port);
  55. StartListening (null);
  56. }
  57. public TcpServerChannel (int port)
  58. {
  59. this.port = port;
  60. Init (null);
  61. }
  62. public TcpServerChannel (IDictionary properties,
  63. IServerChannelSinkProvider serverSinkProvider)
  64. {
  65. foreach(DictionaryEntry property in properties)
  66. {
  67. switch((string)property.Key)
  68. {
  69. case "port":
  70. port = Convert.ToInt32(property.Value);
  71. break;
  72. case "priority":
  73. priority = Convert.ToInt32(property.Value);
  74. break;
  75. case "bindTo":
  76. bindAddress = IPAddress.Parse((string)property.Value);
  77. break;
  78. case "rejectRemoteRequests":
  79. if(Convert.ToBoolean(properties["rejectRemoteRequests"]))
  80. bindAddress = IPAddress.Loopback;
  81. break;
  82. }
  83. }
  84. Init (serverSinkProvider);
  85. }
  86. public TcpServerChannel (string name, int port,
  87. IServerChannelSinkProvider serverSinkProvider)
  88. {
  89. this.name = name;
  90. this.port = port;
  91. Init (serverSinkProvider);
  92. }
  93. public TcpServerChannel (string name, int port)
  94. {
  95. this.name = name;
  96. this.port = port;
  97. Init (null);
  98. }
  99. public object ChannelData
  100. {
  101. get {
  102. return channel_data;
  103. }
  104. }
  105. public string ChannelName
  106. {
  107. get {
  108. return name;
  109. }
  110. }
  111. public int ChannelPriority
  112. {
  113. get {
  114. return priority;
  115. }
  116. }
  117. public string GetChannelUri ()
  118. {
  119. return "tcp://" + host + ":" + port;
  120. }
  121. public string[] GetUrlsForUri (string uri)
  122. {
  123. if (!uri.StartsWith ("/")) uri = "/" + uri;
  124. string [] chnl_uris = channel_data.ChannelUris;
  125. string [] result = new String [chnl_uris.Length];
  126. for (int i = 0; i < chnl_uris.Length; i++)
  127. result [i] = chnl_uris [i] + uri;
  128. return result;
  129. }
  130. public string Parse (string url, out string objectURI)
  131. {
  132. return TcpChannel.ParseChannelUrl (url, out objectURI);
  133. }
  134. void WaitForConnections ()
  135. {
  136. try
  137. {
  138. while (true)
  139. {
  140. TcpClient client = listener.AcceptTcpClient ();
  141. CreateListenerConnection (client);
  142. }
  143. }
  144. catch
  145. {}
  146. }
  147. internal void CreateListenerConnection (TcpClient client)
  148. {
  149. lock (_activeConnections)
  150. {
  151. if (_activeConnections.Count >= _maxConcurrentConnections)
  152. Monitor.Wait (_activeConnections);
  153. if (server_thread == null) return; // Server was stopped while waiting
  154. ClientConnection reader = new ClientConnection (this, client, sink);
  155. Thread thread = new Thread (new ThreadStart (reader.ProcessMessages));
  156. thread.Start();
  157. thread.IsBackground = true;
  158. _activeConnections.Add (thread);
  159. }
  160. }
  161. internal void ReleaseConnection (Thread thread)
  162. {
  163. lock (_activeConnections)
  164. {
  165. _activeConnections.Remove (thread);
  166. Monitor.Pulse (_activeConnections);
  167. }
  168. }
  169. public void StartListening (object data)
  170. {
  171. if (server_thread == null)
  172. {
  173. listener.Start ();
  174. if (port == 0) {
  175. port = ((IPEndPoint)listener.LocalEndpoint).Port;
  176. channel_data.ChannelUris = new String [1];
  177. channel_data.ChannelUris [0] = GetChannelUri ();
  178. }
  179. server_thread = new Thread (new ThreadStart (WaitForConnections));
  180. server_thread.IsBackground = true;
  181. server_thread.Start ();
  182. }
  183. }
  184. public void StopListening (object data)
  185. {
  186. if (server_thread == null) return;
  187. lock (_activeConnections)
  188. {
  189. server_thread.Abort ();
  190. server_thread = null;
  191. listener.Stop ();
  192. foreach (Thread thread in _activeConnections)
  193. thread.Abort();
  194. _activeConnections.Clear();
  195. Monitor.PulseAll (_activeConnections);
  196. }
  197. }
  198. }
  199. class ClientConnection
  200. {
  201. TcpClient _client;
  202. TcpServerTransportSink _sink;
  203. Stream _stream;
  204. TcpServerChannel _serverChannel;
  205. byte[] _buffer = new byte[TcpMessageIO.DefaultStreamBufferSize];
  206. public ClientConnection (TcpServerChannel serverChannel, TcpClient client, TcpServerTransportSink sink)
  207. {
  208. _serverChannel = serverChannel;
  209. _client = client;
  210. _sink = sink;
  211. }
  212. public Stream Stream
  213. {
  214. get { return _stream; }
  215. }
  216. public byte[] Buffer
  217. {
  218. get { return _buffer; }
  219. }
  220. public void ProcessMessages()
  221. {
  222. _stream = _client.GetStream();
  223. try
  224. {
  225. bool end = false;
  226. while (!end)
  227. {
  228. MessageStatus type = TcpMessageIO.ReceiveMessageStatus (_stream);
  229. switch (type)
  230. {
  231. case MessageStatus.MethodMessage:
  232. _sink.InternalProcessMessage (this);
  233. break;
  234. case MessageStatus.CancelSignal:
  235. end = true;
  236. break;
  237. }
  238. }
  239. }
  240. catch
  241. {}
  242. _stream.Close();
  243. _serverChannel.ReleaseConnection (Thread.CurrentThread);
  244. }
  245. }
  246. }