TcpServerChannel.cs 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  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. using System.Runtime.Remoting.Channels.Simple;
  17. namespace System.Runtime.Remoting.Channels.Tcp
  18. {
  19. public class TcpServerChannel : IChannelReceiver, IChannel
  20. {
  21. int port = 0;
  22. string name = "tcp";
  23. string host;
  24. int priority = 1;
  25. Thread server_thread = null;
  26. TcpListener listener;
  27. TcpServerTransportSink sink;
  28. ChannelDataStore channel_data;
  29. void Init (IServerChannelSinkProvider serverSinkProvider)
  30. {
  31. if (serverSinkProvider == null) {
  32. // FIXME: change soap for binary
  33. serverSinkProvider = new SimpleServerFormatterSinkProvider ();
  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. while (true)
  117. {
  118. TcpClient client = listener.AcceptTcpClient ();
  119. ClientConnection reader = new ClientConnection (client, sink);
  120. ThreadPool.QueueUserWorkItem ( new WaitCallback( reader.ProcessMessages));
  121. }
  122. }
  123. public void StartListening (object data)
  124. {
  125. if (server_thread == null) {
  126. listener.Start ();
  127. if (port == 0) {
  128. port = ((IPEndPoint)listener.LocalEndpoint).Port;
  129. channel_data.ChannelUris = new String [1];
  130. channel_data.ChannelUris [0] = GetChannelUri ();
  131. }
  132. server_thread = new Thread (new ThreadStart (WaitForConnections));
  133. server_thread.IsBackground = true;
  134. server_thread.Start ();
  135. }
  136. }
  137. public void StopListening (object data)
  138. {
  139. if (server_thread != null) {
  140. server_thread.Abort ();
  141. server_thread = null;
  142. listener.Stop ();
  143. }
  144. }
  145. }
  146. class ClientConnection
  147. {
  148. TcpClient _client;
  149. TcpServerTransportSink _sink;
  150. Stream _stream;
  151. byte[] _buffer = new byte[TcpMessageIO.DefaultStreamBufferSize];
  152. public ClientConnection (TcpClient client, TcpServerTransportSink sink)
  153. {
  154. _client = client;
  155. _sink = sink;
  156. }
  157. public Stream Stream
  158. {
  159. get { return _stream; }
  160. }
  161. public byte[] Buffer
  162. {
  163. get { return _buffer; }
  164. }
  165. public void ProcessMessages(object data)
  166. {
  167. _stream = _client.GetStream();
  168. bool end = false;
  169. while (!end)
  170. {
  171. MessageType type = TcpMessageIO.ReceiveMessageType (_stream);
  172. switch (type)
  173. {
  174. case MessageType.MethodMessage:
  175. _sink.InternalProcessMessage (this);
  176. break;
  177. case MessageType.CancelSignal:
  178. end = true;
  179. break;
  180. }
  181. }
  182. _stream.Close();
  183. }
  184. }
  185. }