2
0

TcpServerChannel.cs 7.0 KB

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