(DISTFILES): Comment out a few missing files.
[mono-project.git] / mcs / class / System.Runtime.Remoting / System.Runtime.Remoting.Channels.Simple / SimpleServerChannel.cs
blob6be220869591db664f168287e19e2b5c737b4961
1 //
2 // System.Runtime.Remoting.Channels.Simple.SimpleServerChannel.cs
3 //
4 // Author: Dietmar Maurer (dietmar@ximian.com)
5 //
6 // 2002 (C) Copyright, Ximian, Inc.
7 //
9 //
10 // Permission is hereby granted, free of charge, to any person obtaining
11 // a copy of this software and associated documentation files (the
12 // "Software"), to deal in the Software without restriction, including
13 // without limitation the rights to use, copy, modify, merge, publish,
14 // distribute, sublicense, and/or sell copies of the Software, and to
15 // permit persons to whom the Software is furnished to do so, subject to
16 // the following conditions:
17 //
18 // The above copyright notice and this permission notice shall be
19 // included in all copies or substantial portions of the Software.
20 //
21 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
22 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
23 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
24 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
25 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
26 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
27 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
30 using System.Collections;
31 using System.Runtime.Remoting.Messaging;
32 using System.Text.RegularExpressions;
33 using System.Net.Sockets;
34 using System.Net;
35 using System.Threading;
36 using System.IO;
38 namespace System.Runtime.Remoting.Channels.Simple
40 public class SimpleServerChannel : IChannelReceiver, IChannel
42 int port = 0;
43 string name = "simple";
44 string host;
45 int priority = 1;
46 Thread server_thread = null;
47 TcpListener listener;
48 SimpleServerTransportSink sink;
49 ChannelDataStore channel_data;
51 void Init (IServerChannelSinkProvider provider) {
52 if (provider == null) {
53 provider = new SimpleServerFormatterSinkProvider ();
56 IServerChannelSink next_sink = ChannelServices.CreateServerChannelSinkChain (provider, this);
58 host = Dns.GetHostByName(Dns.GetHostName()).HostName;
60 string [] uris = null;
62 if (port != 0) {
63 uris = new String [1];
64 uris [0] = GetChannelUri ();
67 channel_data = new ChannelDataStore (uris);;
69 sink = new SimpleServerTransportSink (next_sink);
71 listener = new TcpListener (port);
72 StartListening (null);
75 public SimpleServerChannel (int port)
77 this.port = port;
78 Init (null);
81 public SimpleServerChannel (IDictionary properties,
82 IServerChannelSinkProvider serverSinkProvider)
84 port = (int)properties ["port"];
85 Init (serverSinkProvider);
88 public SimpleServerChannel (string name, int port,
89 IServerChannelSinkProvider serverSinkProvider)
91 name = name;
92 this.port = port;
93 Init (serverSinkProvider);
96 public SimpleServerChannel (string name, int port)
98 name = name;
99 this.port = port;
100 Init (null);
103 public object ChannelData
105 get {
106 return channel_data;
110 public string ChannelName
112 get {
113 return name;
117 public int ChannelPriority
119 get {
120 return priority;
124 string GetChannelUri ()
126 return "simple://" + host + ":" + port;
129 public string[] GetUrlsForUri (string uri)
131 string [] chnl_uris = channel_data.ChannelUris;
133 if (uri.IndexOf ('/') != 0)
134 uri = "/" + uri;
136 string [] result = new String [chnl_uris.Length];
138 for (int i = 0; i < chnl_uris.Length; i++) {
139 result [i] = chnl_uris [i] + uri;
142 return result;
145 public string Parse (string url, out string objectURI)
147 int port;
149 string host = SimpleChannel.ParseSimpleURL (url, out objectURI, out port);
151 return GetChannelUri ();
154 void WaitForConnections ()
156 TcpClient client = listener.AcceptTcpClient ();
157 Stream network_stream = client.GetStream ();
159 while (true) {
161 sink.InternalProcessMessage (network_stream);
166 public void StartListening (object data)
168 if (server_thread == null) {
169 listener.Start ();
170 if (port == 0) {
171 port = ((IPEndPoint)listener.LocalEndpoint).Port;
172 channel_data.ChannelUris = new String [1];
173 channel_data.ChannelUris [0] = GetChannelUri ();
176 server_thread = new Thread (new ThreadStart (WaitForConnections));
177 server_thread.Start ();
181 public void StopListening (object data)
183 if (server_thread != null) {
184 server_thread.Abort ();
185 server_thread = null;
186 listener.Stop ();