2 // System.Runtime.Remoting.Channels.Simple.SimpleServerChannel.cs
4 // Author: Dietmar Maurer (dietmar@ximian.com)
6 // 2002 (C) Copyright, Ximian, Inc.
9 using System.Collections;
10 using System.Runtime.Remoting.Messaging;
11 using System.Text.RegularExpressions;
12 using System.Net.Sockets;
14 using System.Threading;
17 namespace System.Runtime.Remoting.Channels.Simple
19 public class SimpleServerChannel : IChannelReceiver, IChannel
22 string name = "simple";
25 Thread server_thread = null;
27 SimpleServerTransportSink sink;
28 ChannelDataStore channel_data;
30 void Init (IServerChannelSinkProvider provider) {
31 if (provider == null) {
32 provider = new SimpleServerFormatterSinkProvider ();
35 IServerChannelSink next_sink = ChannelServices.CreateServerChannelSinkChain (provider, this);
37 host = Dns.GetHostByName(Dns.GetHostName()).HostName;
39 string [] uris = null;
42 uris = new String [1];
43 uris [0] = GetChannelUri ();
46 channel_data = new ChannelDataStore (uris);;
48 sink = new SimpleServerTransportSink (next_sink);
50 listener = new TcpListener (port);
51 StartListening (null);
54 public SimpleServerChannel (int port)
60 public SimpleServerChannel (IDictionary properties,
61 IServerChannelSinkProvider serverSinkProvider)
63 port = (int)properties ["port"];
64 Init (serverSinkProvider);
67 public SimpleServerChannel (string name, int port,
68 IServerChannelSinkProvider serverSinkProvider)
72 Init (serverSinkProvider);
75 public SimpleServerChannel (string name, int port)
82 public object ChannelData
89 public string ChannelName
96 public int ChannelPriority
103 string GetChannelUri ()
105 return "simple://" + host + ":" + port;
108 public string[] GetUrlsForUri (string uri)
110 string [] chnl_uris = channel_data.ChannelUris;
112 if (uri.IndexOf ('/') != 0)
115 string [] result = new String [chnl_uris.Length];
117 for (int i = 0; i < chnl_uris.Length; i++) {
118 result [i] = chnl_uris [i] + uri;
124 public string Parse (string url, out string objectURI)
128 string host = SimpleChannel.ParseSimpleURL (url, out objectURI, out port);
130 return GetChannelUri ();
133 void WaitForConnections ()
135 TcpClient client = listener.AcceptTcpClient ();
136 Stream network_stream = client.GetStream ();
140 sink.InternalProcessMessage (network_stream);
145 public void StartListening (object data)
147 if (server_thread == null) {
150 port = ((IPEndPoint)listener.LocalEndpoint).Port;
151 channel_data.ChannelUris = new String [1];
152 channel_data.ChannelUris [0] = GetChannelUri ();
155 server_thread = new Thread (new ThreadStart (WaitForConnections));
156 server_thread.Start ();
160 public void StopListening (object data)
162 if (server_thread != null) {
163 server_thread.Abort ();
164 server_thread = null;