CORBAServerChannel.cs 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. //
  2. // System.Runtime.Remoting.Channels.CORBA.CORBAServerChannel.cs
  3. //
  4. // Author: Dietmar Maurer ([email protected])
  5. //
  6. // 2002 (C) Copyright, Ximian, Inc.
  7. //
  8. using System.Collections;
  9. using System.Runtime.Remoting.Messaging;
  10. using System.Text.RegularExpressions;
  11. using System.Net.Sockets;
  12. using System.Net;
  13. using System.Threading;
  14. using System.IO;
  15. namespace System.Runtime.Remoting.Channels.CORBA
  16. {
  17. public class CORBAServerChannel : IChannelReceiver, IChannel
  18. {
  19. int port = 0;
  20. string name = "simple";
  21. string host;
  22. int priority = 1;
  23. Thread server_thread = null;
  24. TcpListener listener;
  25. CORBAServerTransportSink sink;
  26. ChannelDataStore channel_data;
  27. void Init (IServerChannelSinkProvider provider) {
  28. if (provider == null) {
  29. provider = new CORBAServerFormatterSinkProvider ();
  30. }
  31. IServerChannelSink next_sink = ChannelServices.CreateServerChannelSinkChain (provider, this);
  32. host = Dns.GetHostByName(Dns.GetHostName()).HostName;
  33. string [] uris = null;
  34. if (port != 0) {
  35. uris = new String [1];
  36. uris [0] = GetChannelUri ();
  37. }
  38. channel_data = new ChannelDataStore (uris);;
  39. sink = new CORBAServerTransportSink (next_sink);
  40. listener = new TcpListener (port);
  41. StartListening (null);
  42. }
  43. public CORBAServerChannel (int port)
  44. {
  45. this.port = port;
  46. Init (null);
  47. }
  48. public CORBAServerChannel (IDictionary properties,
  49. IServerChannelSinkProvider serverSinkProvider)
  50. {
  51. port = (int)properties ["port"];
  52. Init (serverSinkProvider);
  53. }
  54. public CORBAServerChannel (string name, int port,
  55. IServerChannelSinkProvider serverSinkProvider)
  56. {
  57. name = name;
  58. this.port = port;
  59. Init (serverSinkProvider);
  60. }
  61. public CORBAServerChannel (string name, int port)
  62. {
  63. name = name;
  64. this.port = port;
  65. Init (null);
  66. }
  67. public object ChannelData
  68. {
  69. get {
  70. return channel_data;
  71. }
  72. }
  73. public string ChannelName
  74. {
  75. get {
  76. return name;
  77. }
  78. }
  79. public int ChannelPriority
  80. {
  81. get {
  82. return priority;
  83. }
  84. }
  85. string GetChannelUri ()
  86. {
  87. return "corba://" + host + ":" + port;
  88. }
  89. public string[] GetUrlsForUri (string uri)
  90. {
  91. string [] chnl_uris = channel_data.ChannelUris;
  92. if (uri.IndexOf ('/') != 0)
  93. uri = "/" + uri;
  94. string [] result = new String [chnl_uris.Length];
  95. for (int i = 0; i < chnl_uris.Length; i++) {
  96. result [i] = chnl_uris [i] + uri;
  97. }
  98. return result;
  99. }
  100. public string Parse (string url, out string objectURI)
  101. {
  102. int port;
  103. string host = CORBAChannel.ParseCORBAURL (url, out objectURI, out port);
  104. return GetChannelUri ();
  105. }
  106. void WaitForConnections ()
  107. {
  108. while (true) {
  109. TcpClient client = listener.AcceptTcpClient ();
  110. sink.InternalProcessMessage (client.GetStream ());
  111. client.Close ();
  112. }
  113. }
  114. public void StartListening (object data)
  115. {
  116. if (server_thread == null) {
  117. listener.Start ();
  118. if (port == 0) {
  119. port = ((IPEndPoint)listener.LocalEndpoint).Port;
  120. channel_data.ChannelUris = new String [1];
  121. channel_data.ChannelUris [0] = GetChannelUri ();
  122. }
  123. server_thread = new Thread (new ThreadStart (WaitForConnections));
  124. server_thread.Start ();
  125. }
  126. }
  127. public void StopListening (object data)
  128. {
  129. if (server_thread != null) {
  130. server_thread.Abort ();
  131. server_thread = null;
  132. listener.Stop ();
  133. }
  134. }
  135. }
  136. }