SimpleServerChannel.cs 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. //
  2. // System.Runtime.Remoting.Channels.Simple.SimpleServerChannel.cs
  3. //
  4. // Author: Dietmar Maurer ([email protected])
  5. //
  6. // 2002 (C) Copyright, Ximian, Inc.
  7. //
  8. //
  9. // Permission is hereby granted, free of charge, to any person obtaining
  10. // a copy of this software and associated documentation files (the
  11. // "Software"), to deal in the Software without restriction, including
  12. // without limitation the rights to use, copy, modify, merge, publish,
  13. // distribute, sublicense, and/or sell copies of the Software, and to
  14. // permit persons to whom the Software is furnished to do so, subject to
  15. // the following conditions:
  16. //
  17. // The above copyright notice and this permission notice shall be
  18. // included in all copies or substantial portions of the Software.
  19. //
  20. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  21. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  22. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  23. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  24. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  25. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  26. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  27. //
  28. using System.Collections;
  29. using System.Runtime.Remoting.Messaging;
  30. using System.Text.RegularExpressions;
  31. using System.Net.Sockets;
  32. using System.Net;
  33. using System.Threading;
  34. using System.IO;
  35. namespace System.Runtime.Remoting.Channels.Simple
  36. {
  37. public class SimpleServerChannel : IChannelReceiver, IChannel
  38. {
  39. int port = 0;
  40. string name = "simple";
  41. string host;
  42. int priority = 1;
  43. Thread server_thread = null;
  44. TcpListener listener;
  45. SimpleServerTransportSink sink;
  46. ChannelDataStore channel_data;
  47. void Init (IServerChannelSinkProvider provider) {
  48. if (provider == null) {
  49. provider = new SimpleServerFormatterSinkProvider ();
  50. }
  51. IServerChannelSink next_sink = ChannelServices.CreateServerChannelSinkChain (provider, this);
  52. host = Dns.GetHostByName(Dns.GetHostName()).HostName;
  53. string [] uris = null;
  54. if (port != 0) {
  55. uris = new String [1];
  56. uris [0] = GetChannelUri ();
  57. }
  58. channel_data = new ChannelDataStore (uris);;
  59. sink = new SimpleServerTransportSink (next_sink);
  60. listener = new TcpListener (port);
  61. StartListening (null);
  62. }
  63. public SimpleServerChannel (int port)
  64. {
  65. this.port = port;
  66. Init (null);
  67. }
  68. public SimpleServerChannel (IDictionary properties,
  69. IServerChannelSinkProvider serverSinkProvider)
  70. {
  71. port = (int)properties ["port"];
  72. Init (serverSinkProvider);
  73. }
  74. public SimpleServerChannel (string name, int port,
  75. IServerChannelSinkProvider serverSinkProvider)
  76. {
  77. name = name;
  78. this.port = port;
  79. Init (serverSinkProvider);
  80. }
  81. public SimpleServerChannel (string name, int port)
  82. {
  83. name = name;
  84. this.port = port;
  85. Init (null);
  86. }
  87. public object ChannelData
  88. {
  89. get {
  90. return channel_data;
  91. }
  92. }
  93. public string ChannelName
  94. {
  95. get {
  96. return name;
  97. }
  98. }
  99. public int ChannelPriority
  100. {
  101. get {
  102. return priority;
  103. }
  104. }
  105. string GetChannelUri ()
  106. {
  107. return "simple://" + host + ":" + port;
  108. }
  109. public string[] GetUrlsForUri (string uri)
  110. {
  111. string [] chnl_uris = channel_data.ChannelUris;
  112. if (uri.IndexOf ('/') != 0)
  113. uri = "/" + uri;
  114. string [] result = new String [chnl_uris.Length];
  115. for (int i = 0; i < chnl_uris.Length; i++) {
  116. result [i] = chnl_uris [i] + uri;
  117. }
  118. return result;
  119. }
  120. public string Parse (string url, out string objectURI)
  121. {
  122. int port;
  123. string host = SimpleChannel.ParseSimpleURL (url, out objectURI, out port);
  124. return GetChannelUri ();
  125. }
  126. void WaitForConnections ()
  127. {
  128. TcpClient client = listener.AcceptTcpClient ();
  129. Stream network_stream = client.GetStream ();
  130. while (true) {
  131. sink.InternalProcessMessage (network_stream);
  132. }
  133. }
  134. public void StartListening (object data)
  135. {
  136. if (server_thread == null) {
  137. listener.Start ();
  138. if (port == 0) {
  139. port = ((IPEndPoint)listener.LocalEndpoint).Port;
  140. channel_data.ChannelUris = new String [1];
  141. channel_data.ChannelUris [0] = GetChannelUri ();
  142. }
  143. server_thread = new Thread (new ThreadStart (WaitForConnections));
  144. server_thread.Start ();
  145. }
  146. }
  147. public void StopListening (object data)
  148. {
  149. if (server_thread != null) {
  150. server_thread.Abort ();
  151. server_thread = null;
  152. listener.Stop ();
  153. }
  154. }
  155. }
  156. }