TcpServerChannel.cs 6.9 KB

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