2005-05-18 Lluis Sanchez Gual <lluis@novell.com>
[mono.git] / mcs / class / System.Runtime.Remoting / System.Runtime.Remoting.Channels.Tcp / TcpServerChannel.cs
1 //
2 // System.Runtime.Remoting.Channels.Tcp.TcpServerChannel.cs
3 //
4 // Author: Rodrigo Moya (rodrigo@ximian.com)
5 //         Lluis Sanchez Gual (lluis@ideary.com)
6 //
7 // 2002 (C) Copyright, Ximian, Inc.
8 //
9
10 //
11 // Permission is hereby granted, free of charge, to any person obtaining
12 // a copy of this software and associated documentation files (the
13 // "Software"), to deal in the Software without restriction, including
14 // without limitation the rights to use, copy, modify, merge, publish,
15 // distribute, sublicense, and/or sell copies of the Software, and to
16 // permit persons to whom the Software is furnished to do so, subject to
17 // the following conditions:
18 // 
19 // The above copyright notice and this permission notice shall be
20 // included in all copies or substantial portions of the Software.
21 // 
22 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
23 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
24 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
25 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
26 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
27 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
28 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
29 //
30
31 using System.Collections;
32 using System.Runtime.Remoting.Messaging;
33 using System.Text.RegularExpressions;
34 using System.Net.Sockets;
35 using System.Net;
36 using System.Threading;
37 using System.IO;
38 using System.Runtime.Remoting.Channels;
39
40 namespace System.Runtime.Remoting.Channels.Tcp
41 {
42         public class TcpServerChannel : IChannelReceiver, IChannel
43         {
44                 int port = 0;
45                 string name = "tcp";
46                 string host = null;
47                 int priority = 1;
48                 bool supressChannelData = false;
49                 bool useIpAddress = false;
50                 
51                 IPAddress bindAddress = IPAddress.Any;
52                 Thread server_thread = null;
53                 TcpListener listener;
54                 TcpServerTransportSink sink;
55                 ChannelDataStore channel_data;
56                 
57                 RemotingThreadPool threadPool;
58                 
59                 void Init (IServerChannelSinkProvider serverSinkProvider) 
60                 {
61                         if (serverSinkProvider == null) \r
62                         {
63                                 serverSinkProvider = new BinaryServerFormatterSinkProvider ();
64                         }
65
66                         if (host == null)
67                         {
68                                 if (useIpAddress) {
69                                         if (!bindAddress.Equals(IPAddress.Any)) host = bindAddress.ToString ();
70                                         else {
71                                                 IPHostEntry he = Dns.Resolve (Dns.GetHostName());
72                                                 if (he.AddressList.Length == 0) throw new RemotingException ("IP address could not be determined for this host");
73                                                 host = he.AddressList [0].ToString ();
74                                         }
75                                 }
76                                 else
77                                         host = Dns.GetHostByName(Dns.GetHostName()).HostName;
78                         }
79                         
80                         // Gets channel data from the chain of channel providers
81
82                         channel_data = new ChannelDataStore (null);
83                         IServerChannelSinkProvider provider = serverSinkProvider;\r
84                         while (provider != null)\r
85                         {\r
86                                 provider.GetChannelData(channel_data);\r
87                                 provider = provider.Next;\r
88                         }\r
89
90                         // Creates the sink chain that will process all incoming messages
91
92                         IServerChannelSink next_sink = ChannelServices.CreateServerChannelSinkChain (serverSinkProvider, this);\r
93                         sink = new TcpServerTransportSink (next_sink);
94                 }
95                 
96                 public TcpServerChannel (int port)
97                 {
98                         this.port = port;
99                         Init (null);
100                 }
101
102                 public TcpServerChannel (IDictionary properties,
103                                          IServerChannelSinkProvider serverSinkProvider)
104                 {
105                         foreach(DictionaryEntry property in properties)
106                         {\r
107                                 switch((string)property.Key)\r
108                                 {\r
109                                         case "name":
110                                                 name = property.Value.ToString();
111                                                 break;
112                                         case "port":\r
113                                                 port = Convert.ToInt32(property.Value);
114                                                 break;\r
115                                         case "priority":\r
116                                                 priority = Convert.ToInt32(property.Value);
117                                                 break;\r
118                                         case "bindTo":\r
119                                                 bindAddress = IPAddress.Parse((string)property.Value);
120                                                 break;\r
121                                         case "rejectRemoteRequests":\r
122                                                 if(Convert.ToBoolean(properties["rejectRemoteRequests"]))
123                                                         bindAddress = IPAddress.Loopback;
124                                                 break;
125                                         case "supressChannelData":
126                                                 supressChannelData = Convert.ToBoolean (property.Value);
127                                                 break;\r
128                                         case "useIpAddress":
129                                                 useIpAddress = Convert.ToBoolean (property.Value);
130                                                 break;
131                                         case "machineName":
132                                                 host = property.Value as string;
133                                                 break;
134                                 }\r
135                         }                       
136                         Init (serverSinkProvider);
137                 }
138
139                 public TcpServerChannel (string name, int port,
140                                          IServerChannelSinkProvider serverSinkProvider)
141                 {
142                         this.name = name;
143                         this.port = port;
144                         Init (serverSinkProvider);
145                 }
146                 
147                 public TcpServerChannel (string name, int port)
148                 {
149                         this.name = name;
150                         this.port = port;
151                         Init (null);
152                 }
153                 
154                 public object ChannelData
155                 {
156                         get {
157                                 if (supressChannelData) return null;
158                                 else return channel_data;
159                         }
160                 }
161
162                 public string ChannelName
163                 {
164                         get {
165                                 return name;
166                         }
167                 }
168
169                 public int ChannelPriority
170                 {
171                         get {
172                                 return priority;
173                         }
174                 }
175
176                 public string GetChannelUri ()
177                 {
178                         return "tcp://" + host + ":" + port;
179                 }
180                 
181                 public string[] GetUrlsForUri (string uri)\r
182                 {\r
183                         if (!uri.StartsWith ("/")) uri = "/" + uri;
184 \r
185                         string [] chnl_uris = channel_data.ChannelUris;\r
186                         string [] result = new String [chnl_uris.Length];\r
187 \r
188                         for (int i = 0; i < chnl_uris.Length; i++) \r
189                                 result [i] = chnl_uris [i] + uri;\r
190                         \r
191                         return result;\r
192                 }\r
193
194                 public string Parse (string url, out string objectURI)
195                 {
196                         return TcpChannel.ParseChannelUrl (url, out objectURI);
197                 }
198
199                 void WaitForConnections ()
200                 {
201                         try
202                         {
203                                 while (true) 
204                                 {
205                                         Socket socket = listener.AcceptSocket ();
206                                         ClientConnection reader = new ClientConnection (this, socket, sink);
207                                         try {
208                                                 if (!threadPool.RunThread (new ThreadStart (reader.ProcessMessages)))
209                                                         socket.Close ();
210                                         } catch {}
211                                 }
212                         }
213                         catch
214                         {}
215                 }
216
217                 public void StartListening (object data)
218                 {
219                         listener = new TcpListener (bindAddress, port);
220                         if (server_thread == null) \r
221                         {
222                                 threadPool = RemotingThreadPool.GetSharedPool ();
223                                 listener.Start ();
224                                 
225                                 if (port == 0)
226                                         port = ((IPEndPoint)listener.LocalEndpoint).Port;
227
228                                 string[] uris = new String [1];
229                                 uris = new String [1];
230                                 uris [0] = GetChannelUri ();
231                                 channel_data.ChannelUris = uris;
232
233                                 server_thread = new Thread (new ThreadStart (WaitForConnections));
234                                 server_thread.IsBackground = true;
235                                 server_thread.Start ();
236                         }
237                 }
238
239                 public void StopListening (object data)
240                 {
241                         if (server_thread == null) return;
242                         
243                         server_thread.Abort ();
244                         listener.Stop ();
245                         threadPool.Free ();
246                         server_thread.Join ();
247                         server_thread = null;
248                 }
249         }
250
251         class ClientConnection
252         {
253                 static int _count;
254                 int _id;
255                 Socket _socket;
256                 TcpServerTransportSink _sink;
257                 Stream _stream;
258                 TcpServerChannel _serverChannel;
259
260                 byte[] _buffer = new byte[TcpMessageIO.DefaultStreamBufferSize];
261
262                 public ClientConnection (TcpServerChannel serverChannel, Socket socket, TcpServerTransportSink sink)
263                 {
264                         _serverChannel = serverChannel;
265                         _socket = socket;
266                         _sink = sink;
267                         _id = _count++;
268                 }
269
270                 public Stream Stream
271                 {
272                         get { return _stream; }
273                 }
274
275                 public byte[] Buffer
276                 {
277                         get { return _buffer; }
278                 }
279
280                 public void ProcessMessages()
281                 {
282                         byte[] buffer = new byte[256];
283                         NetworkStream ns = new NetworkStream (_socket);
284                         _stream = new BufferedStream (ns);
285
286                         try
287                         {
288                                 bool end = false;
289                                 while (!end)
290                                 {
291                                         MessageStatus type = TcpMessageIO.ReceiveMessageStatus (_stream, buffer);
292
293                                         switch (type)
294                                         {
295                                                 case MessageStatus.MethodMessage:
296                                                         _sink.InternalProcessMessage (this);
297                                                         break;
298
299                                                 case MessageStatus.Unknown:
300                                                 case MessageStatus.CancelSignal:
301                                                         end = true;
302                                                         break;
303                                         }
304                                         _stream.Flush ();
305                                 }
306                         }
307                         catch (Exception ex)
308                         {
309 //                              Console.WriteLine (ex);
310                         }
311                         finally
312                         {
313                                 try {
314                                         _stream.Close();
315                                         _socket.Close ();
316                                 }
317                                 catch { }
318                         }
319                 }
320                 
321                 public int Id
322                 {
323                         get { return _id; }
324                 }
325                 
326                 public IPAddress ClientAddress
327                 {
328                         get {
329                                 IPEndPoint ep = _socket.RemoteEndPoint as IPEndPoint;
330                                 if (ep != null) return ep.Address;
331                                 else return null;
332                         }
333                 }
334         }
335 }